class backtrader.analyzers.PeriodStats()
时间段内基础统计信息
参数:
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器
ret_anal = cerebro.addanalyzer(bt.analyzers.PeriodStats, _name='myAnalysis')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印
for ret_key,ret_val in thestrat.analyzers.myAnalysis.get_analysis().items():
#if ret_val > 0:
print(ret_key,' :',ret_val)
结果如下:
average : 0.024533824266138593
stddev : 0.0012478242661386751
positive : 2
negative : 0
nochange : 0
best : 0.025781648532277268
worst : 0.023285999999999918
class backtrader.analyzers.Returns()
使用对数法计算的总回报、平均回报、复合回报和年化回报
参数:
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器
ret_anal = cerebro.addanalyzer(bt.analyzers.Returns, _name='myAnalysis')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印
for ret_key,ret_val in thestrat.analyzers.myAnalysis.get_analysis().items():
#if ret_val > 0:
print(ret_key,' :',ret_val)
结果:
总回报、平均回报、复合回报和年化回报
rtot : 0.04847392369449283
ravg : 9.467563221580632e-05
rnorm : 0.02414514457151587
rnorm100 : 2.414514457151587
总回报率:Total return rate
平均回报率:Average Return
复合回报率:Compound rate of return
年化回报率:Annualized rate of return
rnorm100 = rnorm * 100 ,百分数。
指标好像少一个,如何对应? ?
class backtrader.analyzers.SharpeRatio()
分析器使用无风险资产(简单说就是利率为基准)计算策略的夏普比率 。
参数:
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器
ret_anal = cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='myAnalysis')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印
for ret_key,ret_val in thestrat.analyzers.myAnalysis.get_analysis().items():
#if ret_val > 0:
print(ret_key,' :',ret_val)
#SharpeRatio_A 11.647332609673251
结果:
sharperatio : 11.647332609673251
class backtrader.analyzers.SharpeRatio_A()
以年化形式直接返回夏普比率的夏普比率的扩展
参数变化SharpeRatio更改为:
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器
ret_anal = cerebro.addanalyzer(bt.analyzers.SharpeRatio_A, _name='myAnalysis')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印
for ret_key,ret_val in thestrat.analyzers.myAnalysis.get_analysis().items():
#if ret_val > 0:
print(ret_key,' :',ret_val)
#SharpeRatio_A 11.647332609673251
结果与SharpeRatio一样:
sharperatio : 11.647332609673251
class backtrader.analyzers.SQN()
SQN或系统质量号。由Van K. Tharp定义,用于对交易系统进行分类:
SQN 是一个用于评估投资组合或交易策略性能的指标,它结合了多个因素来评估策略的表现。
SQN 的计算公式如下:
SQN = ((\text{Return} - \text{Benchmark Return}) / \text{Volatility})
其中:
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器
ret_anal = cerebro.addanalyzer(bt.analyzers.SQN, _name='myAnalysis')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印
for ret_key,ret_val in thestrat.analyzers.myAnalysis.get_analysis().items():
#if ret_val > 0:
print(ret_key,' :',ret_val)
结果:
sqn : 1.1860238182921676
trades : 8
SMA策略低于平均值
trades : 8 ,是指交易次数 ??
class backtrader.analyzers.TimeReturn()
分析器通过查看时间范围的开始和结束来计算回报。
分析器用于计算基于时间的收益率,即根据时间间隔(如每日、每周、每月等)计算资产的收益率。
参数:
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器
ret_anal = cerebro.addanalyzer(bt.analyzers.TimeReturn, _name='myAnalysis')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印
for ret_key,ret_val in thestrat.analyzers.myAnalysis.get_analysis().items():
if ret_val > 0:
print(ret_key.date(),' :',ret_val)
结果:
2005-04-13 : 0.0015395856950837228
2005-04-19 : 5.984185521934471e-05
2005-05-19 : 0.0017150257963813864
2005-05-23 : 0.0020788862921095053
2005-05-26 : 0.002654561971983549
2005-05-30 : 0.001265512365993482
2005-06-01 : 0.004961734152849395
2005-06-02 : 0.000517540606838951
2005-06-07 : 0.003589195333038475
2005-06-10 : 0.0021029437191142364
2005-06-13 : 0.0016029885002128985
2005-06-14 : 0.00030345943758836036
2005-06-16 : 0.0012574479222025037
2005-06-17 : 0.001841740509878731
2005-06-21 : 0.0017502455650599824
2005-06-22 : 0.00024588566316663396
2005-06-23 : 0.0008713804485009913
2005-06-28 : 0.0029625820861196583
2005-06-29 : 0.0016581506213559916
2005-06-30 : 0.0002978930563926063
2005-07-01 : 0.0026982272646871586
2005-07-04 : 0.000703637887611297
2005-07-06 : 0.0016146812399955301
2005-07-08 : 0.005448675299571937
2005-07-11 : 0.002177211914961452
2005-07-13 : 0.0024191560584254646
2005-07-14 : 0.0016389541628245574
2005-07-15 : 0.0001366035023950829
2005-07-19 : 0.0037192422526510782
2005-07-25 : 0.0005287632376123064
2005-07-26 : 0.0004652633034682996
2005-07-27 : 0.0007760655844533115
2005-07-28 : 0.0021912277880440367
2005-08-02 : 0.002902767633697234
2005-08-08 : 0.0011795533530187807
2005-08-09 : 0.0038428693424423788
2005-08-10 : 0.0038941270066321643
2005-08-19 : 0.004385257699426548
2005-08-22 : 0.00022357483354995544
2005-09-14 : 0.0013365926037898213
2005-09-15 : 2.9794832783203162e-06
2005-09-16 : 0.0027361506581160544
2005-09-20 : 0.0019194638191974978
2005-09-23 : 0.0019209072178909548
2005-09-26 : 0.006895646936516009
2005-09-28 : 0.004467026757668302
2005-09-30 : 0.0015538391460983014
2005-10-03 : 0.002050523803992288
2005-10-04 : 0.0014637678169524548
2005-10-10 : 0.0006997249595950272
2005-10-11 : 0.000622092287242415
2005-10-14 : 0.0018049375378306198
2005-10-17 : 0.0006974586907024793
2005-11-14 : 0.0006361911237420248
2005-11-15 : 0.0001609334937371365
2005-11-17 : 0.0012473221893940512
2005-11-18 : 0.0022926909291562936
2005-11-21 : 0.0022646315733088063
2005-11-22 : 4.948564619344786e-05
2005-11-23 : 0.0020703769827916663
2005-11-25 : 0.000685251179660451
2005-11-29 : 0.0010448162864695743
2005-12-01 : 0.005381645381645539
2005-12-02 : 0.00179215704810054
2005-12-06 : 0.0017176630721154051
2005-12-08 : 0.0004960986370081688
2005-12-12 : 0.0013067801901180953
2005-12-13 : 0.001403422501967011
2005-12-15 : 0.00031259061933242016
2005-12-16 : 0.003386322832718891
2005-12-20 : 0.000980888836191518
2005-12-21 : 0.0030229935467702695
2005-12-23 : 0.0008247966071086577
2005-12-27 : 0.0012425146780581375
2005-12-29 : 0.0010926937679414106
2006-01-02 : 0.002482199502387372
2006-01-03 : 0.0009757990146477269
2006-01-04 : 0.0037124072506860006
2006-01-06 : 0.0016255554789954552
2006-01-09 : 0.00046410591303880366
2006-01-11 : 0.002298310397160108
2006-01-12 : 0.00015403185650031403
2006-01-16 : 0.0014742521282327115
2006-01-19 : 0.0022544771666943575
2006-01-25 : 0.00444897762847507
2006-01-26 : 8.991461044005611e-05
2006-02-03 : 0.0001397460140626361
2006-02-06 : 0.0003752095897318064
2006-02-09 : 0.0054208544494713795
2006-02-13 : 0.0031049326776848574
2006-02-14 : 0.0006826628909508692
2006-02-16 : 0.0025939202320917065
2006-02-17 : 0.001088993984835529
2006-02-21 : 0.001237098621075905
2006-02-22 : 0.0037705690492109145
2006-02-24 : 0.0012257584802524146
2006-02-27 : 0.0014024542950161756
2006-03-01 : 0.0030512153071882153
2006-03-06 : 0.001955343910202023
2006-03-09 : 0.0028812418804018414
2006-03-10 : 0.0039628095126820195
2006-03-13 : 0.00256029885283926
2006-03-14 : 0.0008197850448474764
2006-03-15 : 0.0008354765681548582
2006-03-20 : 0.0009241227812630814
2006-03-21 : 0.0005905078271137842
2006-03-22 : 0.0019521394237009826
2006-03-24 : 0.0010330330560976986
2006-03-29 : 0.0014323952832235864
2006-03-30 : 0.004653201182804878
2006-04-03 : 0.002392036535716402
2006-04-05 : 0.0013271304479858248
2006-04-10 : 0.0019664878103768935
2006-04-13 : 0.0002903392517377146
2006-04-19 : 0.004858331743586186
2006-05-02 : 0.0022164978755350173
2006-05-04 : 0.0020377510024653933
2006-05-05 : 0.003009468651528735
2006-05-08 : 0.0003083037435374081
2006-05-09 : 0.0012875635380440453
2006-05-16 : 2.927214803882805e-05
2006-07-04 : 0.0006010371794182845
2006-07-06 : 0.004287954523179538
2006-07-10 : 0.0014830460867807371
2006-07-12 : 0.0012467972405789673
2006-07-19 : 0.00928301280110988
2006-07-20 : 0.0003913467145069127
2006-07-24 : 0.0074792139972232885
2006-07-26 : 0.0009054548515738947
2006-07-27 : 0.0008351988389367904
2006-07-28 : 0.0038002081368817553
2006-08-02 : 0.005464325550304183
2006-08-04 : 0.0049052527651920474
2006-08-08 : 0.0008873913141316248
2006-08-09 : 0.0038210994287413147
2006-08-14 : 0.004299094269437198
2006-08-15 : 0.00459777940969075
2006-08-16 : 0.002377927872103003
2006-08-17 : 0.0008847779555061219
2006-08-22 : 0.0014798064451857496
2006-08-24 : 0.0022178255426841265
2006-08-28 : 0.0026491065066505115
2006-08-30 : 0.0010657022302591468
2006-09-01 : 0.0011754337244374025
2006-09-04 : 0.001610350935149496
2006-09-08 : 0.0010076065558493053
2006-09-12 : 0.004551630434782572
2006-09-13 : 0.001602759180361124
2006-09-15 : 0.0014924811195344834
2006-09-20 : 0.005910780381585212
2006-09-21 : 0.001521639872155367
2006-09-25 : 0.0009050890293378355
2006-09-26 : 0.00489211348932872
2006-09-27 : 0.002229066801087809
2006-09-29 : 0.00042364290823693196
2006-10-04 : 0.003312558417480016
2006-10-05 : 0.0023986629339283194
2006-10-06 : 4.284979717761317e-05
2006-10-10 : 0.0020178223516438276
2006-10-11 : 0.000638624827870693
2006-10-12 : 0.0030904150596475777
2006-10-16 : 0.0002745949487785726
2006-10-18 : 0.003977544615378692
2006-10-20 : 0.001077851804382579
2006-10-23 : 0.001972513595950076
2006-10-25 : 0.00048506280759652576
2006-10-26 : 0.0007702427351461427
2006-11-01 : 0.0009028328510023442
2006-11-03 : 0.0015033369524779516
2006-11-06 : 0.005189340813464227
2006-11-07 : 0.002605787034418272
2006-11-08 : 8.932937149741527e-05
2006-11-13 : 0.002098669462383018
2006-11-15 : 0.002301275470188102
2006-11-16 : 8.246827548541447e-05
2006-11-20 : 0.0017273949042788672
2006-11-29 : 0.004553456612619522
2006-12-04 : 0.00035448726561670973
2006-12-20 : 0.0017237396943083905
2006-12-27 : 0.00587163548396985
class backtrader.analyzers.TradeAnalyzer()
提供已平仓交易的统计数据(同时记录未平仓交易的数量)
分析器用于计算和评估交易策略中的交易行为和性能指标,例如交易的数量、成功率、平均盈亏比等。
方法:
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器
ret_anal = cerebro.addanalyzer(bt.analyzers.TradeAnalyzer, _name='myAnalysis')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印
for ret_key,ret_val in thestrat.analyzers.myAnalysis.get_analysis().items():
print('*' * 20)
print(ret_key,' :')
print('*' * 20)
for k,v in ret_val.items():
print(k,':',v)
结果:
********************
total :
********************
total : 9
open : 1
closed : 8
********************
streak :
********************
won : AutoOrderedDict([('current', 1), ('longest', 2)])
lost : AutoOrderedDict([('current', 0), ('longest', 2)])
********************
pnl :
********************
gross : AutoOrderedDict([('total', 497.74999999999955), ('average', 62.21874999999994)])
net : AutoOrderedDict([('total', 497.74999999999955), ('average', 62.21874999999994)])
********************
won :
********************
total : 4
pnl : AutoOrderedDict([('total', 804.5299999999993), ('average', 201.13249999999982), ('max', 264.0999999999999)])
********************
lost :
********************
total : 4
pnl : AutoOrderedDict([('total', -306.77999999999975), ('average', -76.69499999999994), ('max', -140.0899999999997)])
********************
long :
********************
total : 8
pnl : AutoOrderedDict([('total', 497.74999999999955), ('average', 62.21874999999994), ('won', AutoOrderedDict([('total', 804.5299999999993), ('average', 201.13249999999982), ('max', 264.0999999999999)])), ('lost', AutoOrderedDict([('total', -306.77999999999975), ('average', -76.69499999999994), ('max', -140.0899999999997)]))])
won : 4
lost : 4
********************
short :
********************
total : 0
pnl : AutoOrderedDict([('total', 0.0), ('average', 0.0), ('won', AutoOrderedDict([('total', 0.0), ('average', 0.0), ('max', 0.0)])), ('lost', AutoOrderedDict([('total', 0.0), ('average', 0.0), ('max', 0.0)]))])
won : 0
lost : 0
********************
len :
********************
total : 325
average : 40.625
max : 91
min : 6
won : AutoOrderedDict([('total', 266), ('average', 66.5), ('max', 91), ('min', 52)])
lost : AutoOrderedDict([('total', 59), ('average', 14.75), ('max', 26), ('min', 6)])
long : AutoOrderedDict([('total', 325), ('average', 40.625), ('max', 91), ('min', 6), ('won', AutoOrderedDict([('total', 266), ('average', 66.5), ('max', 91), ('min', 52)])), ('lost', AutoOrderedDict([('total', 59), ('average', 14.75), ('max', 26), ('min', 6)]))])
short : AutoOrderedDict([('total', 0), ('average', 0.0), ('max', 0), ('min', 9223372036854775807), ('won', AutoOrderedDict([('total', 0), ('average', 0.0), ('max', 0), ('min', 9223372036854775807)])), ('lost', AutoOrderedDict([('total', 0), ('average', 0.0), ('max', 0), ('min', 9223372036854775807)]))])
class backtrader.analyzers.Transactions()
分析器报告系统中每个数据发生的事务。
查看订单执行位置,每一个next循环都从0位置开始。
接下来将使用结果来记录交易。
参数:
方法:
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器
ret_anal = cerebro.addanalyzer(bt.analyzers.Transactions, _name='myAnalysis')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印
for ret_key,ret_val in thestrat.analyzers.myAnalysis.get_analysis().items():
print(ret_key.date(),':',ret_val)
结果:
记录了每次交易的信息:
列名对照
‘date’, ‘amount’, ‘price’, ‘sid’, ‘symbol’, ‘value’
2005-04-11 : [[1, 3088.47, 0, '2005-2006-day-001', -3088.47]]
2005-04-19 : [[-1, 2948.38, 0, '2005-2006-day-001', 2948.38]]
2005-05-19 : [[1, 3034.88, 0, '2005-2006-day-001', -3034.88]]
2005-08-26 : [[-1, 3258.45, 0, '2005-2006-day-001', 3258.45]]
2005-09-13 : [[1, 3353.61, 0, '2005-2006-day-001', -3353.61]]
2005-10-19 : [[-1, 3330.0, 0, '2005-2006-day-001', 3330.0]]
2005-11-14 : [[1, 3405.94, 0, '2005-2006-day-001', -3405.94]]
2006-01-26 : [[-1, 3578.92, 0, '2005-2006-day-001', 3578.92]]
2006-02-03 : [[1, 3677.05, 0, '2005-2006-day-001', -3677.05]]
2006-04-20 : [[-1, 3820.93, 0, '2005-2006-day-001', 3820.93]]
2006-05-02 : [[1, 3839.24, 0, '2005-2006-day-001', -3839.24]]
2006-05-16 : [[-1, 3711.46, 0, '2005-2006-day-001', 3711.46]]
2006-07-04 : [[1, 3664.59, 0, '2005-2006-day-001', -3664.59]]
2006-07-27 : [[-1, 3649.29, 0, '2005-2006-day-001', 3649.29]]
2006-07-28 : [[1, 3671.71, 0, '2005-2006-day-001', -3671.71]]
2006-12-04 : [[-1, 3935.81, 0, '2005-2006-day-001', 3935.81]]
2006-12-19 : [[1, 4121.01, 0, '2005-2006-day-001', -4121.01]]
class backtrader.analyzers.VWR()
可变性加权回报:对数回报具有更好的夏普比率
别名:
VariabilityWeightedReturn
参考文献:
参数:
timeframe (default: None) ,见前章
compression (default: None),见前章
tann (default: None),用于平均回报率年度化(标准化)的期数。如果没有,那么将使用标准t值,即:
tau (default: 2.0),用于计算的因子(参见文献)
sdev_max (default: 0.20),最大标准偏差(见文献)
fund (default: None),见前章
方法:
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器
ret_anal = cerebro.addanalyzer(bt.analyzers.VWR, _name='myAnalysis')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印
for ret_key,ret_val in thestrat.analyzers.myAnalysis.get_analysis().items():
print(ret_key,':',ret_val)
结果:
vwr : 1.522055167476388
以TimeReturn为例:
for i in dir(bt.analyzers.TimeReturn) :
if i [:1] != '_':
print (i)
方法和属性:
与策略的很像!
create_analysis
csv
frompackages
get_analysis
next
nextstart
notify_cashvalue
notify_fund
notify_order
notify_trade
on_dt_over
packages
params
pprint
prenext
print
start
stop
前面的调用都使用参数的默认值,对参数进行赋值,调整分析结果:
bt.analyzers.TimeReturn.params.timeframe = bt.TimeFrame.Months
bt.analyzers.TimeReturn.params.compression = 30
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器
#time_return = bt.analyzers.TimeReturn() # 创建 TimeReturn 分析器实例
#ret_anal = cerebro.addanalyzer(time_return, _name='myAnalysis') # 将 TimeReturn 分析器添加到 Cerebro 引擎中
bt.analyzers.TimeReturn.params.timeframe = bt.TimeFrame.Months
bt.analyzers.TimeReturn.params.compression = 30
ret_anal = cerebro.addanalyzer(bt.analyzers.TimeReturn, _name='myAnalysis')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印
for ret_key,ret_val in thestrat.analyzers.myAnalysis.get_analysis().items():
if ret_val > 0:
print(ret_key.date(),' :',ret_val)
结果与前面的TimeReturn测试结果没有变化!!
似乎没有起作用!!??