Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Scope of Variables

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

    Scope of Variables

    Hi,

    Is the scope of the variables specified in the Variables section of the code restricted to the specific instance of the chart?

    Suppose I have an indicator, and in that indicator I have a private double variable A that I specify in the Variables section of the code and update through calculations on each bar. Now suppose I create a chart say 10 min CL chart and apply the indicator. So far so good. The variable A is defined and is updated. Suppose I now create a new chart, say a 10 min CL chart as before and apply the same indicator to it. Will the value of the variable A in the second chart be confined to the second chart? That is, will I have a different value for each instance of the variable A?

    Please clarify. Thank you.

    #2
    Zeos,

    Each indicator is its own "instance", so these variables, though called the same thing in the code, are actually stored in separate parts of your memory. The indicator itself knows this variable as A, but other indicator instances don't know where this variable is. This is part of an object-oriented programming design called encapsulation.

    http://en.wikipedia.org/wiki/Encapsu...ed_programming)
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Duh for me. :-)
      Thanks for the clarification.

      Comment


        #4
        Zeos,

        Its not really an easy concept so I wouldn't worry about not knowing it right away. There are some exceptions to this such as declaring static variables that can sort of muck up the visual people have of how it all works.

        Please feel free to contact us any time.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Hi AdamP,

          I am in fact using static variables. Any suggestions for static variables? Please advise.

          Comment


            #6
            Zeos,

            A static variable like this? :

            public static double my_variable = 3;

            If not, this is not considered a "static" variable.

            Adam P.NinjaTrader Customer Service

            Comment


              #7
              Yes, exactly like that. Also, private static double x =0.5;

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              633 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              364 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
              567 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              568 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X