• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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:

    https://ninjatrader.com/support/help...t8/?update.htm

    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, 11:39 AM. 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 edmata1109, 12-04-2019, 09:19 PM
              8 responses
              28 views
              0 likes
              Last Post koganam
              by koganam
               
              Started by maindoor, 12-03-2019, 10:45 PM
              9 responses
              60 views
              0 likes
              Last Post koganam
              by koganam
               
              Started by w0lverine, Today, 08:22 PM
              0 responses
              1 view
              0 likes
              Last Post w0lverine  
              Started by dkbyond, Today, 04:11 PM
              0 responses
              5 views
              0 likes
              Last Post dkbyond
              by dkbyond
               
              Started by markus3500, Today, 03:29 PM
              1 response
              18 views
              0 likes
              Last Post NinjaTrader_EricB  
              Working...
              X