Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy not able to Enable from the strategy tab

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

    Strategy not able to Enable from the strategy tab

    Hello, for Ninja 8, I had the working strategy that I was able to disable and enable but now It no longer Enable the strategy.
    The check mark displayed for a split second and then disappears.
    Is there any cleanup I need to do ?

    I try to remove it and add again from chart trader but it is not able to Enable.
    I also restarted the Ninja but same result.

    I see these messages in the log tab. Not sure where this got changed or need to be changed in strategy or in strategy edit screen.

    Click image for larger version

Name:	image.png
Views:	156
Size:	164.2 KB
ID:	1306146

    Please advise.

    Thanks,
    Last edited by mtamaku; 06-04-2024, 08:24 PM.

    #2
    Hello mtamaku,

    "Error on calling 'OnBarUpdate' method on bar 0: 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​"

    This message is indicating the specific index requested from a collection does not exist. Indexes must be a non-negative number and less than the size of the collection. The error may be indicating the index requested is larger than the number of elements in the collection.

    In the case of barsAgo values with Series, any barsAgo index must be less than CurrentBar (the total number of bars is the size of a Series collection).
    Code:
    // require BarsInProgress 0 to have 3 bars processed, require BarsInProgress 1 to have 4 bars processed
    if (CurrentBars[0] < 3 || CurrentBars[1] < 4)
    return;
    
    Print(Times[0][3]); // this does not cause an error as CurrentBars[0] is equal to or greater than 3
    
    Print(Times[1][4]); // this does not cause an error as CurrentBars[1] is equal to or greater than 4
    
    Print(Times[0][5]); // this line of code will cause an invalid index error. When CurrentBars[0] is 4 there is no bar 5 bars ago​
    The help guide discusses ‘Make sure you have enough bars in the data series you are accessing’ .
    NinjaScript > Educational Resources > Tips > Make sure you have enough bars in the data series you are accessing

    Further, Series are updated when OnBarUpdate() is updated. Attempting to use a Series or Series dependent object (such as Draw methods) from a non-data-driven method, including from: the OnRender() method, a button click event handler, or a timer elapsed method hander, will result in an indexing error as the Series indexes are not up-to-date.

    Call TriggerCustomEvent() from any non-data-driven method to update all Series before using the series in the coded logic.
    NinjaScript > Language Reference > Common > TriggerCustomEvent()

    This also applies to barsAgo values supplied to Drawing Objects.
    Code:
    if (CurrentBar < 1)
    return;
    
    Draw.Dot(this, “myDot”, 5, Brushes.Blue); // this line of code will cause an invalid index error as the start barsAgo parameter is 5 when the CurrentBar processed is bar 1.

    Indexing errors can also occur when using an invalid index with arrays, lists, and other collections where the index used is equal to or larger than the .Count() or .Length of the collection, as well as with method calls that use indexes such as: <string>.Subtring() which uses a string position index, <string>.Format() which uses indexes for format items in the string (placeholders {0}), and Draw methods which use barsAgo indexes.

    Dotnetperls is a 3rd party educational site with an in-depth review of arrays.
    C# Array Examples - Dot Net Perls
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi, where do I need to set this value ? in the code Or in a chart trader. The same strategy was working fine till something got changed may be accidently. I have limited knowledge of C## language so not able to follow the links you provided. Thanks

      I have these setting in my strategy when I go in edit strategy to provide inputs such as profit, loss, time range..

      Click image for larger version

Name:	image.png
Views:	148
Size:	35.3 KB
ID:	1306196
      Last edited by mtamaku; 06-05-2024, 09:30 AM.

      Comment


        #4
        Hello mtamaku,

        This would be in the code of the script in the OnBarUpdate() override method.

        One of the lines of code is using an invalid index. The logic will need to be changed to prevent this.

        If this is not a script you have written yourself, I would recommend contacting the original author or vendor to report the error.

        If this is a script you have written yourself, you will need to start by identifying the line of code with the invalid index.

        Add prints above each condition, assignment, and method call in OnBarUpdate(). Print the line number of the print and the Time[0] of the bar.
        The last print to appear will be one line above the line causing the error.

        Below is a link to a support article on using Print() to understand behavior.



        You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Big Thank you for your help.
          Here is the culprit code was causing the error..I should have known from what was changed that made it stop working and could not think of these three Print statements that I thought can not error out since it was compiling ok..

          Print(@"Cum Profit ===>" + Convert.ToString(SystemPerformance.AllTrades.Trade sPerformance.Currency.CumProfit));
          // Print(@"EMA0 VALUE ==> " + Convert.ToString(EMA1[0]));
          // Print(@"EMA1 VALUE ==> " + Convert.ToString(EMA1[1]));
          // Print(@"EMA2 VALUE ==> " + Convert.ToString(EMA1[2]));

          As per your suggestion, it was throwing error in the Log Output right after Cum Profit last print statement .

          Now I am able to run the strategy like before and check mark is there to enable it...

          Again, thanks for help.
          Last edited by mtamaku; 06-05-2024, 07:46 PM.

          Comment


            #6
            BTW, I am an old school programmer and can use some of my knowledge while have no clue on these modern languages. like java and C++ and C##....about to retire from IT
            But I can figure things out here and there..lol

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            567 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            330 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            548 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            548 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X