我尝试使用 Pyalgotrade 库中的列表函数在 python 中编写随机振荡器。
我的代码如下:
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
import numpy
import talib
class MyStrategy(strategy.BacktestingStrategy):
def __init__(self, feed, instrument):
strategy.BacktestingStrategy.__init__(self, feed)
self.__instrument = instrument
def onBars(self, bars):
barDs = self.getFeed().getDataSeries("002389.SZ")
self.__stoch = indicator.STOCH(barDs, 20, 3, 3)
bar = bars[self.__instrument]
self.info("%0.2f, %0.2f" % (bar.getClose(), self.__stoch[-1]))
# Downdload then Load the yahoo feed from the CSV file
yahoofinance.download_daily_bars('002389.SZ', 2013, '002389.csv')
feed = yahoofeed.Feed()
feed.addBarsFromCSV("002389.SZ", "002389.csv")
# Evaluate the strategy with the feed's bars.
myStrategy = MyStrategy(feed, "002389.SZ")
myStrategy.run()
我得到了这样的错误:
File "/Users/johnhenry/Desktop/simple_strategy.py", line 46, in onBars
self.info("%0.2f, %0.2f" % (bar.getClose(), self.__stoch[-1]))
TypeError: float argument required, not numpy.ndarray
随机:
pyalgotrade.talibext.indicator.STOCH(barDs,计数,fastk_period=-2147483648,slowk_period=-2147483648,slowk_matype=0,slowd_period=-2147483648,slowd_matype=0)