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/

    Is next() datas[0].Close in the future?

    General Code/Help
    4
    7
    126
    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.
    • Kjiessar
      Kjiessar last edited by

      Hi I'm new and this is my first post.

      When next is executed I'm not sure where I'm in the current time.
      So the question:

      Is next() dataclose[0] in the future?

      What I already tried:

      def next(self):
              self.log(self.datetime.datetime(ago=0))
              ...
      

      2021-01-30, 2021-01-30 00:00:00
      2021-01-30, Close, 34269.52

      Dataframe Entry:

      Date
      2021-01-30    34269.523438
      Name: Close, dtype: float64
      

      So it looks lik I'm in 2021-01-30 00:00:00 (morning) and can already access open and close. Close will only be calculatet when market has closed. Does that mean my strategies should not access dataclose[0] because it would be cheating and unrealistic?

      D 1 Reply Last reply Reply Quote 0
      • D
        davidavr @Kjiessar last edited by

        @kjiessar You are always receiving data in "bars". A bar is a summary of all the trades over a certain time interval. Each interval will have its own Open, High, Low and Close. You receive the bar innext() just after the interval is complete so the Close is never in the future, it always just happened. The Open will be further in the past by the interval of time of that bar.

        Typically if you are using daily data, then next() is being called at the end of the trading day. If you place an order, the first opportunity for it to get filled is at the next days's Open price.

        1 Reply Last reply Reply Quote 3
        • Kjiessar
          Kjiessar last edited by

          Thank you that helped a lot.
          So in general it looks like this:
          326da240-fc7c-4d48-a535-0976f682b276-image.png

          There is no way to get in between an open and a close slice. If you want to do that you would need to use a finer granularity, like minutes instead of days. But then there woul again a be open and close.

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

            @kjiessar said in Is next() datas[0].Close in the future?:

            There is no way to get in between an open and a close slice. If you want to do that you would need to use a finer granularity, like minutes instead of days. But then there woul again a be open and close.

            Actually, while I understand what you are saying, this is not entirely correct.

            There is no way to get between bars because the ending time of the first bar is exactly the starting time of the next bar, market hours open and close aside for the moment.

            The concept of the close bar is a construct we make, and allows us to quantify a period of time, and then another. Once we get to the end of the bar, we can conceptually stop, reassess, make decisions and move to the next bar. In reality, there is no time stop of course.

            In backtrader, the bars are concrete blocks of time, and all the activity occurs between them. There are other methods other than time of course, like ticks, volume, or dollar values, but these are not part of how Backtrader works.

            RunBacktest.com

            1 Reply Last reply Reply Quote 2
            • Kjiessar
              Kjiessar last edited by

              Thank you for your explanation
              I was reading and found a reference to this post about BarReplayer_Open

              https://community.backtrader.com/topic/217/order_target_percent-calculation-problems

              Corresponding Blogpost
              https://www.backtrader.com/docu/filters-reference/

              So is it possible with the BarReplayer_Open to create orders in between open and close?

              run-out A 2 Replies Last reply Reply Quote 0
              • run-out
                run-out @Kjiessar last edited by

                @kjiessar You should have a look at resample and replay. Using multiple time frames is a way to allow for trades within the 'bigger' bar.

                RunBacktest.com

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

                  @kjiessar said in Is next() datas[0].Close in the future?:

                  So is it possible with the BarReplayer_Open to create orders in between open and close?

                  Orders are created at the moment the open price is available and executed during the bar. You can also take a look on cheat-on-open feature, which allows order creation at open price. This gives less hassle compare to the bar replayer mentioned.

                  • If my answer helped, hit reputation up arrow at lower right corner of the post.
                  • Python Debugging With Pdb
                  • New to python and bt - check this out
                  1 Reply Last reply Reply Quote 1
                  • 1 / 1
                  • First post
                    Last post
                  Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors