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/

    IBTEST.py some explanations

    General Code/Help
    3
    9
    121
    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.
    • J T
      J T last edited by

      Call me dumb but if someone can help explain to me these code I would be really grateful. Afterall if i ask I am a fool for a day, if not will be a fool forever!

      1st section (pls see comments in the code where I am refering to):
      The 1st section of this, I believe is just the data coming in from IB. And we are just joining it all up.
      2nd section:
      This is where I am lost. Why is there a data1? What is it actually? Is this section of code actually needed. What does it do?

      def next(self, frompre=False):
            # 1st section:
              txt = list()
              txt.append('Data0')
              txt.append('%04d' % len(self.data0))
              dtfmt = '%Y-%m-%dT%H:%M:%S.%f'
              txt.append('{}'.format(self.data.datetime[0]))
              txt.append('%s' % self.data.datetime.datetime(0).strftime(dtfmt))
              txt.append('{}'.format(self.data.open[0]))
              txt.append('{}'.format(self.data.high[0]))
              txt.append('{}'.format(self.data.low[0]))
              txt.append('{}'.format(self.data.close[0]))
              txt.append('{}'.format(self.data.volume[0]))
              txt.append('{}'.format(self.data.openinterest[0]))
              txt.append('{}'.format(self.sma[0]))
              print(', '.join(txt))
      
             # 2nd section: 
              if len(self.datas) > 1 and len(self.data1):
                  txt = list()
                  txt.append('Data1')
                  txt.append('%04d' % len(self.data1))
                  dtfmt = '%Y-%m-%dT%H:%M:%S.%f'
                  txt.append('{}'.format(self.data1.datetime[0]))
                  txt.append('%s' % self.data1.datetime.datetime(0).strftime(dtfmt))
                  txt.append('{}'.format(self.data1.open[0]))
                  txt.append('{}'.format(self.data1.high[0]))
                  txt.append('{}'.format(self.data1.low[0]))
                  txt.append('{}'.format(self.data1.close[0]))
                  txt.append('{}'.format(self.data1.volume[0]))
                  txt.append('{}'.format(self.data1.openinterest[0]))
                  txt.append('{}'.format(float('NaN')))
                  print(', '.join(txt))
      

      Thanks alot!

      vladisld 1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        2nd section prints 2nd data feed if more than one data feed were loaded into system and data feed values are delivered. Check out Docs - Platform Concepts, section Shortcuts for Data Feeds.

        vladisld 1 Reply Last reply Reply Quote 2
        • vladisld
          vladisld @J T last edited by

          @J-T said in IBTEST.py some explanations:

          This is where I am lost. Why is there a data1? What is it actually? Is this section of code actually needed. What does it do?

          data1 data feed is added to cerebro if --data1 command line is specified for ibtest.py

          I guess it may be used if one would like to track one instrument and trade another ( see https://www.backtrader.com/blog/posts/2015-09-03-multidata-strategy/multidata-strategy/)

          usage: ibtest.py [-h] ... --data0 DATA0 [--data1 DATA1] ...
          Test Interactive Brokers integration
          
          optional arguments:
          ...
            --data0 DATA0         data 0 into the system (default: None)
            --data1 DATA1         data 1 into the system (default: None)
          ...
          
          
          1 Reply Last reply Reply Quote 1
          • vladisld
            vladisld @ab_trader last edited by

            @ab_trader, sorry I didn't notice you've already answered.

            A 1 Reply Last reply Reply Quote 0
            • A
              ab_trader @vladisld last edited by

              @vladisld np

              1 Reply Last reply Reply Quote 0
              • J T
                J T last edited by

                Thanks guys.

                I am trying out paper trade in IB,

                broker = ibstore.getbroker()
                cerebro.setbroker(broker)
                
                print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())  # Print out the starting conditions
                    strat = cerebro.run(maxcpus=2, exactbars=True)
                

                Can I ask why is the value 0? or am I doing something wrong here.

                Starting Portfolio Value: 0.00
                
                vladisld 1 Reply Last reply Reply Quote 0
                • vladisld
                  vladisld @J T last edited by

                  @J-T said in IBTEST.py some explanations:

                  Starting Portfolio Value: 0.00

                  It is because the connection to IB hasn't been established yet. The connection is started only upon cerebro.run.

                  1 Reply Last reply Reply Quote 1
                  • J T
                    J T last edited by

                    Thanks again.

                    I am connected to ib paper trading. The main code for connection as follows. I just simplify IBTEST.py.

                    def Main():
                        cerebro = bt.Cerebro()
                    
                        storekwargs = dict(host='127.0.0.1', port=7497, clientId=1, notifyall=False, _debug=False, reconnect=-1, timeout=3, timeoffset=False, timerefresh=60)
                    ibstore = bt.stores.IBStore(**storekwargs)
                    
                        cerebro.broker = ibstore.getbroker()
                    
                        data = ibstore.getdata(dataname='EUR.USD-CASH-IDEALPRO', 
                    timeframe=bt.TimeFrame.Minutes, qcheck=60, tz='Asia/Singapore', backfill=False, latethrough=False, historical=False)
                    
                        cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=1)
                        cerebro.addstrategy(BaseStrategy)
                        strat = cerebro.run(maxcpus=2, exactbars=-1)
                    
                    if __name__ == '__main__':
                        main()
                    
                    class BaseStrategy(bt.Strategy):
                        def logdata(self):
                            txt = list()
                            txt.append('{:4d}'.format(len(self.data)))
                            dtfmt = '%Y-%m-%d %H:%M:%S'
                            # txt.append('{}'.format(self.data.datetime[0]))
                            txt.append('{}'.format(self.data.datetime.datetime(0).strftime(dtfmt)))
                            txt.append('{:.4f}'.format(self.data.open[0]))
                            txt.append('{:.4f}'.format(self.data.high[0]))
                            txt.append('{:.4f}'.format(self.data.low[0]))
                            txt.append('{:.4f}'.format(self.data.close[0]))
                            txt.append('{:.4f}'.format(self.data.volume[0]))
                            print(','.join(txt))
                    
                        data_live = False
                    

                    I am getting volume as -1 or 0. -1 during the backfill, 0 when live.
                    Snapshot here:

                    ...
                     282,2020-04-13 11:41:00,1.0932,1.0933,1.0932,1.0933,-1.0000
                     283,2020-04-13 1:42:00,1.0933,1.0934,1.0933,1.0934,-1.0000
                     284,2020-04-13 11:43:00,1.0934,1.0934,1.0933,1.0933,-1.0000
                     285,2020-04-13 11:44:00,1.0933,1.0933,1.0933,1.0933,-1.0000
                     286,2020-04-13 11:45:00,1.0933,1.0933,1.0932,1.0933,-1.0000
                     287,2020-04-13 11:46:00,1.0933,1.0933,1.0933,1.0933,-1.0000
                     288,2020-04-13 11:47:00,1.0933,1.0933,1.0933,1.0933,-1.0000
                    ***** DATA NOTIFY: LIVE
                     289,2020-04-13 11:49:00,1.0933,1.0933,1.0933,1.0933,0.0000
                     290,2020-04-13 11:50:00,1.0933,1.0933,1.0933,1.0933,0.0000
                     291,2020-04-13 11:51:00,1.0933,1.0935,1.0933,1.0935,0.0000
                     292,2020-04-13 11:52:00,1.0934,1.0934,1.0934,1.0934,0.0000
                     293,2020-04-13 11:53:00,1.0935,1.0935,1.0934,1.0934,0.0000
                     294,2020-04-13 11:54:00,1.0934,1.0934,1.0934,1.0934,0.0000
                     295,2020-04-13 11:55:00,1.0934,1.0935,1.0934,1.0934,0.0000
                     296,2020-04-13 11:56:00,1.0934,1.0935,1.0934,1.0935,0.0000
                     297,2020-04-13 11:57:00,1.0935,1.0935,1.0934,1.0935,0.0000
                     298,2020-04-13 11:58:00,1.0935,1.0935,1.0934,1.0934,0.0000
                    

                    Any insights why is the volume like this?

                    J T 1 Reply Last reply Reply Quote 0
                    • J T
                      J T @J T last edited by

                      Ignore me.... new to fx. Think fx has no volume.

                      On the other hand, is it possible to help let me know if these 2 lines of code is possible. In all examples I have seen so far, the call has been to ticks. I do not think I need ticks but 1 minute bars will do. I wonder if the below makes sense. The data I get back is almost the same for open high low and close.

                      data = ibstore.getdata(dataname='EUR.USD-CASH-IDEALPRO', timeframe=bt.TimeFrame.Minutes, qcheck=60, tz='Asia/Singapore',
                                                 backfill=False, _latethrough=True, historical=False)
                      cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=1, takelate=True)
                      

                      Or should I really be doing ticks and then resample:

                      data = ibstore.getdata(dataname='EUR.USD-CASH-IDEALPRO', timeframe=bt.TimeFrame.Ticks, qcheck=60, tz='Asia/Singapore',
                      backfill=False, _latethrough=True, historical=False)
                      cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=1, takelate=True)

                      Stay safe guys and appreciate your help as always.

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