Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Using Values from Another Indicator

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

    Using Values from Another Indicator

    I have a custom indicator, and I'd like to access some of it's calculated values from within another custom indicator.

    Is this possible, and if so, what would I need to do in order to access the values?
    The indicator that has the values I'd like to access is non-plotting.

    #2
    Hello FatCanary,

    Thank you for your note.

    Is the indicator in question that you want to get values from one you created? If so, you can modify the script to expose the values you want to use in another indicator.

    This example from our help guide illustrates how to expose indicator values that are not plots so they may be accessed by another script:



    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi Kate

      I've looked at the link you suggested, but I didn't get what I needed from it. My indicator does not have a series.

      The indicator in question has been created by me. However, it's the exposing of the values that I think I need help with.
      I assumed I just needed to set the variables holding values to public, but that seems to not have worked.

      I may also need some guidance on how to 'call' the indicator from another indicator.

      Comment


        #4
        Hello FatCanary,

        Thank you for your reply.

        That example demonstrates not only exposing Series<T> variables, but also variables that are not a series. Make sure you're expanding the #region Properties section, as this demonstrates exposing an internal variable as a property that can then be accessed. I'm including the relevant sections of the code below:

        Code:
        /* If you happen to have an object that does not have an ISeries class that can be used, you
        will need to manually ensure its values are kept up-to-date. This process will be done in the
        "Properties" region of the code. */
        private double exposedVariable;
        
        // OnStateChange and OnBarUpdate omitted for brevity
        
        // in #region Properties:
        
        public double ExposedVariable
        {
        // We need to call the Update() method to ensure our exposed variable is in up-to-date.
        get { Update(); return exposedVariable; }
        }

        To access the public ExposedVariable in that example, the included strategy prints the value it receives from the Indicator like this:

        Code:
        /* Print our exposed variable. Because we manually kept it up-to-date it will print values that
        match the bars object. */
        Print(SampleBoolSeries().ExposedVariable);
        You'd access any variable you exposed in the original script in a similar way, filling in any required parameters the indicator needs in the parenthesis.

        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Many thanks for the guidance Kate. I can now access the values.

          Comment


            #6
            I may have been a bit premature.

            I'm able to access the exposed variable, but only when reloading the indicator. After that, the 'calling' indicator does not get the updated variable value.

            The variable value is updated in OnBarUpdate of indicator 1, and then used in a method that is called by OnBarUpdate in indicator 2.

            Any suggestions, please?
            Last edited by FatCanary; 06-14-2021, 07:17 AM.

            Comment


              #7
              Hello FatCanary,

              Thank you for your reply.

              If you've followed the structure in the example, we'd really need to take a look at a simplified version of your code to see what may be occurring. If you're ok with posting that here you can do so, otherwise you can send it in to scriptingsupport [at] ninjatrader [dot] com with 3139105 ATTN Kate W in the subject line of the email and a link to this post in the body.

              Thanks in advance; I look forward to assisting you further.
              Kate W.NinjaTrader Customer Service

              Comment


                #8
                Hi Kate

                I'm using Close[0] just for testing purposes.

                Indicator 1:
                Code:
                protected override void OnBarUpdate()
                {
                ...
                exposedMfe = Close[0];
                Print("exposedMfe: " + ExposedMfe);
                ...
                }
                Code:
                #region Properties
                [Display(Name = "Chart location for info", Description = "", Order = 4, GroupName = "Parameters")]
                public NinjaTrader.NinjaScript.DrawingTools.TextPosition TextLocation
                { get; set; }
                
                public double ExposedMfe
                {
                // Update() calls OnBarUpdate to ensure our exposed variable is up-to-date
                get
                {
                Update();
                return exposedMfe; }
                }
                #endregion
                Indicator 2:
                Code:
                ...
                mfe = MyMFE().ExposedMfe;
                
                string strMfe = Bars.Instrument.MasterInstrument.FormatPrice(mfe);
                ...


                Print("exposedMfe: " + ExposedMfe); produces the correct value from indicator 1. However, in indicator 2, the value stays fixed at the initial value.
                Last edited by FatCanary; 06-14-2021, 10:01 AM.

                Comment


                  #9
                  I think indicator 2 may be the problem. I've just tried exposing a variable from another indicator, and it works fine.

                  Comment


                    #10
                    Hello FatCanary,

                    Thank you for your reply.

                    Where within the second indicator is the example code you have there located? In OnBarUpdate? I would expect that if you're calling it with each pass through On Bar Update you should see the value updated, like in the example script.

                    Thanks in advance; I look forward to assisting you further.
                    Kate W.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Kate

                      That's what I thought, as it is in a method that is called from OnBarUpdate.

                      I've added:
                      Code:
                      [NinjaScriptProperty]
                      [XmlIgnore]
                      to indicator 1 properties, and it appears I need to provide an argument when calling from indicator 2.
                      Am I going down the correct track?
                      Last edited by FatCanary; 06-14-2021, 10:34 AM.

                      Comment


                        #12
                        Nope, that's made no difference.

                        Comment


                          #13
                          That's what I thought, as it is in a method that is called from OnBarUpdate
                          It's actually assigned in a method called by OnRender in indicator 2, and also from a timer method.
                          Last edited by FatCanary; 06-14-2021, 10:43 AM.

                          Comment


                            #14
                            Hello FatCanary,

                            Thank you for your reply.

                            It should work accessing the variable in OnRender or from a timer method. I've modified the SampleBoolSeries example to print updated values from OnRender and that's working correctly for me, do you see the same testing the example? If so, we'd really need to take a look at a simplified version of your code to see what may be occurring. Any code unnecessary to reproduce should be removed.

                            Thanks in advance; I look forward to assisting you further.
                            Kate W.NinjaTrader Customer Service

                            Comment


                              #15
                              It appears that getting the value of the exposed variable has to be done in OnBarUpdate.

                              Code:
                              valMfe = SampleBoolSeries().ExposedVariable;
                              has to be in OnBarUpdate. If it's in OnRender, the Close[0] value has no relation to any recent close value, and it does not update.

                              Also, I can have:
                              Code:
                              protected override void OnBarUpdate()
                              {...
                              Print("Exposed bool variable: " + SampleBoolSeries().ExposedVariable);
                              ...}
                              and
                              Code:
                              private void UpdateValues()
                              {...
                              valMfe = SampleBoolSeries().ExposedVariable;
                              ...}
                              and the value updates without issue. Without the Print statement in OnBarUpdate, I cannot then access the exposed variable elsewhere.

                              So, whilst I have what I require, you may need to put me down me as 'confused'.
                              Last edited by FatCanary; 06-15-2021, 06:29 AM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Taddypole, 04-26-2024, 02:47 PM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Eduardo  
                              Started by futtrader, 04-21-2024, 01:50 AM
                              6 responses
                              58 views
                              0 likes
                              Last Post futtrader  
                              Started by sgordet, Today, 11:48 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post sgordet
                              by sgordet
                               
                              Started by Trader146, Today, 11:41 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post Trader146  
                              Started by jpapa, 04-23-2024, 07:22 AM
                              2 responses
                              21 views
                              0 likes
                              Last Post rene69851  
                              Working...
                              X