我试图在简单的烛台结构上编写各种谓词。例如,连续“绿色蜡烛”的一个组成部分'谓词需要回顾-4
简单地说,我尝试使用' higher_highs'进行测试。谓语。如果前一个蜡烛的关闭低于当前蜡烛关闭,则该函数返回true。以下是我的代码:
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 example中有一段回顾期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指标,那么我怎样才能访问以前的吟游诗人?
答案 0 :(得分:0)
问题是,在第一次调用eventOccurred时,bards只有一个项目,所以试图做bards [-2]会失败。首先检查吟游诗人的长度。