用于对照参考中涉及的方法和属性。
anal = bt.analyzers
str_attr = ''
for i in dir(anal) :
if i[:1] != '_':
str_attr += i + ','
print (str_attr)
属性方法:
Analyzer,AnnualReturn,AutoDict,AutoOrderedDict,Calmar,DrawDown,GrossLeverage,LogReturnsRolling,MAXINT,Order,OrderedDict,PeriodStats,Position,PositionsValue,PyFolio,Returns,SQN,SharpeRatio,SharpeRatio_A,TimeDrawDown,TimeFrame,TimeFrameAnalyzerBase,TimeReturn,TradeAnalyzer,Transactions,VWR,VariabilityWeightedReturn,absolute_import,annualreturn,average,bt,calmar,collections,division,drawdown,items,iteritems,itervalues,leverage,logreturnsrolling,math,periodstats,positions,print_function,pyfolio,range,returns,sharpe,sqn,standarddev,sys,timereturn,tradeanalyzer,transactions,unicode_literals,vwr,
class backtrader.analyzers.AnnualReturn()
通过查看年初和年末来计算年回报率
参数:(None)
属性:
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import datetime
import backtrader as bt
import backtrader.analyzers as btanalyzers
import backtrader.feeds as btfeeds
import backtrader.strategies as btstrats
# 数据源
dataname = './datas/2005-2006-day-001.txt'
data = btfeeds.BacktraderCSVData(dataname=dataname)
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器 ,年化收益率
ret_ann = cerebro.addanalyzer(bt.analyzers.AnnualReturn, _name='myAnnualReturn')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印年化收益率
print('Annual Return:', thestrat.analyzers.myAnnualReturn.get_analysis()) #
# 遍历dict
for year,ann_ret in thestrat.analyzers.myAnnualReturn.get_analysis().items():
print(year,':',ann_ret)
结果如下:
Annual Return: OrderedDict([(2005, 0.023285999999999918), (2006, 0.025781648532277268)])
2005 : 0.023285999999999918
2006 : 0.025781648532277268
返回值:cerebro.run()
print(thestrats)
print(thestrat)
[]
class backtrader.analyzers.Calmar()
卡玛比率,描述收益与最大回撤之间的关系,也可以叫做单位回撤收益率。单位回撤收益率计算方法是:卡玛比率=区间年化收益率/区间最大回撤 .
该分析器计算CalmarRatio时间范围,该时间范围可能不同于基础数据参数中使用的时间范围
参数:
timeframe (default: None), 如果将使用系统中第一个数据的时间范围
NoTimeFrame考虑整个数据集没有时间约束。
compression (default: None),仅用于小于日单位的时间范围,例如提供的数据时间周期 是TimeFrame.Minutes,分钟线,按60作为压缩到指定的小时线。
如果None,则将使用系统的第一个数据的时间
None ??
fund (default: None) ,如果None,将自动检测Broker(fundmode - True/False),以决定回报是基于总净资产值还是基于基金价值。请参见Broker文档中的set_fundmode
可以设置set_fundmode 为True or False
get_analysis(),返回带有时间段和相应滚动卡尔马尔比率的键的OrderedDict
calmar 最新计算的卡尔马尔比率
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器 ,年化收益率
ret_anal = cerebro.addanalyzer(bt.analyzers.Calmar, _name='myCalmar')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印卡玛比率
#print('Annual Return:', thestrat.analyzers.myCalmar.get_analysis()) #
for mon,ann_ret in thestrat.analyzers.myCalmar.get_analysis().items():
print(mon.date(),':',ann_ret)
结果:
居然都是空值,不明白,难道需要重写Calmar方法 ??
2005-01-31 : nan
2005-02-28 : nan
2005-03-31 : nan
2005-04-30 : nan
2005-05-31 : nan
2005-06-30 : nan
2005-07-31 : nan
2005-08-31 : nan
2005-09-30 : nan
2005-10-31 : nan
2005-11-30 : nan
2005-12-31 : nan
2006-01-31 : nan
2006-02-28 : nan
2006-03-31 : nan
2006-04-30 : nan
2006-05-31 : nan
2006-06-30 : nan
2006-07-31 : nan
2006-08-31 : nan
2006-09-30 : nan
2006-10-31 : nan
2006-11-30 : nan
2006-12-31 : nan
class backtrader.analyzers.DrawDown()
计算交易系统回撤数据,回撤比例和回撤值,回撤长度,最大回撤百分比、数值和长度。
参数:
fund (default: None),如果None,将自动检测Broker的实际模式(fundmode - True/False),以决定回报是基于总净资产值还是基于基金价值。请参见代理文档中的set_fundmode 针对特定行为将其设置为True或False 。
get_analysis()
返回字典(含子字典)以drawdown stats为值,以下键/属性可用:
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器
ret_anal = cerebro.addanalyzer(bt.analyzers.DrawDown, _name='myAnalysis')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印卡玛比率
#print('Annual Return:', thestrat.analyzers.myCalmar.get_analysis()) #
for ret_key,ret_val in thestrat.analyzers.myAnalysis.get_analysis().items():
print(ret_key,':',ret_val)
for k,v in thestrat.analyzers.myAnalysis.get_analysis()['max'].items() :
print('max ',k,':',v)
结果:
len : 29
drawdown : 1.639577759765371
moneydown : 174.97000000000116
max : AutoOrderedDict([('len', 122), ('drawdown', 3.430658473286551), ('moneydown', 357.97000000000116)])
max len : 122
max drawdown : 3.430658473286551
max moneydown : 357.97000000000116
个人理解是否准确?
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器
ret_anal = cerebro.addanalyzer(bt.analyzers.DrawDown, _name='myAnalysis')
ret_anal1 = cerebro.addanalyzer(bt.analyzers.Calmar, _name='myCalmar')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印卡玛比率
#print('Annual Return:', thestrat.analyzers.myCalmar.get_analysis()) #
for ret_key,ret_val in thestrat.analyzers.myAnalysis.get_analysis().items():
print(ret_key,':',ret_val)
for k,v in thestrat.analyzers.myAnalysis.get_analysis()['max'].items() :
print(k,':',v)
for mon,ann_ret in thestrat.analyzers.myCalmar.get_analysis().items():
print(mon.date(),':',ann_ret)
class backtrader.analyzers.TimeDrawDown()
分析器计算所选时间范围内的交易系统最大回撤。
参数参见 3.Calmar
运行期间可用属性:(如何使用 ??)
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器
ret_anal = cerebro.addanalyzer(bt.analyzers.TimeDrawDown, _name='myAnalysis')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印
#print('Annual Return:', thestrat.analyzers.myCalmar.get_analysis()) #
for ret_key,ret_val in thestrat.analyzers.myAnalysis.get_analysis().items():
print(ret_key,':',ret_val)
结果:
maxdrawdown : 3.430658473286551
maxdrawdownperiod : 122
没有drawdown返回值,和说明中不一样!
class backtrader.analyzers.GrossLeverage()
分析器在一个时间范围内计算当前策略的总杠杆
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器
ret_anal = cerebro.addanalyzer(bt.analyzers.GrossLeverage, _name='myAnalysis')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印
#print('Annual Return:', thestrat.analyzers.myCalmar.get_analysis()) #
for ret_key,ret_val in thestrat.analyzers.myAnalysis.get_analysis().items():
if ret_val > 0 :
print(ret_key.date(),':',ret_val)
显示不为0 的值,结果:
2005-04-11 : 0.3083026341730942
2005-04-12 : 0.30723354693080185
2005-04-13 : 0.3082984806951912
2005-04-14 : 0.30793763004588026
2005-04-15 : 0.30365364891359753
2005-04-18 : 0.2989851227062312
2005-05-19 : 0.30898507819318366
2005-05-20 : 0.30889131465002206
2005-05-23 : 0.3103250704071641
2005-05-24 : 0.3100161955926152
2005-05-25 : 0.30954782409885
2005-05-26 : 0.3113758196609664
2005-05-27 : 0.31123127087111446
2005-05-30 : 0.31210181453136954
2005-05-31 : 0.31072696020311497
2005-06-01 : 0.31413006448656455
2005-06-02 : 0.3144848464151482
2005-06-03 : 0.31332890646222566
2005-06-06 : 0.31228619248042416
2005-06-07 : 0.3147457040015663
2005-06-08 : 0.31411007555308107
2005-06-09 : 0.3139266744136486
2005-06-10 : 0.31536642029997347
2005-06-13 : 0.31646212365521403
2005-06-14 : 0.31666948674845236
2005-06-15 : 0.3156204312224119
2005-06-16 : 0.3164799221241206
2005-06-17 : 0.31773647449745146
2005-06-20 : 0.3166202237470675
2005-06-21 : 0.31781421638937896
2005-06-22 : 0.31798191485853566
2005-06-23 : 0.318575694675329
2005-06-24 : 0.3165422094666248
2005-06-27 : 0.3145860469413599
2005-06-28 : 0.3166106440052748
2005-06-29 : 0.3177419306468978
2005-06-30 : 0.31794511006268883
2005-07-01 : 0.3197804968720007
2005-07-04 : 0.3202587885421532
2005-07-05 : 0.3197377837403592
2005-07-06 : 0.320834419661782
2005-07-07 : 0.3171617264076662
2005-07-08 : 0.320862128154992
2005-07-11 : 0.32233754293084504
2005-07-12 : 0.3216592985441333
2005-07-13 : 0.3232963502781166
2005-07-14 : 0.3244036217745983
2005-07-15 : 0.32449589800081363
2005-07-18 : 0.32435613650222944
2005-07-19 : 0.3268597083170186
2005-07-20 : 0.32617585498775764
2005-07-21 : 0.32591963417103303
2005-07-22 : 0.3254532785791589
2005-07-25 : 0.3258097655902719
2005-07-26 : 0.32612329569184867
2005-07-27 : 0.3266458626639844
2005-07-28 : 0.3281181089339718
2005-07-29 : 0.3276852576062352
2005-08-01 : 0.3272843401353705
2005-08-02 : 0.3292314254432949
2005-08-03 : 0.3288171696172357
2005-08-04 : 0.3266312471326457
2005-08-05 : 0.32462357206754333
2005-08-08 : 0.32541927602239296
2005-08-09 : 0.32800167777305167
2005-08-10 : 0.33060837378271785
2005-08-11 : 0.3295114837231646
2005-08-12 : 0.3281908579235726
2005-08-15 : 0.3276766478745317
2005-08-16 : 0.3264185858306728
2005-08-17 : 0.32580510370701843
2005-08-18 : 0.32484738650314027
2005-08-19 : 0.32779517787495577
2005-08-22 : 0.3279454323630516
2005-08-23 : 0.3258770227304379
2005-08-24 : 0.32554193578989804
2005-08-25 : 0.32315206450109335
2005-09-13 : 0.33072213791169336
2005-09-14 : 0.331615495696633
2005-09-15 : 0.3316174871311535
2005-09-16 : 0.33344129217823315
2005-09-19 : 0.33276126173019444
2005-09-20 : 0.3340395487214401
2005-09-21 : 0.33080399536625027
2005-09-22 : 0.32983639924995994
2005-09-23 : 0.33112125326246195
2005-09-26 : 0.3357020176096656
2005-09-27 : 0.3346058130670914
2005-09-28 : 0.33756492825778184
2005-09-29 : 0.3364761767669498
2005-09-30 : 0.3375055865206854
2005-10-03 : 0.33886126722997173
2005-10-04 : 0.3398276063334612
2005-10-05 : 0.3368743416650984
2005-10-06 : 0.33459989203106183
2005-10-07 : 0.3339380461343413
2005-10-10 : 0.33440378042219204
2005-10-11 : 0.33481758527200356
2005-10-12 : 0.33299734779557816
2005-10-13 : 0.33111261080835563
2005-10-14 : 0.3323177355907317
2005-10-17 : 0.33278309182192406
2005-10-18 : 0.3313365776886652
2005-11-14 : 0.3389875296410687
2005-11-15 : 0.3390938915701305
2005-11-16 : 0.3376221440005097
2005-11-17 : 0.3384473133460264
2005-11-18 : 0.3399605797377472
2005-11-21 : 0.3414519484478333
2005-11-22 : 0.3414845355111219
2005-11-23 : 0.34284509390283413
2005-11-24 : 0.3420471310421751
2005-11-25 : 0.3424976852789672
2005-11-28 : 0.3416539362662066
2005-11-29 : 0.3423410690282281
2005-11-30 : 0.3412602712602713
2005-12-01 : 0.34478639851270665
2005-12-02 : 0.3459585435186896
2005-12-05 : 0.34465539877064966
2005-12-06 : 0.34577912980039865
2005-12-07 : 0.3450385706847048
2005-12-08 : 0.3453633350419261
2005-12-09 : 0.34474574902533117
2005-12-12 : 0.3456009047993706
2005-12-13 : 0.3465180161201776
2005-12-14 : 0.3459257548129618
2005-12-15 : 0.3461301483948379
2005-12-16 : 0.3483368900632573
2005-12-19 : 0.3479754591608257
2005-12-20 : 0.3486143960278179
2005-12-21 : 0.3505775957649484
2005-12-22 : 0.35051610793610516
2005-12-23 : 0.3510513585737414
2005-12-27 : 0.35185668615467947
2005-12-28 : 0.3514087561713805
2005-12-29 : 0.3521166942219574
2005-12-30 : 0.3497487505936757
2006-01-02 : 0.35135880743907844
2006-01-03 : 0.3519911338521485
2006-01-04 : 0.35438790886042554
2006-01-05 : 0.3542488138297408
2006-01-06 : 0.35529681462505286
2006-01-09 : 0.35559588638456835
2006-01-10 : 0.3539164976351775
2006-01-11 : 0.355397993129598
2006-01-12 : 0.3554972670820689
2006-01-13 : 0.35293070820504946
2006-01-16 : 0.35388324720294734
2006-01-17 : 0.3517215510522214
2006-01-18 : 0.34919161588795106
2006-01-19 : 0.3506555481280161
2006-01-20 : 0.347956290857279
2006-01-23 : 0.34754134046659035
2006-01-24 : 0.34679643178643343
2006-01-25 : 0.3496896479940735
2006-02-03 : 0.35942733636367197
2006-02-06 : 0.3596675952225605
2006-02-07 : 0.35957251431139253
2006-02-08 : 0.3589820057748136
2006-02-09 : 0.36243813584294265
2006-02-10 : 0.36049897428944067
2006-02-13 : 0.362478435826771
2006-02-14 : 0.3629133512401996
2006-02-15 : 0.3626228539150404
2006-02-16 : 0.36427188194257915
2006-02-17 : 0.36496343294425343
2006-02-20 : 0.36490437431097383
2006-02-21 : 0.3656890794760873
2006-02-22 : 0.368071808356821
2006-02-23 : 0.3677555118144409
2006-02-24 : 0.3685295420832612
2006-02-27 : 0.3694139102532041
2006-02-28 : 0.36538206501063375
2006-03-01 : 0.36731253070162323
2006-03-02 : 0.3647191296502845
2006-03-03 : 0.3628805364561822
2006-03-06 : 0.36412389292978486
2006-03-07 : 0.3635763518105039
2006-03-08 : 0.36250943234770167
2006-03-09 : 0.36434091991091205
2006-03-10 : 0.3668499728614117
2006-03-13 : 0.36846688636777436
2006-03-14 : 0.36898418369704167
2006-03-15 : 0.36951094253103506
2006-03-16 : 0.36936234970674847
2006-03-17 : 0.3689204031870685
2006-03-20 : 0.36950305977304887
2006-03-21 : 0.3698751534270091
2006-03-22 : 0.3711028483501979
2006-03-23 : 0.3705986887392677
2006-03-24 : 0.371248210122292
2006-03-27 : 0.3686833912250478
2006-03-28 : 0.3676432998143192
2006-03-29 : 0.36854778898296114
2006-03-30 : 0.37147245410295476
2006-03-31 : 0.37021232446390523
2006-04-03 : 0.3717152046492194
2006-04-04 : 0.36999262918860537
2006-04-05 : 0.37082762300714417
2006-04-06 : 0.37066877602094256
2006-04-07 : 0.3683537095899281
2006-04-10 : 0.36959339649130907
2006-04-11 : 0.36625933698805474
2006-04-12 : 0.36553131115270493
2006-04-13 : 0.36571546884886813
2006-04-18 : 0.3651534533665034
2006-04-19 : 0.36822283641522024
2006-05-02 : 0.3713788723361088
2006-05-03 : 0.36893525338193306
2006-05-04 : 0.37021859107929533
2006-05-05 : 0.3721082117326383
2006-05-08 : 0.37230173345802514
2006-05-09 : 0.3731088955863923
2006-05-10 : 0.37145865080929336
2006-05-11 : 0.3699017382546132
2006-05-12 : 0.36454758148626604
2006-05-15 : 0.36211208305484144
2006-07-04 : 0.3579435032876294
2006-07-05 : 0.3546643144173283
2006-07-06 : 0.3574196696513529
2006-07-07 : 0.35672534045091253
2006-07-10 : 0.3576779336778245
2006-07-11 : 0.35460991517465956
2006-07-12 : 0.35541358373976745
2006-07-13 : 0.35109767298845374
2006-07-14 : 0.34760582247803107
2006-07-17 : 0.34698273724633
2006-07-18 : 0.3465608491862727
2006-07-19 : 0.3525709414248364
2006-07-20 : 0.3528242115433573
2006-07-21 : 0.350747033951786
2006-07-24 : 0.35556688710998724
2006-07-25 : 0.3554766803773741
2006-07-26 : 0.3560597392106296
2006-07-28 : 0.36121543454685995
2006-07-31 : 0.3600486063229181
2006-08-01 : 0.35683271028587005
2006-08-02 : 0.3603280858700622
2006-08-03 : 0.3585497343558313
2006-08-04 : 0.36168085112592097
2006-08-07 : 0.35799244103054806
2006-08-08 : 0.3585616478527944
2006-08-09 : 0.36100331771045846
2006-08-10 : 0.3590215465285064
2006-08-11 : 0.35900025788704554
2006-08-14 : 0.3617441798259906
2006-08-15 : 0.36466530858842494
2006-08-16 : 0.3661725046557094
2006-08-17 : 0.3667328055093399
2006-08-18 : 0.3662006656789018
2006-08-21 : 0.36533326111619974
2006-08-22 : 0.36627105729012255
2006-08-23 : 0.36420977974831714
2006-08-24 : 0.3656167311657891
2006-08-25 : 0.3655737974361677
2006-08-28 : 0.3672500195265229
2006-08-29 : 0.36714261603375536
2006-08-30 : 0.36781633557486665
2006-08-31 : 0.36725795129326644
2006-09-01 : 0.36800082443803867
2006-09-04 : 0.36901692861710367
2006-09-05 : 0.36781024499744697
2006-09-06 : 0.36502373199537463
2006-09-07 : 0.36301986868110325
2006-09-08 : 0.3636610479809
2006-09-11 : 0.3631657608695653
2006-09-12 : 0.36605126124298376
2006-09-13 : 0.36706570249886916
2006-09-14 : 0.3665218914929543
2006-09-15 : 0.36746593663998156
2006-09-18 : 0.3672439180321705
2006-09-19 : 0.36551306695339064
2006-09-20 : 0.36924134284959037
2006-09-21 : 0.3701996721399587
2006-09-22 : 0.3675037374682762
2006-09-25 : 0.36807568523294365
2006-09-26 : 0.3711520806220687
2006-09-27 : 0.37255070701043785
2006-09-28 : 0.37247870309804426
2006-09-29 : 0.37274443546960967
2006-10-02 : 0.37232864185674586
2006-10-03 : 0.37158688690375874
2006-10-04 : 0.37366166921359567
2006-10-05 : 0.3751604486849874
2006-10-06 : 0.37518722178580793
2006-10-09 : 0.3751378385018408
2006-10-10 : 0.3763961602682225
2006-10-11 : 0.37679415499371766
2006-10-12 : 0.3787141860295573
2006-10-13 : 0.3786635937282515
2006-10-16 : 0.37883416272951975
2006-10-17 : 0.3757376437828629
2006-10-18 : 0.3782108379164093
2006-10-19 : 0.3779420519568178
2006-10-20 : 0.37861181633180657
2006-10-23 : 0.3798351000287309
2006-10-24 : 0.37954131780123773
2006-10-25 : 0.379842133317204
2006-10-26 : 0.3803194377683742
2006-10-27 : 0.37973251310825673
2006-10-30 : 0.3790075765175275
2006-10-31 : 0.3790005242865823
2006-11-01 : 0.3795606772891793
2006-11-02 : 0.3772217877561558
2006-11-03 : 0.37815662787612303
2006-11-06 : 0.38136692573695513
2006-11-07 : 0.3829747621017791
2006-11-08 : 0.3830298756552195
2006-11-09 : 0.38298288471773245
2006-11-10 : 0.38245098242199643
2006-11-13 : 0.3837442994417774
2006-11-14 : 0.38363952821034975
2006-11-15 : 0.3850546867751812
2006-11-16 : 0.38510539607277233
2006-11-17 : 0.3832937041248838
2006-11-20 : 0.384357162425366
2006-11-21 : 0.38431788328016525
2006-11-22 : 0.3842549106635977
2006-11-23 : 0.3837223062257577
2006-11-24 : 0.381538345538685
2006-11-27 : 0.377436270124163
2006-11-28 : 0.37725074854916707
2006-11-29 : 0.3800735567116958
2006-11-30 : 0.3779662286227259
2006-12-01 : 0.3746978043706757
2006-12-19 : 0.39137099345055276
2006-12-20 : 0.392418306133805
2006-12-21 : 0.39204525953298947
2006-12-22 : 0.3897996601035001
2006-12-27 : 0.3933616195441228
2006-12-28 : 0.39311913508574914
2006-12-29 : 0.3924993426492948
class backtrader.analyzers.PositionsValue()
相同的参数参看 3.Calmar
headers (default: False),保存结果的字典添加一个初始键,以数据名称(Datetime作为键值)
cash (default: False),包括实际现金作为额外头寸(标题“现金”将用作名称)
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器
ret_anal = cerebro.addanalyzer(bt.analyzers.PositionsValue, _name='myAnalysis')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印
#print('Annual Return:', thestrat.analyzers.myCalmar.get_analysis()) #
for ret_key,ret_val in thestrat.analyzers.myAnalysis.get_analysis().items():
if ret_val[0]> 0 :
print(ret_key,':',ret_val)
执行结果:
2005-04-11 : [3080.6]
2005-04-12 : [3065.18]
2005-04-13 : [3080.54]
2005-04-14 : [3075.33]
2005-04-15 : [3013.89]
2005-04-18 : [2947.79]
2005-05-19 : [3051.79]
2005-05-20 : [3050.45]
2005-05-23 : [3070.98]
2005-05-24 : [3066.55]
2005-05-25 : [3059.84]
2005-05-26 : [3086.08]
2005-05-27 : [3084.0]
2005-05-30 : [3096.54]
2005-05-31 : [3076.75]
2005-06-01 : [3125.88]
2005-06-02 : [3131.03]
2005-06-03 : [3114.27]
2005-06-06 : [3099.2]
2005-06-07 : [3134.82]
2005-06-08 : [3125.59]
2005-06-09 : [3122.93]
2005-06-10 : [3143.85]
2005-06-13 : [3159.83]
2005-06-14 : [3162.86]
2005-06-15 : [3147.55]
2005-06-16 : [3160.09]
2005-06-17 : [3178.48]
2005-06-20 : [3162.14]
2005-06-21 : [3179.62]
2005-06-22 : [3182.08]
2005-06-23 : [3190.8]
2005-06-24 : [3161.0]
2005-06-27 : [3132.5]
2005-06-28 : [3162.0]
2005-06-29 : [3178.56]
2005-06-30 : [3181.54]
2005-07-01 : [3208.54]
2005-07-04 : [3215.6]
2005-07-05 : [3207.91]
2005-07-06 : [3224.11]
2005-07-07 : [3170.06]
2005-07-08 : [3224.52]
2005-07-11 : [3246.4]
2005-07-12 : [3236.33]
2005-07-13 : [3260.67]
2005-07-14 : [3277.2]
2005-07-15 : [3278.58]
2005-07-18 : [3276.49]
2005-07-19 : [3314.06]
2005-07-20 : [3303.77]
2005-07-21 : [3299.92]
2005-07-22 : [3292.92]
2005-07-25 : [3298.27]
2005-07-26 : [3302.98]
2005-07-27 : [3310.84]
2005-07-28 : [3333.05]
2005-07-29 : [3326.51]
2005-08-01 : [3320.46]
2005-08-02 : [3349.91]
2005-08-03 : [3343.63]
2005-08-04 : [3310.62]
2005-08-05 : [3280.49]
2005-08-08 : [3292.41]
2005-08-09 : [3331.29]
2005-08-10 : [3370.84]
2005-08-11 : [3354.16]
2005-08-12 : [3334.15]
2005-08-15 : [3326.38]
2005-08-16 : [3307.42]
2005-08-17 : [3298.2]
2005-08-18 : [3283.84]
2005-08-19 : [3328.17]
2005-08-22 : [3330.44]
2005-08-23 : [3299.28]
2005-08-24 : [3294.25]
2005-08-25 : [3258.52]
2005-09-13 : [3325.55]
2005-09-14 : [3338.99]
2005-09-15 : [3339.02]
2005-09-16 : [3366.57]
2005-09-19 : [3356.28]
2005-09-20 : [3375.64]
2005-09-21 : [3326.78]
2005-09-22 : [3312.26]
2005-09-23 : [3331.55]
2005-09-26 : [3400.93]
2005-09-27 : [3384.24]
2005-09-28 : [3429.42]
2005-09-29 : [3412.75]
2005-09-30 : [3428.51]
2005-10-03 : [3449.34]
2005-10-04 : [3464.24]
2005-10-05 : [3418.84]
2005-10-06 : [3384.15]
2005-10-07 : [3374.1]
2005-10-10 : [3381.17]
2005-10-11 : [3387.46]
2005-10-12 : [3359.85]
2005-10-13 : [3331.42]
2005-10-14 : [3349.58]
2005-10-17 : [3356.61]
2005-10-18 : [3334.79]
2005-11-14 : [3412.34]
2005-11-15 : [3413.96]
2005-11-16 : [3391.59]
2005-11-17 : [3404.12]
2005-11-18 : [3427.18]
2005-11-21 : [3450.01]
2005-11-22 : [3450.51]
2005-11-23 : [3471.43]
2005-11-24 : [3459.15]
2005-11-25 : [3466.08]
2005-11-28 : [3453.11]
2005-11-29 : [3463.67]
2005-11-30 : [3447.07]
2005-12-01 : [3501.43]
2005-12-02 : [3519.63]
2005-12-05 : [3499.4]
2005-12-06 : [3516.84]
2005-12-07 : [3505.34]
2005-12-08 : [3510.38]
2005-12-09 : [3500.8]
2005-12-12 : [3514.07]
2005-12-13 : [3528.34]
2005-12-14 : [3519.12]
2005-12-15 : [3522.3]
2005-12-16 : [3556.76]
2005-12-19 : [3551.1]
2005-12-20 : [3561.11]
2005-12-21 : [3591.99]
2005-12-22 : [3591.02]
2005-12-23 : [3599.47]
2005-12-27 : [3612.21]
2005-12-28 : [3605.12]
2005-12-29 : [3616.33]
2005-12-30 : [3578.93]
2006-01-02 : [3604.33]
2006-01-03 : [3614.34]
2006-01-04 : [3652.46]
2006-01-05 : [3650.24]
2006-01-06 : [3666.99]
2006-01-09 : [3671.78]
2006-01-10 : [3644.94]
2006-01-11 : [3668.61]
2006-01-12 : [3670.2]
2006-01-13 : [3629.25]
2006-01-16 : [3644.41]
2006-01-17 : [3610.07]
2006-01-18 : [3570.17]
2006-01-19 : [3593.22]
2006-01-20 : [3550.8]
2006-01-23 : [3544.31]
2006-01-24 : [3532.68]
2006-01-25 : [3578.0]
2006-02-03 : [3678.48]
2006-02-06 : [3682.32]
2006-02-07 : [3680.8]
2006-02-08 : [3671.37]
2006-02-09 : [3726.81]
2006-02-10 : [3695.63]
2006-02-13 : [3727.46]
2006-02-14 : [3734.48]
2006-02-15 : [3729.79]
2006-02-16 : [3756.47]
2006-02-17 : [3767.7]
2006-02-20 : [3766.74]
2006-02-21 : [3779.51]
2006-02-22 : [3818.48]
2006-02-23 : [3813.29]
2006-02-24 : [3826.0]
2006-02-27 : [3840.56]
2006-02-28 : [3774.51]
2006-03-01 : [3806.03]
2006-03-02 : [3763.73]
2006-03-03 : [3733.95]
2006-03-06 : [3754.07]
2006-03-07 : [3745.2]
2006-03-08 : [3727.96]
2006-03-09 : [3757.59]
2006-03-10 : [3798.46]
2006-03-13 : [3824.97]
2006-03-14 : [3833.48]
2006-03-15 : [3842.16]
2006-03-16 : [3839.71]
2006-03-17 : [3832.43]
2006-03-20 : [3842.03]
2006-03-21 : [3848.17]
2006-03-22 : [3868.48]
2006-03-23 : [3860.13]
2006-03-24 : [3870.89]
2006-03-27 : [3828.53]
2006-03-28 : [3811.45]
2006-03-29 : [3826.3]
2006-03-30 : [3874.61]
2006-03-31 : [3853.74]
2006-04-03 : [3878.64]
2006-04-04 : [3850.11]
2006-04-05 : [3863.92]
2006-04-06 : [3861.29]
2006-04-07 : [3823.11]
2006-04-10 : [3843.52]
2006-04-11 : [3788.81]
2006-04-12 : [3776.94]
2006-04-13 : [3779.94]
2006-04-18 : [3770.79]
2006-04-19 : [3820.96]
2006-05-02 : [3862.24]
2006-05-03 : [3821.97]
2006-05-04 : [3843.08]
2006-05-05 : [3874.32]
2006-05-08 : [3877.53]
2006-05-09 : [3890.94]
2006-05-10 : [3863.56]
2006-05-11 : [3837.86]
2006-05-12 : [3750.44]
2006-05-15 : [3711.16]
2006-07-04 : [3670.75]
2006-07-05 : [3618.64]
2006-07-06 : [3662.39]
2006-07-07 : [3651.33]
2006-07-10 : [3666.51]
2006-07-11 : [3617.78]
2006-07-12 : [3630.5]
2006-07-13 : [3562.56]
2006-07-14 : [3508.25]
2006-07-17 : [3498.62]
2006-07-18 : [3492.11]
2006-07-19 : [3585.65]
2006-07-20 : [3589.63]
2006-07-21 : [3557.08]
2006-07-24 : [3632.93]
2006-07-25 : [3631.5]
2006-07-26 : [3640.75]
2006-07-28 : [3710.6]
2006-07-31 : [3691.87]
2006-08-01 : [3640.6]
2006-08-02 : [3696.35]
2006-08-03 : [3667.91]
2006-08-04 : [3718.09]
2006-08-07 : [3659.03]
2006-08-08 : [3668.1]
2006-08-09 : [3707.19]
2006-08-10 : [3675.44]
2006-08-11 : [3675.1]
2006-08-14 : [3719.11]
2006-08-15 : [3766.38]
2006-08-16 : [3790.94]
2006-08-17 : [3800.1]
2006-08-18 : [3791.4]
2006-08-21 : [3777.25]
2006-08-22 : [3792.55]
2006-08-23 : [3758.98]
2006-08-24 : [3781.87]
2006-08-25 : [3781.17]
2006-08-28 : [3808.57]
2006-08-29 : [3806.81]
2006-08-30 : [3817.86]
2006-08-31 : [3808.7]
2006-09-01 : [3820.89]
2006-09-04 : [3837.61]
2006-09-05 : [3817.76]
2006-09-06 : [3772.21]
2006-09-07 : [3739.7]
2006-09-08 : [3750.08]
2006-09-11 : [3742.06]
2006-09-12 : [3788.96]
2006-09-13 : [3805.55]
2006-09-14 : [3796.65]
2006-09-15 : [3812.11]
2006-09-18 : [3808.47]
2006-09-19 : [3780.18]
2006-09-20 : [3841.31]
2006-09-21 : [3857.14]
2006-09-22 : [3812.73]
2006-09-25 : [3822.12]
2006-09-26 : [3872.92]
2006-09-27 : [3896.18]
2006-09-28 : [3894.98]
2006-09-29 : [3899.41]
2006-10-02 : [3892.48]
2006-10-03 : [3880.14]
2006-10-04 : [3914.73]
2006-10-05 : [3939.86]
2006-10-06 : [3940.31]
2006-10-09 : [3939.48]
2006-10-10 : [3960.67]
2006-10-11 : [3967.39]
2006-10-12 : [3999.93]
2006-10-13 : [3999.07]
2006-10-16 : [4001.97]
2006-10-17 : [3949.57]
2006-10-18 : [3991.38]
2006-10-19 : [3986.82]
2006-10-20 : [3998.19]
2006-10-23 : [4019.02]
2006-10-24 : [4014.01]
2006-10-25 : [4019.14]
2006-10-26 : [4027.29]
2006-10-27 : [4017.27]
2006-10-30 : [4004.92]
2006-10-31 : [4004.8]
2006-11-01 : [4014.34]
2006-11-02 : [3974.62]
2006-11-03 : [3990.46]
2006-11-06 : [4045.22]
2006-11-07 : [4072.86]
2006-11-08 : [4073.81]
2006-11-09 : [4073.0]
2006-11-10 : [4063.84]
2006-11-13 : [4086.14]
2006-11-14 : [4084.33]
2006-11-15 : [4108.83]
2006-11-16 : [4109.71]
2006-11-17 : [4078.36]
2006-11-20 : [4096.74]
2006-11-21 : [4096.06]
2006-11-22 : [4094.97]
2006-11-23 : [4085.76]
2006-11-24 : [4048.16]
2006-11-27 : [3978.25]
2006-11-28 : [3975.11]
2006-11-29 : [4023.09]
2006-11-30 : [3987.23]
2006-12-01 : [3932.09]
2006-12-19 : [4100.48]
2006-12-20 : [4118.54]
2006-12-21 : [4112.1]
2006-12-22 : [4073.5]
2006-12-27 : [4134.86]
2006-12-28 : [4130.66]
2006-12-29 : [4119.94]
每天有可能有多个成交仓位,因此是list类型,第一个仓位值:
2005-04-11 : [3080.6]
对应的成交数据,以收盘价计算:
2005-04-11,3088.47,3088.47,3073.75,3080.60,0,0
class backtrader.analyzers.PyFolio()
使用4个子分析器收集数据并将其转换为与pyfolio兼容的数据集 。
4个子分析器:
时间范围和压缩都是按照pyfolio的默认行为设置的,pyfolio处理每日数据并对其进行向上采样以获得类似年度回报的值。
方法:
get_analysis(),同上
get_pf_items(),返回一个由4个元素组成的元组,可用于进一步处理
returns, positions, transactions, gross_leverage
必须有numpy pandas
方法pyfolio.create_full_tear_sheet ,当前版本已经不可用了。
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器
ret_anal = cerebro.addanalyzer(bt.analyzers.PyFolio, _name='myAnalysis')
# 运行策略
thestrats = cerebro.run()
thestrat = thestrats[0]
# 打印
#print('Annual Return:', thestrat.analyzers.myCalmar.get_analysis()) #
for ret_key,ret_val in thestrat.analyzers.myAnalysis.get_analysis().items():
print(ret_key,':')
#returns
if ret_key == 'returns' :
for k,v in ret_val.items() :
if v != 0 :
print(k.date(),':',v)
#print(k,v)
# positions
if ret_key == 'positions' :
for k,v in ret_val.items() :
if v[0] != 0 :
print(k,v)
结果:
returns :
2005-04-11 : -0.0007869999999998711
2005-04-12 : -0.0015432145098192018
2005-04-13 : 0.0015395856950837228
2005-04-14 : -0.0005214134808902982
2005-04-15 : -0.006152083838163347
2005-04-18 : -0.006659667802470848
... ...
positions :
Datetime ['2005-2006-day-001', 'cash']
2005-04-11 [3080.6, 6911.530000000001]
2005-04-12 [3065.18, 6911.530000000001]
2005-04-13 [3080.54, 6911.530000000001]
2005-04-14 [3075.33, 6911.530000000001]
2005-04-15 [3013.89, 6911.530000000001]
2005-04-18 [2947.79, 6911.530000000001]
2005-05-19 [3051.79, 6825.03]
... ...
for i in thestrat.analyzers.myAnalysis.get_pf_items():
print(i)
结果:
index
2005-01-03 00:00:00+00:00 0.000000
2005-01-04 00:00:00+00:00 0.000000
2005-01-05 00:00:00+00:00 0.000000
2005-01-06 00:00:00+00:00 0.000000
2005-01-07 00:00:00+00:00 0.000000
...
2006-12-21 00:00:00+00:00 -0.000614
2006-12-22 00:00:00+00:00 -0.003680
2006-12-27 00:00:00+00:00 0.005872
2006-12-28 00:00:00+00:00 -0.000400
2006-12-29 00:00:00+00:00 -0.001020
Name: return, Length: 512, dtype: float64
2005-2006-day-001 cash
Datetime
2005-01-03 00:00:00+00:00 0.00 10000.00
2005-01-04 00:00:00+00:00 0.00 10000.00
2005-01-05 00:00:00+00:00 0.00 10000.00
2005-01-06 00:00:00+00:00 0.00 10000.00
2005-01-07 00:00:00+00:00 0.00 10000.00
... ... ...
2006-12-21 00:00:00+00:00 4112.10 6376.74
2006-12-22 00:00:00+00:00 4073.50 6376.74
2006-12-27 00:00:00+00:00 4134.86 6376.74
2006-12-28 00:00:00+00:00 4130.66 6376.74
2006-12-29 00:00:00+00:00 4119.94 6376.74
[512 rows x 2 columns]
amount price sid symbol \
date
2005-04-11 23:59:59.999989+00:00 1 3088.47 0 2005-2006-day-001
2005-04-19 23:59:59.999989+00:00 -1 2948.38 0 2005-2006-day-001
2005-05-19 23:59:59.999989+00:00 1 3034.88 0 2005-2006-day-001
2005-08-26 23:59:59.999989+00:00 -1 3258.45 0 2005-2006-day-001
2005-09-13 23:59:59.999989+00:00 1 3353.61 0 2005-2006-day-001
2005-10-19 23:59:59.999989+00:00 -1 3330.00 0 2005-2006-day-001
2005-11-14 23:59:59.999989+00:00 1 3405.94 0 2005-2006-day-001
2006-01-26 23:59:59.999989+00:00 -1 3578.92 0 2005-2006-day-001
2006-02-03 23:59:59.999989+00:00 1 3677.05 0 2005-2006-day-001
2006-04-20 23:59:59.999989+00:00 -1 3820.93 0 2005-2006-day-001
2006-05-02 23:59:59.999989+00:00 1 3839.24 0 2005-2006-day-001
2006-05-16 23:59:59.999989+00:00 -1 3711.46 0 2005-2006-day-001
2006-07-04 23:59:59.999989+00:00 1 3664.59 0 2005-2006-day-001
2006-07-27 23:59:59.999989+00:00 -1 3649.29 0 2005-2006-day-001
2006-07-28 23:59:59.999989+00:00 1 3671.71 0 2005-2006-day-001
2006-12-04 23:59:59.999989+00:00 -1 3935.81 0 2005-2006-day-001
2006-12-19 23:59:59.999989+00:00 1 4121.01 0 2005-2006-day-001
value
date
2005-04-11 23:59:59.999989+00:00 -3088.47
2005-04-19 23:59:59.999989+00:00 2948.38
2005-05-19 23:59:59.999989+00:00 -3034.88
2005-08-26 23:59:59.999989+00:00 3258.45
2005-09-13 23:59:59.999989+00:00 -3353.61
2005-10-19 23:59:59.999989+00:00 3330.00
2005-11-14 23:59:59.999989+00:00 -3405.94
2006-01-26 23:59:59.999989+00:00 3578.92
2006-02-03 23:59:59.999989+00:00 -3677.05
2006-04-20 23:59:59.999989+00:00 3820.93
2006-05-02 23:59:59.999989+00:00 -3839.24
2006-05-16 23:59:59.999989+00:00 3711.46
2006-07-04 23:59:59.999989+00:00 -3664.59
2006-07-27 23:59:59.999989+00:00 3649.29
2006-07-28 23:59:59.999989+00:00 -3671.71
2006-12-04 23:59:59.999989+00:00 3935.81
2006-12-19 23:59:59.999989+00:00 -4121.01
index
2005-01-03 23:59:59.999989+00:00 0.000000
2005-01-04 23:59:59.999989+00:00 0.000000
2005-01-05 23:59:59.999989+00:00 0.000000
2005-01-06 23:59:59.999989+00:00 0.000000
2005-01-07 23:59:59.999989+00:00 0.000000
...
2006-12-21 23:59:59.999989+00:00 0.392045
2006-12-22 23:59:59.999989+00:00 0.389800
2006-12-27 23:59:59.999989+00:00 0.393362
2006-12-28 23:59:59.999989+00:00 0.393119
2006-12-29 23:59:59.999989+00:00 0.392499
Name: gross_lev, Length: 512, dtype: float64
class backtrader.analyzers.LogReturnsRolling()
用于计算滚动对数收益率的分析器 。
参数:
前一年的最后一次收盘用作参考价格,以查看本年度的回报 问题是第一次计算,如果因为数据中没有以前的收盘价。因此,当此参数为True时,开盘价将用于第一次计算。
This requires the data feed to have an open price (for close the standard [0] notation will be used without reference to a field price)
要求数据加载具有open,(对于close,将使用标准的[0])
否则将使用初始收盘价。
# 实例化
cerebro = bt.Cerebro()
# 加载数据源
cerebro.adddata(data)
# 加载SMA交叉策略
cerebro.addstrategy(btstrats.SMA_CrossOver)
# 添加分析器
ret_anal = cerebro.addanalyzer(bt.analyzers.LogReturnsRolling, _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.0015384017480636204
2005-04-19 5.9840064766955636e-05
2005-05-19 0.0017135568189565573
2005-05-23 0.002076728398161644
2005-05-26 0.0026510448452642743
2005-05-30 0.0012647122801604461
2005-06-01 0.004949465316348376
2005-06-02 0.0005174067288886129
... ...
2006-11-16 8.246487516412817e-05
2006-11-20 0.0017259046735989803
2006-11-29 0.004543120992380261
2006-12-04 0.0003544244498504552
2006-12-20 0.0017222557600743557
2006-12-27 0.005854464613617208