Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    Chris L.NinjaTrader Customer Service

    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.
        Chris L.NinjaTrader Customer Service

        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.
            Chris L.NinjaTrader Customer Service

            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 bortz, 11-06-2023, 08:04 AM
              47 responses
              1,610 views
              0 likes
              Last Post aligator  
              Started by jaybedreamin, Today, 05:56 PM
              0 responses
              9 views
              0 likes
              Last Post jaybedreamin  
              Started by DJ888, 04-16-2024, 06:09 PM
              6 responses
              19 views
              0 likes
              Last Post DJ888
              by DJ888
               
              Started by Jon17, Today, 04:33 PM
              0 responses
              6 views
              0 likes
              Last Post Jon17
              by Jon17
               
              Started by Javierw.ok, Today, 04:12 PM
              0 responses
              22 views
              0 likes
              Last Post Javierw.ok  
              Working...
              X