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/

    Example code for live trading using binance

    General Code/Help
    4
    11
    4117
    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.
    • Noah Bastola
      Noah Bastola last edited by

      Hi all,

      I was wondering if there was any kind of reference that I could look at to get started with live trading. I use the platform Binance, and I want to feed in downloaded csv data into the backtesting strategy I have made.

      In the code below, I download the most recent 5 minute bar data and feed it into the backtrader. My plan was to re-run the backtrader every 5 minutes with the next set of downloaded data and send orders requests using the Binance API.

      # Add a strategy
      cerebro.addstrategy(TestStrategy)
      df = pd.DataFrame(get_klines('DLTBTC',5))
      df[0]=pd.to_datetime(df[0],unit='ms')
      df.to_csv('data.csv',index=False)
      datapath = os.path.join(os.getcwd() + '/data.csv')#modpath, '../../datas/orcl-1995-2014.txt')
      
      # datapath = os.path.join(os.getcwd() + '/'+path)#'/historicalDLTBTC.csv'#modpath, '../../datas/orcl-1995-2014.txt')
      
      # Create a Data Feed
      data = bt.feeds.GenericCSVData(
          dataname=datapath,
          # Do not pass values before this date
          dtformat = ('%Y-%m-%d %H:%M:%S'),
          datetime = 0,
          high = 2,
          low = 3,
          open=1,
          close=4,
          volume=5,
          openinterest=-1,
          timeframe = bt.TimeFrame.Minutes,
          compression=5,
          )
      

      If this is the best way to go about live trading using binance, how can I retrieve variables from the init portion of the TestStrategy class so that I can keep track of the trades and position?

          # To keep track of pending orders and buy price/commission
          self.order = None
          self.buyprice = None
          self.buycomm = None
          self.loopIteration = 0
          self.buyType = None
          self.inTrade = False
      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        Docs - Cerebro

        The instance of the executed strategies are returned by cerebro.run

        1 Reply Last reply Reply Quote 0
        • Rodrigo Brito
          Rodrigo Brito last edited by

          Hi @Noah-Bastola,

          I'm using backtrader for bitcoin/usdt trades in production. The easy way is integrate with binance API through CCXT Store lib. A simple example is available below:

          https://gist.github.com/rodrigo-brito/3b0fca2487c92ad97869247edd5fd852

          Noah Bastola 2 Replies Last reply Reply Quote 1
          • Noah Bastola
            Noah Bastola @Rodrigo Brito last edited by

            @rodrigo-brito How do I import ccxtbt, as shown in your imports?

            1 Reply Last reply Reply Quote 0
            • Noah Bastola
              Noah Bastola @Rodrigo Brito last edited by

              @rodrigo-brito Actually I was able to load the library, but could you run me through how the code works? I ran the code as given, but it doesn't seem to fetch any data. This is the output I get:

              2019-03-18 17:28:40.131205 - fetch_ohlcv - Attempt 0
              Fetching: BTC/USDT, TF: 5m, Since: 1552944300000, Limit: 99999

              1 Reply Last reply Reply Quote 0
              • Rodrigo Brito
                Rodrigo Brito last edited by

                Hi @Noah-Bastola, to install, you should the pip:

                pip install git+git://github.com/Dave-Vallance/bt-ccxt-store@master#bt-ccxt-store
                
                

                Maybe, are you using an old version. Make shure that parameter fromdate are defined with a enough value for you indicators.

                1 Reply Last reply Reply Quote 0
                • P
                  pfederra last edited by

                  Hi @Noah-Bastola, I did follow the below and managed to setup the environment with the correct versions:

                  conda create --name DVX python=3.7
                  source activate DVX
                  conda install -c anaconda git
                  pip install ccxt
                  pip install git+https://github.com/Dave-Vallance/bt-ccxt-store.git

                  Give it a try!

                  1 Reply Last reply Reply Quote 0
                  • P
                    pfederra last edited by

                    Hi @Rodrigo Brito,
                    As you mentioned, you are using Binance for prod trading. I have a question in regards to the order status. I do place order correctly on Binance since I can see the order in the dashboard. However, in the notify_order() when it comes to printing this line:
                    self.log (order.executed.price,order.executed.value,order.executed.comm)

                    It always prints 0 for all 3 values, I am not sure why I cant see the order execution details. Does it work for you?

                    Thanks

                    1 Reply Last reply Reply Quote 0
                    • Rodrigo Brito
                      Rodrigo Brito last edited by

                      Hi @pfederra. I'm receiving zero values too. I think it is a issue related to bt-ccxt-store.

                      P 1 Reply Last reply Reply Quote 0
                      • P
                        pfederra @Rodrigo Brito last edited by

                        Thanks for the confirmation. Do you know if there is any active development happening on the CCXT integration to Back-trader? Is there anywhere that we can report the bugs?

                        P 1 Reply Last reply Reply Quote 0
                        • P
                          pfederra @pfederra last edited by

                          Hi @Rodrigo Brito, On another note, have you faced any other major/minor issues/bugs while running the Backtrader+CCXT live so we need to be aware of before go live? Any special error handling or Binance broker specific issues we might face? Thanks

                          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(); }); }