Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    if balance == 0 or not balance['free'][currency]

    General Code/Help
    2
    2
    18
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      Kevin last edited by

      from future import (absolute_import, division, print_function,
      unicode_literals)
      import backtrader as bt
      from ccxtbt import CCXTStore
      from datetime import datetime, timedelta

      class St(bt.Strategy):
      def logdata(self):
      txt = []
      txt.append('{}'.format(len(self)))

          txt.append('{}'.format(
              self.data.datetime.datetime(0).isoformat())
          )
          txt.append('{:.2f}'.format(self.data.open[0]))
          txt.append('{:.2f}'.format(self.data.high[0]))
          txt.append('{:.2f}'.format(self.data.low[0]))
          txt.append('{:.2f}'.format(self.data.close[0]))
          txt.append('{:.2f}'.format(self.data.volume[0]))
          print(','.join(txt))
      
      def next(self):
          self.logdata()
      

      def main(args=None):
      cerebro = bt.Cerebro(quicknotify=True)
      config = {'apiKey': '****************',
      'secret': '********************',
      'enableRateLimit': True,
      }

      store = CCXTStore(exchange='bitmex', currency='XBT', config=config, retries=5, debug=False, sandbox=True)
      
      broker_mapping = {
          'order_types': {
              bt.Order.Market: 'market',
              bt.Order.Limit: 'limit',
              bt.Order.Stop: 'stop',  # 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}
          }
      }
      
      broker = store.getbroker(broker_mapping=broker_mapping)
      cerebro.setbroker(broker)
      
      hist_start_date = datetime.utcnow() - timedelta(minutes=50)
      data = store.getdata(dataname='XBT/BTC', name="XBTBTC",
                           timeframe=bt.TimeFrame.Minutes, fromdate=hist_start_date,
                           compression=1, ohlcv_limit=50, drop_newest=True)
      
      
      cerebro.resampledata(data, timeframe=bt.TimeFrame.Seconds,
                           compression=10)
      cerebro.addstrategy(St)
      cerebro.run()
      cerebro.plot(style="candlesticks")
      

      if name == 'main':
      main()

      Got this: if balance == 0 or not balance['free'][currency]
      KeyError: 'XBT'

      Considering that I am using sandbox account, whats wrong with code?

      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @Kevin last edited by

        @kevin said in if balance == 0 or not balance['free'][currency]:

        KeyError: 'XBT'

        I'm guessing it doesn't like this symbol.

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
        $(document).ready(function () { app.coldLoad(); }); }