Stop loss executed at least 2 days after entering market
-
Hi
I have been using Backtrader for a couple of weeks now and I would like to thank the developers and community for this amazing tool.
While looking at the output of a 2 SMA crossover with bracket orders, i notice that if i use the close price for a day as the price for buy/sell, the order is executed the next day as expected and if possible.
However, a stop loss takes 2 days to trigger even though the range of values on the day of the purchase should trigger it.
Consider:
Date,Open,High,Low,Close,Adj Close,Volume 2004-10-04,1.240202,1.243503,1.226001,1.241095,1.241095,0 2004-10-11,1.241203,1.250000,1.223496,1.248206,1.248206,0 2004-10-18,1.248299,1.272993,1.245501,1.272896,1.272896,0
And consider the output:
CrossUp On:2004-10-04 23:59:59.999989 Buy Price:1.241095 Stop Price:1.239095 Take Profit:1.243095 (inside next) 2004-10-11: Order ref: 1 / Type Buy / Status Completed Created At 1.241095 Executed At 1.241095 Current Position 62054 (inside notify_oder) 2004-10-18: Order ref: 3 / Type Sell / Status Completed Created At 1.243095 Executed At 1.248299 Current Position 0 (inside notify_order)
My stop loss had the value 1.243095 and the range of values for 2004-10-11 was [1.223496,1.250000] which should trigger it.
At the same time, I understand that this is a complicated problem to solve and this may have been a decision. I notice that for orders, the opening price is used first for comparison and if the price does not trigger the order (e.g. opening price is not higher than my stop loss for a long position), then the daily range is used.
With the stop loss case, it is hard to trigger on the same day as the purchase because in real life, with the limited data that is provided (e.g. weekly data) , you can't really tell if the price that triggers the buy comes before the stop or vice-versa, so you push the order to the next day where it is easier to make a valid assumption.
I hope this makes sense. I checked the code and even though i have coded for many years, sometimes getting used to other people's code take me some time.
Thanks in advance.
-
-
@rainmaker Please note in bracket orders docs the following:
o put some logic into it, the following rules apply: The 3 orders are submitted together to avoid having any of them triggered independently The low/high side orders are marked as children of the main side The children are not active until the main side is executed The cancellation of the main side cancels both the low and high side The execution of the main side activates both the low and high side Upon being active The execution or cancellation of any of low/high side orders automatically cancels the other
If you wish to have finer granularity in the order execution, have a look at replay or resample your lines to have smaller and larger bars.
-
@run-out Thank you so much for fast response.
I missed that. However, i just wanted to really clarify. Are my assumptions correct? Do you guys use first the open price and then the hi-low range?
I understand what you mentioned above but it still does not touch my point. Or maybe it does in which case, i would like to know how.
Thanks once more
-
@rainmaker said in Stop loss executed at least 2 days after entering market:
Do you guys use first the open price and then the hi-low range?
Try this resource.
-
@run-out said in Stop loss executed at least 2 days after entering market:
@rainmaker said in Stop loss executed at least 2 days after entering market:
Do you guys use first the open price and then the hi-low range?
Try this resource.
@run-out I believe that answers perfectly. For whoever reads it:
"If the open price of the bar is below the limit price the order executes immediately with the open price. The order has been swept during the opening phase of the session"
"If the open price has not penetrated below the limit price but the low price is below the limit price, then the limit price has been seen during the session and the order can be executed"
Same for stops.
Thanks @run-out