Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Nested indicators - synchronisity and onbarupdate event

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

    Nested indicators - synchronisity and onbarupdate event

    I want to have a base indicator "base1" which can call and run other indicators e.g "ad1", "ad2" etc

    I will declare "ad1" and instantiate in "base2" state= configure

    First question; is will "ad1" be added to "base1" in a syncronous fashion? i.e will base1 wait in state configure until ad1 has reached a certain state and what state willl that be?

    Second question; Once both base1 and ad1 are instantiated, will they both receive onbarupdates at the same time and how to then get a value from ad1 output into base1 in a syncronous fashion. i.e I would like onbarupdate runs on ad2 which then outputs a value back to base1 will then waits for next onbarupdate.

    Thanks

    #2
    Hello b16_aln,

    Thank you for your post.

    One thing to note, as mentioned on the help guide page for OnStateChange(), is that "Resources created in State.Configure and not disposed of immediately will be kept and utilized if the NinjaScript object resides in grids (e.g. Strategy tab on Control Center), even if it is not enabled. Try to create resources in State.Historical or State.DataLoaded instead, if possible." I am bringing this up since you mentioned you will declare and instantiate indicators in State.Configure; this could potentially result in a performance impact and a better practice would be to do this in State.Historical or State.DataLoaded.

    As for the states that the indicators will go through or if the host indicator (base1) will wait in a certain state for the hosted indicators (ad1) to reach a certain state, there is no rule of thumb to go by. You could add prints inside of each indicator and review the output in the NinjaScript Output window to understand which state each indicator is in and when.

    What you would be doing by calling the hosted indicator(s) and saving them to their own instance would simply store a reference to that specific indicator instance so that it may be reused throughout the script. This is described on the NinjaScript Best Practices page and, as it mentions there, this practice is by no means a requirement and this process does not need to be followed strictly:


    To ensure OnBarUpdate() functionality of a hosted indicator within a host script, there are three options:
    1) Calling Update on the hosted indicator within the host script
    2) Including a plot in the hosted indicator and accessing the plot in the host script
    3) Including a plot in the hosted indicator and adding the indicator to the chart with AddChartIndicator (this is for strategies only and would not apply in your case of an indicator calling other indicators)

    For more information regarding OnBarUpdate():


    Please let us know if we may be of further assistance.​
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Hello Emily,

      Thank you for the detailed response and suggestions.

      To clarify regarding onbarupdate and nested indicators;
      1. the onbarupdate event gets fired from the ChartControl of owner chart?
      2. Only base1 will receive the Onbarupdate trigger ?
      3. In order for ad1 to receive an onbarupdate trigger I need to call the Update method from base1?
      Is this correct? ad1 does not see the OnBarUpdate trigger from the owner chart at all? or is it the case that both see the Chart onbarupdate event and the point of calling Update is to manually ensure that ad1 has received the onbarupdate at that specific point in time, as base1 and ad1 run in an async fashion therefore there is no way to know which one completes the update first. So calling Update is a manual refresh at a specific time, in effect ad1 will be updated twice if this is called?

      Thanks
      Last edited by b16_aln; 07-13-2023, 01:57 AM.

      Comment


        #4
        Hello b16_aln,

        Thank you for your reply.

        I have addressed your questions below:
        1. the onbarupdate event gets fired from the ChartControl of owner chart?
          • OnBarUpdate() is an event driven method and it is called whenever a bar is updated. It is driven by the input series for the script, and in the case of multi-timeframe and instrument scripts that call AddDataSeries() there will be additional calls of OnBarUpdate() for each time an added series is updated as well. OnBarUpdate() is not tied to ChartControl; it is also called in the case of a script that is not on a chart, like a strategy run from the strategies tab of the Control Center or a SuperDOM indicator. It is tied to and driven by Bars objects of a data series.
        2. Only base1 will receive the Onbarupdate trigger ?
          • base1 will receive OnBarUpdate() and then the 3 options I mentioned in my previous post are ways to ensure that OnBarUpdate() will function and be called for the hosted script, such as ad1. It is not that base1 is the only script that will receive OnBarUpdate() events for its bars objects, the concept is that you must ensure that the bars objects() from the hosted/child script are synced properly to receive OnBarUpdate() events as well
        3. In order for ad1 to receive an onbarupdate trigger I need to call the Update method from base1?
          • Calling Update() is one way to ensure that the hosted script, such as ad1, would receive OnBarUpdate() triggers. There is a reference sample script that demonstrates how to expose indicator values that are not plots found here:
          • https://ninjatrader.com/support/help...alues_that.htm
          • In this example, the strategy SampleBoolSeriesStrategy is the host script and it calls the indicator SampleBoolSeries, the hosted script. If you search for Update() in SampleBoolSeries.cs you should see it in line 120 where Update() is called to ensure exposedVariable is up-to-date when it is called in the print statement in line 79 of the strategy script SampleBoolSeries.Strategy.cs
        If your hosted script, ad1, has a plot and is not exposing a value that is not a plot, you could simply access the plot in the host script, base1, and that will sync up OnBarUpdate(). Calling the Update() method will not be needed in this case. That is because plots added from AddPlot() create a Series<double> object that is synced up with the Bars object.

        Your concern about a script being updated twice is not correct; if the values are already up to date, Update() will not be run. This is noted in the definition on the following page:


        I hope this helps to clarify. Please don't hesitate to reach out with any additional questions or concerns.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Thank you Emily, very thorough answers, much appreciated.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by dwrety, Today, 10:31 AM
          1 response
          7 views
          0 likes
          Last Post dwrety
          by dwrety
           
          Started by apgapg2, Today, 10:04 AM
          0 responses
          10 views
          0 likes
          Last Post apgapg2
          by apgapg2
           
          Started by Preacher, 09-20-2021, 10:11 AM
          13 responses
          1,013 views
          0 likes
          Last Post Gary2751  
          Started by N1tr0, 01-02-2025, 11:25 AM
          8 responses
          120 views
          0 likes
          Last Post N1tr0
          by N1tr0
           
          Started by fincabayano, Today, 05:58 AM
          0 responses
          13 views
          0 likes
          Last Post fincabayano  
          Working...
          X