0

I'm working with an algo on the bitstamp client that works better with 30-min bars, rather than seeing each trade as a bar.

Is there a "right" way to resample those bars into 30-min intervals on the fly?

I can do it no problem with the bitcoincharts broker, but I need the execution from the bitstampbroker, so I was hoping to do it with one.

Josh Peck
  • 1
  • 1

1 Answers1

0

This should help:

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()
Gabriel
  • 492
  • 3
  • 4