PDA

View Full Version : 'If' statements with BarColor comand


KLG08
01-21-2010, 02:08 PM
I'm not new to NJ but this is my first Posting to Forum.

The "BarColor" indicator I wrote compiles with no errors, however I'm sure I'm missing an: If - Then, or If - Else, or the correct use of {}'s or some other problem with the Syntax of C+. I have tried several combination of Syntax and spent a lot of time trying to figure this out. My code in question is underlined below. When I load the indicator, the bars don't change colors.

The description of my desired 'logic' is show above each 'Condition'

#region Variables
// Wizard generated variables
private int fastHMA = 40; // Default setting for FastHMA
private int slowerEMA = 20; // Default setting for SlowerEMA
.... generic code not show ... etc
protected override void OnBarUpdate()
{
if (CurrentBar == 0)
{
// Condition 1 Plots GREEN bars if HMA & EMA are both trending UP.
if ((Rising(HMA(fastHMA))) && (Rising(EMA(slowerEMA))));
BarColor = Color.Green;
// Condition 2 Plots RED bars if HMA & EMA are both trending DOWN.
if ((Falling(HMA(fastHMA))) && (Falling(EMA(slowerEMA))));
BarColor = Color.Red;

// Condition 3 Plots PaleGreen bars if HMA is Falling & EMA is trending UP.
if ((Falling(HMA(fastHMA))) && (Rising(EMA(slowerEMA))));
BarColor = Color.PaleGreen;

// Condition 4 Plots RosyBrown bars if HMA is Rising & EMA is trending DOWN.
if ((Rising(HMA(fastHMA))) && (Falling(EMA(slowerEMA))));
BarColor = Color.RosyBrown;}

}

Thank you for your help.

NinjaTrader_Josh
01-21-2010, 02:37 PM
KLG08,

You should not end the if-statement line with a semicolon.

It should be like this pseudo-code:
if (condition1 && condition2 && others)
{
action to do;
another action to do;
}

KLG08
01-23-2010, 12:39 PM
Thank you Josh, couldn't see the forest or the ;;'s (trees).
Thank you for your prompt help.


It still didn't run correctly untill I changed the if (CurrentBar == 0) return; (which I didn't have underlined because I didn't know it was a problem). It worked fine when I changed it to if (CurrentBar < 1) return;
I'm not sure the significance of the difference since the current bar is the 0 bar which is <1. At any rate it works great. Just an educational question; what is the difference, i.e. <1 works but == 0 does not?

NinjaTrader_Ben
01-24-2010, 11:46 PM
Hello,

I will have someone reply to you on Monday. Thank you for your patience.