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/

    How to skip some data bars when starting back testing using BackTrader

    General Code/Help
    3
    4
    70
    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.
    • Daily Tasks
      Daily Tasks last edited by

      Gettings,

      I am trying to calculate gap up or gap down by comparing highs and lows of two previous bars.

      if d.low[-1] > d.high[0]:
                          print(d.params.name, d.datetime.date(-1), "yesterday's low : ", d.low[-1],  d.datetime.date(0), "today's high : ", d.high[0], "gap down")
                          self.gap[d.params.name] = -1
                      elif d.high[-1] < d.low[0]:
                          print(d.params.name, d.datetime.date(-1), "yesterday's high : ", d.high[-1], d.datetime.date(0), "today's low : ", d.low[0], "gap up")
                          self.gap[d.params.name] = 1
                      else:
                          self.gap[d.params.name] = 0
      

      However bar -2 is a bar from the future, that is today's bar.
      Log 1

      ADANIPORTS 2022-01-31 720.8499755859375 #bar -2
      ADANIPORTS 2020-04-07 248.10000610351562 # bar -1
      ADANIPORTS 2020-04-08 250.85000610351562 # bar 0
      gap down :  ADANIPORTS 2020-04-08 day before's low :  714.5499877929688  should be higher than  yesterday's high :  255.85000610351562
      

      log 2

      ASIANPAINT 2022-01-31 yesterday's low :  3140.0 2020-04-07 today's high :  1635.0 gap down
      

      I would like to know how what is the best way to start backtesting so that -2 bars refer to historical bar instead of a bar in the future.

      Kindly help.

      E 1 Reply Last reply Reply Quote 0
      • E
        EMR @Daily Tasks last edited by

        @daily-tasks -2 refer to historical bar, not a bar in the future.
        You may want to read the basic concepts of Backtrader :

        The platform consider the last set item (before the current live get/set point) to be -1.
        
        As such comparing the current close to the previous close is a 0 vs -1 thing. In a strategy, for example:
        

        https://www.backtrader.com/docu/concepts/

        I may be wrong, but I have, according to the previous questions you had, the feeling that you have not had a look at the documentation. My apologies if it is not the case.
        If it is the case, I strongly encourage you to read the documentation. It will take a bit of time, but it will be quicker that discovering how to use the software by going from problem to problem, and far less expensive than basing an investment decision on backtesting results based on a wrong usage of the software.

        Daily Tasks 1 Reply Last reply Reply Quote 1
        • Daily Tasks
          Daily Tasks @EMR last edited by

          @emr thank you for your prompt response.

          I have read the documentation.
          Unfortunately I was indeed getting a bar from the future date as shown in log 1 and log 2. The first line is close price of ADANIPORTS from 2022-01-31 referenced by [-2] then the next line prints the close price of ADANIPORTS from 2020-04-07 referenced by index [-1]

          Since then, I have been able to fix this issue by adding the following to the beginning of the next() and next_open()

          if len(self) < 3:
              return
          

          This way when at least three bars have been processed, I am able to access previous bars using index [-2] and [-1].


          If you have the time could you also look at the other issue i have raised regarding stop order closing at the close of the candle instead of the specified stop price in the order.
          This is causing my back test to lose more money than expected.

          Thank you for taking the time to help me.

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

            @daily-tasks Check your data sorting and eroneous entries. Backtrader expects sorted data.

            RunBacktest.com

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors