0

I have a strategy that consists of using multiple technical indicators to generate a buy signal.

How would I go about to implement this with Pyalgotrade for multiple lists of tickers in such a way that the strategy is not analyzed on a per ticker basis but on the results from all the trades made from all the lists?

Please find below code illustrating the various parts of the program (sample):

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


class MyStrategy(strategy.BacktestingStrategy):
    def __init__(self, feed, instrument, smaPeriod):
        super(MyStrategy, self).__init__(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].getPriceDataSeries(), smaPeriod)

    def onEnterOk(self, position):
        execInfo = position.getEntryOrder().getExecutionInfo()
        self.info("BUY at $%.2f" % (execInfo.getPrice()))

    def onEnterCanceled(self, position):
        self.__position = None

    def onExitOk(self, position):
        execInfo = position.getExitOrder().getExecutionInfo()
        self.info("SELL at $%.2f" % (execInfo.getPrice()))
        self.__position = None

    def onExitCanceled(self, position):
        # If the exit was canceled, re-submit it.
        self.__position.exitMarket()

    def onBars(self, bars):
        # Wait for enough bars to be available to calculate a SMA.
        if self.__sma[-1] is None:
            return

        bar = bars[self.__instrument]
        # If a position was not opened, check if we should enter a long position.
        if self.__position is None:
            if bar.getPrice() > self.__sma[-1]:
                # Enter a buy market order for 10 shares. The order is good till canceled.
                self.__position = self.enterLong(self.__instrument, 10, True)
        # Check if we have to exit the position.
        elif bar.getPrice() < self.__sma[-1] and not self.__position.exitActive():
            self.__position.exitMarket()


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

    # Evaluate the strategy with the feed.
    myStrategy = MyStrategy(feed, "orcl", smaPeriod)
    myStrategy.run()
    print "Final portfolio value: $%.2f" % myStrategy.getBroker().getEquity()

run_strategy(15)
RageAgainstheMachine
  • 901
  • 2
  • 11
  • 28

2 Answers2

1

You can add multiple tickers like this:

class LiveFeed(barfeed.BaseBarFeed):
    def __init__(self, identifiers, frequency, maxLen=dataseries.DEFAULT_MAX_LEN):
        barfeed.BaseBarFeed.__init__(self, frequency, maxLen)
        if not isinstance(identifiers, list):
            raise Exception("identifiers must be a list")
        ...
        for instrument in identifiers:
            self.registerInstrument(instrument)

        self.__instruments = identifiers    

They can be initialized and then accessed inside Strategy like this

class Strategy(strategy.BaseStrategy):
    def __init__(self, instruments,feed, brk):
        strategy.BaseStrategy.__init__(self, feed, brk)
        self.__position = None
        self.__instrument = instruments # list of instruments

    def onBars(self, bars): # default onBars() - called every 1 minute
        self.debug("strategy got bars")
        for instrument in bars.getInstruments():
            bar = bars[instrument]
            self.info("%s: Date: %s Open: %s High: %s Low: %s Close: %s Volume: %s" % (instrument, bar.getDateTime(), bar.getOpen(), bar.getHigh(), bar.getLow(), bar.getClose(), bar.getVolume())
0

Keep current positions in an array. Eg:

self.__position = []

Load up multiple instruments and during onbars loop through them all.

There are quite a few examples around and the Google group is a good resource.

https://groups.google.com/forum/#!topic/pyalgotrade/s45Dy39vimk

kbcool
  • 695
  • 6
  • 18