2

我正在尝试在简单的烛台结构上编写各种谓词。例如,“连续 3 根绿色蜡烛”谓词的一个组成部分需要回溯 -4

首先,我尝试使用“higher_highs”谓词对其进行测试。如果前一根蜡烛的收盘价低于当前蜡烛的收盘价,则函数返回真。下面是我的代码:

from pyalgotrade import eventprofiler
from pyalgotrade.barfeed import csvfeed

class single_event_strat( eventprofiler.Predicate ):
    def __init__(self,feed):
        pass

    def higher_highs(self, instrument, bards):
        #prev_three = bards[-4]
        #prev_two = bards[-3]
        prev = bards[-2]
        curr = bards[-1]
        if prev.getOpen() < curr.getOpen():
            return True
        return False

    def eventOccurred(self, instrument, bards):
        if self.higher_highs(instrument, bards):
            return True
        else: 
            return False


def main(plot):
    feed = csvfeed.GenericBarFeed(0)
    feed.addBarsFromCSV('FCT', "FCT_daily_converted.csv")
    predicate = single_event_strat(feed)
    eventProfiler = eventprofiler.Profiler( predicate, 20, 20)
    eventProfiler.run(feed, True)

    results = eventProfiler.getResults()
    print "%d events found" % (results.getEventCount())
    if plot:
        eventprofiler.plot(results)

if __name__ == "__main__":
    main(True)

但是我得到一个IndexError

Traceback (most recent call last):
  File "C:\Users\David\Desktop\Python\Coursera\Computational Finance\Week2\PyAlgoTrade\Bitfinex\FCT\FCT_single_event_test.py", line 44, in <module>
    main(True)
  File "C:\Users\David\Desktop\Python\Coursera\Computational Finance\Week2\PyAlgoTrade\Bitfinex\FCT\FCT_single_event_test.py", line 36, in main
    eventProfiler.run(feed, True)
  File "C:\Python27\lib\site-packages\pyalgotrade\eventprofiler.py", line 215, in run
    disp.run()
  File "C:\Python27\lib\site-packages\pyalgotrade\dispatcher.py", line 102, in run
    eof, eventsDispatched = self.__dispatch()
  File "C:\Python27\lib\site-packages\pyalgotrade\dispatcher.py", line 90, in __dispatch
    if self.__dispatchSubject(subject, smallestDateTime):
  File "C:\Python27\lib\site-packages\pyalgotrade\dispatcher.py", line 68, in __dispatchSubject
    ret = subject.dispatch() is True
  File "C:\Python27\lib\site-packages\pyalgotrade\feed\__init__.py", line 105, in dispatch
    self.__event.emit(dateTime, values)
  File "C:\Python27\lib\site-packages\pyalgotrade\observer.py", line 59, in emit
    handler(*args, **kwargs)
  File "C:\Python27\lib\site-packages\pyalgotrade\eventprofiler.py", line 172, in __onBars
    eventOccurred = self.__predicate.eventOccurred(instrument, self.__feed[instrument])
  File "C:\Users\David\Desktop\Python\Coursera\Computational Finance\Week2\PyAlgoTrade\Bitfinex\FCT\FCT_single_event_test.py", line 20, in eventOccurred
    if self.higher_highs(instrument, bards):
  File "C:\Users\David\Desktop\Python\Coursera\Computational Finance\Week2\PyAlgoTrade\Bitfinex\FCT\FCT_single_event_test.py", line 11, in higher_highs
    prev = bards[-2]
  File "C:\Python27\lib\site-packages\pyalgotrade\dataseries\__init__.py", line 90, in __getitem__
    return self.__values[key]
  File "C:\Python27\lib\site-packages\pyalgotrade\utils\collections.py", line 141, in __getitem__
    return self.__values[key]
IndexError: list index out of range

我仍在试图弄清楚 EP 是如何工作的。这很有趣,因为在buyongap示例中有一个回顾期bards[-2]

def __gappedDown(self, instrument, bards):
    ret = False
    if self.__stdDev[instrument][-1] is not None:
        prevBar = bards[-2]
        currBar = bards[-1]
        low2OpenRet = (currBar.getOpen(True) - prevBar.getLow(True)) / float(prevBar.getLow(True))
        if low2OpenRet < (self.__returns[instrument][-1] - self.__stdDev[instrument][-1]):
            ret = True
    return ret

但是它位于if self.__stdDev[instrument][-1] is not None:语句中,我的谓词不需要 TA 指示符,那么我如何访问以前的吟游诗人?

4

1 回答 1

1

问题是第一次调用 eventOccurred bards 只有一个项目,所以尝试做 bards[-2] 会失败。首先检查吟游诗人的长度。

于 2016-06-01T02:18:30.240 回答