0

我正在尝试通过包含分钟数据的 CSV 文件使用ninjaTrader提要。该程序按预期运行七个 onBars 调用,然后吐出一个错误:

self.info(str(instr) + "当前开盘价: %.2f" % (bars.getBar(instr).getOpen())) AttributeError: 'NoneType' 对象没有属性 'getOpen'

我的代码:

class myExample(strategy.BacktestingStrategy):
    def __init__(self, feed, instruments):
        self.__position = None
        strategy.BacktestingStrategy.__init__(self, feed, 10000)
        self.__instruments = instruments


    def onBars(self, bars):
        for instr in self.__instruments:
            self.info(str(instr) + " current Open price: %.2f" % (bars.getBar(instr).getOpen()))

feed = ninjatraderfeed.Feed(Frequency.MINUTE)
feed.addBarsFromCSV("AMZN",  "AMZN.csv")
feed.addBarsFromCSV("MMM",  "MMM.csv")
strat = myExample(feed, ["AMZN","MMM"])
strat.run()

亚马逊.csv

20161227 093100 761.8262    763.77  761.58  763.4   11629
20161227 093200 762.12  762.48  761.2   761.85  15101
20161227 093300 762.215 762.769 761.77  761.77  13181
20161227 093400 762.3516    762.62  762.066 762.2211    6483
20161227 093500 762.73  762.88  762.2   762.392 8093
20161227 093600 763.055 763.36  762.7318    762.7318    11446
20161227 093700 762.45  763.4595    762.45  763.17  8561
20161227 093800 762.8   763 762.45  762.75  10116

MMM.csv

20161227 093100 178.95  178.95  178.775 178.775 200
20161227 093200 178.58  178.75  178.57  178.73  15968
20161227 093300 178.9   178.935 178.765 178.765 604
20161227 093400 179.12  179.14  178.91  178.91  2517
20161227 093500 178.8167    179.15  178.8167    179.12  4236
20161227 093600 178.74  178.92  178.74  178.918 2900
20161227 093800 178.78  178.78  178.78  178.78  100
20161227 093900 178.95  178.99  178.9   178.9   1300

输出:

2016-12-27 09:31:00+00:00 strategy [INFO] AMZN current Open price: 761.83
2016-12-27 09:31:00+00:00 strategy [INFO] MMM current Open price: 178.95
2016-12-27 09:32:00+00:00 strategy [INFO] AMZN current Open price: 762.12
2016-12-27 09:32:00+00:00 strategy [INFO] MMM current Open price: 178.58
2016-12-27 09:33:00+00:00 strategy [INFO] AMZN current Open price: 762.22
2016-12-27 09:33:00+00:00 strategy [INFO] MMM current Open price: 178.90
2016-12-27 09:34:00+00:00 strategy [INFO] AMZN current Open price: 762.35
2016-12-27 09:34:00+00:00 strategy [INFO] MMM current Open price: 179.12
2016-12-27 09:35:00+00:00 strategy [INFO] AMZN current Open price: 762.73
2016-12-27 09:35:00+00:00 strategy [INFO] MMM current Open price: 178.82
2016-12-27 09:36:00+00:00 strategy [INFO] AMZN current Open price: 763.05
2016-12-27 09:36:00+00:00 strategy [INFO] MMM current Open price: 178.74
2016-12-27 09:37:00+00:00 strategy [INFO] AMZN current Open price: 762.45
Traceback (most recent call last):
  File "example.py", line 43, in <module>
    strat.run()
  File "/home/work/pyCharmZipEnv/local/lib/python2.7/site-packages/pyalgotrade/strategy/__init__.py", line 512, in run
    self.__dispatcher.run()
  File "/home/work/pyCharmZipEnv/local/lib/python2.7/site-packages/pyalgotrade/dispatcher.py", line 109, in run
    eof, eventsDispatched = self.__dispatch()
  File "/home/work/pyCharmZipEnv/local/lib/python2.7/site-packages/pyalgotrade/dispatcher.py", line 97, in __dispatch
    if self.__dispatchSubject(subject, smallestDateTime):
  File "/home/work/pyCharmZipEnv/local/lib/python2.7/site-packages/pyalgotrade/dispatcher.py", line 75, in __dispatchSubject
    ret = subject.dispatch() is True
  File "/home/work/pyCharmZipEnv/local/lib/python2.7/site-packages/pyalgotrade/feed/__init__.py", line 108, in dispatch
    self.__event.emit(dateTime, values)
  File "/home/work/pyCharmZipEnv/local/lib/python2.7/site-packages/pyalgotrade/observer.py", line 61, in emit
    handler(*args, **kwargs)
  File "/home/work/pyCharmZipEnv/local/lib/python2.7/site-packages/pyalgotrade/strategy/__init__.py", line 505, in __onBars
    self.onBars(bars)
  File "example.py", line 30, in onBars
    self.info(str(instr) + " current Open price: %.2f" % (bars.getBar(instr).getOpen()))
AttributeError: 'NoneType' object has no attribute 'getOpen

我在 ninjatraderfeed.py 中唯一改变的是 getDelimiter() 使用“,”而不是“;”

为什么它会在七次 onBars 调用后停止工作?

编辑:从每个 csv 文件复制前 8 个值。

4

1 回答 1

0

在“20161227 093700”,只有 AMZN 分钟数据。bars.getBar('MMM')返回,当您尝试从访问时None抛出。AttributeErrorgetOpenNone

您可以在使用特定栏之前检查是否存在。

if bars.getBar(instr):
    self.info(str(instr) + " current Open price: %.2f" % (bars.getBar(instr).getOpen()))
于 2017-01-15T03:07:23.820 回答