NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 02-24-2010, 08:17 PM   #1
JT454
Junior Member
 
Join Date: Nov 2007
Posts: 15
Thanks: 0
Thanked 0 times in 0 posts
Default 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
JT454 is offline  
Reply With Quote
Old 02-24-2010, 09:56 PM   #2
Grissom
Junior Member
 
Join Date: Feb 2010
Posts: 3
Thanks: 0
Thanked 0 times in 0 posts
Default 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() 
{
return SMA(10)[0] < SMA(20)[0];
}
Grissom is offline  
Reply With Quote
Old 02-25-2010, 09:23 AM   #3
borland
Member
 
Join Date: Jan 2009
Posts: 94
Thanks: 0
Thanked 14 times in 12 posts
Default

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
}
borland is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
indicator as variable e-man Version 7 Beta General Questions & Bug Reports 3 10-20-2009 03:38 PM
Variable input? ju1234 Strategy Development 3 11-24-2008 09:15 AM
Variable Declaration? GreenTrade Strategy Development 7 07-03-2008 01:34 PM
Global Variable... maxpi Strategy Development 6 11-26-2007 09:47 PM
Variable rounded Richard Von Indicator Development 3 08-03-2007 06:55 AM


All times are GMT -6. The time now is 09:18 AM.