0

在每个柱上使用 pyalgotrade 时,我需要股票的平均成交价……我无法找到一种方法来使用 getAvgFillPrice 函数的标注来获得相同的成交价。这将帮助我决定是否进入下一笔交易。任何帮助将不胜感激。我尝试了以下代码:

from pyalgotrade.broker import backtesting
from pyalgotrade import broker
from pyalgotrade import plotter
from pyalgotrade.broker import Order

class Str1(strategy.BacktestingStrategy):
    def __init__(self, feed, instrument, qty):
        strategy.BacktestingStrategy.__init__(self,feed,1000000)
        self.__qty = qty
        self.__instrument = instrument
        self.__priceDS = feed[instrument].getPriceDataSeries()
        self.__lowDS = feed[instrument].getLowDataSeries()
        self.__highDS = feed[instrument].getLowDataSeries()


    def onBars(self, bars):

        shares = self.getBroker().getShares(self.__instrument)
        avg_price = Order.getAvgFillPrice(self.__instrument)
        price = bars[self.__instrument].getPrice()
        bar = bars[self.__instrument]
        print(avg_price)

但我收到以下错误:

Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.3\helpers\pydev\pydevd.py", line 1664, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.3\helpers\pydev\pydevd.py", line 1658, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.3\helpers\pydev\pydevd.py", line 1068, in run
pydev_imports.execfile(file, globals, locals)  # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "E:/pyalgotrader/new.py", line 109, in <module>
myStrategy.run()
File "E:\pyalgotrader\venv\lib\site-packages\pyalgotrade\strategy\__init__.py", line 512, in run
self.__dispatcher.run()
File "E:\pyalgotrader\venv\lib\site-packages\pyalgotrade\dispatcher.py", line 109, in run
eof, eventsDispatched = self.__dispatch()
File "E:\pyalgotrader\venv\lib\site-packages\pyalgotrade\dispatcher.py", line 97, in __dispatch
if self.__dispatchSubject(subject, smallestDateTime):
File "E:\pyalgotrader\venv\lib\site-packages\pyalgotrade\dispatcher.py", line 75, in __dispatchSubject
ret = subject.dispatch() is True
File "E:\pyalgotrader\venv\lib\site-packages\pyalgotrade\feed\__init__.py", line 108, in dispatch
self.__event.emit(dateTime, values)
File "E:\pyalgotrader\venv\lib\site-packages\pyalgotrade\observer.py", line 61, in emit
handler(*args, **kwargs)
File "E:\pyalgotrader\venv\lib\site-packages\pyalgotrade\strategy\__init__.py", line 505, in __onBars
self.onBars(bars)
File "E:/pyalgotrader/new.py", line 38, in onBars
avg_price = Order.getAvgFillPrice(self.__instrument)
File "E:\pyalgotrader\venv\lib\site-packages\pyalgotrade\broker\__init__.py", line 275, in getAvgFillPrice
return self.__avgFillPrice
AttributeError: 'str' object has no attribute '_Order__avgFillPrice'

在这方面的任何帮助将不胜感激......

4

1 回答 1

0

Order.getAvgFillPrice直接调用 Order 类,而不是该类的实例。

您需要在代码中的某处实际创建订单。例如,假设您的代码在示例中提供的 Str1 类中执行,您将需要如下所示的内容:

    my_order = self.getBroker().createMarketOrder(Order.Action.BUY, \
                                                  self.__instrument, \
                                                  self.__qty)
    avg_price = my_order.getAvgFillPrice()
于 2018-09-01T18:21:00.957 回答