For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Bollinger Band Strategy does not work properly
-
Even though the buy and sell signals get triggered it seems like the orders get executed delayed or whatever. Here is my code for init and next:
def __init__(self): # self.dataclose = self.datas[0].close self.bb_inds = dict() for d in self.datas: bb = bt.ind.BollingerBands(d, period=21, devfactor=2.0, movav=bt.ind.MovAv.Simple) self.bb_inds[d] = dict() self.bb_inds[d]["bb_top"] = bb.top self.bb_inds[d]["bb_bot"] = bb.bot 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 enough cash if order.status in [order.Completed]: if order.isbuy(): self.log('BUY EXECUTED, %.2f' % order.executed.price) elif order.issell(): self.log('SELL EXECUTED, %.2f' % order.executed.price) self.bar_executed = len(self) 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 next(self): for d in self.datas: dt, dn = self.datetime.date(), d._name pos = self.getposition(d).size # Simply log the closing price of the series from the reference # self.log('Close, %.2f' % d.close[0]) # Check if we are in the market #if not self.position: if d.close[0] < self.bb_inds[d]["bb_bot"][0]: # BUY, BUY, BUY!!! (with default parameters) self.log('BUY CREATE, %.2f' % d.close[0]) # Keep track of the created order to avoid a 2nd order #self.order = self.buy() self.buy(data=d, size=1, price=d.close[0]) # Already in the market ... we might sell if d.close[0] > self.bb_inds[d]["bb_top"][0]: # SELL, SELL, SELL!!! (with all possible default parameters) self.log('SELL CREATE, %.2f' % d.close[0]) # Keep track of the created order to avoid a 2nd order #self.order = self.sell() self.sell(data=d, size=1, price=d.close[0])
The plot:
-
Fixed it.
-
@hexabraum what was the issue?