SAR indicator broken on strategy tester

 

Hi everyone, I had a problem about result on strategy tester. The detail is like this, I want to create an EA using SAR indi. Since this is the first time I'm using indicator that is non-line indicator I tried a simple condition to show when SAR indi change it direction. So I use this code to achieve that:

//+------------------------------------------------------------------+
//|                                                         Test.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

double sar(int shift=0){
   return iSAR(Symbol(),PERIOD_CURRENT,0.02,0.2,shift);

int CheckOpenPosition(int shift=0)
  {
   int result=0;   
   if(sar(shift+1)>Open[shift+1] && sar(shift)<Open[shift]) result=1;
   if(sar(shift+1)<Open[shift+1] && sar(shift)>Open[shift]) result=2;  
   return result;
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {      
//---

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {      
   static datetime preTime=0,curTime;
   bool newBar=false;
   curTime=Time[0];
   if(preTime!=curTime)
     {
      preTime=curTime;
      newBar=true;
     }
   
   if(newBar && CheckOpenPosition(1)==1) Alert("Buy signal");
   if(newBar && CheckOpenPosition(1)==2) Alert("Sell signal");  
  }

//+------------------------------------------------------------------+

In here I use both OnInit to show the alert function and OnTick for strategy tester. 

The problem that I faced is when I used CheckOpenPosition() (shift 0) the strategy tester is somewhat broken (should return "buy signal" and "sell signal" continuously, log attached), but when I used CheckOpenPosition(1) (shift 1) strategy tester accept it and working properly (log attached). Is there anyone knows why using shift 0 would result in broken result but when using 1 shift before working perfectly?

Files:
 
You are only checking the first tick of a new bar. It is unlikely to have changed.
 
Luandre Ezra:

Hi everyone, I had a problem about result on strategy tester. The detail is like this, I want to create an EA using SAR indi. Since this is the first time I'm using indicator that is non-line indicator I tried a simple condition to show when SAR indi change it direction. So I use this code to achieve that:

In here I use both OnInit to show the alert function and OnTick for strategy tester. 

The problem that I faced is when I used CheckOpenPosition() (shift 0) the strategy tester is somewhat broken (should return "buy signal" and "sell signal" continuously, log attached), but when I used CheckOpenPosition(1) (shift 1) strategy tester accept it and working properly (log attached). Is there anyone knows why using shift 0 would result in broken result but when using 1 shift before working perfectly?

Move variables preTime=0,curTime; to global scope.

If you put it on OnTick(), every time there is a new tick, the values of preTime and curTime will always be different, and newBar will always be true.

And as a result it will always return "buy signal" and "sell signal" continuously.

 
William Roeder #:
You are only checking the first tick of a new bar. It is unlikely to have changed.

that's the purpose of the code is to send buy signal when SAR indi change from above to under the price and vice versa. The time length of the two logs is the same. The problem is that the result for "sar shift 0 log" should be like the "sar shift 1 log" but it just print 3 signal and end it. What I don't understand is why "sar shift 1" is working fine while "sar shift 0" only print 3 signal and stop.

 
Roberto Jacobs #:

Move variables preTime=0,curTime; to global scope.

If you put it on OnTick(), every time there is a new tick, the values of preTime and curTime will always be different, and newBar will always be true.

And as a result it will always return "buy signal" and "sell signal" continuously.

The code for newBar is working like it should intended. if you see the log it is printing "buy signal" and "sell signal" when the indi change from above to under and vice versa. The problem that I facing is not about the signal return continuously but rather that with "sar shift 0" it only print 3 signal. The correct result for "sar shift 0" log should be similar with "sar shift 1" log with 30 minutes time difference. 

 

in short, this code is working with SAR indi:

int CheckOpenPosition(int shift=0)
  {
   int result=0;  
   if(sar(2)>Open[2] && sar(1)<Open[1]) result=1;
   if(sar(2)<Open[2] && sar(1)>Open[1]) result=2;  
   return result;

while this code isn't:

int CheckOpenPosition(int shift=0)
  {
   int result=0;   
   if(sar(1)>Open[1] && sar()<Open[0]) result=1;
   if(sar(1)<Open[1] && sar()>Open[0]) result=2;
   return result;

*the difference only in shift

I don't understand why the second code isn't working in strategy tester while using script it works fine.

Reason: