Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using Values from Another Indicator

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

    #16
    I'm not able to access the updated indicator one object from indicator two.
    Indicator two gets the indicator one object when the indicators load, but after that, any change to the exposed variable in indicator one is not reflected in indicator two.

    Indicator one:
    Code:
    protected override void OnBarUpdate()
    {
    exposedMfe = Close[0];
    exposedHasPosition = hasPosition;
    ...
    }
    
    #region Properties
    [Browsable(false)] // Prevents showing up on the UI property grid
    public double ExposedMfe
    {
    // Update() calls OnBarUpdate to ensure our exposed variable is up-to-date
    get { Update(); return exposedMfe; }
    }
    
    [Browsable(false)] // Prevents showing up on the UI property grid
    public bool ExposedHasPosition
    {
    get { Update(); return exposedHasPosition; }
    }
    I can retrieve the updated value of ExposedMfe without any problem, but ExposedHasPosition does not update in indicator two.

    Indicator two:
    Code:
    protected override void OnBarUpdate()
    {
    if (State == State.Realtime)[INDENT]{
    valMfe = MyMFE().ExposedMfe;
    hasPosition = MyMFE().ExposedHasPosition;
    }[/INDENT]
       }
    Any help would be much appreciated, as I have spent way too long on this.

    Comment


      #17
      Think I have solved it with:
      Code:
      private static bool exposedHasPosition        = false;
      ...
      
      #region Properties
      [Browsable(false)] // Prevents showing up on the UI property grid
      public bool ExposedHasPosition
      {[INDENT]get { return exposedHasPosition; }
      //It looks like Update() is not required[/INDENT]
       
      
      
        }
      Looks like the static modifier is the key to getting this to work.
      Last edited by FatCanary; 06-21-2021, 03:13 AM.

      Comment


        #18
        I have a similar question - I would like to expose values calculated by an indicator A to an indicator B. The main reason is that indicator A draws in one separate panel, and indicator B should draw on the price panel. I expose as per the example above variables calculated by indicator A. When I want to use these in indicator B, I'd use something like valMfe = IndicatorA().ExposedVariable.
        But the problem is my indicator A takes a number of parameters that I want programmable - if I code this in indicator B, I would need to call IndicatorA(10, 20, 30, etc..) hardcoded. Instead, I want to use the active parameters in use for that indicator.
        Not sure if that is clear - I do not want to have to hardcode values of parameters in use for indicatorA when accessing the exposed variable, instead I want to use the current active indicatorA parameters and retrieve its exposed variable.

        Comment


          #19
          Hello Laurentvan,

          If your indicator A has user input properties you can just pass those to indicator b instead of hard coding values like you had shown. You would replace the numbers you had shown with the property names.

          Comment


            #20
            Thanks - how do I do that programmatically? e.g from Indicator B, how do I access the user input property of indicator A? (these are typically entered from the Indicators dialog manually),
            Thank you!

            Comment


              #21
              Hello Laurentvan,

              Whichever indicator you add to the chart would need to pass the values to the indicator being called in code. For example if you call IndicatorA in IndicatorB's code you would pass the user input that you configured in the user interface to IndicatorA. You would need to define the same user input in both indicators.

              For example if you have a user input called Period you would add that user input to the parameters when you call the indicator.

              IndicatorA(Period)

              Comment


                #22
                Thank you and sorry that is still not exactly what I am looking for.
                IndicatorA has something like 20 user input properties, manually entered and modified in the Indicators window/Properties section. I run tests with different values entered manually in this window.
                I could indeed add all the properties into IndicatorB as well but it's a lot of work to modify in both places each time and mirror both IndicatorA and B.

                Would there be another way to expose a variable from IndicatorA to IndicatorB, without having to duplicate all these properties?
                Thanks again

                Comment


                  #23
                  Hello Laurentvan,

                  If you have 20 user inputs in indicatorA you already have the code required to modify indicatorB, you can copy/paste those same inputs into indicatorB to update it. That will make the overloads for the indicator automatically when you compile and then you would pass the user inputs from indicatorA to B when you call B. That would be the suggested way to pass user inputs to an indicator that you call in code so its default values for the properties are being set at the right time. Trying to expose properties in a different way would require using more lines of code and using Update() in each of the public properties which is less efficient overall. In either case you still need to make a public property inside of indicaorB that matches the input inside indicatorA so the value can be passed.



                  Comment


                    #24
                    Thank you - I am still unsure of how this works.
                    Say I have added Indicator A manually through the Indicator window. Do you suggest there is a way to add a new Indicator B programmatically from within the Indicator A? If so, how do I do this?
                    Once added, am I able to map the Indicator B to a specific panel to display its content?
                    Thanks again.

                    Comment


                      #25
                      Hello Laurentvan,

                      You cannot add another indicator visually from an indicator, you can only call it in code to get its values. Only strategies can visually add indicators that it calls by using AddChartIndicator.

                      Comment


                        #26
                        Hi, I'm having trouble exposing a variable from an indicator created by a third party vendor, to an indicator that I created.
                        I was successful, however, in exposing the variable from this indicator to a strategy I created by doing this: See below:

                        //Successful in exposing variable from Indicator to this Strategy
                        public class MyCustomStrategy4 : Strategy
                        {
                        private static Indicators.Gemify.ICTFVG _fvg;
                        private static double exposedDownLowerPrice;
                        }

                        else if (State == State.DataLoaded)
                        {
                        _fvg = ICTFVG(Close, true, Indicators.Gemify.FVGPeriodTypes.Minute, 5, 1000, true, 10, 1.1, 0.01, 0, true, true, Brushes.Maroon, Brushes.Maroon, Brushes.DarkGreen, Brushes.DarkGreen, 13, 4, false, TextPosition.TopRight, new SimpleFont("Verdana", 12), Brushes.WhiteSmoke, Brushes.DimGray, Brushes.Blue, 50);
                        AddChartIndicator(_fvg);
                        }
                        protected override void OnBarUpdate()
                        {
                        var exposedDownLowerPrice = _fvg.ExposedDownLowerPrice;
                        Print("---Exposed Down Lower Price MyCustomStrategy--- = " + _fvg.ExposedDownLowerPrice);
                        }
                        //End Strategy

                        I tried using the exact same code above to expose the same variable from Indicator 1 to Indicator 2, only omitting the line containing AddChartIndicator(_fvg);
                        I receive a value of zero and then get this error.
                        (Indicator 'ICTFVG': Error on calling 'OnBarUpdate' method on bar 5: Object reference not set to an instance of an object.)
                        I assume that bar 5 is returning "null" but I'm also getting a value of zero on bars 1 through 4 so it is not seeing this value from Indicator 1 at all.
                        Does anyone know how I can get the value of exposedDownLowerPrice from Indicator 1 to Indicator 2 without getting the error? Thanks, Derek.

                        See below for how I am exposing the variable in Indicator 1:
                        //Indicator 1
                        public class ICTFVG : Indicator
                        {
                        private static double exposedDownLowerPrice;
                        }
                        protected override void OnBarUpdate()
                        {
                        exposedDownLowerPrice = fvg.lowerPrice;
                        Print("---Exposed Down Lower Price--- = " + exposedDownLowerPrice);
                        }

                        region Properties

                        [Browsable(false)]
                        [XmlIgnore]
                        public double ExposedDownLowerPrice
                        {
                        get { Update(); return exposedDownLowerPrice; } /
                        }

                        #endregion​

                        Comment


                          #27
                          Hello jonesy73,

                          The error Object reference not set to an instance of an object means an object you used was null when trying to use it. Based on the code you provided that may be the fvg variable, you can add a print before that line to check if its null.

                          Print(fvg == null);

                          The static variables you are using also are not the correct way to store an indicator instance, using static in C# is a very advanced concept which our support cannot assist with. static in NinjaScript is not something we recommend using because that goes against the instance based architecture of NinjaScript. You would normally just have a private variable without static to store your indicator instance and then create the instance and assign it to the variable in State.DataLoaded in your strategy. I would suggest using the strategy builder to select the indicator in a condition and then press view code, that will show you the correct syntax to create and store the indicator to a variable.

                          Comment


                            #28
                            Originally posted by NinjaTrader_Jesse View Post
                            Hello jonesy73,

                            The error Object reference not set to an instance of an object means an object you used was null when trying to use it. Based on the code you provided that may be the fvg variable, you can add a print before that line to check if its null.

                            Thanks for the quick reply. I changed the static variable to a non-static one. private double exposedDownLowerPrice;

                            Print(fvg == null);

                            I added the prints to dataloaded and onbarupdate and received this:

                            else if (State == State.DataLoaded)
                            {

                            Print(_fvg == null);//Prints true

                            _fvg = ICTFVG(Close, true, Indicators.Gemify.FVGPeriodTypes.Minute, 5, 1000, true, 10, 1.1, 0.01, 0, true, true, Brushes.Maroon, Brushes.Maroon, Brushes.DarkGreen, Brushes.DarkGreen, 13, 4, true, TextPosition.TopRight, new SimpleFont("Verdana", 12), Brushes.WhiteSmoke, Brushes.DimGray, Brushes.Blue, 50);

                            Print(_fvg == null);//Prints false

                            }
                            //In addition, It prints false in OnBarUpdate but I still get the "object reference" error.


                            The static variables you are using also are not the correct way to store an indicator instance, using static in C# is a very advanced concept which our support cannot assist with. static in NinjaScript is not something we recommend using because that goes against the instance based architecture of NinjaScript. You would normally just have a private variable without static to store your indicator instance and then create the instance and assign it to the variable in State.DataLoaded in your strategy. I would suggest using the strategy builder to select the indicator in a condition and then press view code, that will show you the correct syntax to create and store the indicator to a variable.

                            The problem I'm having is not exposing the variable from the indicator to a strategy. I was able to do this successfully by coding it myself (without strategy builder) with no errors. I was unable to use strategy builder because the Indicator wasn't available in the indicator list under "Conditions" (maybe NT doesn't see it because it's in a folder Gemify => ICTFVG).


                            I
                            What I'm having a problem with is exposing the variable from Indicator 1 to Indicator 2. I'm getting a zero value and then the Null error. I'm a beginner programmer so can you tell me what my null prints mean and possibly how to get the value from Indicator one to print on Indicator 2? eg. when my object _fvg is causing the error? Also, why my variable isn't getting a value other than zero? I don't have visual studio so I can't really step though it. Thanks, Derek.

                            Comment


                              #29
                              Hello jonesy73,

                              The print I suggested is how in C# you can tell if an object is null. The error you got was because an object was null in OnBarUpdate. The indicator with a problem is mentioned in the error ICTFVG.

                              You can see the following example of exposing a value that is not a plot to another script, the sample uses a strategy to get the value but it would be the exact same process when using an indicator.

                              Comment


                                #30
                                Originally posted by NinjaTrader_Jesse View Post
                                Hello jonesy73,

                                The print I suggested is how in C# you can tell if an object is null. The error you got was because an object was null in OnBarUpdate. The indicator with a problem is mentioned in the error ICTFVG.

                                You can see the following example of exposing a value that is not a plot to another script, the sample uses a strategy to get the value but it would be the exact same process when using an indicator.
                                https://ninjatrader.com/support/help...alues_that.htm
                                Hi Jesse, thanks for posting the SampleBoolSeries example. With this, I was able to fix my exposedVariable problem. But I'm still getting the object reference error.
                                Using prints, I've found the line of code that is causing the object reference error. This is the line:
                                future = Times[iDataSeries][0].AddDays(ChartBars.Properties.DaysBack);
                                Normally the value of "future" will print correctly and not cause an error...something like this:
                                2024-07-21 4:30:00 AM
                                But right where I get the object reference error, the value of "future" always prints this:
                                0001-01-01 12:00:00 AM
                                How would I go about checking that line (with code) so that if I get this second value, it won't give me the object reference error.
                                eg. something like ??:
                                if (future == 0001-01-01 12:00:00 AM)

                                {
                                //do something
                                }
                                Thank you, Derek​

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                612 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                355 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                105 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                561 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                564 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X