My trading logic is straightforward:
- If a buy/sell signal is received enter the market with a long/short position using market order and cheat-on-open (triggered in
next
)
- As soon as this order is executed I place a stop and a take profit order (triggered in
notify_order
)
Now if an opposing signal is generated the order to flip the position from long to short or vice versa is again triggered in next
with cheat-on-open. Then, for the subsequent call of notify_order
I first loop through two lists that keep references of all stop and all take profit orders and cancel all of them using self.cancel(stop_loss)
and self.cancel(take_profit)
. Then again the stop and take profit orders for this new position are placed.
Looking at the log it seems to me that if e.g. an order is placed and executed on the next open then the cancelation of the corresponding stop and take profit orders are initiated on that next candle which in turn takes effect only on the following candle, which is already t+2 looking from the candle on which the market entry order was placed.
If the market moved by a lot and/or if e.g. the take profit order is placed very close to the entry of the position it seems that during the candle t+1 the order for market entry and the take profit are executed (before the cancelation takes effect at t+2). This is not my desired behaviour as it leaves me with a too large position.
E.g. these two community posts are similar but only solved my issue partially:
I'm on a 1m timeframe and do have fairly close take profit orders.
From the observed behaviour I'd have two questions:
- Did I understand correctly the sequence of order execution/cancelation?
- How can I ensure if a new trading signal is generated that I only flip the position between long/short and place new stop and take profit orders after all previous orders have been canceled successfully such that I don't run into double executions (actual trade and e.g. take profit from previous trade)? Ideally within the same/next candle, as would be possible when trading live.
Following an excerpt from my log with some annotations:
2020-12-26 19:37:00, BUY CREATE (flip from short to long), 25799.62, 99.36
2020-12-26 19:38:00, STOP CANCELED
2020-12-26 19:38:00, TAKE PROFIT CANCELED
2020-12-26 19:38:00, BUY EXECUTED, Price: 25785.12, Cost: -0.0105, Comm 0.0080
2020-12-26 19:38:00, STOP SELL PLACED @ 25269.42
2020-12-26 19:38:00, TAKE PROFIT SELL PLACED @ 25798.01
2020-12-26 19:38:00, OPERATION PROFIT, GROSS -0.0084, NET -0.0164
2020-12-26 19:38:00, Close: 25832.46, Signal: -1, Position: 0.00039
!!! position still ok
2020-12-26 19:38:00, Portfolio Value: 99.38
2020-12-26 19:38:00, SELL CREATE (flip from long to short), 25832.46, 99.38
2020-12-26 19:39:00, Order Canceled/Margin/Rejected
2020-12-26 19:39:00, Order Canceled/Margin/Rejected
2020-12-26 19:39:00, Order Canceled/Margin/Rejected
2020-12-26 19:39:00, STOP CANCELED
2020-12-26 19:39:00, TAKE PROFIT CANCELED
2020-12-26 19:39:00, SELL EXECUTED, Price: 25835.29, Cost: -0.0067, Comm 0.0080
2020-12-26 19:39:00, STOP BUY PLACED @ 26352.00
2020-12-26 19:39:00, TAKE PROFIT BUY PLACED @ 25822.37
2020-12-26 19:39:00, OPERATION PROFIT, GROSS 0.0194, NET 0.0114
2020-12-26 19:39:00, Close: 25811.52, Signal: 1, Position: -0.00039
!!! position still ok, flipped successfully
2020-12-26 19:39:00, Portfolio Value: 99.38
2020-12-26 19:39:00, BUY CREATE (flip from short to long), 25811.52, 99.38
2020-12-26 19:40:00, Order Canceled/Margin/Rejected
2020-12-26 19:40:00, Order Canceled/Margin/Rejected
2020-12-26 19:40:00, Order Canceled/Margin/Rejected
2020-12-26 19:40:00, STOP CANCELED
2020-12-26 19:40:00, TAKE PROFIT CANCELED
2020-12-26 19:40:00, BUY EXECUTED, Price: 25809.76, Cost: -10.0011, Comm 0.0040
2020-12-26 19:40:00, STOP SELL PLACED @ 25293.56
2020-12-26 19:40:00, TAKE PROFIT SELL PLACED @ 25822.66
2020-12-26 19:40:00, STOP CANCELED
2020-12-26 19:40:00, TAKE PROFIT CANCELED
2020-12-26 19:40:00, BUY EXECUTED, Price: 25809.76, Cost: 19.9905, Comm 0.0080
2020-12-26 19:40:00, STOP SELL PLACED @ 25293.56
2020-12-26 19:40:00, TAKE PROFIT SELL PLACED @ 25822.66
2020-12-26 19:40:00, OPERATION PROFIT, GROSS 0.0099, NET 0.0019
2020-12-26 19:40:00, Close: 25828.81, Signal: 1, Position: 0.00077
!!! position not ok anymore, flipped successfully AND take profit from previous trade executed before it was canceled successfully
2020-12-26 19:40:00, Portfolio Value: 99.38
2020-12-26 19:40:00, Position too large: 0.00077
2020-12-26 19:41:00, Order Canceled/Margin/Rejected