Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

In Strategy Analyzer/WFO: load data prior to start date

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    In Strategy Analyzer/WFO: load data prior to start date

    Hi everyone,

    I'm trying to figure out how to make the walk forward optimization work properly.

    Let's say I have a simple strategy that utilizes a SMA, calculated over a period of 40 bars. When running the strategy on a chart, I'd simply load 40 bars prior to the date I want to start the strategy at and add something like this to my script:
    Code:
    if (CurrentBar < 40)
    return;
    So far so good. If I want to backtest the strategy in the Strategy Analyzer, the same logic applies to get accurate results.
    However, how can I make this work in a WFO?

    For example: if I want to do a WFO with 7 days optimization and 1 day test periods. The 40 bars required for my SMA amount to about 2 days of data in total. Now I get the situation where the WFO actually skips two days of the 7 in the optimization and more importantly won't even do any trades in the 1 day test period, correct? Because the strategy won't trade before 40 bars have passed, which never happens in the 1 day test period in my example.

    I've tried using AddDataSeries() with the barsToLoad overload, but this apparently doesn't work in the Strategy Analyzer. What I need is to ensure that all historical data required for proper calculation of my indicators is present right at the start of each optimization and test period, so that I do not have to skip the first X bars. How can I do this?

    I've found some info on BarsRequest, but going that route seems rather cumbersome just to make a WFO work properly.

    Any help is appreciated.

    Thanks!

    #2
    Hello sereot,

    Each Walk Forward Optimization is a separate backtest. You may need to increase the test period if you are wanting more data in the test period backtest.

    A check for current bars will affect each backtest in the optimization.

    Below is a link to a forum post that discusses this.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      thanks for replying and linking the thread. This confirms my own findings.

      Correct me if I'm wrong, but doesn't this behavior make WFOs fundamentally inaccurate whenever a strategy uses any kind of indicator that requires data over a period of bars?
      In a chart, or in a simple backtest in the Strategy Analyzer I can always just add the amount of data/days my indicators need in front of the actual start date. But in a WFO I can't.

      Increasing the test period will only make the error smaller, but the inaccuracy will always be there. And in my use case for a WFO it's not really an option. I want to have a test period of 1 day and my indicator needs 2 days of data. I can increase the test period to 3 days, so that I get one day of actual trading in each 3 day test period. But since test periods are subsequent and not overlapping it will mean that the WFO ignores two days of potential trading in every test period.

      Is there really no (simple) workaround for this? Also, if the feature request in the other thread is still alive, feel free to add my vote to it.

      Thanks.

      Comment


        #4
        Hello sereot,

        The data would be the same number of days. I'm not certain how this would be inaccurate.

        In a walk forward you can increase the days of the test period. Have you increased the number of days in the test period?

        Are you saying the data itself is not the downloaded market data?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea,

          from my understanding in each (out of sample) test period of a WFO, a separate backtest is done, with the parameters that were found in the (in sample) optimization period, correct?

          Consequently, in each "backtest" of a test period, the bars index starts at 0 again. Now, assume I have a strategy that uses an SMA over a period of 40 bars. To ensure that my indicator can calculate properly, I'd add this to the beginning of OnBarUpdate()
          Code:
          if (CurrentBar < 40) return;
          This code gets executed in each test period. And since each test period starts at a bar index 0, the WFO actually "ignores" 40 bars in every single test period. In the case where the test period doesn't even have 40 bars, the WFO would never execute any trades.

          This problem can easily be solved in a normal backtest or when loading the strategy onto a chart, because I can simply load more data in front of my "start date". But in a WFO I can't do that.
          Does this make my problem more clear? I'd be happy to hear that I'm missing something or that there's a workaround. Please let me know if that's the case.

          Thanks!



          Comment


            #6
            Hello sereot,

            This is correct. For each optimization backtest CurrentBar starts at 0.

            If you increase the test period and optimization period, there should be enough data to skip the first 40 bars.

            With a normal backtest in the Strategy Analyzer, it would not be possible to add more bars to be start of the test. The test starts where the start date is selected.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by pmachiraju, 11-01-2023, 04:46 AM
            8 responses
            148 views
            0 likes
            Last Post rehmans
            by rehmans
             
            Started by mattbsea, Today, 05:44 PM
            0 responses
            5 views
            0 likes
            Last Post mattbsea  
            Started by RideMe, 04-07-2024, 04:54 PM
            6 responses
            33 views
            0 likes
            Last Post RideMe
            by RideMe
             
            Started by tkaboris, Today, 05:13 PM
            0 responses
            5 views
            0 likes
            Last Post tkaboris  
            Started by GussJ, 03-04-2020, 03:11 PM
            16 responses
            3,282 views
            0 likes
            Last Post Leafcutter  
            Working...
            X