Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 DayTradingDEMON, Today, 09:28 AM
      4 responses
      21 views
      0 likes
      Last Post DayTradingDEMON  
      Started by geddyisodin, Yesterday, 05:20 AM
      9 responses
      50 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by George21, Today, 10:07 AM
      1 response
      12 views
      0 likes
      Last Post NinjaTrader_ChristopherJ  
      Started by Stanfillirenfro, Today, 07:23 AM
      9 responses
      24 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by navyguy06, Today, 09:28 AM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Working...
      X