Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Variables in Strategies

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

    Variables in Strategies

    Hi everyone

    I'm having problems using a variable in a strategy.

    I've initially defined in an 'if' statement:

    Code:
    Variable0 = Bars.BarsSinceSession - 1;
    Later on, I've tried to use the following, as part of an 'if' condition:

    Code:
    High[Bars.BarsSinceSession - Variable0]
    but this won't compile, with the message:

    'the best overloaded method for 'NinjaTrader Data.I DataSeries.this[int] has some invalid arguments.'
    I'm used to defining variables in indicators but in strategies, they seem to work differently.

    Any advice on this would be much appreciated.

    #2
    Hello,

    Thank you for the question.

    If you are using Variable0-9 in a Strategy, this will by default be of Double type. In a strategy Variable0 - 9 would already be assigned so if you need a int instead of a double you would want to use a different variable name.

    This is confusing the compiler because you are subtracting an double from an int which would in turn result in a double as the double may or may not have a decimal place.

    To correct this there are two ways to do so, one would be create a int type of variable and use that instead. The other would be to cast the double as an int. This in general should cut off the remainder but I would still personally use the first option over the second to ensure there are no errors.

    Here are examples of both:

    Code:
    protected override void OnBarUpdate()
    {
          //the first way
    	
            int newVariable0 = 10;
    	double high = High[Bars.BarsSinceSession - newVariable0];
       			
          //The second way
    
    	double high2 = High[Bars.BarsSinceSession - (int)Variable0];
    			
    }
    Please let me know if I may be of additional assistance.

    Comment


      #3
      Thanks very much, Jesse, for your very helpful reply.

      I've been hitting one or two other problems, and I'm thinking of 'converting' this strategy into an indicator. (As the object is to draw lines, I don't suppose it matters whether it's one or the other.)

      You've told me how I can store a value, whether it's an int or a double, in a strategy, but I'm not too clear as to how to do this in an indicator.

      As before, I simply wish to store some int/double values, using bools to indicate when the values should change so many bars ahead, once a line is drawn.

      Could you kindly let me know the best way to store values in an indicator.

      Thanking you in advance.

      Comment


        #4
        Hello,

        Thank you for the questions.

        In an indicator, storing data would actually be identical to a strategy.

        The main differences would be if you want to see the data plotted on the chart or not.
        A Indicator has an additional item called a Plot which a Strategy does not have, so if you need a visual plot an indicator is a better choice.

        Storing simple values would still be the same, you can simply define variables like the following :

        Code:
        int intNumber = 10;
        double doubleNumber = 200.34;
        bool trueFalse = false;
        string testMessage = "Hello world!";
        These are your basic types, these can be used in any NinjaScript file but these are really only good at holding 1 value.

        You may not need a DataSeries yet, but this is one of the other storage options you have in both a Strategy or an Indicator. I just wanted to mention this to let you know what tools you have at your disposal.
        http://www.ninjatrader.com/support/h...sub=Dataseries

        I look forward to being of further assistance.

        Comment


          #5
          Again, Jesse, thanks very much.

          What I'm specifically looking for on this occasion is how to store a value that's defined 'on the fly'.

          Let me give an example:

          Let's say, when a certain condition is met, I wish to store a value such as:

          ValueA = CurrentBar - 1

          I wish the system to store this value until another condition is met a few bars later, so I can make a calculation such as:

          ValueB = ValueA * 2

          Could you kindly explain exactly how I should go about this in an indicator? Do I define ValueA somehow in Variables?

          Looking forward to hearing from you again.

          Comment


            #6
            Hello,

            Thank you for the clarification.

            This would be known as the "Scope" of the variable.

            When using NinjaScript you are in turn using C# programming language. This is important to know because you will be using C# syntax.

            When using variables, the scope at which you define the variable matters and will allow the variable to be used either temporarily or persist through multiple bar updates.

            If we take a look at the top of a indicator (or a strategy, it doesn't matter which, both are using C# syntax) we will see the parent Class

            Code:
             public class Test : Indicator
            {
            
            }
            This would be the first scope in which you can define variables. Inside the { } would be the body of the class, this is the class level scope.

            Variables can be defined in this scope like such:

            Code:
             public class Test : Indicator
            {
                 private double testDouble = 0;
            }
            Declaring a variable in this way will give the entire class access to this variable and will persist through bar updates. When I say persist I mean that if OnBarUpdate were to add one to this variable, then next time it was run if it added to the variable it would be set to 2 etc..

            Next you have a inner scope of a method body or

            Code:
             protected override void OnBarUpdate()
            {
                 double testDouble = 0;
            }
            If we look at this example, the scope is the OnBarUpdate method, because I have defined the variable in OnbarUpdate, this variable can only be used in OnBarUpdate. Also note there is not a private modifier in front of double. This is important as you will only use the private modifier in class scope definitions or the first example.

            Finally here is the combination of the two examples

            Code:
             public class Test : Indicator
            {
                 private double testDouble = 0;
            
                 protected override void OnBarUpdate()
                 {
                      testDouble = testDouble + 1; 
                      Print(testDouble);
            
                      double anotherDouble = 1;
                 }      
            }
            If you were to run this, you would add 1 to testDouble every time OnBarUpdate is run or count 1. 2. 3. 4....etc..

            I would recommend searching online for some general C# tutorials as well, this will help greatly in understanding the core concepts of C# ultimately helping you learn NinjaScript in the process.

            Please let me know if I may be of additional assistance.

            Comment


              #7
              Thanks as always, Jesse. That's a terrific explanation!

              I've done quite a bit of coding now in NinjaScript but this is something I've never really understood.

              Indeed, I've looked online for C# examples on many matters but the problem is that, very often, these examples are explained on or for other platforms and I've found it difficult (and I know other Forum users have) to translate and integrate fully these concepts into NinjaScript.

              The other thing is that, many times (and I'm sure like a great many Forum users), I've found a solution and applied it and it's worked, without fully understanding the reason why it's worked - now I understand why it works!

              I indeed set up a test indicator with the below code on a Renko chart.

              (For Forum first-timers, just to explain that the code does this: if this bar close level is less than the previous bar close level, print (to the Output Window) a value twice the number of the current bar - counting from the left. The results show that when this condition isn't met, the previous value is stored.)

              This is precisely what I wanted to achieve!

              Much obliged!

              ---

              My test:

              Variables

              private int testDouble = 0;

              OnBarUpdate()

              if( CurrentBar < 2)
              return;

              if( Close[0] < Close[1])

              testDouble = CurrentBar * 2;

              Print(testDouble);

              From the Output Window:

              4
              4
              4
              4
              4
              14
              16
              16
              16
              16
              16
              16
              28
              30
              32
              34
              Last edited by arbuthnot; 01-21-2015, 10:46 AM.

              Comment

              Latest Posts

              Collapse

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