0

我想实施自己的回测策略,但无法根据需要修改代码

from pyalgotrade.tools import yahoofinance
yahoofinance.download_daily_bars('orcl', 2000, 'orcl-2000.csv')

from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed
from pyalgotrade.technical import ma

#class to create objects
class MyStrategy(strategy.BacktestingStrategy):
    def __init__(self, feed, instrument):
        strategy.BacktestingStrategy.__init__(self, feed)
        # We want a 15 period SMA over the closing prices.
        self.__sma = ma.SMA(feed[instrument].getCloseDataSeries(), 15)
        self.__instrument = instrument

    def onBars(self, bars):
        bar = bars[self.__instrument]
        self.info("%s %s" % (bar.getClose(), self.__sma[-1]))

# Load the yahoo feed from the CSV file
feed = yahoofeed.Feed()
feed.addBarsFromCSV("orcl", "orcl-2000.csv")

# Evaluate the strategy with the feed's bars.
myStrategy = MyStrategy(feed, "orcl")
myStrategy.run()
4

2 回答 2

1

从文档 文档中稍作修改:

from pyalgotrade.tools import yahoofinance; 

for instrument in ["AA","ACN"]:
    for year in [2015, 2016]:
        yahoofinance.download_daily_bars(instrument, year, r'D:\tmp\Trading\%s-%s.csv' % (instrument,year)) 
于 2016-05-25T10:26:12.353 回答
0

“orcl”是股票甲骨文的名称。如果您想使用不同的股票代码,请将其放在那里。

您需要在此处访问 yahoo Finance:http: //finance.yahoo.com/q/hp ?s=ORCL&a=02&b=12&c=2000&d=05&e=26&f=2015&g=d 然后将文件另存为 orcl-2000.csv

该程序从目录中读取 orcl-2000.csv 文件并打印价格。如果你想通过 python 下载数据,那么使用类似的命令

instrument = "orcl"
feed = yahoofinance.build_feed([instrument], 2011, 2014, ".")

这将使从 2011 年到 2014 年的文件显示为 orcl-2011-yahoofinance.csv 等等。

于 2015-06-26T14:37:58.860 回答