Orders executed in weird sequence
nimrare last edited by
I am having a problem with a strategy where I am rebalancing two assets. See the code below:
class RebalancingStrategy(bt.Strategy): def __init__(self, verbose=1): # verbosity self.verbose = verbose def next(self): asset_0_target = 0.75 asset_1_target = 0.25 rebalance_spread = 0.05 # calculate current position weights asset_0_weight = round((self.getposition(self.datas).size * self.datas.close) / self.broker.getvalue(), 4) asset_1_weight = round((self.getposition(self.datas).size * self.datas.close) / self.broker.getvalue(), 4) asset_0_spread = abs(asset_0_target - asset_0_weight) asset_1_spread = abs(asset_1_target - asset_1_weight) total_spread = asset_0_spread + asset_1_spread if total_spread > rebalance_spread: # determine sell order and issue it first if asset_0_weight > asset_0_target: sell_asset_num = 0 sell_asset_target = asset_0_target buy_asset_num = 1 buy_asset_target = asset_1_target else: sell_asset_num = 1 sell_asset_target = asset_1_target buy_asset_num = 0 buy_asset_target = asset_0_target self.order_target_percent(data=self.datas[sell_asset_num], target=sell_asset_target, exectype=bt.Order.Limit, price=self.datas[sell_asset_num].open) self.order_target_percent(data=self.datas[buy_asset_num], target=buy_asset_target, exectype=bt.Order.Limit, price=self.datas[buy_asset_num].open)
For optical reasons I excluded the notify functions. From what I see in the docs, I assume that the order that gets issued first, will be executed first. As you can see, I am issuing the "sell" order first to get free cash for the buy order. Below is the output from the notify functions.
2018-10-02 - BUY VIXY 1148 @ 21.76 2018-10-02 - BUY SPY 257 @ 291.56 2018-10-11 - SELL VIXY -222 @ 27.6 2018-10-11 - BUY SPY 22 @ 277.08 2018-10-25 - SELL VIXY -121 @ 31.58 2018-10-25 - BUY SPY 14 @ 267.38 2018-11-07 - REJ VIXY 99 @ 0.0 2018-11-07 - SELL SPY -9 @ 277.56 2018-11-08 - REJ VIXY 165 @ 0.0 2018-11-08 - SELL SPY -6 @ 280.11 2018-12-10 - SELL VIXY -19 @ 33.23 2018-12-10 - BUY SPY 19 @ 263.37 2018-12-21 - SELL VIXY -92 @ 36.91 2018-12-21 - BUY SPY 13 @ 246.74 2018-12-26 - SELL VIXY -73 @ 40.38 2018-12-26 - BUY SPY 13 @ 235.97
What I don't understand is why sometimes the BUY order is still issued first and therefore obviously rejected. Could you point me to what I am missing. Would be really grateful. :)
I assume that the order that gets issued first, will be executed first
The assumption is partially right and therefore absolutely wrong.
... exectype=bt.Order.Limit, ...
Orders will be evaluated in the order in which they are issued (inserted into the system), i.e.: FIFO. If you were using
Marketorders, that would mean that execution would also follow the FIFO pattern.
But if you issue
Limitorders, and thus you cannot guarantee execution order (you cannot actually even guarantee execution at all, with or without time constraints imposed on the order)
With no single data point it is obviously impossible to show a practical example.
nimrare last edited by
Ah, that makes sense. Thanks a lot for your response.