Backtrader Community

    • 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 implement walk-forward optimization with backtrader

    General Discussion
    3
    3
    2480
    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.
    • dimitar-petrov
      dimitar-petrov last edited by

      Anyone has hints about implementing a walk-fowrad testing with backtrader.

      I planning to follow the ideas shared by Kevin Davey in "Building Algorithmic Trading Systems" to test strategies.

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

        Let's take wikipedia as an initial reference:

        • Walk forward optimization

        Quickly quoting:

        • In-Sample Data: It is a past segment of market data (historical data) reserved for testing purposes. This data is used for the initial testing and any optimization and is the original parameters of a system under test.
        • Out-of-Sample Data: It is the reserved data set (historical data) which is not a part of the in-sample data. It is important as this ensures that the system is tested on another period of historical data not earlier thus removing any bias or influences in the checking of the system's performance.

        Unless you have another use case in mind it seems like:

        • You work on the algorithm using a well-known data set.

        • Once you are satisfied you run the results on a non-well-known data set which lets you check the validity of the idea and the implementation without having overfitted the parameters and logic to a specific set of data

        That would be done with the same code in different passes in backtrader

        Is there anything specific, a special use case you would have in mind?

        1 Reply Last reply Reply Quote 0
        • dimitar-petrov
          dimitar-petrov last edited by

          Hello,

          I have the following scenario in mind:

          Pass the system the following inputs:

          • historical data
          • in period duration (5Y)
          • out period duration (1Y)
          • parameters and ranges

          Let's say we have 10 years worth of data, then the ideal processing would be as follows:

          1. Break the data in multiple chunks according to the periods defined above
            • In Period 1: 2000-2005
            • Out Period 1: 2005-2006
            • In Period 2: 2001:2006
            • Out Period 2: 2006-2007
            • In Period 3: 2002-2007
            • Out Period 3: 2007-2008

          Than start executiing the execution.

          1. Optimize parameters for first In Period (2000-2005) - cerebro.optstrategy()
          2. Get best parameters from previous run and test on Out Period 1 - cerebro.addstrategy()
          3. Optimize again on the next In Period
          4. Test with the parameters from previous step on the next out period
          5. Optimize again ......
          6. Test again on the next Out Period .....

          At the end plot the combined strategy during all out periods from 2005 to 2010 by taking into consideration that parameters are being changed every year with the optimized values from last 5 years worth of data.

          I am open for any ideas for implementation.

          So far I am thinking to read the data in pandas, break it and loop through it in chunks and feed it to cerebro consecuently.

          I have no idea how to combine the outputs from each cerebro run and how to plot all individual strategy executions into a single figure.

          BR,
          dpetrov

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