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/

    Interactive broker- a wrong historical close price

    General Code/Help
    3
    4
    506
    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.
    • P
      PavelAn last edited by PavelAn

      Hello,
      I've imported a historical close prices for FB but they are wrong!
      I've got:

      Server Version: 76
      TWS Time at connection:20181006 22:40:53 MSK
      1,2018-09-18T00:00:00,159.51
      2,2018-09-19T00:00:00,159.90
      3,2018-09-20T00:00:00,163.37
      

      But they must be:
      1,2018-09-18T00:00:00,160.30
      2,2018-09-19T00:00:00,163.06
      3,2018-09-20T00:00:00,166.02

      https://finance.yahoo.com/quote/FB/history?period1=1537218000&period2=1537390800&interval=1d&filter=history&frequency=1d
      My code:

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      
      import datetime
      import backtrader as bt
      
      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 run(args=None):
          cerebro = bt.Cerebro(stdstats=False)
          
          store = bt.stores.IBStore(port=7497)
          dtformat = '%Y-%m-%d'
          data = store.getdata(dataname='FB', historical=True, fromdate = datetime.datetime.strptime('2018-09-17', dtformat), todate = datetime.datetime.strptime('2018-09-20', dtformat), timeframe=bt.TimeFrame.Days, compression=1 ) 
      	
          cerebro.adddata(data)
          cerebro.addstrategy(St)
      
          cerebro.run()
      
      	
      	
      if __name__ == '__main__':
          run()
      

      Where is my mistake? Please, help.

      Z B 2 Replies Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        I don't think you are making anything wrong.

        Since you use port 7497 (which is usually for the Paper Trading Acccount), it's where the different may be. Try the regular account (you can download data and forbid any trading via the API in the settings)

        1 Reply Last reply Reply Quote 0
        • Z
          z_kvn @PavelAn last edited by

          @pavelan Just a wild guess, please don't take my words without testing. Backtrader is using "rightedge=True" by default, meaning at timestamp "2018-09-18T00:00:00" you are seeing the information for the period "2018-09-17T00:00:01" to "2018-09-17T23:59:59" roughly. In other words, "2018-09-18T00:00:00" marks the end of "2018-09-17".

          While on most other places like Bloomberg or even InteractiveBroker, a timestamp "2018-09-18" marks the beginning of a bar, meaning that the OHLC for the bar associated with timestamp "2018-09-18T00:00:00" is for what has happened during the day time of "2018-09-18".

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators @PavelAn last edited by

            @pavelan said in Interactive broker- a wrong historical close price:

            data = store.getdata(dataname='FB', historical=True, fromdate = datetime.datetime.strptime('2018-09-17', dtformat), todate = datetime.datetime.strptime('2018-09-20', dtformat), timeframe=bt.TimeFrame.Days, compression=1 ) 
            
            cerebro.adddata(data)
            cerebro.addstrategy(St)
            

            There is no resampling, historical=True and timeframe=bt.TimeFrame.Days. The user should be getting the bars which are delivered (unless, it happens sometimes, the user has posted output which doesn't correspond to the sample code)

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