首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pyalgotrade - TA-LIB -指示器返回“无”

Pyalgotrade - TA-LIB -指示器返回“无”
EN

Stack Overflow用户
提问于 2016-11-16 23:24:12
回答 1查看 868关注 0票数 0

我正在与Pyalgotrade一起测试python的交易策略。Pyalgotrade允许使用一个名为called的库,这是一个技术分析库。由于某些原因,当我使用PPO指示器时,它会返回"None“。该指示器包含一些参数:(http://gbeced.github.io/pyalgotrade/docs/v0.12/html/talib.html)

我提供了一个输出片段,目前这只是当天的收盘价,以及这个指标的产出。“‘DDD”是我一直在测试的代码。

我一直在努力让这件事起作用的时间比我想承认的要长。我怎么才能解决这个问题?

产出:

代码语言:javascript
复制
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

这是我的密码:

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-17 05:08:27

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

代码语言:javascript
复制
def PPO(ds, count, fastperiod=-2**31, slowperiod=-2**31, matype=0):

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

而talibext指示器只计算一次,在输入新条时不会计算新结果。

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

代码语言:javascript
复制
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]))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40644122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档