Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

EMA data available in OnBarUpdate, missing in MouseMove

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

    EMA data available in OnBarUpdate, missing in MouseMove

    Hi,

    can anyone explain why 6315 entries can be loaded into an EMA during OnBarUpdate, but only index [0] be available in that EMA by the time the MouseMove handler is called?
    - at first I thought it was due to [0] always being the current bar, then doing the math in MouseMove to subtract blah blah to get the same index. However, by the time MouseMove is called the EMA simply is not there. Zero code in my indicator messes with it.

    All standard checks (bars visible on the chart, valid index, etc.) do not apply. A dump of it in OnBarUpdate shows it, the same dump routine in the MouseMove handler shows a single entry only.


    Thanks

    #2
    Hello trober,

    A mouse move event is not a NInjaScript event so you would need to use a TriggerCustomEvent handler to access data in that context, have you tried putting the code in question inside of TriggerCustomEvent?

    Comment


      #3
      Hi Jesse,

      thank you for replying. No, I have not tried that. Reading the doc, it's not apparent how to wire up a mouse click handler with it.

      Furthermore, the C# compiler has no problem resolving references such as EMA(20) - with OnBarUpdate and MouseClicked(object sender, MouseButtonEventArgs e). In addition, both see index [0] just fine. It's any index greater than [0] that MouseClicked throws an exception.

      This appears to be related to NinjaTrader internal plumbing?



      I merely want a mouse click to give me the value of an EMA for the bar clicked. Certainly many have developed mouse event handlers which have full access to all the indicator's data, including built-in like EMA(20) before?

      Perhaps I don't know what to search for to find a code starter.

      Comment


        #4
        Hello trober,

        As mentioned a mouse event is not a NinjaScript event, you would have to use TriggerCustomEvent there for access to any NinjaScript based properties.

        TriggerCustomEvent(o =>
        {
        // your code here
        }, null);​

        Comment


          #5
          Hi Jesse,

          thank you for your reply. A "mouse event" vs. a "NinjaScript event" is relevant how? Both can resolve a reference to EMA(20)[0]. So, why can OnBarUpdate access all the entries in the generic list, but the mouse event can ONLY access [0]?

          Keep in mind, in both OnBarUpdate and the mouse event .Count is sufficient for minimum bars for the EMA(20). If both OnBarUpdate and the mouse event routine show .Count = 6315 then the mouse event SHOULD be able to index those entries.

          Perhaps this is some P/Invoke-related design internal to NinjaTrader 8. e.g. the entries are actually in unmanaged memory with a .Net C generic list wrapper.

          I need some kind of answer along those lines: mouse events should be able to access all the indicator's data.

          Regards,
          Ben
          Last edited by trober; 12-04-2024, 11:46 AM.

          Comment


            #6
            Hello trober,

            A NinjaScript event is something driven by NinjaScript which is the trading API, mouse events are application events through WPF.

            You need to use TriggerCustomEvent to access NinjaScript properties in non NinjaScript events.

            Comment


              #7
              Hi Jesse,

              can I *please* get a more in-depth explanation before I invest in doing that? I'm not even sure it will work - I have not found any example of a mouse event using EMA data. Do you have one?

              Both OnBarUpdate and the mouse event handler routine can resolve a reference to EMA(20)[0] as well as see the same EMA(20).Count value.
              That alone should suffice for being able to access any index in EMA(20) in a mouse event handler.
              The fact OnBarUpdate is a callback from NinjaTrader and the mouse event handler a callback from the presentation subsystem is irrelevant. Heap is heap.

              The fact it cannot has to be something related to NinjaTrader P/Invoking to unmanaged memory or related.

              Merely investing in some work-around like that without an understanding of why a compiler reference and runtime reference resolve with both, OnBarUpdate and the mouse event, is not a trivial matter that one can use that reference while the other cannot.

              I would expect EMA data to be in unmanaged memory, not the CLR managed memory, for optimized speed. So, there is an explanation for what is going on vs. "NinjaTrader event" and "mouse event".

              Comment


                #8
                Hello trober,

                I don't have more in depth information about the internals that I can provide, using TriggerCustomEvent is a normal process when using NinjaScript and is required when you use UI events like button events. The information we have on this topic is in the helpguide, here is a link.



                I can only suggest using TriggerCustomEvent based on the details you gave. You can refer to the sample I provided on how to insert that into your button event.

                Comment


                  #9
                  Hi Jesse,

                  thank you. I will look into that, but my point is still not addressed: why EMA{20).Count = 5713 in OnBarUpdate and MouseClicked, yet the latter cannot access anything but offset zero, EMA(20)[0] without an access violation.

                  It takes two minutes to see this in-action. Please see attached example. Run it and click in the chart to see this:

                  Click image for larger version

Name:	image.png
Views:	101
Size:	5.0 KB
ID:	1326686

                  and Console output:

                  Click image for larger version

Name:	image.png
Views:	100
Size:	26.8 KB
ID:	1326687

                  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].


                  ​My post is seeking an answer to why this is happening. Anyone would see this is a bug in NinjaTrader 8.

                  Comment


                    #10
                    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

                    Latest Posts

                    Collapse

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