pyalgotrade利用雅虎的历史数据计算VWAP似乎是

2024-06-16 16:41:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始使用pyalgotrade,使用我在网上得到的修改过的示例代码,它使用VWAP(volume-adjusted average)计算,以及软件自己获取雅虎历史数据的方法,我已经注意到输出VWAP的计算似乎是错误的,因为yahoo调整了它的音量,而软件工具假设音量没有调整。在

这是密码。注意执行后产生的graph,并注意到VWAP计算中的中断。我比较了纳斯达克的数据和yahoos的数据,发现雅虎的交易量是经过调整的,尽管它并没有声明是调整过的。我确实设置了标记以使用调整后的数据,但软件没有使用它来计算VWAP,因为我相信它假定音量未经调整。在

如果有人能帮我找出一种方法来纠正这个问题,我将不胜感激。我可以用一个方法来覆盖它自己的VWAP计算。我也不知道如何向供应商报告错误,因为我是新手。在

 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)

Tags: fromimportselftrueifdeffeedprice