def runstrat(): args = parse_args() cerebro = bt.Cerebro() data = btfeeds.BacktraderCSVData(dataname=args.data) cerebro.adddata(data) cerebro.addstrategy(St, datalines=args.datalines, lendetails=args.lendetails) cerebro.run(runonce=False, exactbars=args.save) if args.plot: cerebro.plot(style='bar')
def runstrategy(): args = parse_args() # Create a cerebro cerebro = bt.Cerebro() # Get the dates from the args fromdate = datetime.datetime.strptime(args.fromdate, '%Y-%m-%d') todate = datetime.datetime.strptime(args.todate, '%Y-%m-%d') # Create the 1st data data = btfeeds.BacktraderCSVData( dataname=args.data, fromdate=fromdate, todate=todate) # Add the 1st data to cerebro cerebro.adddata(data) # Add a strategy cerebro.addstrategy(SMACrossOver, period=args.period, stake=args.stake) # Add the commission - only stocks like a for each operation cerebro.broker.setcash(args.cash) commtypes = dict( none=None, perc=bt.CommInfoBase.COMM_PERC, fixed=bt.CommInfoBase.COMM_FIXED) # Add the commission - only stocks like a for each operation cerebro.broker.setcommission(commission=args.comm, mult=args.mult, margin=args.margin, percabs=not args.percrel, commtype=commtypes[args.commtype], stocklike=args.stocklike) # And run it cerebro.run() # Plot if requested if args.plot: cerebro.plot(numfigs=args.numfigs, volume=False)
class testStrategy(bt.Strategy): def next(self): print( self.data0.datetime.datetime(0), len(self.data0), self.data0.open[0], self.data0.high[0], self.data0.low[0], self.data0.close[0], ) # Create a cerebro entity cerebro = bt.Cerebro(stdstats=False) # Add a strategy cerebro.addstrategy(testStrategy) # Load the Data datapath = './2006-min-005.csv' data = btfeeds.BacktraderCSVData( dataname=datapath, timeframe=bt.TimeFrame.Minutes, todate=datetime(2006, 1, 3), ) cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=2) cerebro.run()