What is "orcl" in the following pyalgotrade code? Further, i have a csv file and how can i upload data from it into my code?

468 Views Asked by At

I wanted to implement my own strategy for backtesting but am unable to modify the code according to my needs

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()
2

There are 2 best solutions below

0
On

"orcl" is the name of the stock Oracle. If you want to use a different stock ticker, place it there.

You need to go to yahoo finance here: http://finance.yahoo.com/q/hp?s=ORCL&a=02&b=12&c=2000&d=05&e=26&f=2015&g=d then save the file as orcl-2000.csv

This program reads in the orcl-2000.csv file from the directory and prints the prices. If you want to download the data through python, then use a command like

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

This will make files that say orcl-2011-yahoofinance.csv and so on from 2011 through 2014.

0
On

Slightly modified from the documentation documentation:

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))