pyalgotrade入门
入门代码解析:
from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed
#继承自BacktestingStrategy里面有feed,这就是策略类
class MyStrategy(strategy.BacktestingStrategy):
def __init__(self, feed, instrument):
#在有父类的时候赋值的方法把feed用进初始化
super(MyStrategy, self).__init__(feed)
#简单的赋值
self.__instrument = instrument
#bars是一个字典类型的数据,取到字典里面的数据而已(bar)
#这个函数用来制定策略用,这是每次交易打印出收盘价(可以理解为每次价格变化都调用的方法)
def onBars(self, bars):
#bar是对某一特定时期内证券交易活动的总结。这里就是orcl这只股票的2000年股票价格的总结
bar = bars[self.__instrument]
#框架自带的打印
http://self.info(bar.getClose())
# 获取feed
feed = yahoofeed.Feed()
#这里向feed里bars池子增加一个orcl股票价格走势的总结(一个bar),如果有需要可以继续加入
feed.addBarsFromCSV("orcl", "orcl-2000.csv")
# 初始化策略
myStrategy = MyStrategy(feed, "orcl")
#策略执行
myStrategy.run()
orcl-2000.csv
附录:数据
Date,Open,High,Low,Close,Volume,Adj Close
2000-01-03,124.625,125.187523,111.625,118.125,98114800,27.425189000000003
2000-01-04,115.5,118.625,105.0,107.687523,116824800,25.001910000000002
2000-01-05,101.625,106.375,96.0,102.0,166054000,23.681433
2000-01-06,100.156242,105.0,94.687523,96.0,109880000,22.288407
2000-01-07,95.0,103.5,93.562477,103.375,91755600,24.000668
2000-01-10,108.0,116.0,105.5,115.75,91518000,26.873783000000003
2000-01-11,112.625,114.75,109.5,112.375,86585200,26.090206
2000-01-12,112.25,112.25,103.687523,105.625,83443600,24.523052
2000-01-13,108.5,109.875,103.5,105.062477,55779200,24.392451
2000-01-14,109.0,111.375,104.75,106.812477,57078000,24.79875
2000-01-18,107.875,114.5,105.625,111.25,66780000,25.829014
2000-01-19,56.125,58.25,54.0,57.125,49198400,26.525526
2000-01-20,59.0,60.25,58.125,59.25,54526800,27.512253
2000-01-21,61.5,61.5,59.0,59.6875,50891000,27.715402
2000-01-24,60.25,60.375,54.0,54.1875,50022400,25.161522
2000-01-25,55.0625,57.5,54.875,56.4375,53059200,26.206291
2000-01-26,56.75,58.9375,55.0,55.0625,47569200,25.567821
2000-01-27,55.8125,56.6875,50.0,51.8125,61054000,24.058711
2000-01-28,51.5,51.9375,46.625,47.375,86394000,21.998194
2000-01-31,47.9375,50.125,47.0625,49.953121,68148000,23.195323000000002
2000-02-01,51.25,54.3125,50.0,54.0,57105600,25.074458
2000-02-02,54.9375,56.0,54.0,54.3125,63933000,25.219565
2000-02-03,55.375,57.0,54.25,56.6875,55533200,26.322377000000003
2000-02-04,57.625,58.25,56.8125,57.8125,40916000,26.844761
2000-02-07,59.3125,60.0,58.875,59.9375,44691200,27.831488
2000-02-08,60.75,61.4375,59.0,59.5625,55718000,27.657359999999997
2000-02-09,60.0625,61.3125,58.8125,59.9375,52471600,27.831488
2000-02-10,60.0,62.625,58.0,62.3125,45288600,28.9343
2000-02-11,62.5,64.75,58.75,59.6875,55774000,27.715402
2000-02-14,60.875,62.25,58.625,62.1875,37599800,28.876257
2000-02-15,61.625,63.1875,59.125,61.375,47971400,28.498979
2000-02-16,61.0,62.5625,60.125,61.25,33489200,28.440935999999997
2000-02-17,61.75,62.75,60.0,61.625,40392600,28.615065
2000-02-18,61.6875,62.0,58.375,58.5625,63888400,27.193018
2000-02-22,59.125,61.4375,56.3125,59.3125,75546200,27.541273999999998
2000-02-23,60.1875,63.265620999999996,59.625,63.0625,69664400,29.282556
2000-02-24,63.1875,65.0625,60.25,61.9375,70963200,28.760171000000003
2000-02-25,61.875,71.0,61.875,70.625,103186600,32.794140999999996
2000-02-28,71.25,76.5,68.25,68.625,122316600,31.865457
2000-02-29,73.375,74.4375,71.125,74.25,55586400,34.47738
2000-03-01,73.75,74.5,70.75,71.5,44934400,33.20044
2000-03-02,73.5,73.5625,67.875,68.5,52311000,31.807415000000002
2000-03-03,71.125,75.125,70.171883,75.0,47974400,34.825635999999996
2000-03-06,74.8125,76.9375,72.375,75.75,45520600,35.173893
2000-03-07,76.625,80.0,74.375,74.9375,53982200,34.796615
2000-03-08,77.375,83.5625,75.0,83.125,62781200,38.598414
2000-03-09,84.3125,85.0,80.125,84.0,56476400,39.004713
2000-03-10,84.0,84.0625,80.25,81.625,44699800,37.901901
2000-03-13,78.5,83.625,76.796883,78.75,65803200,36.566918
2000-03-14,81.875,83.75,76.5,77.0,72364000,35.75432
2000-03-15,84.0,85.0,77.0,78.625,144810400,36.508876
2000-03-16,80.6875,82.5625,76.6875,81.9375,95532400,38.047008
2000-03-17,81.75,82.0,79.5,79.8125,66055200,37.060280999999996
2000-03-20,80.5,80.625,77.5625,78.125,47183600,36.276705
2000-03-21,77.875,80.875,76.0,80.6875,52621400,37.466581
2000-03-22,81.0,84.375,78.875,84.0625,53197600,39.033734
2000-03-23,83.125,88.0,82.5625,86.875,50803800,40.339696
2000-03-24,86.8125,89.6875,86.4375,87.0,52821600,40.397738000000004
2000-03-27,89.0625,89.3125,86.9375,88.4375,32597200,41.06523
2000-03-28,87.625,90.0,85.5,86.5625,36039200,40.194589
2000-03-29,86.0625,86.125,82.0625,82.5,51966200,38.3082
2000-03-30,78.5,84.5,78.0,78.4375,70986000,36.421811
2000-03-31,80.1875,81.5625,76.0,78.0625,79842000,36.247683
2000-04-03,78.625,80.625,75.0,76.875,71598000,35.696277
2000-04-04,78.125,78.125,65.078117,75.9375,123633800,35.260957
2000-04-05,73.25,81.0,72.546883,78.25,73736800,36.334747
2000-04-06,80.625,83.4375,80.125,82.1875,62408200,38.163092999999996
2000-04-07,83.6875,87.25,83.0,87.125,46764600,40.455781
2000-04-10,87.5,87.625,82.25,82.5,56241200,38.3082
2000-04-11,80.1875,81.625,76.8125,77.375,70983800,35.928447999999996
2000-04-12,77.9375,80.0,72.9375,73.125,67133000,33.954996
2000-04-13,74.875,77.5,71.75,71.9375,63206800,33.40359
2000-04-14,69.921883,71.281242,60.25,62.5,109951400,29.021364000000002
2000-04-17,60.5,74.875,60.171879000000004,74.8125,115304800,34.738572
2000-04-18,75.5625,79.0625,73.75,78.9375,78738800,36.653982
2000-04-19,78.625,79.5625,74.25,74.5625,46184200,34.622487
2000-04-20,73.8125,75.5,69.875,70.8125,50397800,32.881205
2000-04-24,67.375,72.6875,67.0,72.4375,59725600,33.635760999999995
2000-04-25,74.875,76.375,72.625,75.5625,61764200,35.086828999999994
2000-04-26,76.125,77.8125,71.0625,72.1875,47788800,33.519675
2000-04-27,70.0625,77.5625,69.9375,77.3125,52101000,35.899427
2000-04-28,78.5,80.0,78.125,79.9375,41696600,37.118324
2000-05-01,79.875,81.875,78.75,79.6875,37093400,37.002239
2000-05-02,78.9375,79.5,77.375,77.8125,35341000,36.131598
2000-05-03,76.875,77.125,72.125,75.8125,49603200,35.202914
2000-05-04,75.3125,76.9375,73.0,74.25,39899800,34.47738
2000-05-05,74.0,77.0,73.75,76.8125,35289600,35.667256
2000-05-08,75.3125,76.0,72.0625,72.3125,36528400,33.577718
2000-05-09,73.6875,74.25,71.25,72.0,43535200,33.432611
2000-05-10,70.9375,73.125,67.5,67.625,71169800,31.401116
2000-05-11,68.9375,72.375,68.0,72.375,46590200,33.606739000000005
2000-05-12,73.3125,76.4375,73.0625,74.1875,39038200,34.448359
2000-05-15,74.4375,77.0,70.625,77.0,41971800,35.75432
2000-05-16,77.625,79.75,77.0625,79.1875,38261400,36.770068
2000-05-17,77.75,79.875,76.25,78.1875,33993600,36.305726
2000-05-18,78.3125,78.75,72.4375,73.0625,46444600,33.925974
2000-05-19,72.0,74.0,69.5,70.0625,52228800,32.532949
2000-05-22,70.0625,70.25,62.75,67.8125,91156000,31.48818
2000-05-23,67.0,68.5,62.375,62.625,58574000,29.079406
2000-05-24,61.765620999999996,64.5,58.125,64.25,85195200,29.833962
2000-05-25,65.5,70.8125,65.0,66.4375,80958000,30.849709999999998
2000-05-26,66.8125,69.5,65.0625,67.0,28903800,31.110902000000003
2000-05-30,68.875,74.1875,68.5625,74.1875,38933000,34.448359
2000-05-31,73.25,76.4375,71.75,71.875,50939800,33.374568
2000-06-01,73.9375,77.875,73.5,77.875,53401000,36.160619
2000-06-02,80.5,81.5,78.5,80.1875,57964800,37.23441
2000-06-05,79.25,82.0,79.0,80.9375,38507200,37.582665999999996
2000-06-06,80.5,81.5,76.25,77.0625,40020000,35.783341
2000-06-07,76.75,80.25,75.5625,80.0,34080800,37.147346
2000-06-08,81.875,82.75,80.25,82.375,46768600,38.250157
2000-06-09,82.875,83.75,81.875,82.6875,32975200,38.395264000000005
2000-06-12,82.5625,82.8125,80.375,80.5625,24206000,37.408538
2000-06-13,79.984383,81.75,78.0625,81.6875,37229400,37.930921999999995
2000-06-14,81.875,82.0,78.75,79.0,33191800,36.683004
2000-06-15,79.25,82.625,79.125,82.5,36786800,38.3082
2000-06-16,81.875,83.75,81.0625,81.875,41904400,38.017986
2000-06-19,82.0625,86.0625,81.25,86.0,51077200,39.933396
2000-06-20,86.9375,87.625,84.5,86.046883,57192600,39.955166
2000-06-21,80.8125,87.375,80.3125,86.1875,108053800,40.020461
2000-06-22,84.625,85.25,81.375,81.5,42105600,37.843858000000004
2000-06-23,80.8125,81.484383,78.5625,79.5,40643000,36.915175
2000-06-26,80.25,83.4375,79.25,82.6875,38942400,38.395264000000005
2000-06-27,82.375,83.9375,81.6875,82.25,28581600,38.192115
2000-06-28,82.0625,85.0,81.6875,82.9375,32104800,38.51135
2000-06-29,82.0625,83.125,79.5625,80.875,35751800,37.553645
2000-06-30,80.375,84.5,80.0,84.0625,38093000,39.033734
2000-07-03,81.125,81.75,78.875,80.1875,42136200,37.23441
2000-07-05,76.8125,77.625,72.1875,72.3125,94634200,33.577718
2000-07-06,71.75,75.875,71.75,75.625,50533000,35.11585
2000-07-07,75.9375,77.0,75.5,75.875,31480200,35.231936
2000-07-10,74.625,75.5,72.875,74.125,43957600,34.419337
2000-07-11,73.625,75.25,71.125,72.3125,42121400,33.577718
2000-07-12,73.4375,75.125,73.0,74.6875,34368400,34.68053
2000-07-13,75.75,76.625,74.75,75.75,41625400,35.173893
2000-07-14,76.1875,76.5,75.125,76.125,31712000,35.348021
2000-07-17,75.3125,78.0,74.0625,76.125,32388800,35.348021
2000-07-18,75.6875,76.625,74.0625,74.1875,29967000,34.448359
2000-07-19,75.0,75.9375,73.0,73.75,31558600,34.245209
2000-07-20,74.8125,79.0,74.75,78.125,35382000,36.276705
2000-07-21,77.4375,77.6875,75.0625,75.4375,30925000,35.028786
2000-07-24,77.125,78.625,74.9375,75.0,38060200,34.825635999999996
2000-07-25,75.875,76.5,74.3125,76.0,27118200,35.289978000000005
2000-07-26,74.8125,78.0,74.0625,76.75,31826400,35.638234999999995
2000-07-27,75.9375,78.0,75.0,75.0625,32539000,34.854658
2000-07-28,75.125,76.4375,71.0625,72.375,33203400,33.606739000000005
2000-07-31,71.75,75.5,71.375,75.1875,34406400,34.912701
2000-08-01,75.1875,75.875,72.875,73.125,30190800,33.954996
2000-08-02,73.0,75.25,72.375,73.125,30315000,33.954996
2000-08-03,72.8125,77.5,71.625,77.4375,44941200,35.95747
2000-08-04,78.3125,82.3125,78.125,81.5625,73211600,37.87288
2000-08-07,80.875,82.375,80.75,81.875,40751400,38.017986
2000-08-08,81.0,83.25,80.8125,83.0,34931800,38.540371
2000-08-09,83.5,84.281242,81.1875,81.375,30846000,37.785816
2000-08-10,81.4375,82.375,80.0,80.0625,24436000,37.176367
2000-08-11,79.9375,81.9375,79.25,81.125,22106800,37.66973
2000-08-14,80.1875,82.5625,79.875,82.375,21862200,38.250157
2000-08-15,81.9375,83.5625,81.125,81.25,31208800,37.727773
2000-08-16,81.375,82.25,81.0625,81.1875,22698200,37.698751
2000-08-17,80.875,84.0625,80.8125,83.9375,30085000,38.975691
2000-08-18,83.5,84.75,81.125,81.3125,31121200,37.756794
2000-08-21,82.125,83.375,80.765617,83.1875,20295600,38.627435
2000-08-22,82.875,84.1875,82.1875,83.5625,23251600,38.801563
2000-08-23,82.9375,83.125,81.0,82.875,25104800,38.482328
2000-08-24,82.875,85.0625,82.6875,84.6875,40413200,39.323948
2000-08-25,84.6875,86.8125,84.5,84.625,26866600,39.294926000000004
2000-08-28,85.0,87.9375,84.9375,86.75,25833600,40.281653000000006
2000-08-29,86.8125,89.265617,86.5625,87.75,37678600,40.745995
2000-08-30,87.6875,88.8125,87.125,88.25,27073400,40.978165999999995
2000-08-31,88.4375,91.0,88.4375,90.9375,39840000,42.226084
2000-09-01,92.203117,92.9375,90.875,92.625,30417800,43.009661
2000-09-05,92.375,92.8125,90.5625,91.0625,25535800,42.284127000000005
2000-09-06,91.375,91.75,89.0625,89.25,38473000,41.442507
2000-09-07,90.125,91.75,89.5,91.1875,28559000,42.34217
2000-09-08,90.8125,90.875,86.5,86.5625,34569400,40.194589
2000-09-11,86.0625,86.75,82.375,83.4375,41459600,38.743521
2000-09-12,83.0,85.3125,79.0625,79.375,49606000,36.857132
2000-09-13,77.75,82.75,76.9375,81.8125,68132400,37.988965
2000-09-14,86.375,86.625,82.25,84.9375,64170600,39.440033
2000-09-15,81.125,82.875,78.0,78.3125,122939600,36.363769
2000-09-18,78.8125,80.0,74.75,76.468758,72997800,35.507642
2000-09-19,77.4375,79.75,76.125,79.3125,53198600,36.828111
2000-09-20,79.0,80.625,77.5,79.9375,37352200,37.118324
2000-09-21,79.4375,80.0,75.5,78.9375,27160000,36.653982
2000-09-22,75.9375,80.75,75.5,80.734383,58729400,37.48835
2000-09-25,81.125,82.5625,78.75,78.75,34748600,36.566918
2000-09-26,79.3125,81.0,78.625,79.4375,39993400,36.886153
2000-09-27,80.1875,80.9375,78.0,79.875,48205600,37.089303
2000-09-28,79.5,82.1875,78.375,81.484383,42188600,37.836606
2000-09-29,81.359383,81.6875,78.25,78.75,34843000,36.566918
2000-10-02,79.75,82.125,77.718758,78.75,46136800,36.566918
2000-10-03,79.8125,81.609383,68.75,69.5,96827800,32.271755999999996
2000-10-04,66.0,70.75,60.5,68.125,204852000,31.633286
2000-10-05,68.3125,69.625,66.5,68.3125,54316000,31.720351
2000-10-06,69.25,71.625,66.875,67.625,53378400,31.401116
2000-10-09,67.625,68.75,65.125,66.75,40304000,30.994815999999997
2000-10-10,66.0625,67.25,64.5,64.625,50948400,30.008090000000003
2000-10-11,63.0,65.984383,62.0,62.25,71687200,28.905278000000003
2000-10-12,63.8125,64.875,61.75,63.0,50889400,29.253535
2000-10-13,31.0,35.75,31.0,35.625,38514000,33.084355
2000-10-16,36.5625,37.0,33.375,34.5625,36480500,32.097628
2000-10-17,35.75,35.875,32.875,33.6875,29353900,31.28503
2000-10-18,31.5625,35.375,30.5,33.5625,52547900,31.168945
2000-10-19,36.0,36.5,34.625,36.375,39321000,33.780867
2000-10-20,36.125,36.5,34.9375,35.25,34073400,32.736098
2000-10-23,35.1875,35.25,33.375,34.0625,28316300,31.633286
2000-10-24,35.0,36.5625,34.5,35.8125,36713000,33.258483
2000-10-25,36.5,36.625,34.0,34.375,33361600,31.9235
2000-10-26,34.75,35.875,30.9375,34.0625,49986600,31.633286
2000-10-27,34.5,35.1875,33.0,34.1875,34962500,31.749371999999997
2000-10-30,33.75,34.125,31.375,31.625,35420300,29.36962
2000-10-31,32.625,34.75,32.5,33.0,41881000,30.646559999999997
2000-11-01,32.8125,33.1875,30.375,31.375,62692100,29.137449
2000-11-02,32.4375,32.5,27.25,29.5625,149767500,27.454209999999996
2000-11-03,31.5,31.75,29.5,30.3125,65017500,28.150723
2000-11-06,30.6875,31.0,27.5,27.9375,75551500,25.945099
2000-11-07,28.375,28.4375,26.5,26.5625,58951000,24.668159
2000-11-08,27.375,27.5,24.5,24.8125,63040000,23.042963
2000-11-09,24.6875,27.375,24.0625,27.1875,67280700,25.248586
2000-11-10,26.4375,26.9375,24.875,25.4375,54609400,23.62339
2000-11-13,25.125,25.875,23.5,24.75,61652800,22.984920000000002
2000-11-14,27.375,28.5,26.5,28.375,77494300,26.351398
2000-11-15,28.8125,29.4375,27.703119,28.875,50649700,26.81574
2000-11-16,28.75,29.8125,27.25,27.375,37986600,25.422715
2000-11-17,26.9375,29.25,25.25,28.8125,59636000,26.757696999999997
2000-11-20,24.3125,25.875,24.0,24.75,89778400,22.984920000000002
2000-11-21,24.8125,25.625,23.5,23.875,58647400,22.172322
2000-11-22,23.625,24.0625,22.0625,22.3125,53315300,20.721254000000002
2000-11-24,23.3125,24.25,23.125,24.125,22443900,22.404493
2000-11-27,25.4375,25.8125,22.875,23.125,45665200,21.475808999999998
2000-11-28,23.5,23.8125,22.25,22.65625,43075300,21.040489
2000-11-29,23.1875,23.625,21.8125,22.875,75409600,21.243638
2000-11-30,21.75,27.625,21.5,26.5,84386200,24.610116
2000-12-01,26.375,27.875,25.5,26.4375,48663500,24.552073999999998
2000-12-04,26.25,28.875,26.1875,28.1875,40710400,26.17727
2000-12-05,29.4375,31.5,28.875,31.5,59754700,29.253535
2000-12-06,31.1875,31.625,29.3125,30.1875,42125600,28.034637
2000-12-07,29.625,29.9375,28.125,28.3125,41088300,26.293356
2000-12-08,30.0625,30.625,29.25,30.0625,40052600,27.918552000000002
2000-12-11,30.5,32.25,30.0,31.9375,50279700,29.659834000000004
2000-12-12,31.875,32.5,30.40625,30.75,26481200,28.557021999999996
2000-12-13,31.9375,32.0,28.25,28.375,37933600,26.351398
2000-12-14,29.25,29.9375,27.25,27.5,45894400,25.5388
2000-12-15,29.4375,30.078119,28.1875,28.5625,120004000,26.525526
2000-12-18,30.0,32.4375,29.9375,32.0,61640100,29.717876
2000-12-19,31.8125,33.125,30.125,30.625,58653700,28.440935999999997
2000-12-20,28.0625,29.8125,27.5,28.5,54440500,26.467484000000002
2000-12-21,27.8125,30.25,27.3125,29.5,46719700,27.396167
2000-12-22,30.375,31.984381,30.0,31.875,35568200,29.601791
2000-12-26,31.5,32.1875,30.0,30.9375,20589500,28.73115
2000-12-27,30.375,31.0625,29.375,30.6875,26437500,28.498979
2000-12-28,30.5625,31.625,30.375,31.0625,25053600,28.847236
2000-12-29,30.875,31.3125,28.6875,29.0625,31702200,26.989867999999998