def runstrat(): args = parse_args() cerebro = bt.Cerebro() cerebro.broker.setcash(1000000) cerebro.broker.setcommission(commission=0.0015) # 真实佣金: 0.15% cerebro.addsizer(bt.sizers.PercentSizer, percents=10) #每次投入10%资金 data = utils.get_csv_data(args) cerebro.adddata(data) cerebro.addstrategy(EnterStrategy2, ) # stratruns =cerebro.run() cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='sharpratio') cerebro.addanalyzer(SQN, _name='sqn') if args.printWriter: cerebro.addwriter(bt.WriterFile) # cerebro.run() thestrats = cerebro.run() if args.printAnalysers: utils.printAnalysers(thestrats) if args.plot: cerebro.plot(**(eval('dict(' + args.plot + ')')))
def runstrat(): args = parse_args() cerebro = bt.Cerebro() cerebro.broker.setcash(1000000) cerebro.broker.setcommission(commission=0.0015) # 真实佣金: 0.15% cerebro.addsizer(bt.sizers.PercentSizer, percents=10) #每次投入10%资金 data = bt.feeds.PandasData(dataname=df, ) cerebro.adddata(data) cerebro.addstrategy(bt.strategies.MA_CrossOver, ) # stratruns =cerebro.run() cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='sharpratio') cerebro.addanalyzer(SQN, _name='sqn') if args.printWriter: cerebro.addwriter(bt.WriterFile) # cerebro.run() thestrats = cerebro.run() if args.printAnalysers: utils.printAnalysers(thestrats) # -p=style='bar' if args.plot: cerebro.plot(**(eval('dict(' + args.plot + ')')))
def runstrat(): args = parse_args() cerebro = bt.Cerebro() cerebro.broker.setcash(1000000) cerebro.broker.setcommission(commission=0.0015) # 真实佣金: 0.15% cerebro.addsizer(bt.sizers.PercentSizer, percents=10) #每次投入10%资金 data = utils.get_csv_data(args) cerebro.adddata(data) cerebro.addstrategy( CrossOver3, fast=10, slow=18 ) # cerebro.addanalyzer(btanal.AnnualReturn) cerebro.addanalyzer(btanal.DrawDown) cerebro.addanalyzer(btanal.VWR) # cerebro.addanalyzer(TradeAnalyzer) cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='sharpratio') cerebro.addanalyzer(SQN, _name='sqn') if args.writer: cerebro.addwriter(bt.WriterFile) # cerebro.run() thestrats = cerebro.run() utils.printAnalysers(thestrats) if args.plot: #plot 参数 plotargs = dict() if args.numfigs: plotargs['numfigs'] = args.numfigs if args.plotstyle: plotargs['style'] = args.plotstyle cerebro.plot(**plotargs)
def runstrat(): args = parse_args() cerebro = bt.Cerebro() cerebro.broker.setcash(1000000) cerebro.broker.setcommission(commission=0.0015) # 真实佣金: 0.15% cerebro.addsizer(bt.sizers.PercentSizer, percents=10) #每次投入10%资金 data = datalib.get_csv_data(args) cerebro.adddata(data) cerebro.optstrategy( CrossOver3, fast=range(5, 10), # slow=range(10,12) ) if args.printWriter: cerebro.addwriter(bt.WriterFile) # stratruns =cerebro.run() cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='sharpratio') cerebro.addanalyzer(SQN, _name='sqn') if args.printWriter: cerebro.addwriter(bt.WriterFile) # cerebro.run() thestrats = cerebro.run() if args.printAnalysers: utils.printAnalysers(thestrats) if args.plot: #plot 参数 plotargs = dict() if args.numfigs: plotargs['numfigs'] = args.numfigs if args.plotstyle: plotargs['style'] = args.plotstyle cerebro.plot(**plotargs)
def main(): args = _parse_args() # 选择策略 strats = dict( c2=strat.CrossOver2, c3=strat.CrossOver3, ) # 时间周奇 tframes = dict(D=bt.TimeFrame.Days, W=bt.TimeFrame.Weeks, M=bt.TimeFrame.Months) cerebro = bt.Cerebro() cerebro.broker.setcash(1000000) cerebro.broker.setcommission(commission=0.0015) # 真实佣金: 0.15% cerebro.addsizer(bt.sizers.PercentSizer, percents=10) # 每次投入10%资金 # df = datalib.get_df_data(args.code, index=True) # df = # stock = Stock('002119') # sd = SecurityData(stock, TushareSource, fromdate=args.fromdate) # df = sd.get_data() sd = SymbolData(17, fromdate='2017-01-01') # df = sd.get_daily_price(output='df') kwargs = dict() # 开始时间 if args.fromdate: kwargs['fromdate'] = datetime.datetime.strptime( args.fromdate, '%Y-%m-%d') # 结束时间 if args.todate: kwargs['todate'] = datetime.datetime.strptime(args.todate, '%Y-%m-%d') data = bt.feeds.PandasData(dataname=sd.df, **kwargs) # 时间周期 if args.ktype == 'D': cerebro.adddata(data) else: # Resample the data cerebro.resampledata(data, timeframe=tframes[args.ktype]) cerebro.addstrategy(strats[args.strategy], ) # cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='sharpratio') # cerebro.addanalyzer(SQN, _name='sqn') # cerebro.run() thestrats = cerebro.run() utils.printAnalysers(thestrats) if args.plot: cerebro.plot(style='candle')