Adding indicator causes order to be rejected when using fractional size
-
I have this simple buy and hold strategy:
from imports import * from indicators import BBW # noinspection PyArgumentList from utils.fractional_commision_info import CommInfoFractional class BuyAndHoldTarget(bt.Strategy): def __init__(self): # bt.indicators.BollingerBands(self.datas[0]) # bbw = BBW(self.datas[0]) # VLI_fast = btind.SmoothedMovingAverage(bbw, period=200) # btind.ExponentialMovingAverage(self.datas[0]) # btind.Highest(period=20).plotinfo.subplot = False # btind.Lowest(period=20).plotinfo.subplot = False # self.val_start = self.broker.get_cash() # keep the starting cash self.BTC = self.datas[0] def nextstart(self): # Buy all the available cash self.broker.addcommissioninfo(CommInfoFractional()) size = self.broker.get_cash() / self.data self.buy(size=size) # self.order_target_value(target=self.broker.get_cash()) def notify_order(self, order): if order.status in [bt.Order.Submitted, bt.Order.Accepted]: return # Await further notifications if order.status == order.Completed: otypetxt = 'Buy ' if order.isbuy() else 'Sell' self.log( ('%s Order Completed - ' 'Size: %.2f @Price: %.2f ' 'Value: %.2f Comm: %.2f') % (otypetxt, order.executed.size, order.executed.price, order.executed.value, order.executed.comm) ) def log(self, txt, dt=None): dt = dt or self.BTC.datetime.datetime() print('%s %s' % (dt.isoformat(), txt))
As you can see it uses fractional sizing to buy. When all the indicators are commented out, it successfully buys:
2015-04-01T02:00:00 Buy Order Completed - Size: 0.41 @Price: 244.20 Value: 99.97 Comm: 0.00
but as soon as I uncomment the indicators, the order gets
margin
status and does not get completed!Here's the order details with indicators commented:
Ref: 1 OrdType: 0 OrdType: Buy Status: 4 Status: Completed Size: 0.4093830597289884 Price: None Price Limit: None TrailAmount: None TrailPercent: None ExecType: 0 ExecType: Market CommInfo: <utils.fractional_commision_info.CommInfoFractional object at 0x7fab218ecc10> End of Session: 735689.9999999999 Info: AutoOrderedDict() Broker: None Alive: False
and here's the order details when indicators are uncommented:
Ref: 1 OrdType: 0 OrdType: Buy Status: 7 Status: Margin Size: 0.4081799257112535 Price: None Price Limit: None TrailAmount: None TrailPercent: None ExecType: 0 ExecType: Market CommInfo: None End of Session: 735689.9999999999 Info: AutoOrderedDict() Broker: None Alive: False
uncommenting only the first indicator will do the job
-
Add commission info before running cerebro. Not inside of the strategy.
-
@hghhgghdf-dfdf That is irrelevant and doesn't change the behaviour
-
Just found out this fixes it:
https://community.backtrader.com/topic/3234/issue-with-percentsizer-2020-11-11-order-canceled-margin-rejectedStill don't know why adding an indicator causes this in the first place