trading - Backtesting multiple stocks using Python -


i using pyalgotrade, popular python library testing trading strategy. want write loop can test couples of stocks 1 after another.

suppose want put following 6 stocks strategy, , tun each stock 1 time , several results. how can write loop?

stocks = [aapl, ebay, nflx, bby, goog, wbai]

from pyalgotrade import strategy pyalgotrade.barfeed import yahoofeed pyalgotrade.technical import ma pyalgotrade.tools import yahoofinance   class mystrategy(strategy.backtestingstrategy):     def __init__(self, feed, instrument, smaperiod):         strategy.backtestingstrategy.__init__(self, feed, 1000)         self.__position = none         self.__instrument = instrument         # we'll use adjusted close values instead of regular close values.         self.setuseadjustedvalues(true)         self.__sma = ma.sma(feed[instrument].getadjclosedataseries(), smaperiod)      def onenterok(self, position):         execinfo = position.getentryorder().getexecutioninfo()         self.info("buy @ $%.2f" % (execinfo.getprice()))      def onentercanceled(self, position):         self.__position = none      def onexitok(self, position):         execinfo = position.getexitorder().getexecutioninfo()         self.info("sell @ $%.2f" % (execinfo.getprice()))         self.__position = none      def onexitcanceled(self, position):         # if exit canceled, re-submit it.         self.__position.exitmarket()      def onbars(self, bars):         # wait enough bars available calculate sma.         if self.__sma[-1] none:             return          bar = bars[self.__instrument]         # if position not opened, check if should enter long position.         if self.__position none:             if bar.getadjclose() > self.__sma[-1]:                 # enter buy market order 10 shares. order till canceled.                 self.__position = self.enterlong(self.__instrument, 10, true)         # check if have exit position.         elif bar.getadjclose() < self.__sma[-1]:             self.__position.exitmarket()    def run_strategy(smaperiod):     instruments = ["aapl"]          # download bars.     feed = yahoofinance.build_feed(instruments, 2011, 2013, ".")          # evaluate strategy feed's bars.     mystrategy = mystrategy(feed, "aapl", smaperiod)     mystrategy.run()     print "final portfolio value: $%.2f" % mystrategy.getbroker().getequity()  run_strategy(10) 

def run_strategy(smaperiod,inst):          # download bars.     feed = yahoofinance.build_feed([inst], 2011, 2013, ".")          # evaluate strategy feed's bars.     mystrategy = mystrategy(feed, inst, smaperiod)     mystrategy.run()     print "final portfolio value: $%.2f" % mystrategy.getbroker().getequity() def main():     instruments = ["aapl","ebay", "nflx", "bby"]     inst in instruments:             run_strategy(10,inst) if __name__ == '__main__':         main() 

Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -