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 kinfxhk, 07-14-2026, 09:39 AM
      0 responses
      125 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Started by kinfxhk, 07-13-2026, 10:18 AM
      0 responses
      105 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Started by kinfxhk, 07-13-2026, 09:50 AM
      0 responses
      85 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Started by kinfxhk, 07-13-2026, 07:21 AM
      0 responses
      105 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Started by kinfxhk, 07-11-2026, 02:11 AM
      0 responses
      84 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Working...
      X