I trying to write a Stochastic Oscillator in python using the list function in Pyalgotrade library.
Pyalgotrade library is a Python library for backtesting stock trading strategies. Let’s say you have an idea for a trading strategy and you’d like to evaluate it with historical data and see how it behaves. PyAlgoTrade allows you to do so with minimal effort.
The python code is like this:
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
class MyStrategy(strategy.BacktestingStrategy):
def __init__(self, feed, instrument):
strategy.BacktestingStrategy.__init__(self, feed)
self.__stoch = stoch.StochasticOscillator(feed[instrument].getCloseDataSeries(),20, dSMAPeriod=3, maxLen=3)
self.__instrument = instrument
def onBars(self, bars):
bar = bars[self.__instrument]
self.info("%s %s" % (bar.getClose(), self.__stoch[-1]))
# Downdload then Load the yahoo feed from the CSV file
yahoofinance.download_daily_bars('AAPL', 2013, 'aapl.csv')
feed = yahoofeed.Feed()
feed.addBarsFromCSV("AAPL", "aapl.csv")
# Evaluate the strategy with the feed's bars.
myStrategy = MyStrategy(feed, "AAPL")
myStrategy.run()
The error is like this,including all the trace back.
Traceback (most recent call last):
File "/Users/johnhenry/Desktop/simple_strategy.py", line 47, in <module>
myStrategy.run()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/strategy/__init__.py", line 519, in run
self.__dispatcher.run()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/dispatcher.py", line 102, in run
eof, eventsDispatched = self.__dispatch()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/dispatcher.py", line 90, in __dispatch
if self.__dispatchSubject(subject, smallestDateTime):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/dispatcher.py", line 68, in __dispatchSubject
ret = subject.dispatch() is True
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/feed/__init__.py", line 101, in dispatch
dateTime, values = self.getNextValuesAndUpdateDS()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/feed/__init__.py", line 85, in getNextValuesAndUpdateDS
ds.appendWithDateTime(dateTime, value)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/dataseries/bards.py", line 49, in appendWithDateTime
self.__closeDS.appendWithDateTime(dateTime, value.getClose())
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/dataseries/__init__.py", line 134, in appendWithDateTime
self.getNewValueEvent().emit(self, dateTime, value)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/observer.py", line 59, in emit
handler(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/technical/__init__.py", line 89, in __onNewValue
newValue = self.__eventWindow.getValue()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/technical/stoch.py", line 60, in getValue
lowestLow, highestHigh = get_low_high_values(self.__barWrapper, self.getValues())
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/technical/stoch.py", line 42, in get_low_high_values
lowestLow = barWrapper.getLow(currBar)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/technical/stoch.py", line 31, in getLow
return bar_.getLow(self.__useAdjusted)
AttributeError: 'float' object has no attribute 'getLow'
You're not building the StochasticOscillator properly. You passing the close dataseries, and as explained in the doc, you need to pass in the bar dataseries:
self.__stoch = stoch.StochasticOscillator(feed[instrument], 20, dSMAPeriod=3, maxLen=3)