PSAR EA code problem

ezraluandre

Trader
Jun 17, 2020
33
1
24
30
Hi I had a simple EA code using Parabolic SAR indicator. In here I tried to determine the SAR direction base on SAR position.

MQL4:
//+------------------------------------------------------------------+
//|                                                         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 OnInit()
  {
 
 
 
//---
   return(INIT_SUCCEEDED);
  }
 
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
 
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   bool BUYCondition = sar()<Bid;
   bool SELLCondition = sar()>Bid;
 
   static datetime preTime=0,curTime;
   bool newBar=false;
   curTime=Time[0];
   if(preTime!=curTime)
     {
      preTime=curTime;
      newBar=true;
     }
   if(Period() == PERIOD_MN1)
      newBar = false;   //Minute 1 using tick
 
   //datetime some_time=D'2021.11.02 08:30';
   //int      shift=iBarShift(Symbol(),PERIOD_CURRENT,some_time);
 
 
   if(newBar && BUYCondition)
      Alert("BUY true");
   if(newBar && SELLCondition)
      Alert("SELL true");
  }
 
//+------------------------------------------------------------------+

This code works as intended with small problem,
First, the code would be lagged by 1 bar, if SAR position change from above to below bid price the alert result still show SELL. ]

Second, when I tried to add condition from sar(1)>Bid to sar(1)>Bid && sar()<Bid it do nothing on strategy tester.

Anyone can help me with this issue?
 
Last edited by a moderator:

ezraluandre

Trader
Jun 17, 2020
33
1
24
30
SAR always showed even new bar created and because of this using newBar shouldn't make the result lagging by one bar
 

ezraluandre

Trader
Jun 17, 2020
33
1
24
30
Hi guys, I create a basic EA to send a buy or sell signal depending where there is a reversal on parabolic SAR indicator. When I tried to use this code the result on strategy tester is not working,

MQL4:
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;

but when I use this code it is working

MQL4:
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;

the only difference between the first code and the second is that the second code using shift of 2 shift and 1 shift and where the first code using shift of 0 shift and 1 shift. Anyone knows why the second code is working while the first code isn't while the difference is only on the shift?

*if the code is working properly the result on strategy tester should like "sar with 2 & 1 shift" attachment. And "sar with 1 & 0 shift" attachment is the result of the first code.
 

Attachments

  • sar with 1 & 0 shift.PNG
    sar with 1 & 0 shift.PNG
    9.2 KB · Views: 5
  • sar with 2 & 1 shift.PNG
    sar with 2 & 1 shift.PNG
    14.5 KB · Views: 3
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
18,535
1,355
144
Odesa
www.earnforex.com
Your first variant (when using comparison of 0th and 1st bars) never works because the actual PSAR switch occurs on the current bar when its high or low exceeds the PSAR value. However, because you are checking your trading condition only on new bar, that PSAR reversal is always detected only on the 1st bar, which means that bar 0 and 1 both have PSAR on the same side.

You would notice this yourself if you ran this backtest in visual mode.

You can either ditch the new bar check or work with bars 1 and 2 (instead of 0 and 1) to detect reversals.