0

I'm trying to use the stochastic indicator in my trading strategy but I'm getting the following error:

2016-07-06 16:14:24,414 yahoofinance [INFO] Downloading AAPL 2015 to .\AAPL-2015-yahoofinance.csv
2016-07-06 16:14:24,585 yahoofinance [INFO] Downloading AAPL 2016 to .\AAPL-2016-yahoofinance.csv
Traceback (most recent call last):
  File "bobo.py", line 103, in <module>
    main()
  File "bobo.py", line 99, in main
    run_strategy(inst,10,250,14,5,5,5)
  File "bobo.py", line 90, in run_strategy
    myStrategy = MyStrategy(feed, inst, smaPeriod,emaPeriod,rsiPeriod,fastk_period,slowk_period,slowd_period)
  File "bobo.py", line 28, in __init__
    self.__stoch = indicator.STOCH(feed[instrument],fastk_period,slowk_period,slowd_period)
  File "C:\Users\JDOG\Anaconda2\lib\site-packages\pyalgotrade\talibext\indicator.py", line 803, in STOCH
    ret = call_talib_with_hlc(barDs, count, talib.STOCH, fastk_period, slowk_period, slowk_matype, slowd_period, slowd_matype)
  File "C:\Users\JDOG\Anaconda2\lib\site-packages\pyalgotrade\talibext\indicator.py", line 105, in call_talib_with_hlc
    return talibFunc(high, low, close, *args, **kwargs)
  File "talib/func.pyx", line 9388, in talib.func.STOCH (talib\func.c:87125)
Exception: inputs are all NaN

Here is my code: I think the error comes from the STOCH line.

        from pyalgotrade.tools import yahoofinance
        from pyalgotrade import strategy
        from pyalgotrade.barfeed import yahoofeed
        from pyalgotrade.technical import stoch
        from pyalgotrade import dataseries
        from pyalgotrade.technical import ma
        from pyalgotrade import technical
        from pyalgotrade.technical import highlow
        from pyalgotrade import bar
        from pyalgotrade.talibext import indicator
        from pyalgotrade.technical import rsi
        import numpy
        import talib

        class MyStrategy(strategy.BacktestingStrategy):
            def __init__(self, feed,instrument,smaPeriod,emaPeriod,rsiPeriod,fastk_period,slowk_period,slowd_period):
                strategy.BacktestingStrategy.__init__(self, feed, 1000)
                self.__position = None
                self.__instrument = instrument
                self.setUseAdjustedValues(True)
                self.__prices = feed[instrument].getPriceDataSeries()
                self.__sma = ma.SMA(self.__prices, smaPeriod)
                self.__ema = ma.EMA(self.__prices, emaPeriod)
                self.__rsi = rsi.RSI(self.__prices, rsiPeriod)
                self.__stoch = indicator.STOCH(feed[instrument],fastk_period,slowk_period,slowd_period)

            def get_RSF(): 

                return 0


            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):
                bar = bars[self.__instrument]
                self.info("%s %s %s %s" % (bar.getClose(), self.__rsi[-1], self.__sma[-1], self.__stoch[-1],self.__stoch[-1]))



def run_strategy(inst,smaPeriod,emaPeriod,rsiPeriod,fastk_period,slowk_period,slowd_period):

    feed = yahoofinance.build_feed([inst],2015,2016, ".")

    myStrategy = MyStrategy(feed, inst, smaPeriod,emaPeriod,rsiPeriod,fastk_period,slowk_period,slowd_period)
    myStrategy.run()

    print "Final portfolio value: $%.2f" % myStrategy.getBroker().getEquity()


def main():
    instruments = ['AAPL']
    for inst in instruments:
            run_strategy(inst,10,250,14,5,5,5)


if __name__ == '__main__':
        main()

I'm trying to utilize the slow stochastic in my strategy. Any help is greatly appreciated!!

user3666197
  • 1
  • 6
  • 50
  • 92
RageAgainstheMachine
  • 901
  • 2
  • 11
  • 28

0 Answers0