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/

    Plotting dates range

    General Code/Help
    4
    11
    2673
    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.
    • Maxim Korobov
      Maxim Korobov last edited by

      I'm trying to use linear regression algorithm in backtesting. 90% of time strategy uses data for training, then 10% uses to testing model.
      How to tell cerebro to plot only last 10% of overall data? In other words, how to tell cerebro to plot data from exact date only and skip data before it?

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

        You couldn't up until today. This was a particularly little nifty idea which should have probably been there for a long time.

        Already commited to the development branch: https://github.com/mementum/backtrader/commit/83fe24237dc406e13b81de9da431766b8c6c28e2

        Use start and end as named arguments to cerebro.plot. The defaults are 0 and -1 (standard python for first and last) but you can also pass datetime.date/datetime instances to restrict the plot

        1 Reply Last reply Reply Quote 1
        • Maxim Korobov
          Maxim Korobov last edited by

          10x m8 and gg!

          Will try it in 1.9.31!

          1 Reply Last reply Reply Quote 0
          • Maxim Korobov
            Maxim Korobov last edited by

            Could you suggest please how to convert date time into index using DataFeed?

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

              For starters you can pass a datetime instance as the key to decide where to start/stop plotting. But ...

              No timestamp is guaranteed to be in the data feed (even if in your case you know it is there, but the in most general case, it cannot be guaranteed).

              dtnum = datafeed.date2num(dt_timestamp)

              And you can then use the bisect built-in module to locate the insertion index of dtnum in datafeed.datetime

              If the timestamp is there, the index will match. If not, it will give you the best possible approximation (left or right, that's a choice)

              RandyT 1 Reply Last reply Reply Quote 1
              • Maxim Korobov
                Maxim Korobov last edited by

                Thanks! Work like a charm!

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

                  @backtrader if I understand correctly, this may be a more appropriate way to set a closing bar stop for live trading? Or does this only apply to historical data?

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

                    @RandyT said in Plotting dates range:

                    @backtrader if I understand correctly, this may be a more appropriate way to set a closing bar stop for live trading? Or does this only apply to historical data?

                    The final answer in this thread was meant to provide a way to find the closest index to a timestamp to restrict the actual plotting.

                    May the above question be misplaced? (and therefore intended for another thread)

                    1 Reply Last reply Reply Quote 0
                    • RandyT
                      RandyT last edited by

                      Correct, does not make sense in this context. Please delete.

                      1 Reply Last reply Reply Quote 0
                      • M
                        mpgentleman last edited by

                        Can you show an example of plotting between to specific dates. ie 01/01/2008 to 12/31/2009

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

                          See Blog - Plotting Date Ranges

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