@ab_trader Hi, it does run the close price. However, it seems to work now after I used the slightly edited version of this code in this thread (https://community.backtrader.com/topic/2549/beginner-optimizer-and-analyzer-help/6). Code below. Thanks :)
code_text from __future__ import (absolute_import, division, print_function, unicode_literals) import backtrader.indicators as btind class BBands(bt.Strategy): params = (('BBandsperiod', 20),('logging',True)) def log(self, txt, dt=None): ''' Logging function fot this strategy''' if self.params.logging: dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close # To keep track of pending orders and buy price/commission self.order = None self.buyprice = None self.buycomm = None self.redline = None self.blueline = None # Add a BBand indicator self.bband = bt.indicators.BBands(self.datas[0], period=self.params.BBandsperiod) 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.Canceled, order.Margin]: if order.isbuy(): self.log( 'BUY FAILED, Cancelled or Margin' ) self.log if order.status in [order.Completed, order.Canceled, order.Margin]: if order.isbuy(): self.log( 'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.buyprice = order.executed.price self.buycomm = order.executed.comm else: # Sell self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.bar_executed = len(self) # Write down: no pending order self.order = None def notify_trade(self, trade): if not trade.isclosed: return self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' % (trade.pnl, trade.pnlcomm)) def next(self): # Simply log the closing price of the series from the reference #self.log('Close, %.2f' % self.dataclose[0]) # Check if an order is pending ... if yes, we cannot send a 2nd one if self.order: return if self.dataclose < self.bband.lines.bot and not self.position: self.redline = True if self.dataclose > self.bband.lines.top and self.position: self.blueline = True if self.dataclose > self.bband.lines.mid and not self.position and self.redline: # BUY, BUY, BUY!!! (with all possible default parameters) self.log('BUY CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.buy() if self.dataclose > self.bband.lines.top and not self.position: # BUY, BUY, BUY!!! (with all possible default parameters) self.log('BUY CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.buy() if self.dataclose < self.bband.lines.mid and self.position and self.blueline: # SELL, SELL, SELL!!! (with all possible default parameters) self.log('SELL CREATE, %.2f' % self.dataclose[0]) self.blueline = False self.redline = False # Keep track of the created order to avoid a 2nd order self.order = self.sell() def optimizer_callbacks(cb): print('optcallback executed') if __name__ == '__main__': print('Starting up Cerebro...') cerebro = bt.Cerebro(optreturn=False) cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='sharpe_ratio') cerebro.addanalyzer(bt.analyzers.TradeAnalyzer, _name="trades") cerebro.optcallback(optimizer_callbacks) cerebro.broker.setcommission(commission=0.0) # 0% commission cerebro.optstrategy(BBands, BBandsperiod=range(90,96), logging=False ) cerebro.addsizer(bt.sizers.SizerFix, stake=0.1) start_portfolio_value = 10000.0 cerebro.broker.setcash(start_portfolio_value) #print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) data = bt.feeds.PandasData(dataname = df_daily) cerebro.adddata(data) print('run begin') optimized_runs = cerebro.run(maxcpus=1) print('runs completed: ' + str(len(optimized_runs))) for optimized_run in optimized_runs: for strategy in optimized_run: print(strategy.params.BBandsperiod) try: print(strategy.analyzers.trades.get_analysis()) print(strategy.analyzers.sharpe_ratio.get_analysis()) print('---------------------------------------') except: print('not enough data...')For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
C