0

i need average fill price for shares when using pyalgotrade on each bar... i am unable to figure out a way to get the same using callout for getAvgFillPrice function. this would help me to decide to enter next trade or not. any help will be appreciated. i have tried the following code:

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)

But i get the following error:

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'

Any help will be appreciated in this regard...

Apoorv Kalra
  • 54
  • 1
  • 4

1 Answers1

0

You are calling Order.getAvgFillPrice directly on the Order class, and not on an instance of the class.

Somewhere in your code you need to have actually created an order. For example, assuming your code is executing within the Str1 class you gave in your example, you will needs something that looks like this:

    my_order = self.getBroker().createMarketOrder(Order.Action.BUY, \
                                                  self.__instrument, \
                                                  self.__qty)
    avg_price = my_order.getAvgFillPrice()
Ian Ash
  • 1,087
  • 11
  • 23