入门代码解析:
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]#框架自带的打印(bar.getClose())# 获取feedfeed = 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.4251890000000032000-01-04,115.5,118.625,105.0,107.687523,116824800,25.0019100000000022000-01-05,101.625,106.375,96.0,102.0,166054000,23.6814332000-01-06,100.156242,105.0,94.687523,96.0,109880000,22.2884072000-01-07,95.0,103.5,93.562477,103.375,91755600,24.0006682000-01-10,108.0,116.0,105.5,115.75,91518000,26.8737830000000032000-01-11,112.625,114.75,109.5,112.375,86585200,26.0902062000-01-12,112.25,112.25,103.687523,105.625,83443600,24.5230522000-01-13,108.5,109.875,103.5,105.062477,55779200,24.3924512000-01-14,109.0,111.375,104.75,106.812477,57078000,24.798752000-01-18,107.875,114.5,105.625,111.25,66780000,25.8290142000-01-19,56.125,58.25,54.0,57.125,49198400,26.5255262000-01-20,59.0,60.25,58.125,59.25,54526800,27.5122532000-01-21,61.5,61.5,59.0,59.6875,50891000,27.7154022000-01-24,60.25,60.375,54.0,54.1875,50022400,25.1615222000-01-25,55.0625,57.5,54.875,56.4375,53059200,26.2062912000-01-26,56.75,58.9375,55.0,55.0625,47569200,25.5678212000-01-27,55.8125,56.6875,50.0,51.8125,61054000,24.0587112000-01-28,51.5,51.9375,46.625,47.375,86394000,21.9981942000-01-31,47.9375,50.125,47.0625,49.953121,68148000,23.1953230000000022000-02-01,51.25,54.3125,50.0,54.0,57105600,25.0744582000-02-02,54.9375,56.0,54.0,54.3125,63933000,25.2195652000-02-03,55.375,57.0,54.25,56.6875,55533200,26.3223770000000032000-02-04,57.625,58.25,56.8125,57.8125,40916000,26.8447612000-02-07,59.3125,60.0,58.875,59.9375,44691200,27.8314882000-02-08,60.75,61.4375,59.0,59.5625,55718000,27.6573599999999972000-02-09,60.0625,61.3125,58.8125,59.9375,52471600,27.8314882000-02-10,60.0,62.625,58.0,62.3125,45288600,28.93432000-02-11,62.5,64.75,58.75,59.6875,55774000,27.7154022000-02-14,60.875,62.25,58.625,62.1875,37599800,28.8762572000-02-15,61.625,63.1875,59.125,61.375,47971400,28.4989792000-02-16,61.0,62.5625,60.125,61.25,33489200,28.4409359999999972000-02-17,61.75,62.75,60.0,61.625,40392600,28.6150652000-02-18,61.6875,62.0,58.375,58.5625,63888400,27.1930182000-02-22,59.125,61.4375,56.3125,59.3125,75546200,27.5412739999999982000-02-23,60.1875,63.265620999999996,59.625,63.0625,69664400,29.2825562000-02-24,63.1875,65.0625,60.25,61.9375,70963200,28.7601710000000032000-02-25,61.875,71.0,61.875,70.625,103186600,32.7941409999999962000-02-28,71.25,76.5,68.25,68.625,122316600,31.8654572000-02-29,73.375,74.4375,71.125,74.25,55586400,34.477382000-03-01,73.75,74.5,70.75,71.5,44934400,33.200442000-03-02,73.5,73.5625,67.875,68.5,52311000,31.8074150000000022000-03-03,71.125,75.125,70.171883,75.0,47974400,34.8256359999999962000-03-06,74.8125,76.9375,72.375,75.75,45520600,35.1738932000-03-07,76.625,80.0,74.375,74.9375,53982200,34.7966152000-03-08,77.375,83.5625,75.0,83.125,62781200,38.5984142000-03-09,84.3125,85.0,80.125,84.0,56476400,39.0047132000-03-10,84.0,84.0625,80.25,81.625,44699800,37.9019012000-03-13,78.5,83.625,76.796883,78.75,65803200,36.5669182000-03-14,81.875,83.75,76.5,77.0,72364000,35.754322000-03-15,84.0,85.0,77.0,78.625,144810400,36.5088762000-03-16,80.6875,82.5625,76.6875,81.9375,95532400,38.0470082000-03-17,81.75,82.0,79.5,79.8125,66055200,37.0602809999999962000-03-20,80.5,80.625,77.5625,78.125,47183600,36.2767052000-03-21,77.875,80.875,76.0,80.6875,52621400,37.4665812000-03-22,81.0,84.375,78.875,84.0625,53197600,39.0337342000-03-23,83.125,88.0,82.5625,86.875,50803800,40.3396962000-03-24,86.8125,89.6875,86.4375,87.0,52821600,40.3977380000000042000-03-27,89.0625,89.3125,86.9375,88.4375,32597200,41.065232000-03-28,87.625,90.0,85.5,86.5625,36039200,40.1945892000-03-29,86.0625,86.125,82.0625,82.5,51966200,38.30822000-03-30,78.5,84.5,78.0,78.4375,70986000,36.4218112000-03-31,80.1875,81.5625,76.0,78.0625,79842000,36.2476832000-04-03,78.625,80.625,75.0,76.875,71598000,35.6962772000-04-04,78.125,78.125,65.078117,75.9375,123633800,35.2609572000-04-05,73.25,81.0,72.546883,78.25,73736800,36.3347472000-04-06,80.625,83.4375,80.125,82.1875,62408200,38.1630929999999962000-04-07,83.6875,87.25,83.0,87.125,46764600,40.4557812000-04-10,87.5,87.625,82.25,82.5,56241200,38.30822000-04-11,80.1875,81.625,76.8125,77.375,70983800,35.9284479999999962000-04-12,77.9375,80.0,72.9375,73.125,67133000,33.9549962000-04-13,74.875,77.5,71.75,71.9375,63206800,33.403592000-04-14,69.921883,71.281242,60.25,62.5,109951400,29.0213640000000022000-04-17,60.5,74.875,60.171879000000004,74.8125,115304800,34.7385722000-04-18,75.5625,79.0625,73.75,78.9375,78738800,36.6539822000-04-19,78.625,79.5625,74.25,74.5625,46184200,34.6224872000-04-20,73.8125,75.5,69.875,70.8125,50397800,32.8812052000-04-24,67.375,72.6875,67.0,72.4375,59725600,33.6357609999999952000-04-25,74.875,76.375,72.625,75.5625,61764200,35.0868289999999942000-04-26,76.125,77.8125,71.0625,72.1875,47788800,33.5196752000-04-27,70.0625,77.5625,69.9375,77.3125,52101000,35.8994272000-04-28,78.5,80.0,78.125,79.9375,41696600,37.1183242000-05-01,79.875,81.875,78.75,79.6875,37093400,37.0022392000-05-02,78.9375,79.5,77.375,77.8125,35341000,36.1315982000-05-03,76.875,77.125,72.125,75.8125,49603200,35.2029142000-05-04,75.3125,76.9375,73.0,74.25,39899800,34.477382000-05-05,74.0,77.0,73.75,76.8125,35289600,35.6672562000-05-08,75.3125,76.0,72.0625,72.3125,36528400,33.5777182000-05-09,73.6875,74.25,71.25,72.0,43535200,33.4326112000-05-10,70.9375,73.125,67.5,67.625,71169800,31.4011162000-05-11,68.9375,72.375,68.0,72.375,46590200,33.6067390000000052000-05-12,73.3125,76.4375,73.0625,74.1875,39038200,34.4483592000-05-15,74.4375,77.0,70.625,77.0,41971800,35.754322000-05-16,77.625,79.75,77.0625,79.1875,38261400,36.7700682000-05-17,77.75,79.875,76.25,78.1875,33993600,36.3057262000-05-18,78.3125,78.75,72.4375,73.0625,46444600,33.9259742000-05-19,72.0,74.0,69.5,70.0625,52228800,32.5329492000-05-22,70.0625,70.25,62.75,67.8125,91156000,31.488182000-05-23,67.0,68.5,62.375,62.625,58574000,29.0794062000-05-24,61.765620999999996,64.5,58.125,64.25,85195200,29.8339622000-05-25,65.5,70.8125,65.0,66.4375,80958000,30.8497099999999982000-05-26,66.8125,69.5,65.0625,67.0,28903800,31.1109020000000032000-05-30,68.875,74.1875,68.5625,74.1875,38933000,34.4483592000-05-31,73.25,76.4375,71.75,71.875,50939800,33.3745682000-06-01,73.9375,77.875,73.5,77.875,53401000,36.1606192000-06-02,80.5,81.5,78.5,80.1875,57964800,37.234412000-06-05,79.25,82.0,79.0,80.9375,38507200,37.5826659999999962000-06-06,80.5,81.5,76.25,77.0625,40020000,35.7833412000-06-07,76.75,80.25,75.5625,80.0,34080800,37.1473462000-06-08,81.875,82.75,80.25,82.375,46768600,38.2501572000-06-09,82.875,83.75,81.875,82.6875,32975200,38.3952640000000052000-06-12,82.5625,82.8125,80.375,80.5625,24206000,37.4085382000-06-13,79.984383,81.75,78.0625,81.6875,37229400,37.9309219999999952000-06-14,81.875,82.0,78.75,79.0,33191800,36.6830042000-06-15,79.25,82.625,79.125,82.5,36786800,38.30822000-06-16,81.875,83.75,81.0625,81.875,41904400,38.0179862000-06-19,82.0625,86.0625,81.25,86.0,51077200,39.9333962000-06-20,86.9375,87.625,84.5,86.046883,57192600,39.9551662000-06-21,80.8125,87.375,80.3125,86.1875,108053800,40.0204612000-06-22,84.625,85.25,81.375,81.5,42105600,37.8438580000000042000-06-23,80.8125,81.484383,78.5625,79.5,40643000,36.9151752000-06-26,80.25,83.4375,79.25,82.6875,38942400,38.3952640000000052000-06-27,82.375,83.9375,81.6875,82.25,28581600,38.1921152000-06-28,82.0625,85.0,81.6875,82.9375,32104800,38.511352000-06-29,82.0625,83.125,79.5625,80.875,35751800,37.5536452000-06-30,80.375,84.5,80.0,84.0625,38093000,39.0337342000-07-03,81.125,81.75,78.875,80.1875,42136200,37.234412000-07-05,76.8125,77.625,72.1875,72.3125,94634200,33.5777182000-07-06,71.75,75.875,71.75,75.625,50533000,35.115852000-07-07,75.9375,77.0,75.5,75.875,31480200,35.2319362000-07-10,74.625,75.5,72.875,74.125,43957600,34.4193372000-07-11,73.625,75.25,71.125,72.3125,42121400,33.5777182000-07-12,73.4375,75.125,73.0,74.6875,34368400,34.680532000-07-13,75.75,76.625,74.75,75.75,41625400,35.1738932000-07-14,76.1875,76.5,75.125,76.125,31712000,35.3480212000-07-17,75.3125,78.0,74.0625,76.125,32388800,35.3480212000-07-18,75.6875,76.625,74.0625,74.1875,29967000,34.4483592000-07-19,75.0,75.9375,73.0,73.75,31558600,34.2452092000-07-20,74.8125,79.0,74.75,78.125,35382000,36.2767052000-07-21,77.4375,77.6875,75.0625,75.4375,30925000,35.0287862000-07-24,77.125,78.625,74.9375,75.0,38060200,34.8256359999999962000-07-25,75.875,76.5,74.3125,76.0,27118200,35.2899780000000052000-07-26,74.8125,78.0,74.0625,76.75,31826400,35.6382349999999952000-07-27,75.9375,78.0,75.0,75.0625,32539000,34.8546582000-07-28,75.125,76.4375,71.0625,72.375,33203400,33.6067390000000052000-07-31,71.75,75.5,71.375,75.1875,34406400,34.9127012000-08-01,75.1875,75.875,72.875,73.125,30190800,33.9549962000-08-02,73.0,75.25,72.375,73.125,30315000,33.9549962000-08-03,72.8125,77.5,71.625,77.4375,44941200,35.957472000-08-04,78.3125,82.3125,78.125,81.5625,73211600,37.872882000-08-07,80.875,82.375,80.75,81.875,40751400,38.0179862000-08-08,81.0,83.25,80.8125,83.0,34931800,38.5403712000-08-09,83.5,84.281242,81.1875,81.375,30846000,37.7858162000-08-10,81.4375,82.375,80.0,80.0625,24436000,37.1763672000-08-11,79.9375,81.9375,79.25,81.125,22106800,37.669732000-08-14,80.1875,82.5625,79.875,82.375,21862200,38.2501572000-08-15,81.9375,83.5625,81.125,81.25,31208800,37.7277732000-08-16,81.375,82.25,81.0625,81.1875,22698200,37.6987512000-08-17,80.875,84.0625,80.8125,83.9375,30085000,38.9756912000-08-18,83.5,84.75,81.125,81.3125,31121200,37.7567942000-08-21,82.125,83.375,80.765617,83.1875,20295600,38.6274352000-08-22,82.875,84.1875,82.1875,83.5625,23251600,38.8015632000-08-23,82.9375,83.125,81.0,82.875,25104800,38.4823282000-08-24,82.875,85.0625,82.6875,84.6875,40413200,39.3239482000-08-25,84.6875,86.8125,84.5,84.625,26866600,39.2949260000000042000-08-28,85.0,87.9375,84.9375,86.75,25833600,40.2816530000000062000-08-29,86.8125,89.265617,86.5625,87.75,37678600,40.7459952000-08-30,87.6875,88.8125,87.125,88.25,27073400,40.9781659999999952000-08-31,88.4375,91.0,88.4375,90.9375,39840000,42.2260842000-09-01,92.203117,92.9375,90.875,92.625,30417800,43.0096612000-09-05,92.375,92.8125,90.5625,91.0625,25535800,42.2841270000000052000-09-06,91.375,91.75,89.0625,89.25,38473000,41.4425072000-09-07,90.125,91.75,89.5,91.1875,28559000,42.342172000-09-08,90.8125,90.875,86.5,86.5625,34569400,40.1945892000-09-11,86.0625,86.75,82.375,83.4375,41459600,38.7435212000-09-12,83.0,85.3125,79.0625,79.375,49606000,36.8571322000-09-13,77.75,82.75,76.9375,81.8125,68132400,37.9889652000-09-14,86.375,86.625,82.25,84.9375,64170600,39.4400332000-09-15,81.125,82.875,78.0,78.3125,122939600,36.3637692000-09-18,78.8125,80.0,74.75,76.468758,72997800,35.5076422000-09-19,77.4375,79.75,76.125,79.3125,53198600,36.8281112000-09-20,79.0,80.625,77.5,79.9375,37352200,37.1183242000-09-21,79.4375,80.0,75.5,78.9375,27160000,36.6539822000-09-22,75.9375,80.75,75.5,80.734383,58729400,37.488352000-09-25,81.125,82.5625,78.75,78.75,34748600,36.5669182000-09-26,79.3125,81.0,78.625,79.4375,39993400,36.8861532000-09-27,80.1875,80.9375,78.0,79.875,48205600,37.0893032000-09-28,79.5,82.1875,78.375,81.484383,42188600,37.8366062000-09-29,81.359383,81.6875,78.25,78.75,34843000,36.5669182000-10-02,79.75,82.125,77.718758,78.75,46136800,36.5669182000-10-03,79.8125,81.609383,68.75,69.5,96827800,32.2717559999999962000-10-04,66.0,70.75,60.5,68.125,204852000,31.6332862000-10-05,68.3125,69.625,66.5,68.3125,54316000,31.7203512000-10-06,69.25,71.625,66.875,67.625,53378400,31.4011162000-10-09,67.625,68.75,65.125,66.75,40304000,30.9948159999999972000-10-10,66.0625,67.25,64.5,64.625,50948400,30.0080900000000032000-10-11,63.0,65.984383,62.0,62.25,71687200,28.9052780000000032000-10-12,63.8125,64.875,61.75,63.0,50889400,29.2535352000-10-13,31.0,35.75,31.0,35.625,38514000,33.0843552000-10-16,36.5625,37.0,33.375,34.5625,36480500,32.0976282000-10-17,35.75,35.875,32.875,33.6875,29353900,31.285032000-10-18,31.5625,35.375,30.5,33.5625,52547900,31.1689452000-10-19,36.0,36.5,34.625,36.375,39321000,33.7808672000-10-20,36.125,36.5,34.9375,35.25,34073400,32.7360982000-10-23,35.1875,35.25,33.375,34.0625,28316300,31.6332862000-10-24,35.0,36.5625,34.5,35.8125,36713000,33.2584832000-10-25,36.5,36.625,34.0,34.375,33361600,31.92352000-10-26,34.75,35.875,30.9375,34.0625,49986600,31.6332862000-10-27,34.5,35.1875,33.0,34.1875,34962500,31.7493719999999972000-10-30,33.75,34.125,31.375,31.625,35420300,29.369622000-10-31,32.625,34.75,32.5,33.0,41881000,30.6465599999999972000-11-01,32.8125,33.1875,30.375,31.375,62692100,29.1374492000-11-02,32.4375,32.5,27.25,29.5625,149767500,27.4542099999999962000-11-03,31.5,31.75,29.5,30.3125,65017500,28.1507232000-11-06,30.6875,31.0,27.5,27.9375,75551500,25.9450992000-11-07,28.375,28.4375,26.5,26.5625,58951000,24.6681592000-11-08,27.375,27.5,24.5,24.8125,63040000,23.0429632000-11-09,24.6875,27.375,24.0625,27.1875,67280700,25.2485862000-11-10,26.4375,26.9375,24.875,25.4375,54609400,23.623392000-11-13,25.125,25.875,23.5,24.75,61652800,22.9849200000000022000-11-14,27.375,28.5,26.5,28.375,77494300,26.3513982000-11-15,28.8125,29.4375,27.703119,28.875,50649700,26.815742000-11-16,28.75,29.8125,27.25,27.375,37986600,25.4227152000-11-17,26.9375,29.25,25.25,28.8125,59636000,26.7576969999999972000-11-20,24.3125,25.875,24.0,24.75,89778400,22.9849200000000022000-11-21,24.8125,25.625,23.5,23.875,58647400,22.1723222000-11-22,23.625,24.0625,22.0625,22.3125,53315300,20.7212540000000022000-11-24,23.3125,24.25,23.125,24.125,22443900,22.4044932000-11-27,25.4375,25.8125,22.875,23.125,45665200,21.4758089999999982000-11-28,23.5,23.8125,22.25,22.65625,43075300,21.0404892000-11-29,23.1875,23.625,21.8125,22.875,75409600,21.2436382000-11-30,21.75,27.625,21.5,26.5,84386200,24.6101162000-12-01,26.375,27.875,25.5,26.4375,48663500,24.5520739999999982000-12-04,26.25,28.875,26.1875,28.1875,40710400,26.177272000-12-05,29.4375,31.5,28.875,31.5,59754700,29.2535352000-12-06,31.1875,31.625,29.3125,30.1875,42125600,28.0346372000-12-07,29.625,29.9375,28.125,28.3125,41088300,26.2933562000-12-08,30.0625,30.625,29.25,30.0625,40052600,27.9185520000000022000-12-11,30.5,32.25,30.0,31.9375,50279700,29.6598340000000042000-12-12,31.875,32.5,30.40625,30.75,26481200,28.5570219999999962000-12-13,31.9375,32.0,28.25,28.375,37933600,26.3513982000-12-14,29.25,29.9375,27.25,27.5,45894400,25.53882000-12-15,29.4375,30.078119,28.1875,28.5625,120004000,26.5255262000-12-18,30.0,32.4375,29.9375,32.0,61640100,29.7178762000-12-19,31.8125,33.125,30.125,30.625,58653700,28.4409359999999972000-12-20,28.0625,29.8125,27.5,28.5,54440500,26.4674840000000022000-12-21,27.8125,30.25,27.3125,29.5,46719700,27.3961672000-12-22,30.375,31.984381,30.0,31.875,35568200,29.6017912000-12-26,31.5,32.1875,30.0,30.9375,20589500,28.731152000-12-27,30.375,31.0625,29.375,30.6875,26437500,28.4989792000-12-28,30.5625,31.625,30.375,31.0625,25053600,28.8472362000-12-29,30.875,31.3125,28.6875,29.0625,31702200,26.989867999999998