Home

Awesome

bt-ccxt-store

A fork of Ed Bartosh's CCXT Store Work with some additions added for a projects I have been working on.

Some additions have been made to this repo that I find useful. Your mileage may vary.

Check out the example script to see how to setup and run on Kraken.

Additions / Changes

CCXTBroker

  broker_mapping = {
      'order_types': {
          bt.Order.Market: 'market',
          bt.Order.Limit: 'limit',
          bt.Order.Stop: 'stop-loss', #stop-loss for kraken, stop for bitmex
          bt.Order.StopLimit: 'stop limit'
      },
      'mappings':{
          'closed_order':{
              'key': 'status',
              'value':'closed'
              },
          'canceled_order':{
              'key': 'result',
              'value':1}
              }
      }
      # NOTE - THIS CLOSES A LONG POSITION. TO CLOSE A SHORT, REMOVE THE '-' FROM
      # -self.position.size
      type = 'Post'
      endpoint = '/positions'
      params = {}
      positions = self.broker.private_end_point(type=type, endpoint=endpoint, params=params)
      for position in positions:
          id = position['id']
          type = 'Post'
          endpoint = '/position/close'
          params = {'position_id': id}
          result = self.broker.private_end_point(type=type, endpoint=endpoint, params=params)
          _pos = self.broker.getposition(data, clone=False)
          # A Price of NONE is returned form the close position endpoint!
          _pos.update(-self.position.size, None)

CCXTStore

Redesigned the way that the store is intialized, data and brokers are requested. The store now uses metaparams and has methods for getbroker() and getdata(). A store is initialized in a similar way to other backtrader stores. e.g

# Create a cerebro
  cerebro = bt.Cerebro()

  config = {'urls': {'api': 'https://testnet.bitmex.com'},
                   'apiKey': apikey,
                   'secret': secret,
                   'enableRateLimit': enableRateLimit,
                  }

  # Create data feeds
  store = CCXTStore(exchange='bitmex', currency=currency, config=config, retries=5, debug=False)

  broker = store.getbroker()
  cerebro.setbroker(broker)

  hist_start_date = datetime.utcnow() - timedelta(minutes=fromMinutes)
  data = store.getdata(dataname=symbol, name="LTF",
                           timeframe=get_timeframe(timeframe), fromdate=hist_start_date,
                           compression=1, ohlcv_limit=50, fetch_ohlcv_params = {'partial': False}) #, historical=True)

CCXTFeed