gpt4 book ai didi

python - 断言错误-Pyalgotrade

转载 作者:行者123 更新时间:2023-12-03 08:26:11 25 4
gpt4 key购买 nike

我正在尝试使用技术指标(随机),但此行却出现断言错误:

self.__slow_stoch = stoch.StochasticOscillator(feed[instrument],self.__stoch, 3, self.__slow_d)

我正在使用pyalgotrade在python中运行交易策略。关于如何解决这个问题的任何想法?我尝试为该错误编写一个Try/Exception,但是没有运气...任何想法都将不胜感激!
from pyalgotrade import strategy
from pyalgotrade.tools import yahoofinance
import numpy as np
import pandas as pd
#Technical Analysis Libraries
from pyalgotrade import talibext
from pyalgotrade.talibext import indicator
from pyalgotrade.technical import ma
from pyalgotrade.technical import roc
from pyalgotrade.technical import rsi
from talib import MA_Type
from pyalgotrade.technical import stoch
import talib

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.__stoch = stoch.StochasticOscillator(feed[instrument],5, dSMAPeriod=3, maxLen=1)
self.__slow_d = ma.SMA(self.__stoch,3)
self.__slow_stoch = stoch.StochasticOscillator(feed[instrument],self.__stoch, 3, self.__slow_d)


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): #Verify data here

bar = bars[self.__instrument]
self.info("%s,%s" % (bar.getClose(),self.__slow_stoch[-1])


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

feed = yahoofinance.build_feed([inst],2015,2016, ".") # 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()

码:
    self.__stoch = stoch.StochasticOscillator(feed[instrument],5, dSMAPeriod=3, maxLen=1)
slow_k = self.__stoch.getD()
slow_d = ma.SMA(self.__stoch.getD(), 3)
self.__slow_stoch = stoch.StochasticOscillator(feed[instrument],slow_k, dSMAPeriod=slow_d, maxLen=1)

错误信息:
Traceback (most recent call last):
File "algov1.py", line 224, in <module>
main()
File "algov1.py", line 220, in main
run_strategy(inst)
File "algov1.py", line 212, in run_strategy
myStrategy = MyStrategy(feed, inst)
File "algov1.py", line 91, in __init__
self.__slow_stoch = stoch.StochasticOscillator(feed[instrument],slow_k, dSMAPeriod=slow_d, maxLen=1)
File "C:\Users\JDOG\Anaconda2\lib\site-packages\pyalgotrade\technical\stoch.py", line 90, in __init__
technical.EventBasedFilter.__init__(self, barDataSeries, SOEventWindow(period, useAdjustedValues), maxLen)
File "C:\Users\JDOG\Anaconda2\lib\site-packages\pyalgotrade\technical\stoch.py", line 55, in __init__
technical.EventWindow.__init__(self, period, dtype=object)
File "C:\Users\JDOG\Anaconda2\lib\site-packages\pyalgotrade\technical\__init__.py", line 41, in __init__
assert(isinstance(windowSize, int))
AssertionError

最佳答案

这是StochasticOscillator的类定义。

class StochasticOscillator(technical.EventBasedFilter):

def __init__(self, barDataSeries, period, dSMAPeriod=3, useAdjustedValues=False, maxLen=dataseries.DEFAULT_MAX_LEN):
assert dSMAPeriod > 1, "dSMAPeriod must be > 1"
assert isinstance(barDataSeries, bards.BarDataSeries), \
"barDataSeries must be a dataseries.bards.BarDataSeries instance"

technical.EventBasedFilter.__init__(self, barDataSeries, SOEventWindow(period, useAdjustedValues), maxLen)
self.__d = ma.SMA(self, dSMAPeriod, maxLen)

period和dSMAPeriod均为固定整数。

当调用StochasticOscillator()时,Python将调用 StochasticOscillator.__init__,自动创建并传递 self,然后以正确的顺序和类型传递左侧的参数。

更新:

参见代码,dSMAPeriod用于计算D%,即K%的SMA。当dSMAPeriod为1时,D%等于K%。由于您确实要将dSMAPeriod设置为1,因此可以传入dSMAPeriod = 2,然后使用stoch本身。

关于python - 断言错误-Pyalgotrade,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40777453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com