pyalgotrade VWAP calcuation using yahoo historical data seems to be incorrect

846 views Asked by At

I have just started with pyalgotrade, using a modified sample code I got online that is using VWAP ( volume adjusted average ) calculation, as well as software's own method of acquiring yahoo historical data, I have noted the output VWAP calculation seems to be erroneous since yahoo adjusts its volume while the software tool assumes that volume is unadjusted.

Here is the code. Note the graph produced after execution and note the discontinuation in VWAP calculation. I compared Nasdaq data with yahoos that it has loaded and noted that Yahoo's volume is adjusted although it is not stated as adjusted. I did set the flag to use adjusted data but the software does not use it for VWAP calculation as I believe it assumes that volume is unadjusted.

I would appreciate if someone would identify a method by which I could correct the problem. A method that I could use to override its own VWAP calculation would be fine. Also I don't know how to report the error to the providers either since I am new to this.

 from pyalgotrade import strategy
 from pyalgotrade.barfeed import yahoofeed
 from pyalgotrade import plotter
 from pyalgotrade.tools import yahoofinance
 from pyalgotrade.technical import vwap  #Volume Weighted Average Price

 import os

 class MyStrategy(strategy.BacktestingStrategy):
def __init__(self, feed, instrument, vwapWindowSize):



    strategy.BacktestingStrategy.__init__(self, feed)
    self.__instrument = instrument
    self.setUseAdjustedValues(True)     #   use adjusted close

    self.__vwap = vwap.VWAP(feed[instrument], vwapWindowSize )           # ,useTypicalPrice=True)

def getVWAPDS(self):

  return self.__vwap

def onBars(self, bars):

    vwap = self.__vwap[-1]
    if vwap == None:
        return

    shares = self.getBroker().getShares(self.__instrument)

                   #priceAdj = bars[self.__instrument].getAdjClose()
    price = bars[self.__instrument].getClose()
    notional = shares * price
    if price < vwap * 0.995 and notional > 0:
        self.marketOrder(self.__instrument, -100)
    elif price > vwap * 1.005 and notional < 1000000:
        self.marketOrder(self.__instrument, 100) 


def main(plot):
instrument = "aapl"
vwapWindowSize = 5

# Download the bars.
feed = yahoofinance.build_feed([instrument], 2011, 2016, "./data",)

myStrategy = MyStrategy(feed, instrument, vwapWindowSize)

if plot:
   plt = plotter.StrategyPlotter(myStrategy, True, False, True)
plt.getInstrumentSubplot(instrument).addDataSeries("vwap", myStrategy.getVWAPDS())

myStrategy.run()
print "Result: %.2f" % myStrategy.getResult()

if plot:
    plt.plot()

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

There are 1 answers

0
Gabriel On

It will be fixed in the next version. Thanks for reporting this.