Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multipurpose "variable"

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

    Multipurpose "variable"

    Hi,

    I am using the term "variable" in this question as defined in the dictionary and not necesarely as used in programing in NT (which I am new to, and were given my current level of confusion may not be the same thing).

    I would like to have a variable in my code that I can use in diferent indicators or other parts of the code for say comparison purposes versus other numbers or conditions. How do I create this "variable" and where in the code do I insert it so that the program can look it up every time I call it?

    Many thanks

    #2
    Hello,

    You can create a Boolean variable which can be switched to true under certain conditions. Please have a look at the pseudo code below

    Code:
    bool myBool = false;
    
    if (Close[0] > Close[1])
    {
      myBool = true;
    }
    
    if (myBool == true)
    //use this logic
    
    else if (myBool == false)
    // use this logic
    Last edited by NinjaTrader_Matthew; 08-22-2012, 05:22 PM.
    MatthewNinjaTrader Product Management

    Comment


      #3
      Thanks Matt, will this allow me to change it in the backtesting window to any value?

      Comment


        #4
        The example I gave you was a private variable, but you can make a public Bool which you could change when backtesting.

        Please see our Reference Sample on User Defined Input parameters:



        Instead of an "int" you would use a "bool".
        MatthewNinjaTrader Product Management

        Comment


          #5
          What is the diference between "public" and "private"?

          Comment


            #6
            The public variable will give you the ability to set this variable from the grid parameters.
            MatthewNinjaTrader Product Management

            Comment


              #7
              Matt I've followed the procedure you posted and I am getting the following error:

              "The type'ninjaTrader.Strategy.Test'already contains a definition for variableX"

              Then NT highlights the code

              get
              { return variableX; }

              as the source of the problem . I have added the "public double" code in the parameters section for the variable and the "private double" code in the variables section just as the wizard had done with others.

              Comment


                #8
                You will want to set it like this:

                Code:
                    #region Variables
                
                     private bool variableX;
                     #end reigion
                
                ....
                
                     #region Properties
                
                        [Description("")]
                        [GridCategory("Parameters")]
                        public bool VariableX
                        {
                            get { return variableX; }
                            set { variableX = value; }
                        }		
                        #endregion
                MatthewNinjaTrader Product Management

                Comment


                  #9
                  couple things:

                  1) the variable I am defining is a decimal number, shouldnt I be using the "double" instead of the "bool"?

                  2) I want this variable to have a default setting of say 2. In that case shouldnt the code in the variables region be :

                  private double variableX = 2;

                  instead of your suggested

                  privatedouble variableX;


                  Comment


                    #10
                    Hello,

                    Yes, if you're dealing with a decimal, you'd want to use a double.

                    Yes, when you declare the private double, use a value of 2;
                    MatthewNinjaTrader Product Management

                    Comment


                      #11
                      Thanks for your patience Matt, I am able to define the variable but the problem in the "parameters" region remains and so does the error message described earlier. So so far I am able to define the variable but not able to have it modifiable in the backtesting window.

                      Comment


                        #12
                        "your strategy likely holds one or more recursive properties which could cause ninja trader to crash: varableX"

                        then highlights the following in the parameters section:

                        get { return variableX; }

                        Comment


                          #13
                          Do you have public int VariableX captialized or lowercase? Please make sure it is capitalize where you are declaring the public int.
                          MatthewNinjaTrader Product Management

                          Comment


                            #14
                            Getting nowhere and my NT just crashed.

                            Comment


                              #15
                              Can you provide the entire code you are using.
                              MatthewNinjaTrader Product Management

                              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
                              108 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              572 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              574 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X