Mql4 iSAR() function

 
I have been using the mql4 iSAR() function to produce SAR values. There appears to be a bug in this; on the first spot of a reversal (according to the graph) the API gives a value which indicates the trend has not reversed - it is only on the 2nd spot of the reversal that I am given a value representing the reversal.

For example...

GBP$ on 15 min candles today. SAR setup with step=0.02 and max=0.2. You can see on the graph the SAR had the following values...

12:30pm GMT 1.9722 (spot above candle)
12:45pm GMT 1.9721 (spot above candle)
13:00pm GMT 1.9677 (spot below candle)
13:15pm GMT 1.9679 (spot below candle)

But my mql4 script calls the iSAR() function on the first tick of each new bar; and produces the following results...

12:30pm GMT 1.9722
12:45pm GMT 1.9721
13:00pm GMT 1.9719 (it is always the first spot of a reverse that is wrong and was above the candle instead of below it)
13:15pm GMT 1.9679

Here's where the i(SAR) function is called:

string thisPair = "";
int thisTimeframe = PERIOD_M15;

int numBars = 0;
bool firstCandle = true;

int init() {

thisPair = Symbol();

return(0);
}

int deinit() {

return(0);
}

int start() {

int i = iBars(thisPair, thisTimeframe);
if (i > numBars) {
numBars = i;

if (firstCandle == true) {
firstCandle = false;
} else {
double thisCandleClose = iClose(thisPair, thisTimeframe, 0);
double thisSAR = iSAR(thisPair, thisTimeframe, 0.02, 0.2, 0);

Print("thisSAR = ", thisSAR, "; thisCandleClose = ", thisCandleClose);
}
}

return(0);
}




Any help in solving this dilemma is much appreciated.
 
On the first tick you should check the previous bar not the current one. SAR changes its value during bar forming, therefore at opening its value can be different than at bar closing.
Reason: