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

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 ScottWalsh, Today, 04:52 PM
              0 responses
              1 view
              0 likes
              Last Post ScottWalsh  
              Started by ScottWalsh, Today, 04:29 PM
              0 responses
              5 views
              0 likes
              Last Post ScottWalsh  
              Started by rtwave, 04-12-2024, 09:30 AM
              2 responses
              22 views
              0 likes
              Last Post rtwave
              by rtwave
               
              Started by tsantospinto, 04-12-2024, 07:04 PM
              5 responses
              70 views
              0 likes
              Last Post tsantospinto  
              Started by cre8able, Today, 03:20 PM
              0 responses
              7 views
              0 likes
              Last Post cre8able  
              Working...
              X