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/

    Bar data N days back

    General Code/Help
    2
    9
    1958
    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.
    • S
      sspy last edited by sspy

      I want to access the data N days ago. I have written the following code

          def next(self):
              print('next:: current period:', len(self))
              print('date %s, current price %.2f, previous price %.2f' % (self.datas[0].datetime.datetime(), self.data.close[0], self.data.close[-1]))
      

      the dataset I use consists of 3 lines

      2018-01-01,50.1,...
      2018-01-02,50.2,...
      2018-01-03,50.3,...
      

      and the output is

      next:: current period: 1
      date 2018-01-01 23:59:59.999989, current price 50.10, previous price 50.30
      next:: current period: 2
      date 2018-01-02 23:59:59.999989, current price 50.20, previous price 50.10
      next:: current period: 3
      date 2018-01-03 23:59:59.999989, current price 50.30, previous price 50.20
      

      How can day 1 previous price be 50.30? Why it takes the value of the last day and doesn't throws an error?

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

        Because the data is preloaded and the magic of array arithmetic in Python does the rest ...

        @sspy said in Bar data N days back:

        I want to access the data N days ago. I have written the following code

        You may want to do it better: Docs - Platform Concepts - See: Lines - Delayed Indexing

        S B 2 Replies Last reply Reply Quote 0
        • S
          sspy @backtrader last edited by

          @backtrader said in Bar data N days back:

          Because the data is preloaded

          Can this behavior be disabled? It really confuses me.

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

            @sspy said in Bar data N days back:

            Can this behavior be disabled? It really confuses me.

            You are simply doing things wrong. But you can disable preloading: Docs - Cerebro

            S 1 Reply Last reply Reply Quote 0
            • S
              sspy @backtrader last edited by sspy

              @backtrader said in Bar data N days back:

              You are simply doing things wrong. But you can disable preloading: Docs - Cerebro

              Thank you for your prompt reply. Finally I was able to get it right.

              The only moment by disabling preloading I was able to get

              next:: current period: 1
              date 2018-01-01 23:59:59.999989, current price 50.10, previous price 50.10
              

              it's certainly better, but still not how it should

              1 Reply Last reply Reply Quote 0
              • S
                sspy last edited by

                I'm completely confused. Is there a simple way to get data N days back?

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

                  It would seem you are unaware of the Python arithmetic with arrays.

                  As quoted above ..

                  @backtrader said in Bar data N days back:

                  You may want to do it better: Docs - Platform Concepts - See: Lines - Delayed Indexing

                  Using a delayed index provides the proper automatic constraint to the platform.

                  You really want to read the document linked above and also Docs - Operating the Platform

                  S 1 Reply Last reply Reply Quote 0
                  • S
                    sspy @backtrader last edited by

                    @backtrader said in Bar data N days back:

                    It would seem you are unaware of the Python arithmetic with arrays.

                    I understand the python arrays but this logic is unacceptable here, because we can't get future data, so the -1 has to be the previous day, -2 the day before it and so on and if no such day is available to throw an exception, but not start everything in a circle.

                    @backtrader said in Bar data N days back:

                    Using a delayed index provides the proper automatic constraint to the platform.

                    Can you show me an example of proper way to get data for 2 days back or exeption if no such data is available?

                    1 Reply Last reply Reply Quote 0
                    • S
                      sspy last edited by

                      I was able to find a solution,

                      self.data.close.get(size=1, ago=-1)[0]
                      

                      works right as I want

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