Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing Public Property in Another Indicator

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

    Accessing Public Property in Another Indicator

    I have successfully retrieved `Series<double>` values in one indicator from another.

    However, trying to access a public property in `ExposingIndicator` from `CallingIndicator` using:
    Code:
    protected override void OnBarUpdate()
    {
        Print(ExposingIndicator().BarMarginOffset);
    }
    produces the following errors:

    ERROR: Indicator 'ExposingIndicator': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.
    ERROR: Indicator 'CallingIndicator': Error on calling 'EventHandlerBarsUpdate' method: Object reference not set to an instance of an object.

    The value for `BarMarginOffset` is returned but then `CallingIndicator` crashes. My guess is that something is null somewhere.

    This one has me stumped. Any pointers would be greatly appreciated.

    Just to clarify, I'm trying to access a value from another running indicator. So, I guess what I am asking is, how do I hook into a currently running indicator in order to access its properties?
    Last edited by FatCanary; 03-17-2023, 05:45 AM.

    #2
    Hello, thanks for writing in. The Series<T> objects need to be initialized in State.DataLoaded first. See the reference code here for an example:

    https://ninjatrader.com/support/help...ng_indicator_v alues_that.htm

    Kind regards,
    -ChrisL​

    Comment


      #3
      Hi Chris

      I don't have an issue accessing a series object. I'm trying to access (in this instance) an int property from an already running indicator.

      Comment


        #4
        Hi, Are you sure that this int property is public? If the code for ExposingIndicator is open source you can look into the source code to confirm this. Unfortunately, there is not enough information here to know what is null in the ExposingIndicator script. If the script is not open source the developer would need to advise how to access this variable.

        If the script is open source, you can use Visual Studio debugging to find the exact line of code causing the null reference exception:

        Comment


          #5
          Both these indicators are my own.
          I was hoping that I was missing something obvious, but it appears not.

          Why does OnStateChange() get called when accessing a property of another indicator?

          Comment


            #6
            Hi, this method gets called because you are initializing the indicator in your script. The visual studio debugging procedure will help you find the line of code that is null in the exposing indicator. You should initialize your indicator rather than just calling the constructor on every OnBarUpdate call. See the SampleMACrossover strategy for an example of creating and initializing an indicator variable. In that script, it sets up two SMA objects.

            Comment


              #7
              Thanks for the help Chris.
              I can now go bug hunting knowing that I have not missed something obvious.

              Comment


                #8
                Just in case it helps somebody...

                I have:
                Code:
                private ChartControl chartControl;
                In one of my methods I needed:
                Code:
                if (chartControl == null)
                    return;​
                And it was in no way obvious that particular null check was required in that particular method.
                It was actually in relation to the 'Connection' class.
                Last edited by FatCanary; 03-17-2023, 11:47 AM.

                Comment


                  #9
                  I have:
                  Code:
                  protected override void OnBarUpdate()
                  {​
                      if (testAdjust)
                      {
                           isManualAdjust = true;
                      }
                  }
                  
                  
                  private void AdjustTarget()
                  {
                      testAdjust = true;
                  }
                  
                  [Browsable(false)]
                  public bool IsManualAdjust
                  {
                      get
                      {
                          Update();
                           return isManualAdjust;
                       }
                       set { isManualAdjust = value; }
                  }​​
                  ​
                  After `AdjustTarget()` is called, `testAdjust` is equal to true, but when OnBarUpdate() is called, testAdjust​ is equal to false.
                  I believe this is because `AdjustTarget()` is on the UI thread but OnBarUpdate() is on the Instrument thread.

                  I've tried:
                  Code:
                  protected override void OnBarUpdate()
                  {​
                      Instrument.Dispatcher.InvokeAsync(() =>
                      {
                          if (testAdjust)
                          {
                              isManualAdjust = true ;
                          }
                      });​
                  }
                  but `testAdjust` remains false.

                  I can find no way to set a property in a method and have its value refreshed in OnBarUpdate so that I can access that property from another indicator.

                  Is what I'm trying to do actually possible?

                  Comment


                    #10
                    Hi, the communication between indicators is not supported. It is possible through a property changed event that I detail here:

                    Comment


                      #11
                      Many thanks for that link Chris. Works a treat.

                      Comment

                      Latest Posts

                      Collapse

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