New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open trade at fixed price #251
Comments
The trade opens on next open price, unless If you want the trade to open at a specific price point, issue a limit order: self.buy(limit=longLine[-1]) If you want the trade to close at a specific price point, set its take-profit price: self.buy(..., tp=shortLine[-1])
# or, in later next() calls:
trade = self.trades[-1]
trade.sl = shortLine[-1] |
When I did these changes (adding the Code: def next(self):
if not self.position:
longLine = self.sma[-2] - (self.sma[-2] * (self.longLineParam / 100))
shortLine = self.sma[-2] + (self.sma[-2] * (self.shortLineParam / 100))
if self.data.Close[-1] <= longLine:
self.buy(limit=longLine, sl=self.data.Close[-1] - (self.atr * self.atrSl))
elif self.data.Close[-1] >= shortLine:
self.sell(limit=shortLine, sl=self.data.Close[-1] + (self.atr * self.atrSl))
elif self.position:
trade = self.trades[-1]
trade.tp = self.sma[-2]
bt = Backtest(ohlcv, MAShiftSl, cash=10000, commission=0, trade_on_close=True) |
trade.tp = self.sma[-2] but shortLine = self.sma[-2] + (self.sma[-2] * (self.shortLineParam / 100)) ? I don't know what I'm looking at. Unless you can pack it all into a small, reproducible example (i.e. something worth of a bug report), it's all anyone's guess. 😬 |
Tried |
@JanssensKobe Have you maybe got it working, or what's the real issue here? |
Just saw this thread. I too would love to see this happen. In my case, I would like to define a custom purchase, and close price to make life so much easier. As an example, let's say I want to buy when the price of the current candle crosses over the 9 period moving average. Even with I've been looking through the source code trying to jerry-rig my own solution, and here's what I've got so far, at least for the In the main script (notice my new "specific_price" variable):
Then, in the
Then, under the
Finally, under the
Making these changes, and using my simple buying only example (no closing logic), you will see in the plots that your order gets processed at the price you specified. The trick now is, implementing the same logic into the Long story short, it would be cool to see the ability to define a specific entry price when using Anyway just my 2 cents. Thanks! |
Exactly. Precomputing where the next MA is going to be is one way to do it. The only other way I'm aware of is to use more fine-grained data (more candles). |
Isn't it possible to just check if the limit price is been hit within the current candle? |
I can let @kernc follow up if needed, but yes, that's what using limit prices does, but the issue is, this library trades close-to-close (or open-to-open if specified). So what that means is, the orders only get processed at the close of the current (or open of the next) candle. So if you set a limit price right now above the current candle's close price, and you're trading on close, the order will fulfill right away which is fine. The issue here is, we don't always know where to set the limit price prior to the current candle. For example, if we're waiting for the current candle to cross over a moving average, we want to issue a trade AT the moving average price, not wait until close. So in order to accomplish what we want, we would have had to set a buy-stop order at the NEXT candle's moving average in order to have an order trigger at that price, but you can't really do that because A) moving averages are generally always in motion as the price moves in the current candle, and B) you're now forward peaking into your dataset, which isn't what you want to do. What we're chatting about here is just an elementary way to kind of make it work. I took a stab at trying to implement a "purchase at a specific price" logic into the library so that the order would process right away at the designated price (in the current candle), and not wait until the close to process. Hope that (maybe) clears it up? |
Ooh yeah I understand, I didn't know the library only uses close-to-close/open-to-open candles. Thanks for your explanation @windowshopr! |
No problem @JanssensKobe , another option like kernc mentioned might be to use a smaller timeframe (with bigger period moving averages in our example) to be able to get a closer entry price, that would work too. |
Expected Behavior
I would expect that the trade would open the 'longLine' and close on the 'sma' line (in case of a long trade), but the trade opens/closes always on the close price. Is there a way to make sure that the the trade opens on the long/short line and closes on the sma line. These lines are based on the previous candle close value, so these are fixed values.
Code
Additional info
Example:
The text was updated successfully, but these errors were encountered: