-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Stop loss on the same day the position was opened #119
Comments
|
Nevermind, I misunderstood. I guess the reason SL isn't hit on the same bar is that we iterate over a copy of orders state: backtesting.py/backtesting/backtesting.py Line 774 in d146b43
so once we turn an order into a trade: backtesting.py/backtesting/backtesting.py Lines 881 to 883 in d146b43
the subsequent SL order that was placed in the queue isn't picked up in the same broker iteration. |
But imagine an order on that day placed with Due to this ambiguity, and the resolving complexity, I'm kinda leaning to wontfix. Have a better idea? |
I don't think the strategy should allow stop orders for exactly that reason. I would limit the entry point of a position to open and close, and the users would have still enough freedom to implement their logic to decide whether the strategy should open a position in the next iteration or not. In the previous versions it worked perfectly. |
Glad you think so. 😊 The following commit should restore the previous behavior for plain market orders. Thanks. |
Thank you for the quick fix!!! 😊 |
guys i am having trouble with following code, i set buying order with a condition so it should take when close price hit the given level and it does but take buy position at the wrong level which is not given anywhere but one thing is there that it open order before my tp and close position at earliest with profit of 0.5 points only. Although as per my understanding it should open position at close price when the giving condition is true i.e. 3903 but it is open position at 3911 and closing it 3911.5. can anyone help how can i resolve this problem? def trade_level(self): |
if it is not possible to sell in to strength with a real limit order that executes intra candle, but always have to wait for a close or open, will simply make all the results quite unrealiable as its not close to any real world situation. Can anyone guide me on how to implement this functionality properly into the library? |
This is the relevant code part: backtesting.py/backtesting/backtesting.py Lines 935 to 966 in 0ce24d8
I wonder why we exclude limit orders here ... 🤔🤨 backtesting.py/backtesting/backtesting.py Lines 948 to 949 in 0ce24d8
Maybe that should be an if True: ... ?
|
There are several cases here. I agree on that when price reachs both (TP and SL) within the same candle the result is unreliable, although I would go the route @rokups suggested, making SL trigger before than TP for green candles and TP before SL for red candles (if the trade is long). But IMO at the very least it should be allowed to trigger the TP/SL when only one child order is triggered within the opening candle, take profit or stop loss. So for instance:
Actual behaviour Desired behaviour: The very same argument works the other way around obviously, when SL gets triggered on the same candle of the limit buy but TP price is above the high of the candle (for long trades) so there is no confusion about what child order gets triggered first. So this is how code would look like IMO (new code between *******):
|
Expected Behavior
Strategy closes the position at 720
Actual Behavior
Strategy closes the position on next candle at open price, 705.58
Steps to Reproduce
Run the following code. You can see the wrong behaviour on 2012-10-18
Additional info
The text was updated successfully, but these errors were encountered: