Cerebro是控制中心:
1.收集所有的输入(Data Feeds)、演员(Stratgegies)、观众(Observers)、评论家(Analyzers)和记录者(Writers),确保表演(Show)在任何时候都继续。
2.执行回测,数据载入,交易。
3.返回回测结果。
4.结果绘图展示。
cerebro = bt.Cerebro(**kwargs)
看看cerebro 的方法和属性:
add_order_history,add_signal,add_timer,addanalyzer,addcalendar,adddata,adddatacb,
addindicator,addobserver,addobservermulti,addsizer,addsizer_byidx,addstore,addstorecb,
addstrategy,addtz,addwriter,analyzers,broker,chaindata,datacbs,datas,datasbyname,feeds,
frompackages,getbroker,indicators,iterize,notify_data,notify_store,notify_timer,observers,
optcallback,optcbs,optstrategy,p,packages,params,plot,replaydata,resampledata,
rolloverdata,run,runstop,runstrategies,set_fund_history,setbroker,signal_accumulate,
signal_concurrent,signal_strategy,signals,sizers,stop_writers,storecbs,stores,strats,writers
通过cerebro.py看一下初始化和内部参数:
内部参数:
params = (
('preload', True),
('runonce', True),
('maxcpus', None),
('stdstats', True),
('oldbuysell', False),
('oldtrades', False),
('lookahead', 0),
('exactbars', False),
('optdatas', True),
('optreturn', True),
('objcache', False),
('live', False),
('writer', False),
('tradehistory', False),
('oldsync', False),
('tz', None),
('cheat_on_open', False),
('broker_coo', True),
('quicknotify', False),
)
初始化变量:
def __init__(self):
self._dolive = False
self._doreplay = False
self._dooptimize = False
self.stores = list()
self.feeds = list()
self.datas = list()
self.datasbyname = collections.OrderedDict()
self.strats = list()
self.optcbs = list() # holds a list of callbacks for opt strategies
self.observers = list()
self.analyzers = list()
self.indicators = list()
self.sizers = dict()
self.writers = list()
self.storecbs = list()
self.datacbs = list()
self.signals = list()
self._signal_strat = (None, None, None)
self._signal_concurrent = False
self._signal_accumulate = False
self._dataid = itertools.count(1)
self._broker = BackBroker()
self._broker.cerebro = self
self._tradingcal = None # TradingCalendar()
self._pretimers = list()
self._ohistory = list()
self._fhistory = None
常规加载:
data = bt.BacktraderCSVData(dataname='mypath.days', timeframe=bt.TimeFrame.Days)
cerebro.adddata(data)
resample重采样加载:
data = bt.BacktraderCSVData(dataname='mypath.min', timeframe=bt.TimeFrame.Minutes)
cerebro.resampledata(data, timeframe=bt.TimeFrame.Days)
replay重演加载:
data = bt.BacktraderCSVData(dataname='mypath.min', timeframe=bt.TimeFrame.Minutes)
cerebro.replaydata(data, timeframe=bt.TimeFrame.Days)
时间框架bt.TimeFrame的参数值:
Days,MicroSeconds,Minutes,Months,Names,NoTimeFrame,Seconds,TFrame,TName,Ticks,Weeks,Years,getname,names
重新采样和数据重演的目的为了能够处理周期不一致的限制:时间周期对齐。
后面还要介绍:数据-多时间帧、数据重采样-重采样’和数据-重放部分。
与数据载入不同,数据载入后直接实例化。cerebro直接接受策略类和传递给它的参数。策略类可以实例化多次,并传递不同的参数。
cerebro.addstrategy(MyStrategy, myparam1=value1, myparam2=value2)
也可以用可迭代的参数,优化策略可以用参数从10到19,执行10次。
cerebro.optstrategy(MyStrategy, myparam1=range(10, 20))
还要其他的方法,用于增加回测的效果:
可以使用默认的broker,根据实际需要也可以重写:
broker = MyBroker()
cerebro.broker = broker # property using getbroker/setbroker methods
feed或broker 都可以发送通知,通过Cerebro.notify_store方法接收通知。有三种方式来处理通知:
一、通过cerebro的addnotifycallback(callback)方法调用
callback(msg, *args, **kwargs)
二、在Strategy类中重写notify_store方法,加入到cerebro实例中。
三、重写notify_store,子类Cerebro并覆盖notify_store,推荐方式。
如果没有定义,cerebro有三个默认observer :
Broker observer 跟踪资金和投资组合;
Trades observer跟踪投资效率;
Buy/Sell observer记录操作执行的结果;
result = cerebro.run(**kwargs)
addstrategy 加入的所有策略,返回回测结果是list;
optstrategy加入的1个或多个优化策略,返回回测结果是list of list 类型,记录所有优化策略的结果。
cerebro.plot()
后续专门章节介绍。
csv数据流包括data feeds, strategies, indicators and observers
属性:
示例:
class MyStrategy(bt.Strategy):
def __init__(self):
self.dataclose = self.datas[0].close
self.sma5 = bt.indicators.SimpleMovingAverage(self.dataclose, period=5)
self.sma20 = bt.indicators.SimpleMovingAverage(self.dataclose, period=20)
def next(self):
if self.position:
self.close()
else:
if self.sma5 > self.sma20:
self.buy()
else:
self.sell()
# 创建Cerebro引擎 ,自定义
cerebro = declare_cerebar()
cerebro.addstrategy(MyStrategy)
# 使用WriterFile保存结果到文件
writer = cerebro.addwriter(bt.WriterFile,out='./my_strategy_output.csv',csv=True)
# 运行策略并保存结果到文件
cerebro.run()
输出的csv文件格式:
加载了两个数据源,可以看到有策略,有broker ,有交易的记录,最后是一个策略的总结。
===============================================================================
Id,,len,datetime,open,high,low,close,volume,openinterest,,len,datetime,open,high,low,close,volume,openinterest,MyStrategy,len,datetime,Broker,len,cash,value,BuySell,len,buy,sell,BuySell,len,buy,sell,DataTrades,len,,
1,,1,2019-01-02 00:00:00,51.6,51.8,49.83,49.94,232267.24,,,1,2019-01-02 00:00:00,4.8,5.07,4.76,5.03,461546.56,,MyStrategy,1,737061.0,Broker,1,10000.0,10000.0,BuySell,1,,,BuySell,1,,,DataTrades,1,,
2,,2,2019-01-03 00:00:00,50.04,50.04,48.27,48.96,278029.62,,,2,2019-01-03 00:00:00,4.98,5.22,4.84,5.12,527675.55,,MyStrategy,2,737062.0,Broker,2,10000.0,10000.0,BuySell,2,,,BuySell,2,,,DataTrades,2,,
3,,3,2019-01-04 00:00:00,48.23,49.78,48.0,49.54,241498.01,,,3,2019-01-04 00:00:00,4.94,5.63,4.91,5.49,962593.78,,MyStrategy,3,737063.0,Broker,3,10000.0,10000.0,BuySell,3,,,BuySell,3,,,DataTrades,3,,
4,,4,2019-01-07 00:00:00,49.9,50.15,49.03,49.31,233796.93,,,4,2019-01-07 00:00:00,5.41,6.03,5.36,5.79,936970.24,,MyStrategy,4,737066.0,Broker,4,10000.0,10000.0,BuySell,4,,,BuySell,4,,,DataTrades,4,,
5,,5,2019-01-08 00:00:00,49.48,49.6,48.88,49.02,167844.39,,,5,2019-01-08 00:00:00,5.71,5.97,5.6,5.79,697336.36,,MyStrategy,5,737067.0,Broker,5,10000.0,10000.0,BuySell,5,,,BuySell,5,,,DataTrades,5,,
6,,6,2019-01-09 00:00:00,49.41,50.67,49.15,50.03,330784.86,,,6,2019-01-09 00:00:00,5.69,6.21,5.65,5.8,859710.4,,MyStrategy,6,737068.0,Broker,6,10000.0,10000.0,BuySell,6,,,BuySell,6,,,DataTrades,6,,
7,,7,2019-01-10 00:00:00,49.88,50.15,49.23,49.86,186374.44,,,7,2019-01-10 00:00:00,5.8,6.04,5.68,5.84,572924.11,,MyStrategy,7,737069.0,Broker,7,10000.0,10000.0,BuySell,7,,,BuySell,7,,,DataTrades,7,,
8,,8,2019-01-11 00:00:00,50.05,50.77,49.72,50.69,247667.79,,,8,2019-01-11 00:00:00,6.1,6.14,5.6,5.67,662283.54,,MyStrategy,8,737070.0,Broker,8,10000.0,10000.0,BuySell,8,,,BuySell,8,,,DataTrades,8,,
9,,9,2019-01-14 00:00:00,50.5,50.59,49.48,49.59,217800.59,,,9,2019-01-14 00:00:00,5.67,5.85,5.64,5.74,438742.73,,MyStrategy,9,737073.0,Broker,9,10000.0,10000.0,BuySell,9,,,BuySell,9,,,DataTrades,9,,
10,,10,2019-01-15 00:00:00,49.75,53.1,49.51,52.97,500413.49,,,10,2019-01-15 00:00:00,5.7,5.75,5.4,5.58,435452.63,,MyStrategy,10,737074.0,Broker,10,10000.0,10000.0,BuySell,10,,,BuySell,10,,,DataTrades,10,,
11,,11,2019-01-16 00:00:00,52.8,52.8,52.03,52.2,330077.96,,,11,2019-01-16 00:00:00,5.6,5.61,5.4,5.46,298606.81,,MyStrategy,11,737075.0,Broker,11,10000.0,10000.0,BuySell,11,,,BuySell,11,,,DataTrades,11,,
12,,12,2019-01-17 00:00:00,52.4,52.79,51.56,51.92,233909.86,,,12,2019-01-17 00:00:00,5.46,5.54,5.37,5.42,272807.03,,MyStrategy,12,737076.0,Broker,12,10000.0,10000.0,BuySell,12,,,BuySell,12,,,DataTrades,12,,
13,,13,2019-01-18 00:00:00,52.2,54.5,52.2,53.88,521235.34,,,13,2019-01-18 00:00:00,5.43,5.5,5.26,5.33,275508.98,,MyStrategy,13,737077.0,Broker,13,10000.0,10000.0,BuySell,13,,,BuySell,13,,,DataTrades,13,,
14,,14,2019-01-21 00:00:00,54.65,56.37,54.62,56.0,549111.73,,,14,2019-01-21 00:00:00,5.33,5.58,5.33,5.5,369402.01,,MyStrategy,14,737080.0,Broker,14,10000.0,10000.0,BuySell,14,,,BuySell,14,,,DataTrades,14,,
15,,15,2019-01-22 00:00:00,56.0,56.0,54.65,55.21,335691.56,,,15,2019-01-22 00:00:00,5.49,5.53,5.34,5.39,197944.78,,MyStrategy,15,737081.0,Broker,15,10000.0,10000.0,BuySell,15,,,BuySell,15,,,DataTrades,15,,
16,,16,2019-01-23 00:00:00,54.88,55.55,54.6,55.07,202242.28,,,16,2019-01-23 00:00:00,5.37,5.39,5.21,5.35,181521.29,,MyStrategy,16,737082.0,Broker,16,10000.0,10000.0,BuySell,16,,,BuySell,16,,,DataTrades,16,,
17,,17,2019-01-24 00:00:00,55.3,55.75,54.5,55.2,193597.61,,,17,2019-01-24 00:00:00,5.35,5.55,5.33,5.47,271009.73,,MyStrategy,17,737083.0,Broker,17,10000.0,10000.0,BuySell,17,,,BuySell,17,,,DataTrades,17,,
18,,18,2019-01-25 00:00:00,55.55,55.85,54.72,55.25,241740.03,,,18,2019-01-25 00:00:00,5.42,5.45,5.17,5.19,284873.61,,MyStrategy,18,737084.0,Broker,18,10000.0,10000.0,BuySell,18,,,BuySell,18,,,DataTrades,18,,
19,,19,2019-01-28 00:00:00,56.0,58.55,55.5,57.47,592375.46,,,19,2019-01-28 00:00:00,5.24,5.26,5.0,5.03,190373.5,,MyStrategy,19,737087.0,Broker,19,10000.0,10000.0,BuySell,19,,,BuySell,19,,,DataTrades,19,,
20,,20,2019-01-29 00:00:00,57.6,58.58,56.7,58.58,317132.16,,,20,2019-01-29 00:00:00,4.99,5.09,4.75,4.95,191113.67,,MyStrategy,20,737088.0,Broker,20,10000.0,10000.0,BuySell,20,,,BuySell,20,,,DataTrades,20,,
21,,21,2019-01-30 00:00:00,58.39,59.17,57.2,57.28,286801.78,,,21,2019-01-30 00:00:00,4.82,4.92,4.67,4.9,178214.78,,MyStrategy,21,737089.0,Broker,21,9941.61,9998.890000000001,BuySell,21,56.342,,BuySell,21,,,DataTrades,21,,
22,,22,2019-01-31 00:00:00,57.71,60.66,56.61,60.3,527323.68,,,22,2019-01-31 00:00:00,4.9,4.9,4.62,4.65,190980.32,,MyStrategy,22,737090.0,Broker,22,9999.32,9999.32,BuySell,22,,61.56989999999999,BuySell,22,,,DataTrades,22,-0.6799999999999997,
23,,23,2019-02-01 00:00:00,60.8,61.36,60.22,61.18,390354.08,,,23,2019-02-01 00:00:00,4.7,4.76,4.66,4.74,97998.84,,MyStrategy,23,737091.0,Broker,23,9938.52,9999.7,BuySell,23,59.3167,,BuySell,23,,,DataTrades,23,,
24,,24,2019-02-11 00:00:00,61.87,65.23,60.6,64.7,433981.99,,,24,2019-02-11 00:00:00,4.75,4.99,4.74,4.95,210097.92,,MyStrategy,24,737101.0,Broker,24,10000.390000000001,10000.390000000001,BuySell,24,,66.20845,BuySell,24,,,DataTrades,24,1.0700000000000003,
25,,25,2019-02-12 00:00:00,64.85,66.77,64.3,64.75,437641.7,,,25,2019-02-12 00:00:00,4.96,5.14,4.91,5.06,246317.16,,MyStrategy,25,737102.0,Broker,25,9935.54,10000.29,BuySell,25,63.335499999999996,,BuySell,25,,,DataTrades,25,,
26,,26,2019-02-13 00:00:00,65.48,66.24,64.48,66.07,374356.49,,,26,2019-02-13 00:00:00,5.06,5.22,5.02,5.18,317691.22,,MyStrategy,26,737103.0,Broker,26,10001.02,10001.02,BuySell,26,,67.23359999999998,BuySell,26,,,DataTrades,26,0.6300000000000097,
27,,27,2019-02-14 00:00:00,65.95,68.3,65.59,67.96,446146.46,,,27,2019-02-14 00:00:00,5.16,5.17,5.08,5.11,203807.04,,MyStrategy,27,737104.0,Broker,27,9935.07,10003.029999999999,BuySell,27,64.60615,,BuySell,27,,,DataTrades,27,,
28,,28,2019-02-15 00:00:00,68.0,68.2,66.07,66.4,311272.11,,,28,2019-02-15 00:00:00,5.17,5.21,5.1,5.13,186803.69,,MyStrategy,28,737105.0,Broker,28,10003.07,10003.07,BuySell,28,,69.223,BuySell,28,,,DataTrades,28,2.049999999999997,
29,,29,2019-02-18 00:00:00,67.1,69.9,67.1,69.87,439770.28,,,29,2019-02-18 00:00:00,5.13,5.32,5.13,5.31,273832.18,,MyStrategy,29,737108.0,Broker,29,9935.97,10005.84,BuySell,29,66.09349999999999,,BuySell,29,,,DataTrades,29,,
30,,30,2019-02-19 00:00:00,69.68,70.47,68.61,69.67,339648.52,,,30,2019-02-19 00:00:00,5.34,5.47,5.22,5.41,361249.94,,MyStrategy,30,737109.0,Broker,30,10005.65,10005.65,BuySell,30,,71.52704999999999,BuySell,30,,,DataTrades,30,2.5800000000000125,
31,,31,2019-02-20 00:00:00,69.5,70.48,67.8,69.01,296281.4,,,31,2019-02-20 00:00:00,5.36,5.38,5.21,5.36,233148.01,,MyStrategy,31,737110.0,Broker,31,9936.15,10005.16,BuySell,31,66.783,,BuySell,31,,,DataTrades,31,,
32,,32,2019-02-21 00:00:00,68.96,69.24,67.56,68.05,283498.05,,,32,2019-02-21 00:00:00,5.32,5.64,5.32,5.42,487008.17,,MyStrategy,32,737111.0,Broker,32,10005.109999999999,10005.109999999999,BuySell,32,,70.27859999999998,BuySell,32,,,DataTrades,32,-0.5400000000000063,
33,,33,2019-02-22 00:00:00,67.39,68.5,66.8,68.5,238045.24,,,33,2019-02-22 00:00:00,5.38,5.68,5.33,5.62,465397.77,,MyStrategy,33,737112.0,Broker,33,9937.72,10006.22,BuySell,33,65.798,,BuySell,33,,,DataTrades,33,,
34,,34,2019-02-25 00:00:00,68.66,71.85,67.49,71.26,593506.05,,,34,2019-02-25 00:00:00,5.7,5.93,5.61,5.82,618352.14,,MyStrategy,34,737115.0,Broker,34,10006.38,10006.38,BuySell,34,,72.92774999999999,BuySell,34,,,DataTrades,34,1.269999999999996,
35,,35,2019-02-26 00:00:00,70.96,71.0,68.8,68.92,450653.52,,,35,2019-02-26 00:00:00,5.83,5.91,5.59,5.63,583633.68,,MyStrategy,35,737116.0,Broker,35,9935.42,10004.34,BuySell,35,67.768,,BuySell,35,,,DataTrades,35,,
36,,36,2019-02-27 00:00:00,69.55,72.48,69.15,70.06,488307.43,,,36,2019-02-27 00:00:00,5.63,5.98,5.62,5.88,625095.67,,MyStrategy,36,737117.0,Broker,36,10004.97,10004.97,BuySell,36,,73.5672,BuySell,36,,,DataTrades,36,-1.4099999999999966,
37,,37,2019-02-28 00:00:00,69.8,72.6,68.51,71.5,394477.95,,,37,2019-02-28 00:00:00,5.84,5.85,5.62,5.69,348954.09,,MyStrategy,37,737118.0,Broker,37,9935.17,10006.67,BuySell,37,67.48235000000001,,BuySell,37,,,DataTrades,37,,
38,,38,2019-03-01 00:00:00,72.1,76.12,71.7,75.9,493189.98,,,38,2019-03-01 00:00:00,5.72,6.13,5.64,5.97,689847.76,,MyStrategy,38,737119.0,Broker,38,10007.27,10007.27,BuySell,38,,77.2618,BuySell,38,,,DataTrades,38,2.299999999999997,
39,,39,2019-03-04 00:00:00,76.59,79.5,75.51,77.25,612471.24,,,39,2019-03-04 00:00:00,6.08,6.35,5.88,5.96,788235.36,,MyStrategy,39,737122.0,Broker,39,9930.68,10007.93,BuySell,39,74.37735,,BuySell,39,,,DataTrades,39,,
40,,40,2019-03-05 00:00:00,77.24,78.56,76.77,77.77,332858.88,,,40,2019-03-05 00:00:00,6.03,6.56,5.95,6.56,754527.92,,MyStrategy,40,737123.0,Broker,40,10007.92,10007.92,BuySell,40,,79.7384,BuySell,40,,,DataTrades,40,0.6499999999999915,
41,,41,2019-03-06 00:00:00,77.25,77.4,76.0,76.99,341850.96,,,41,2019-03-06 00:00:00,7.19,7.22,7.02,7.22,938817.64,,MyStrategy,41,737124.0,Broker,41,9930.67,10007.66,BuySell,41,74.86,,BuySell,41,,,DataTrades,41,,
42,,42,2019-03-07 00:00:00,76.08,76.08,73.2,73.97,464176.64,,,42,2019-03-07 00:00:00,7.8,7.94,7.41,7.94,1770102.57,,MyStrategy,42,737125.0,Broker,42,10006.75,10006.75,BuySell,42,,77.2212,BuySell,42,,,DataTrades,42,-1.1700000000000017,
43,,43,2019-03-08 00:00:00,71.74,74.38,71.01,71.44,421400.85,,,43,2019-03-08 00:00:00,7.37,7.78,7.15,7.15,1298727.29,,MyStrategy,43,737126.0,Broker,43,9935.01,10006.45,BuySell,43,69.94485,,BuySell,43,,,DataTrades,43,,
44,,44,2019-03-11 00:00:00,71.6,76.51,71.6,76.0,453330.22,,,44,2019-03-11 00:00:00,7.15,7.45,6.75,7.29,784953.21,,MyStrategy,44,737129.0,Broker,44,10006.61,10006.61,BuySell,44,,77.65765,BuySell,44,,,DataTrades,44,-0.14000000000000057,
45,,45,2019-03-12 00:00:00,75.88,77.15,74.77,75.3,345975.22,,,45,2019-03-12 00:00:00,7.3,7.46,7.17,7.35,825604.04,,MyStrategy,45,737130.0,Broker,45,9930.730000000001,10006.03,BuySell,45,73.64845,,BuySell,45,,,DataTrades,45,,
46,,46,2019-03-13 00:00:00,75.0,77.16,74.65,76.5,320983.42,,,46,2019-03-13 00:00:00,7.35,7.36,6.65,6.82,792530.51,,MyStrategy,46,737131.0,Broker,46,10005.730000000001,10005.730000000001,BuySell,46,,78.31739999999999,BuySell,46,,,DataTrades,46,-0.8799999999999955,
47,,47,2019-03-14 00:00:00,76.9,78.99,75.72,77.15,438312.52,,,47,2019-03-14 00:00:00,6.7,6.82,6.31,6.47,514788.72,,MyStrategy,47,737132.0,Broker,47,9928.830000000002,10005.980000000001,BuySell,47,74.5842,,BuySell,47,,,DataTrades,47,,
48,,48,2019-03-15 00:00:00,76.87,79.59,75.01,78.78,407666.89,,,48,2019-03-15 00:00:00,6.5,6.64,6.35,6.51,403073.54,,MyStrategy,48,737133.0,Broker,48,10005.700000000003,10005.700000000003,BuySell,48,,80.78385,BuySell,48,,,DataTrades,48,-0.030000000000001137,
49,,49,2019-03-18 00:00:00,78.68,86.66,78.5,86.66,785028.53,,,49,2019-03-18 00:00:00,6.55,6.69,6.38,6.69,385961.92,,MyStrategy,49,737136.0,Broker,49,9927.020000000002,10013.680000000002,BuySell,49,77.3225,,BuySell,49,,,DataTrades,49,,
50,,50,2019-03-19 00:00:00,84.93,88.61,84.3,86.0,614712.64,,,50,2019-03-19 00:00:00,6.65,7.32,6.6,7.06,755922.23,,MyStrategy,50,737137.0,Broker,50,10011.950000000003,10011.950000000003,BuySell,50,,89.93914999999998,BuySell,50,,,DataTrades,50,6.25,
51,,51,2019-03-20 00:00:00,85.0,88.18,84.4,85.7,404122.61,,,51,2019-03-20 00:00:00,7.0,7.0,6.66,6.86,432019.58,,MyStrategy,51,737138.0,Broker,51,9926.950000000003,10012.650000000003,BuySell,51,83.134,,BuySell,51,,,DataTrades,51,,
52,,52,2019-03-21 00:00:00,85.72,86.0,84.53,84.9,218635.19,,,52,2019-03-21 00:00:00,6.89,7.32,6.89,7.04,653863.21,,MyStrategy,52,737139.0,Broker,52,10012.670000000002,10012.670000000002,BuySell,52,,87.28999999999999,BuySell,52,,,DataTrades,52,0.7199999999999989,
53,,53,2019-03-22 00:00:00,84.55,86.62,84.01,86.05,287228.31,,,53,2019-03-22 00:00:00,6.96,7.44,6.96,7.34,696941.56,,MyStrategy,53,737140.0,Broker,53,9928.120000000003,10014.170000000002,BuySell,53,82.74985000000001,,BuySell,53,,,DataTrades,53,,
54,,54,2019-03-25 00:00:00,84.5,85.5,82.3,82.81,333485.23,,,54,2019-03-25 00:00:00,7.18,7.3,7.0,7.08,470823.55,,MyStrategy,54,737143.0,Broker,54,10012.620000000003,10012.620000000003,BuySell,54,,86.78249999999998,BuySell,54,,,DataTrades,54,-0.04999999999999716,
55,,55,2019-03-26 00:00:00,83.2,85.67,83.17,84.47,323212.93,,,55,2019-03-26 00:00:00,7.14,7.18,6.73,6.79,392781.78,,MyStrategy,55,737144.0,Broker,55,9929.420000000002,10013.890000000001,BuySell,55,81.92245,,BuySell,55,,,DataTrades,55,,
56,,56,2019-03-27 00:00:00,85.38,89.95,84.55,87.98,494442.64,,,56,2019-03-27 00:00:00,6.83,6.86,6.42,6.65,323508.32,,MyStrategy,56,737145.0,Broker,56,10014.800000000001,10014.800000000001,BuySell,56,,91.29925,BuySell,56,,,DataTrades,56,2.1799999999999926,
57,,57,2019-03-28 00:00:00,89.8,91.89,88.01,90.3,607345.71,,,57,2019-03-28 00:00:00,6.59,7.18,6.56,6.76,395130.51,,MyStrategy,57,737146.0,Broker,57,9925.000000000002,10015.300000000001,BuySell,57,86.68985,,BuySell,57,,,DataTrades,57,,
58,,58,2019-03-29 00:00:00,91.5,98.08,91.14,95.0,574995.53,,,58,2019-03-29 00:00:00,6.77,6.98,6.45,6.9,382708.62,,MyStrategy,58,737147.0,Broker,58,10016.500000000002,10016.500000000002,BuySell,58,,99.5512,BuySell,58,,,DataTrades,58,1.7000000000000028,
===============================================================================
Cerebro:
-----------------------------------------------------------------------------
- Datas:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Data0:
- Name:
- Timeframe: Days
- Compression: 1
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Data1:
- Name:
- Timeframe: Days
- Compression: 1
-----------------------------------------------------------------------------
- Strategies:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- MyStrategy:
*************************************************************************
- Params:
*************************************************************************
- Indicators:
.......................................................................
- SimpleMovingAverage:
- Lines: sma
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Params:
- period: 20
*************************************************************************
- Observers:
.......................................................................
- Broker:
- Lines: cash, value
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Params:
- fund: None
.......................................................................
- BuySell:
- Lines: buy, sell
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Params:
- barplot: True
- bardist: 0.015
.......................................................................
- DataTrades:
- Lines: ,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Params:
- usenames: True
*************************************************************************
- Analyzers:
.......................................................................
- Value:
- Begin: 10000.0
- End: 10016.500000000002
所有异常的基类是BacktraderError,它是Exception的直接子类 。
两种定义方式:
(1)从Backtrader的内置类errors
import backtrader as bt
class Strategy(bt.Strategy):
def __init__(self):
if something_goes_wrong():
raise bt.errors.StrategySkipError
(2)直接从Backtrader
import backtrader as bt
class Strategy(bt.Strategy):
def __init__(self):
if something_goes_wrong():
raise bt.StrategySkipError
Cerebro的内存管理和优化提升,与能够运行一个完整策略关系不大,当前目标是能够实现定制化的策略,进行回测。内存管理和优化提升是性能优化方面的技术资料。后期有需要的时候再学习。