Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Expression As Variable

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

    Expression As Variable

    Is it possible to define a variable with an expression containing Ninja Trader Indicators such as

    SMA(10)[0] < SMA(20)[0]

    where the expression can now be used as any other variable??

    In other words, can I use the above expression as a bool type variable and if so how would the syntax read? Thanks for any input.

    JT454

    #2
    Make it a Method

    You could store it like this and just pass around the variable isLess:

    Code:
    bool isLess = SMA(10)[0] <  SMA(20)[0];
    But if you want to re-calculate the expression every time you use the variable you're probably better off putting it in its own method and using the method wherever you want that expression:

    Code:
    bool IsLess() 
    {[INDENT] return SMA(10)[0] <  SMA(20)[0];
    [/INDENT]}

    Comment


      #3
      Because of precedence of operators, I would add parentheses to avoid ambigious statements. Like this:

      bool isLess = (SMA(10)[0] < SMA(20)[0]);

      But bool has only two values, true and false, hardly a real variable; so for readibility concerns, the conditional statement would commonly be part of an if statement. Like this:

      if ( SMA(10) < SMA(20))
      {
      // set bool variable to true or false, or do something else
      }

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      566 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      330 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      547 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      548 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X