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

Bug?: Optimization of Multi-timeframe strategies fails with an error

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

    Bug?: Optimization of Multi-timeframe strategies fails with an error

    Hi NT folks!

    I'm trying to run Optimization (using Default and Genetic optimizer) of a simple multi timeframe strategy that I got by converting SampleMultiTimeFrame strategy to optimizable by adding parameters for slow/fast period (attached) and it fails with an error.

    Running backtest on this strategy works fine.

    Steps to reproduce (NT 8.0.1.0 64-bit):
    1. Strategy Analyzer
    2. Backtest Type - Optimization
    3. Optimizer - Default or Genetic
    4. Select MyMultiTimeFrame (attached)
    5. set Fast to 2-5, Slow to 5-50
    6. configure some instrument and date range
    7. hit Run button

    Current behavior:
    1. Error message in print window:
    Code:
    Strategy 'MyMultiTimeFrame': Error on calling 'OnBarUpdate' method on bar 20: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
    2. Investigating exception details shows:
    Code:
    ================ EXCEPTION ==================
    'barsAgo' needed to be between 0 and -1 but was 0
    Parameter name: barsAgo
       at NinjaTrader.NinjaScript.Series`1.get_Item(Int32 barsAgo)
       at NinjaTrader.NinjaScript.NinjaScriptBase.get_Item(Int32 barsAgo)
       at NinjaTrader.NinjaScript.Strategies.MyMultiTimeFrame.OnBarUpdate() in c:\Users\luk\Documents\NinjaTrader 8\bin\Custom\Strategies\MyMultiTimeFrame.cs:line 86
    =============================================
    3. Strategy analyzer obviously shows no results and terminates immediately after start

    Thanks for investigating this, it's very important for me!
    Lukas
    Attached Files
    Last edited by lukas.h; 11-15-2016, 11:21 PM.

    #2
    Hello lukas.h, and thank you for your question.

    In addition to making sure you have enough bars for BarsRequiredToTrade, if you are passing a BarsArray member to an indicator, you also have to check its BarsRequiredToPlot. I have annotated a copy of your source and attached it to this reply with some changes from this page of the help guide documentation inside your code.



    Please let us know if there are any other ways we can help.
    Attached Files
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hi Jessica!

      I tried your modified version and it shows the same error. I also tried to tweak it a bit and modified the indicators to EMA (they don't have any requirement for required bars). And it's still failing even when I set BarsRequiredToPlot to some ridiculously high number as 100.

      The error says:
      'barsAgo' needed to be between 0 and -1 but was 0
      Which sounds weird and looks like the bars were not even loaded.

      Also I want to emphasize that:
      Backtest of this strategy runs fine.
      Which means there's probably no problem with the strategy itself.

      btw: is it possible to download the RC version somewhere? Because I think although I've done some changes in my code, this started to appear after I installed the 8.0.1 final release.

      Thanks!
      Lukas
      Last edited by lukas.h; 11-16-2016, 09:16 AM.

      Comment


        #4
        Thank you for mentioning backtesting works correctly, I had overlooked something when I put that strategy together. Adding this line made the attached strategy work outside of backtesting on my end.

        Code:
        [FONT=Courier New]BarsRequiredToPlot = Math.Max(BarsRequiredToPlot, Math.Max(smaSlowB0.BarsRequiredToPlot, smaFastB0.BarsRequiredToPlot));[/FONT]
        Originally posted by lukas.h View Post
        btw: is it possible to download the RC version somewhere? Because I think although I've done some changes in my code, this started to appear after I installed the 8.0.1 final release.

        Thanks!
        Lukas
        Previous versions of NinjaTrader are no longer available for download through the help guide.
        Attached Files
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          This might be helpful

          If been having the exact same behaviour. No errors when backtesting, only when optimizing. I'm also working with a multitimeframe strategy and working with an indicator (SMA) that uses the secondary dataserie.

          After a couple of hours of debugging, I found that the solution was to initialize the indicators in the State.DataLoaded block like:

          else if (State == State.DataLoaded)
          {
          SMA_SLEEPKABEL = SMA(BarsArray[1], SMA_sleepkabel);
          SMA_KORT = SMA(BarsArray[1], SMA_kort);
          SMA_LANG = SMA(BarsArray[1], SMA_lang);
          }

          Initializing the indicator anywhere else would result in an error when optimizing like:
          Strategy 'USMarketsNEW': Error on calling 'OnBarUpdate' method on bar 5998: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by josh18955, 03-25-2023, 11:16 AM
          6 responses
          435 views
          0 likes
          Last Post Delerium  
          Started by FAQtrader, Today, 03:35 PM
          0 responses
          3 views
          0 likes
          Last Post FAQtrader  
          Started by rocketman7, Today, 09:41 AM
          5 responses
          18 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by frslvr, 04-11-2024, 07:26 AM
          9 responses
          126 views
          1 like
          Last Post caryc123  
          Started by selu72, Today, 02:01 PM
          1 response
          14 views
          0 likes
          Last Post NinjaTrader_Zachary  
          Working...
          X