【问题标题】:Pyalgotrade - TA-LIB - Indicator Returns "NONE"Pyalgotrade - TA-LIB - 指标返回“无”
【发布时间】:2016-11-16 23:24:12
【问题描述】:

我正在与 Pyalgotrade 合作,在 python 中测试交易策略。 Pyalgotrade 允许使用名为 TA-LIB 的库,这是一个技术分析库。出于某种原因,当我使用 PPO 指标时,它返回“无”。该指标接受几个参数:(http://gbeced.github.io/pyalgotrade/docs/v0.12/html/talib.html)

我提供了一个输出的 sn-p,目前它只是当天的收盘价,并且应该是该指标的输出。 'DDD' 是我一直在测试的代码。

我一直试图让它工作的时间比我想承认的要长。 我该如何解决这个问题?

输出:

2016-11-08 00:00:00 strategy [INFO] 13.56,None
2016-11-09 00:00:00 strategy [INFO] 13.77,None
2016-11-10 00:00:00 strategy [INFO] 14.06,None
2016-11-11 00:00:00 strategy [INFO] 14.71,None
2016-11-14 00:00:00 strategy [INFO] 14.3,None
2016-11-15 00:00:00 strategy [INFO] 13.91,None

这是我的代码:

from pyalgotrade import strategy
from pyalgotrade.tools import yahoofinance
from pyalgotrade import talibext
from pyalgotrade.talibext import indicator
import talib
import numpy

class MyStrategy(strategy.BacktestingStrategy):
    def __init__(self, feed, instrument):
        super(MyStrategy, self).__init__(feed, 1000)
        self.__position = None
        self.__instrument = instrument
        self.setUseAdjustedValues(True) 
        self.__prices = feed[instrument].getPriceDataSeries()

        self.__PPO = talibext.indicator.PPO(feed,0,12,26,9)

    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" % (bar.getClose(),self.__PPO))

    def run_strategy(inst):
    # Load the yahoo feed from the CSV file

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

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


def main():
    instruments = ['ddd']
    for inst in instruments:
            run_strategy(inst)


if __name__ == '__main__':
        main()

【问题讨论】:

    标签: python stocks quantitative-finance ta-lib pyalgotrade


    【解决方案1】:

    你传递的参数是错误的。这是 PPO 函数签名:

    def PPO(ds, count, fastperiod=-2**31, slowperiod=-2**31, matype=0):
    

    ds 类型为BarDataSeriescount 指定要计算尾部数据的长度。如果计算成功,它将返回一个numpy array

    并且talibext指标只计算一次,当新柱输入时它不会计算新结果。

    因此,您需要在每个onBars 调用中计算 PPO。

    def __init__(self, feed, instrument):
        ...
    
        self.__feed = feed
    
    def onBars(self, bars):
        feed = self.__feed
        self.__PPO = talibext.indicator.PPO(feed[self.__instrument], len(feed[self.__instrument]),12,26, matype=0)
        bar = bars[self.__instrument]
        self.info("%s,%s" % (bar.getClose(),self.__PPO[-1]))
    

    【讨论】:

    • 我用 self.__prices 替换了 feed[self.__instrument],现在它吐出了数据。但是,我不知道数据是什么。我正在尝试获取 PPO 直方图的 3 天斜率。 stockcharts.com/articles/chartwatchers/2011/04/…
    • 查看更新。您想将feed 保存在__init__ 中并在onBars 中使用它。
    • 谢谢。关于 PPO 直方图 3 天斜率的任何想法??
    • PPO 基于 EMA。由于 talib 中的 PPO 与你想要的有点不同,我认为你可以自己实现一个。
    • 另外,为什么每个日期我都会得到一个完整的数据列表?我怎样才能得到每个日期的相应数据?当我使用 ma=1 时,我也意识到出来的数据是 PPO 的黑线。我正在尝试在 onbars 中使用它,但仍然没有运气(我需要角度或坡度): self.__Slope = talibext.indicator.LINEARREG_ANGLE(self.__PPO,3)
    猜你喜欢
    • 2016-05-14
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2017-03-09
    • 2021-07-29
    相关资源
    最近更新 更多