Thanks for the detailed reply!
cerebro.broker.set_checksubmit(checksubmit=False)
Fixed the problem. Thank you so much! :)
Thanks for the detailed reply!
cerebro.broker.set_checksubmit(checksubmit=False)
Fixed the problem. Thank you so much! :)
Digging around, I saw there is also the option of Order.Partial, link: https://www.backtrader.com/docu/order.html
However, seems it is not triggered automatically.
is there a way to
Thanks.
Another general question:
If we know that the AllInSizer have this price gap problem, and we cannot use it with the cheat-on-open option, what would be the correct use case for the AllInSizer?
Thanks.
Thank you so much for the detailed reply!
I have modified my code with the cheat_on_open option, but it seems using the current open price in next_open(), not the next day open price.
class BBand_CrossOver(bt.Strategy):
params = (('period', 20),)
def log(self, txt, dt=None):
dt = dt or self.datas[0].datetime.date(0)
print('{}, {}'.format(dt.isoformat(), txt))
def __init__(self):
self.dataclose = self.data0.close
self.dataopen = self.data0.open
self.bband = bt.indicators.BollingerBands(self.data0, period=self.params.period)
self.buysig = bt.indicators.CrossOver(self.data0, self.bband.lines.bot)
self.sellsig = bt.indicators.CrossOver(self.data0, self.bband.lines.top)
def next_open(self):
self.log('Close: %.2f, Open: %.2f' % (self.dataclose[0], self.dataopen[0]))
if self.position.size:
if self.sellsig < 0:
self.log('SELL CREATE, size {}'.format(self.position.size))
self.sell(size=self.position.size)
elif self.buysig > 0:
size = int(self.broker.getcash() / self.data0.open)
self.log('BUY CREATE, cash {}, size {}, open {}, close {}'.format(self.broker.getcash(), size, self.dataopen[0], self.dataclose[0]))
self.buy(size=size)
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
# Buy/Sell order submitted/accepted to/by broker - Nothing to do
return
# Check if an order has been completed
# Attention: broker could reject order if not enougth cash
if order.status in [order.Completed]:
if order.isbuy():
self.log('BUY EXECUTED, price {}, cost {}, comm {}'.format(order.executed.price, order.executed.value,
order.executed.comm))
elif order.issell():
self.log('SELL EXECUTED, price {}, cost {}, comm {}'.format(order.executed.price, order.executed.value,
order.executed.comm))
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
self.log('Order Canceled/Margin/Rejected')
# Write down: no pending order
self.order = None
def notify_trade(self, trade):
if not trade.isclosed:
return
self.log('OPERATION PROFIT, GROSS {}, NET {}'.format(trade.pnl, trade.pnlcomm))
And here is the main program:
cerebro = bt.Cerebro(cheat_on_open=True)
data = bt.feeds.YahooFinanceData(dataname='BABA', fromdate=datetime.datetime(2016, 1, 1),
todate=bt.datetime.datetime(2016, 12, 31), timeframe=bt.TimeFrame.Days,
compression=1)
cerebro.adddata(data)
cerebro.addstrategy(BBand_CrossOver, period=20)
cerebro.broker.setcash(10000.0)
# cerebro.addsizer(bt.sizers.AllInSizer)
# cerebro.broker.setcommission(commission=0.001)
cerebro.addwriter(bt.WriterFile, csv=True, out='test.csv')
print('start: {}'.format(cerebro.broker.getvalue()))
cerebro.run(optreturn=False)
print('finish: {}'.format(cerebro.broker.getvalue()))
cerebro.plot()
To be more specific, in the output, I have:
2016-06-29, Close: 78.04, Open: 76.98
2016-06-29, BUY CREATE, cash 12071.300000000001, size 156, open 76.98, close 78.04
2016-06-29, Order Canceled/Margin/Rejected
2016-06-30, Close: 79.53, Open: 78.31
Seems here it is using the open price on current day, not next day.
Any suggestions?
Hi,
I am trying to work on a simple Bband strategy, where when close up cross the bband bottom line, I buy; when close down cross the bband top line, I sell.
Here is my code:
class BBand_CrossOver(bt.Strategy):
params = (('period', 20),)
def __init__(self):
self.bband = bt.indicators.BollingerBands(self.data0, period=self.params.period)
self.buysig = bt.indicators.CrossOver(self.data0, self.bband.lines.bot)
self.sellsig = bt.indicators.CrossOver(self.data0, self.bband.lines.top)
def next(self):
if self.position.size:
if self.sellsig < 0:
self.sell()
elif self.buysig > 0:
self.buy()
I tried symbol 'BABA' on it for 2016 data, code:
cerebro = bt.Cerebro()
data = bt.feeds.YahooFinanceData(dataname='BABA', fromdate=datetime.datetime(2016, 1, 1),
todate=bt.datetime.datetime(2016, 12, 31), timeframe=bt.TimeFrame.Days,
compression=1)
cerebro.adddata(data)
cerebro.addstrategy(BBand_CrossOver, period=20)
cerebro.broker.setcash(10000.0)
cerebro.addsizer(bt.sizers.AllInSizer)
cerebro.addwriter(bt.WriterFile, csv=True, out='test.csv')
cerebro.run(optreturn=False)
cerebro.plot()
Interestingly, it did execute some orders, but it did not trigger on all of them.
link: https://www.dropbox.com/s/ysrsbwzde4f00eh/BABA.png?dl=0
You can see it triggered the buy order around Feb 5th, and Sell order around March 20th, which are expected.
However, around June 28th, there should be a up-cross. but the buy order is not triggered.
Is there a good way to debug this?
Can I output the self.bband.lines and the signals in the writer as well?
Thanks very much!