问:重新映射pyalgotrade中的bitstamp条

时间:2017-03-09 08:03:39

标签: algorithmic-trading pyalgotrade

我在bitstamp客户端上使用算法,可以更好地使用30分钟的条形码,而不是将每笔交易视为条形码。

是否有"权利"如何在飞行中将这些条形图重新采样为30分钟?

我可以毫无问题地使用bitcoincharts代理,但我需要从bitstampbroker执行,所以我希望用一个来执行。

1 个答案:

答案 0 :(得分:0)

这应该有所帮助:

from pyalgotrade.bitstamp import barfeed
from pyalgotrade.bitstamp import broker
from pyalgotrade import strategy


class Strategy(strategy.BaseStrategy):
    def __init__(self, feed, brk):
        super(Strategy, self).__init__(feed, brk)
        self._instrument = "BTC"
        self._bid = None
        self._ask = None
        self._resampledBF = self.resampleBarFeed(60, self.onResampledBars)

        # Subscribe to order book update events to get bid/ask prices to trade.
        feed.getOrderBookUpdateEvent().subscribe(self._onOrderBookUpdate)

    def _onOrderBookUpdate(self, orderBookUpdate):
        bid = orderBookUpdate.getBidPrices()[0]
        ask = orderBookUpdate.getAskPrices()[0]

        if bid != self._bid or ask != self._ask:
            self._bid = bid
            self._ask = ask
            self.info("Order book updated. Best bid: %s. Best ask: %s" % (self._bid, self._ask))

    def onResampledBars(self, dt, bars):
        bar = bars[self._instrument]
        self.info("Resampled - Price: %s. Volume: %s." % (bar.getClose(), bar.getVolume()))

    def onBars(self, bars):
        bar = bars[self._instrument]
        self.info("Price: %s. Volume: %s." % (bar.getClose(), bar.getVolume()))


def main():
    barFeed = barfeed.LiveTradeFeed()
    brk = broker.PaperTradingBroker(1000, barFeed)
    strat = Strategy(barFeed, brk)

    strat.run()


if __name__ == "__main__":
    main()