"I understand that candle [i] is the current candle being formed right now"
not really - since i can be any value from 0..limit
current is relative to where standing.
if speaking relative to Time[] and i=0 then Time[i] is current/rightMost/buildingNow chart period and Time[i+1] is at an historically older 'Time' by one chart bar period in seconds.
if speaking relative to a looping construct then Time[i] is current regards whatever doing inside the loop, keeping in mind that i>0 is going left/older regarding Time.
ok drone over.
.
"but how can you have candle [i-1] ??"
if( i>0 ) Time[i] = the i'th bar historically older/leftwards to [0] and [i-1] is ok as i>0, yes? ie, start decrementing i and you go right towards most recent/current Bar[0]
typically, many use [i=1] as first bar going left in a loop. Why? because it has fully formed and now static O,H,L,C,Time,Volume (NOTE:the Open and Time are static values on the first data tick of a new bar [0])
also, at this first data tick - is when the previous Time[0] is now Time[1] and Time[0] at this first data tick is the [now] static Time for this forming new bar [0] with of course it's own static Open value)
.
"slowerEMAafter = iMA(NULL, 0, SlowerEMA, 0, MODE_EMA, PRICE_CLOSE, i-1);"
no can do IF i=0,1,2,..
see? as if i=0 then [i-1] is equiv to [-1] and that bar not exist/is technically illegal array element access... cuz array storage goes from 0,1,...,n-1 where n is dimension size.
.
ok? or not?
me? now all confused too - lol
.
hth
I think the i-1 is only valid for i>0. Or to look at it another way the indicator is only valid up to the last closed candle where 3 values of i are valid.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I have an indicator that alerts when 2 Moving Averages cross.
This indicator has the following section of code--
limit=Bars-counted_bars;
for(i = 0; i <= limit; i++) {
fasterEMAnow = iMA(NULL, 0, FasterEMA, 0, MODE_EMA, PRICE_CLOSE, i);
fasterEMAprevious = iMA(NULL, 0, FasterEMA, 0, MODE_EMA, PRICE_CLOSE, i+1);
fasterEMAafter = iMA(NULL, 0, FasterEMA, 0, MODE_EMA, PRICE_CLOSE, i-1);
slowerEMAnow = iMA(NULL, 0, SlowerEMA, 0, MODE_EMA, PRICE_CLOSE, i);
slowerEMAprevious = iMA(NULL, 0, SlowerEMA, 0, MODE_EMA, PRICE_CLOSE, i+1);
slowerEMAafter = iMA(NULL, 0, SlowerEMA, 0, MODE_EMA, PRICE_CLOSE, i-1);
The FOR loop begins with i=0
fasterEMAnow is looking at candle [i] which is [0]
fasterEMAprevious is looking at candle [i+1] which is [1]
but fasterEMAafter is looking at candle [i-1] which is [-1] This is the statement I do not understand.
How do these statements determine if a cross occurs?
In addition, the alert only happens on a closed candle, never an open candle.
Any guidance would be appreciated. Thank you.