Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

EMA(20).Count = 5415 yet EMA(20)[1] Invalid Index

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

    EMA(20).Count = 5415 yet EMA(20)[1] Invalid Index

    Why is [0] a valid index on EMA(20)[0], but [1] for EMA(20)[1] always throws an invalid index exception - when EMA(20).Count = 5472?

    That's more than enough bars. The doc says [1] is the prior candle. This is in real-time data mode, not loading historical data.


    Print("EMA(20).Count: " + EMA(20).Count);

    Print($"EMA(20)[0]: {EMA(20)[0]}");
    double emaStart0 = EMA(20)[0];

    Print($"EMA(20)[1]: ...");
    double emaStart1 = EMA(20)[1];
    Print($"EMA(20)[1] WORKED!!!");

    And, the output:
    EMA(20).Count: 5472
    EMA(20)[0]: 20995.25
    EMA(20)[1]: ...

    Unhandled exception: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index.​​


    NOTE: EMA(20)[1] works in OnBarUpdate. It throws the above exception in a mouse handler. Yet, the EMA is globally accessible data. Why???


    The mouse handler:

    protected void MouseClicked(object sender, MouseButtonEventArgs e)
    {
    // paste above in here
    }


    Then, add this to OnStateChanged:

    else if (State == State.Historical)
    {
    if (ChartControl != null)
    {
    ChartControl.MouseLeftButtonDown += MouseClicked;
    }
    }​
    Last edited by trober; 12-04-2024, 02:52 AM.

    #2
    Hello trober,

    The barsAgo indexes for series increase as each bar is processed in OnBarUpdate().

    When CurrentBar is 0, there is one bar processed. Using an index of [1] will result in an error.
    When CurrentBar is 1, there are two bars processed. Using an index of [2] will result in an error.

    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.


    From a mouse click, TriggerCustomEvent() must be called first to synchronize the series.
    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.

    Comment


      #3
      Hi Chelsea,
      please see the Print output. There are more than enough bars in the EMA(20) to index [1]. The issue is [1] works in OnBarUpdate, but not in the mouse click event.

      Why is that when both can reference the same EMA(20) ie. the compiler can resolve a reference to it. It has to be something internal to NinjaTrader 8.

      Comment


        #4
        Hello trober,

        May I confirm you are calling TriggerCustomEvent in the mouse click event handler method?

        What print output are you referring to?

        "Why is that when both can reference the same EMA(20) ie. the compiler can resolve a reference to it. It has to be something internal to NinjaTrader 8."

        Series are updated with OnBarUpdate() runs.
        Mouse click event handlers are not and require calling TriggerCustomEvent() first.

        Comment


          #5
          Hi Chelsa,

          I am not using TriggerCustomEvent. The point of this ticket: EMA{20} is reference'able in OnBarUpdate and a mouse click event; therefore, both SHOULD be able to use ALL the EMA(20) data. However, the mouse click event cannot access anything greater than offset zero, [0].

          The print I am referring to is in the post above.

          Attached is a full indicator example.

          The reason it has something to do with NinjaTrader:
          -- in OnBarUpdate and MouseClicked routine:

          1. both can get the value of EMA(20)[0]
          2. Both see EMA(20).Count = 5713
          3. MouseClicked cannot access any EMA(20) offset greater than [0].

          If it compiles and is referencable and the .Net CLR is reporting .Count = 5713, that many elements should be accessible by BOTH methods.

          Please see attached indicator and run. Click in whatever chart and you will see the access violation from the MouseClicked routine:

          Click image for larger version

Name:	image.png
Views:	48
Size:	5.0 KB
ID:	1326682

          Output window:

          Click image for larger version

Name:	image.png
Views:	34
Size:	26.8 KB
ID:	1326684​​​
          Attached Files

          Comment


            #6
            After resolving this with your suggestion, it appears NinjaTrader would point to WPF and apartment threading as the reason.

            EMA data should not be owned by a given thread. That's where NinjaTrader's architecture is the actual culprit. fwiw, EMA data could have been shared by any thread given the right architecture. Map the EMA data to global memory.

            Comment


              #7
              Hello trober,

              To access anything data series related, including price, time, and volume series, drawing objects, and indicators from a non-data-driven thread you MUST use TriggerCustomEvent().

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by abelsheila, 05-14-2025, 07:38 PM
              2 responses
              35 views
              0 likes
              Last Post hglover945  
              Started by nailz420, 05-14-2025, 09:14 AM
              1 response
              73 views
              0 likes
              Last Post NinjaTrader_ChristopherJ  
              Started by NinjaTrader_Brett, 05-12-2025, 03:19 PM
              0 responses
              359 views
              1 like
              Last Post NinjaTrader_Brett  
              Started by domjabs, 05-12-2025, 01:55 PM
              2 responses
              67 views
              0 likes
              Last Post domjabs
              by domjabs
               
              Started by Morning Cup Of Trades, 05-12-2025, 11:50 AM
              1 response
              88 views
              0 likes
              Last Post NinjaTrader_ChristopherJ  
              Working...
              X