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 Hwop38, 05-04-2026, 07:02 PM
      0 responses
      161 views
      0 likes
      Last Post Hwop38
      by Hwop38
       
      Started by CaptainJack, 04-24-2026, 11:07 PM
      0 responses
      310 views
      0 likes
      Last Post CaptainJack  
      Started by Mindset, 04-21-2026, 06:46 AM
      0 responses
      245 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      350 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      179 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Working...
      X