公司多个门户是做二级域名还是做多个网站,已经买了域名怎么做网站,公司宣传片视频制作,做电视外贸什么网站好Backtrader 量化回测实践#xff08;7#xff09;——在jupyter中执行bt的samples
Backtrader提供了大量的测试用例#xff0c;在samples目录下#xff0c;测试程序主要都是用argparse解析参数#xff0c;但是不能在jupyter中直接执行。
找到一个解决方法#xff0c;可…Backtrader 量化回测实践7——在jupyter中执行bt的samples
Backtrader提供了大量的测试用例在samples目录下测试程序主要都是用argparse解析参数但是不能在jupyter中直接执行。
找到一个解决方法可以方便在jupyter中执行samples中的示例。
把datas目录上传到ipython的当前路径以calmar-test.py程序为例简单修改程序如下
#!/usr/bin/env python
# -*- coding: utf-8; py-indent-offset:4 -*-
###############################################################################
#
# Copyright (C) 2015-2023 Daniel Rodriguez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
###############################################################################
from __future__ import (absolute_import, division, print_function,unicode_literals)import argparse
import datetimeimport backtrader as bt%matplotlib inlineclass St(bt.SignalStrategy):params ()def __init__(self):ma1, ma2, bt.ind.SMA(period15), bt.ind.SMA(period50)self.signal_add(bt.signal.SIGNAL_LONG, bt.ind.CrossOver(ma1, ma2))def next2(self):passdef runstrat(argsNone):args parse_args(args)cerebro bt.Cerebro()# Data feed kwargskwargs dict()# Parse from/to-datedtfmt, tmfmt %Y-%m-%d, T%H:%M:%Sfor a, d in ((getattr(args, x), x) for x in [fromdate, todate]):if a:strpfmt dtfmt tmfmt * (T in a)kwargs[d] datetime.datetime.strptime(a, strpfmt)# Data feeddata0 bt.feeds.YahooFinanceCSVData(datanameargs.data0, **kwargs)cerebro.adddata(data0)# Brokercerebro.broker bt.brokers.BackBroker(**eval(dict( args.broker )))cerebro.addanalyzer(bt.analyzers.Calmar)# Sizercerebro.addsizer(bt.sizers.FixedSize, **eval(dict( args.sizer )))# Strategycerebro.addstrategy(St, **eval(dict( args.strat )))# Executest0 cerebro.run(**eval(dict( args.cerebro )))[0]i 1for k, v in st0.analyzers.calmar.get_analysis().items():print(i, : .join((str(k), str(v))))i 1if args.plot: # Plot if requested to#cerebro.plot(**eval(dict( args.plot )))cerebro.plot(iplotFalse)def parse_args(pargsNone):parser argparse.ArgumentParser(formatter_classargparse.ArgumentDefaultsHelpFormatter,description(Sample Skeleton))parser.add_argument(--data0, default./datas/orcl-1995-2014.txt,requiredFalse, helpData to read in)# Defaults for datesparser.add_argument(--fromdate, requiredFalse, default,helpDate[time] in YYYY-MM-DD[THH:MM:SS] format)parser.add_argument(--todate, requiredFalse, default,helpDate[time] in YYYY-MM-DD[THH:MM:SS] format)parser.add_argument(--cerebro, requiredFalse, default,metavarkwargs, helpkwargs in keyvalue format)parser.add_argument(--broker, requiredFalse, default,metavarkwargs, helpkwargs in keyvalue format)parser.add_argument(--sizer, requiredFalse, default,metavarkwargs, helpkwargs in keyvalue format)parser.add_argument(--strat, requiredFalse, default,metavarkwargs, helpkwargs in keyvalue format)parser.add_argument(--plot, requiredFalse, default,nargs?, const{},metavarkwargs, helpkwargs in keyvalue format)return parser.parse_args(pargs)if __name__ __main__:#runstrat()#runstrat(--plot.split())runstrat(--plot --fromdate1998-01-01 --todate2000-01-01.split())
修改点如下 jupyter环境在import 后增加 %matplotlib inline 调整数据导入路径因为在当前路径 parser.add_argument(--data0, default./datas/orcl-1995-2014.txt,requiredFalse, helpData to read in)
3.绘图参数 在jupyter中绘图参数
#cerebro.plot(**eval(dict( args.plot )))
cerebro.plot(iplotFalse)4.调用参数 通过split方法带入调用参数。 #runstrat()runstrat(--plot --fromdate1998-01-01 --todate2000-01-01.split())
修改以上内容后就可以直接在jupyter中执行Backtrader的示例了。