Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error in Optimization backtest

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

    Error in Optimization backtest

    Hi,
    I am facing a unique problem. I am trying run backtest and do optimization of parameters for different combinations. For some reason, the code that works in backtest mode doesn't work in optimization mode.

    It gives errors when accessing the series data
    Indicator 'IchimokuCloud': Error on calling 'OnBarUpdate' method on bar 65: 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.
    Specifically it fails in below step

    ChikouSpan[KijunPeriod] = Close[0];

    protected override void OnBarUpdate()
    {

    if ((CurrentBar < TenkanPeriod) || (CurrentBar < KijunPeriod) || (CurrentBar < ChikouPeriod) )
    return;

    double fastSum = MAX(High, TenkanPeriod)[0] + MIN(Low, TenkanPeriod)[0];
    double mediumSum = MAX(High, KijunPeriod)[0] + MIN(Low, KijunPeriod)[0];

    TenkanSen[0] = fastSum / 2.0;
    KijunSen[0] = mediumSum / 2.0;


    ChikouSpan[KijunPeriod] = Close[0];


    SenkouSpanA[0] = (TenkanSen[0] + KijunSen[0]) / 2.0;

    SenkouSpanB[0] = (MAX(High, ChikouPeriod)[0] + MIN(Low, ChikouPeriod)[0]) / 2.0;


    }​
    Optimizer is not giving proper results. For example, I know the parameters for TenkanSen, KijunSen, ChikouSpan which will have net profit of say 8k. When run back test optimization mode for max net profit, which includes the known range, t will give a profit of 2k. I am not sure what is causing with the latest update.

    Is anyone else facing this issue?
    Attached Files
    Last edited by sambathraj; 02-08-2023, 08:22 PM. Reason: Added video

    #2
    Hello sambathraj,

    The error you are seeing is an index error which is generally associated with using BarsAgo. To answer this question you would need to use a Print to understand what part of your code is hitting an error.

    If the line ChikouSpan[KijunPeriod] = Close[0]; is causing an error that means that KijunPeriod was not a valid BarsAgo that could be set in that series when you tried to set it. You would need to print out that value to see what it is before the error happens.

    Print(KijunPeriod);
    ChikouSpan[KijunPeriod] = Close[0];


    Once that is known you could adjust the conditions to allow it to work. A BarsAgo is being used that is not valid when its used so the index error is shown.

    JesseNinjaTrader Customer Service

    Comment


      #3
      I tried to do that. In the backtest mode, the code works fine. When I switch to optimization mode, it starts failing. I only thing i can think of is, in multi-threaded mode, optimization doesn't work well. Also the occurrence is very random.

      Comment


        #4
        Hello sambathraj,

        Without using a print to understand the problem it would be anyone's guess at what's happening. Using prints is extremely important when you run into an error so you can find out the specifics of the use case. A general description of the problem or just the error is really not enough information to know what's happening.

        You should see in the output window what the value of the print is before the error to give a clue on the reason. You may need to output other values like the CurrentBar to see if you are using a BarsAgo that is too large for the current amount of bars processed.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Ok. I attached the debugger and kept the breakpoint.

          when the current bar is 53, this error occurs when trying to get the value for Close[0]. Any Idea how to proceed further?

          Click image for larger version  Name:	image.png Views:	0 Size:	314.4 KB ID:	1235703
          Update :
          Seems like the issue is with the Series. If I run the optimization with multiple parameters, the Time[0] is null or Close[0] is null. This seems to be wrong.
          Last edited by sambathraj; 02-16-2023, 10:00 PM.

          Comment


            #6
            Bing Chat GPT Suggested below. I had some OnRender methods. Even after commenting the methods, I get the same issues.
            Click image for larger version

Name:	image.png
Views:	142
Size:	313.4 KB
ID:	1235714

            Comment


              #7
              Hello sambathraj,

              I would not suggest using a chatbot for NinjaScript help, all 3 points it provided are unhelpful or incorrect. Chatbots lack the context of NinjaScript to know if its answer is correct, That means it can often provides non factual information or information that is completely incorrect. You should always reference the help guide or inquire with support if you need help.

              The error in visual studio is not going to be helpful here, debugging in visual studio often wont be helpful for index errors. Using Try/Catch is also not useful in NinjaScript for debugging.

              What you need to do is remove the try/catch and allow NinjaTrader to have an error so you can actually see what's happening.

              I would suggest to exit visual studio and use the NinjaScript output window here. For example:

              Code:
              Print(CurrentBar + " " + KijunPeriod);
              ChikouSpan[KijunPeriod] = Close[0];
              You should see some output similar to:

              0 0
              1 0
              2 0
              3 0

              etc...

              Once you hit the error:

              65 123
              Error on calling 'OnBarUpdate' method on bar 65: ​

              At this point you need to check what the value is for KijunPeriod, if its less than CurrentBar that should work, if its equal to or greater than CurrentBar you will get an index error. In the above demonstration the BarsAgo was 123 but the CurrentBar was 65 which would make an error.

              The code where you said its having an error is setting a Series<double> which has the same index locations as the chart bars. If you supply an invalid BarsAgo on that series thats going to give you an error because you cant set an invalid series location.

              Close[0] should not be the issue here, you can confirm that by simply setting the series to a static value like:

              Code:
              ChikouSpan[KijunPeriod] = 1;

              Last edited by NinjaTrader_Jesse; 02-17-2023, 08:41 AM.
              JesseNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by kujista, Today, 06:23 AM
              0 responses
              1 view
              0 likes
              Last Post kujista
              by kujista
               
              Started by traderqz, Yesterday, 04:32 PM
              1 response
              10 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by f.saeidi, Today, 05:56 AM
              1 response
              4 views
              0 likes
              Last Post Jltarrau  
              Started by Jltarrau, Today, 05:57 AM
              0 responses
              4 views
              0 likes
              Last Post Jltarrau  
              Started by Stanfillirenfro, Yesterday, 09:19 AM
              7 responses
              52 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Working...
              X