I am working on this indicator step by step and got stuck with the part where I compare the values of bar low with ema20 ("// check for ema20AboveINVALID condition" section in my code).
namespace NinjaTrader.NinjaScript.Indicators { public class EMAX2SymbolWithN6 : Indicator { private EMA ema20; private EMA ema50; private EMA ema200; private SimpleFont smallFont; private bool X1ABOVE = false; private bool ema20AboveINVALID = false; private int X1ABOVEBarNumber; private int lastX1Bar; [Range(1, int.MaxValue), NinjaScriptProperty] public int EMAPeriod1 { get; set; } [Range(1, int.MaxValue), NinjaScriptProperty] public int EMAPeriod2 { get; set; } [Range(1, int.MaxValue), NinjaScriptProperty] public int EMAPeriod3 { get; set; } [Range(1, int.MaxValue), NinjaScriptProperty] public int TextOffset { get; set; } protected override void OnStateChange() { if (State == State.SetDefaults) { Name = "EMA X2 Symbol wN 6"; IsOverlay = true; EMAPeriod1 = 20; EMAPeriod2 = 50; EMAPeriod3 = 200; smallFont = new Gui.Tools.SimpleFont("Arial", 10); TextOffset = 30; } } protected override void OnBarUpdate() { Draw.Text(this, "barNumber" + CurrentBar, false, CurrentBar.ToString(), 0, High[0] + (TickSize * 20), 0, Brushes.Black, smallFont, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0); { ema20 = EMA(EMAPeriod1); ema50 = EMA(EMAPeriod2); ema200 = EMA(EMAPeriod3); if (CurrentBar < 35) { return; } // classical X2 if ((CrossAbove(ema20, ema50, 1) && ema20[0] > ema200[0] && ema50[0] > ema200[0]) || (CrossBelow(ema20, ema50, 1) && ema20[0] < ema200[0] && ema50[0] < ema200[0])) { Draw.Text(this, "X2"+CurrentBar, "X2", 0, Low[0] - (TickSize*TextOffset), Brushes.Black); } // DON`T CHANGE classical X2 //ABOVE200 START ************************** //X1 ABOVE condition if (CrossAbove(ema20, ema50, 1)&& ema20[0] < ema200[0]) { X1ABOVE = true; lastX1Bar = CurrentBar; // update lastX1Bar Print("X1ABOVE flag set to true at bar " + CurrentBar); Draw.Text(this, "X1-A"+CurrentBar, "X1-A", 0, Low[0] - (TickSize*TextOffset), Brushes.Orange); } if (X1ABOVE) { // check for ema20AboveINVALID condition for (int i = lastX1Bar + 1; i < CurrentBar; i++) { if (Low[i] < ema20[i]) { ema20AboveINVALID = true; Print("ema20AboveCHECK condition met at bar " + i + ", Low=" + Low[i] + ", Close=" + Close[i] + ", EMA20=" + ema20[i]); // Print("ema20AboveCHECK condition met at bar " + i); break; } } // X1 ABOVE becomes false if ((CrossBelow(ema20, ema50, 1) && ema50[0] < ema200[0]) || (CrossAbove(ema50, ema200, 1) && ema20[0] > ema50[0])) { X1ABOVE = false; ema20AboveINVALID = false; Print("X1ABOVE flag set to false at bar " + CurrentBar); } } //X2 ABOVE200 condition if(CrossAbove(ema50, ema200, 1) && ema20[0] > ema50[0]) { Print("X2 ABOVE200 at bar " + CurrentBar); Draw.Text(this, "AV-X2"+CurrentBar, "AV-X2", 0, Low[0] - (TickSize*TextOffset), Brushes.Blue); X1ABOVE = false; Print("After X2ABOVE200 X1ABOVE flag set to false at bar " + CurrentBar); } //ABOVE200 END ************************** /* //X1 BELOW condition if (CrossBelow(ema20, ema50, 1)&& ema20[0] > ema200[0]) { // Print("X1 BELOW " + Instrument.FullName + " at: " + Time[0]); Draw.Text(this, "X1-B"+CurrentBar, "X1-B", 0, Low[0] - (TickSize*TextOffset), Brushes.Orange); } //ABOVE200 condition if(CrossAbove(ema50, ema200, 1) && ema20[0] > ema50[0]) { // Print("ABOVE200: VALID " + Instrument.FullName + " at: " + Time[0]); Draw.Text(this, "AV-X2"+CurrentBar, "AV-X2", 0, Low[0] - (TickSize*TextOffset), Brushes.Blue); } //BELOW200 condition if(CrossBelow(ema50, ema200, 1) && ema20[0] < ema50[0]) { // Print("BELOW200: VALID " + Instrument.FullName + " at: " + Time[0]); Draw.Text(this, "BV-X2"+CurrentBar, "BV-X2", 0, Low[0] - (TickSize*TextOffset), Brushes.Green); } */ } } } }
X1ABOVE flag set to true at bar 117
ema20AboveCHECK condition met at bar 118, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
ema20AboveCHECK condition met at bar 119, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
ema20AboveCHECK condition met at bar 120, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
X1ABOVE flag set to false at bar 121
X1ABOVE flag set to true at bar 173
ema20AboveCHECK condition met at bar 174, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
ema20AboveCHECK condition met at bar 175, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
ema20AboveCHECK condition met at bar 176, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
ema20AboveCHECK condition met at bar 177, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
ema20AboveCHECK condition met at bar 178, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
ema20AboveCHECK condition met at bar 179, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
ema20AboveCHECK condition met at bar 180, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
ema20AboveCHECK condition met at bar 181, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
ema20AboveCHECK condition met at bar 182, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
ema20AboveCHECK condition met at bar 183, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
ema20AboveCHECK condition met at bar 184, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
ema20AboveCHECK condition met at bar 174, Low=0,92457, Close=0,92468, EMA20=0,924789733676327
ema20AboveCHECK condition met at bar 174, Low=0,92453, Close=0,92471, EMA20=0,924782139992867
ema20AboveCHECK condition met at bar 174, Low=0,92457, Close=0,9248, EMA20=0,924783840945927
...
The Low value of bar 118, 119 and 120 is the low value of bar no. 1. The bar close value of bar 118, 119 and 120 is the close value of bar 1. Also the ema20 value seems to be from bar 1. Noticeable here is, that all three emas are not drawn on the chart till bar No. 20. Then the printouts continue to show values for bar 1 for bars 174 till 184. after that the printout shows bar no. 174 again but now with the values of bar 12. Then the next bar 174 is showing the values of bar 13 and so on. So it is obvious that the script is stuck somewhere within bar 1 and bar 31. Also the other printouts show only values from bars 1 to 31.
I am also attaching a screenshot of the chart where this is happening:
This is driving me crazy but I hope someone can help me solve this problem.
Thanks!
Best
Peter
Comment