Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Synchronize execution between two indicators

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

    Synchronize execution between two indicators

    On a chart I have two Indicators. Indicator A and Indicator B. Indicator A is developed by someone else and is insanely complicated but I do understand its skeletal structure. That developer is friendly but not very friendly so I cannot pester him too much. Indicator B is being written by me and I want to read few data series that are part of Indicator A.

    Once I have good enough Indicator B, I will cut-paste-move it to strategy.

    Amongst the very unusual thing that Indicator A does is that it defers calculation until the very last candle. This has been confirmed by its developer and I don't know why he is doing it that way.

    Code:
    protected override void OnBarUpdate()
    {
    int currentBar = CurrentBar;
    ...
    if (currentBar >= Bars.Count - 1) && (currentBar > minimumBarsNeeded))
    {
     ... // Bulk of processing.
    }
    }
    About 85 to 90% of code in OnBarUpdate constitutes body of that if statement.

    Ninja sometimes calls OnBarUpdate of Indicator B before Indicator A and this results in a disaster.

    Therefore, I want to add something in code of Indicator B so that its execution will always be synchorized with execution of Indicator A.

    Indicator B must never run before Indicator A is done updating all of its values.


    How do I do that?

    #2
    Hello cdjindia,

    Thank you for your post.

    I do not believe this is possible, however I am looking into this further to be certain.

    Comment


      #3
      Hello cdjindia,

      Thank you for your patience.

      There really is not much in this area however, using Update() we can ensure the other indicator updates before accessing it.
      Please let me know if you have any questions.

      Comment


        #4
        Originally posted by cdjindia View Post
        On a chart I have two Indicators. Indicator A and Indicator B. Indicator A is developed by someone else and is insanely complicated but I do understand its skeletal structure. That developer is friendly but not very friendly so I cannot pester him too much. Indicator B is being written by me and I want to read few data series that are part of Indicator A.

        Once I have good enough Indicator B, I will cut-paste-move it to strategy.

        Amongst the very unusual thing that Indicator A does is that it defers calculation until the very last candle. This has been confirmed by its developer and I don't know why he is doing it that way.

        Code:
        protected override void OnBarUpdate()
        {
        int currentBar = CurrentBar;
        ...
        if (currentBar >= Bars.Count - 1) && (currentBar > minimumBarsNeeded))
        {
         ... // Bulk of processing.
        }
        }
        About 85 to 90% of code in OnBarUpdate constitutes body of that if statement.

        Ninja sometimes calls OnBarUpdate of Indicator B before Indicator A and this results in a disaster.

        Therefore, I want to add something in code of Indicator B so that its execution will always be synchorized with execution of Indicator A.

        Indicator B must never run before Indicator A is done updating all of its values.


        How do I do that?
        You will need to create and set a state variable in OnStartUp() of Indicator A, and turn it off, or change its value after Indicator A has completed its processing (i.e., the last statement of OBU).

        Expose the variable for public access.

        From Indicator B, read the state value that you created in Indicator A, and act accordingly.

        Comment


          #5
          hi koganam

          I cannot modify code of Indicator A. If I ever get a chance to do this, first thing I'd do is to get rid of this calculation deferring from Indicator A. "Sentinel" approach suggested by cannot be used here.

          Ideally, I am looking for a solution that can suspend execution of B without missing out on chance to execute OBU when it resumes. Given that Ninja is based off .NET, why can't I use the synchronizations techniques from .NET?

          Code:
          if (IndicatorA.IsUpdated() == false) 
          Thread.Sleep(1234);
          Aren't there any global objects in NinjaTrader or NinjaTrader.CBI namespace that I can thread-lock?
          Last edited by cdjindia; 01-17-2014, 05:32 AM.

          Comment


            #6
            Hello cdjindia,

            Using Thread.Sleep() will effect the entire thread not just Indicator B.

            There are not any methods that I am aware of that will do what you are looking for.

            Does having the same condition as Indicator A and using the IndicatorA.Update() before you want to execute your code not work for what you are looking for?
            JCNinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_JC View Post
              Hello cdjindia,

              Using Thread.Sleep() will effect the entire thread not just Indicator B.

              There are not any methods that I am aware of that will do what you are looking for.

              Does having the same condition as Indicator A and using the IndicatorA.Update() before you want to execute your code not work for what you are looking for?
              No. Calling Update() on Indicator A does not seem to be reliable enough.

              From what I can observe, They seem to be running criss-cross to one another.
              Sometimes Indicator A runs first for a bar while at other times Indicator B sneaks ahead for the same last bar.

              Print statements that get written by both the indicators are interleaved with one another in Output Window.

              Comment


                #8
                Hello cdjindia,

                Using Update() on Indicator A will make sure that the OnBarUpdate() is going to be current but could you let me know what time frame you are using on the Indicators?
                JCNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_JC View Post
                  Hello cdjindia,

                  Using Update() on Indicator A will make sure that the OnBarUpdate() is going to be current but could you let me know what time frame you are using on the Indicators?
                  Timeframe is not yet firmly set but usually it is a Renko or 150 to 250 Ticks. I've exiremented with 1 and 5 minutes as well

                  You may have a point w.r.t. Update, so this is how I use it

                  Code:
                  namespace NinjaTrader.Indicator
                  {
                    public class IndicatorB : Indicator
                    {
                      private [COLOR="Red"]IndicatorA[/COLOR]  [COLOR="Red"]indya [/COLOR]= IndicatorA(.....);
                      private int minimumBars = 20;
                      
                      protected override void OnBarUpdate()
                      {
                        if ((CurrentBar >= Bars.Count - 1) && (CurrentBar > minimumBars))
                        {
                          [COLOR="red"]indya[/COLOR].[COLOR="RoyalBlue"]Update[/COLOR]();
                          ...
                        }
                      }
                    }
                  }

                  Comment


                    #10
                    Hello cdjindia,

                    Thanks for the snippet.

                    With a timeframe like, 1 Minute or 5 Minutes I would expect them to be synced meaning that IndicatorA (indya) will be up-to-date before the rest of the code is called.

                    Using a Tick based timeframe this could be off if there are multiple bars that happen in the same second.

                    When you were testing with the Minute timeframes did you still see it off?
                    JCNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    666 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    377 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    110 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    575 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    580 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X