Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to properly code a strategy that depends on indicator data...

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

    How to properly code a strategy that depends on indicator data...

    Hello fabulous NinjaTrader support staff!

    I am coding a sophisticated strategy that depends on data from indicators. The reason why I've set things up this way is so that my custom indicators can draw their many curves onto their own chart pane instead of being overlaid on the main chart.

    Many times I have run into what I think are threading issues between my custom indicators and my custom strategy. For example if my strategy is at bar 1234 (CurrentBars[0] == 1234) and requests the data from the indicator it owns for its data, that indicator may or may not be at the right bar... many times it's at bar 1, and sometimes some bar behind. (I see this when I print my strategy and indicator's CurrentBars[0] and they are frequently out of sync.

    I assume that my strategy and indicators run on different threads and that I need thread synchronization techniques for my strategy to access indicator data safely...

    Perhaps it is inappropriate for my strategy to instantiate my custom indicators and call its public functions?

    Could you steer me toward the proper way for a strategy to instantiate a slave indicator and how to safely access its public data?

    Many thanks!

    #2
    Hi JPPoulin, thanks for your post.

    The indicators and the hosting strategy all run on the same thread, so threading is not the issue. Be sure to call the indicator's Update method before accessing it to make sure it calls its OnBarUpdate method:



    Please try this in your code and the values should be in sync.

    Comment


      #3
      Ah great advice! That's definitively the missing element.

      Question: What would be a good assert in my indicator to trap out-of-sync issues... would the following line in my Indicator's OnBarUpdate() work?

      Debug.Assert(_oStrategy.CurrentBars[0] == CurrentBars[0]);
      Last edited by JPPoulin; 11-14-2019, 12:39 PM. Reason: Read docs

      Comment


        #4
        Hi, thanks for your reply.

        If you want to do that with assert that's fine, there's not much need for it though. It would not do anything to synchronize the data together.

        Comment


          #5
          Yes, that assert in Indicator's OnBarUpdate() is just a safety precaution.

          To recap, to avoid the issue I've run into, all I need to do is call Update() on all the Indicators my strategy is using.

          Q: I also override OnExecutionUpdate(). Do I also need to update my indicators in this callback or is OnBarUpdate() sufficient?

          Comment


            #6
            Hi JPPoulin thanks for your reply.

            Yes, you can do the same thing in OnExecutionUpdate if you need to update the values before accessing them.

            Comment


              #7
              Awesome. I've done all three things and now the super-complex issues I was running into are gone!

              I recommend the Debug.Assert() call. Better to be absolutely sure the indicators and strategy are all synced up or stale data can easily make its way to the deciding strategy!!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Mindset, 04-21-2026, 06:46 AM
              0 responses
              88 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by M4ndoo, 04-20-2026, 05:21 PM
              0 responses
              134 views
              0 likes
              Last Post M4ndoo
              by M4ndoo
               
              Started by M4ndoo, 04-19-2026, 05:54 PM
              0 responses
              68 views
              0 likes
              Last Post M4ndoo
              by M4ndoo
               
              Started by cmoran13, 04-16-2026, 01:02 PM
              0 responses
              119 views
              0 likes
              Last Post cmoran13  
              Started by PaulMohn, 04-10-2026, 11:11 AM
              0 responses
              67 views
              0 likes
              Last Post PaulMohn  
              Working...
              X