SAR strange values ONLY on direction changes

 

I'm creating basic EA, using SAR. The problem is when the direction of SAR changes i get strange values of SAR.

Working of advisor: When SAR changes its direction, it detects whether it goes up or down, and open trade.

int OnInit()
  {
.
.
.
   sarHandle = iSAR(_Symbol,PERIOD_M1,0.02,0.2);
.
.
.

  }

void OnTick()
  {
.
.
.
CopyBuffer(sarHandle,0,0,3,sar);
   checkForTrade(target, sar);
   Print(sar[0]);
   Print(sar[1]);
   Print(sar[2]);
   Print(sar[1] - sar[2]);
}

int checkForTrade(int targetToPass/*not yet implemented*/, double& sarArray[])
{
   if(((sar[1] - sar[2]) > -0.0003) && ((sar[1] - sar[2]) < 0.0003)){ printf("No trade"); return 0;}//no trade
   if((sar[1] - sar[2]) >= 0.0003){ printf("Long trade at %02d:%02d ", date.hour,date.min); return 1;}//long trade
   if((sar[1] - sar[2]) <= -0.0003) {printf("Short Trade at %02d:%02d", date.hour,date.min); return -1;}///short  trade
 return 0;  
}

As can be seen from Log.jpg, SAR values where there is no change in direction (15:09 to 15:07 and 15:11 to 15:09) are right, but when there was change in direction (15:10 to 15:08) i get strange value at 15:10.

This happens on any change in direction of SAR.

I cannot find similiar problem, nor reading mql5 reference gave me any clues what is happening.

So can anybody explain to me what happens/how to fix that/where to read about that?


EDIT.

It happens during backtest, and during real time

Files:
chart.jpg  278 kb
Log.jpg  170 kb
 
Start the indicator on the chart with the same parameters and check.
 
Sorry, I'm new to MT5 and MQL5 and I'm not sure what you mean by that. It was all tested on 1 minute chart, and the parameters of the SAR indicator (step 0.02 , max 0.2) were the same as in the code.
 
salacki #:
Sorry, I'm new to MT5 and MQL5 and I'm not sure what you mean by that. It was all tested on 1 minute chart, and the parameters of the SAR indicator (step 0.02 , max 0.2) were the same as in the code.
Show the code where you get sar values.
 

I get sarHandle in OnInit(), and then in OnTick()  i copy buffer and read sar values, which i attached in Log.jpg (there is also chart.jpg for that specific timeframe). Entire process of reading Sar values is there in OnTick() which i already attached.
But here just in case i attach entire code

#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
input int    startH=14;
input int    startM=0;
input int    endH=16;
input int    endM=30;
input int target=0;
int sarHandle;
double sar[3];
MqlDateTime date, datePrevious;

int OnInit()
  {
   sarHandle = iSAR(_Symbol,PERIOD_M1,0.02,0.2);
   TimeCurrent(datePrevious);
   return(INIT_SUCCEEDED);
  }

void OnTick()
  {
   TimeCurrent(date); 
   if(datePrevious.min == date.min) return;
   TimeCurrent(datePrevious);
   if(date.hour<(startH+1) || date.hour>(endH+1)) return;
   if((date.hour==startH+1 && date.min<=startM) || (date.hour==endH+1 && date.min>=endM)) return;
   CopyBuffer(sarHandle,0,0,3,sar);
   checkForTrade(target, sar);
   Print(sar[0]);
   Print(sar[1]);
   Print(sar[2]);
   Print(sar[1] - sar[2]);

  }

int checkForTrade(int targetToPass/*not yet implemented*/, double& sarArray[])
{
   if(((sar[1] - sar[2]) > -0.0003) && ((sar[1] - sar[2]) < 0.0003)){ printf("No trade"); return 0;}//no trade
   if((sar[1] - sar[2]) >= 0.0003){ printf("Long trade at %02d:%02d ", date.hour,date.min); return 1;}//long trade
   if((sar[1] - sar[2]) <= -0.0003) {printf("Short Trade at %02d:%02d", date.hour,date.min); return -1;}///short  trade
 return 0;  
}
Files:
Log.jpg  170 kb
chart.jpg  278 kb
 
salacki #:

I get sarHandle in OnInit(), and then in OnTick()  i copy buffer and read sar values, which i attached in Log.jpg (there is also chart.jpg for that specific timeframe). Entire process of reading Sar values is there in OnTick() which i already attached.
But here just in case i attach entire code

Seems correct.

So what is the problem ? I don't see anything strange. Please explain your issue with more details.

 

The problem is when there is switch in direction of SAR indicator(in real time), iSAR returns strange value  at this certain minute when switch occured and only then. For example in my aforementioned test, at 15:08 - 15:10 SAR should have returned 1.10055 for minute 15:10 yet it returned  1.10009, but when i read value of SAR from 15:09 - 15:11 it returns the value of SAR at 15:10 correctly.

 
salacki #:
The problem is when there is switch in direction of SAR indicator, iSAR returns strange value  at this certain minute when switch occured and only then. For example in my aforementioned test, at 15:08 - 15:10 SAR should have returned 1.10055 for minute 15:10 yet it returned  1.10009, but when i read value of SAR from 15:09 - 15:11 it returns the value of SAR at 15:10 correctly.
 CopyBuffer(sarHandle,0,0,3,sar);

You got values from 0 to 3, so 4 values. But are you aware that the values are not indexed as series ?

sar[0] is the oldest value, not the more recent.

Please read carefully the documentation.

 

I am aware that value at 0 is the oldest one and it goes from 0 -> N and N is most recent. 

Besides I'm  sure CopyBuffer() use value count not index, so

CopyBuffer(sarHandle,0,0,3,sar);

will return 3 values not 4. (Unless im misunderstanding term "count", but when i tested it i got 0 at last index)

 
salacki #:

I am aware that value at 0 is the oldest one and it goes from 0 -> N and N is most recent. 

Besides I'm  sure CopyBuffer() use value count not index, so

will return 3 values not 4. (Unless im misunderstanding term "count", but when i tested it i got 0 at last index)

No you are right, I am tired.