2016-11-24 102 views
0

我試圖用技術指標(隨機指標),但我得到斷言錯誤這條線:斷言錯誤 - Pyalgotrade

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

我使用pyalgotrade運行在python交易策略。任何想法如何解決這個問題?我嘗試寫這個錯誤的嘗試/例外,但沒有運氣......任何想法非常感謝!

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 
+0

你能發佈完整的斷言錯誤追溯? – gzc

+0

我用新的代碼和錯誤更新了發佈。 – RageAgainstheMachine

+0

第二個參數和第三個參數必須是整數類型。 – gzc

回答

1

這是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本身。

+0

對不起,我是一個菜鳥。我該如何將其納入我的代碼? 類隨機指標(technical.EventBasedFilter): 高清__init __(自我,barDataSeries,期間,dSMAPeriod = 3,useAdjustedValues =假MAXLEN = dataseries.DEFAULT_MAX_LEN): – RageAgainstheMachine

+0

那類的定義,你必須通過以下的'__init__'正確的參數定義。 – gzc

+0

如何將我的代碼合併到一個允許我計算出緩慢的stoch的方法中,以便我可以在self.info中的onbars函數中查看它? 自.__ STOCH = stoch.StochasticOscillator(進料[儀器],5,dSMAPeriod = 3,MAXLEN = 1) slow_k =自.__ stoch.getD() slow_d = ma.SMA(個體.__ stoch.getD( ),3) self .__ slow_stoch = stoch.StochasticOscillator(feed [instrument],slow_k,dSMAPeriod = slow_d,maxLen = 1) – RageAgainstheMachine