sar strategy only return sell

 

Hi everyone, I had a test with SAR indicator. Currently I want to test about SAR position on the chart. This is my code:

//+------------------------------------------------------------------+
//|                                                         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);
  }

bool BUYCondition = false;
bool SELLCondition = false;
void CheckOpenPosition(int shift=0)
  {
   if(sar(shift)<Bid) BUYCondition = true;
   if(sar(shift)>Bid) SELLCondition = true;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {

   datetime some_time=D'2021.11.02 08:30';
   int      shift=iBarShift(Symbol(),PERIOD_CURRENT,some_time);

   CheckOpenPosition(shift);
   if(BUYCondition)
      Alert("BUY true");
   if(SELLCondition)
      Alert("SELL true");
   
//---
   return(INIT_SUCCEEDED);
  }

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

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

  }

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

The problem is every position of SAR indicator it always return SELL.

First I thought that because of CheckOpenPosition function that I wrote but when I'm not using that function the problem still occur. 

Second I thought about the tick data that I used so instead using data from the tick data that I download so I tried to use data outside of that downloaded data and still the result is the same.

if anyone knows why this problem occur please kindly help me.

 
Luandre Ezra: I want to test about SAR position on the chart. 
  1. int OnInit()
      {
       ⋮
       CheckOpenPosition(…

    Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  2. Once you set those variables to true, they will forever be true.
       if(sar(shift)<Bid) BUYCondition = true;
       if(sar(shift)>Bid) SELLCondition = true;
    Fix and simplify
    BUYCondition = sar(shift)<Bid;
    SELLCondition = !BUYCondition;

 

Hi, I already applying what you suggest. I change the code from OnInit to OnTick and adding newBar func to easily debugging,

void OnTick()
  {
   bool BUYCondition = sar()<Bid;
   bool SELLCondition = !BUYCondition;

   static datetime preTime=0,curTime;
   bool newBar=false;
   curTime=Time[0];
   if(preTime!=curTime)
     {
      preTime=curTime;
      newBar=true;
     }

   if(newBar && BUYCondition)
      Alert("BUY true");
   if(newBar && SELLCondition)
      Alert("SELL true");

  }

the problem when only return SELL is true is no more but according to the Alert from strategy tester the result is somewhat lagging by a bar. When the chart on 2020.01.03 12:30 should be BUY the Alert still recognize it with SELL (lagging one bar, image attach). Do you know why this is happened?

Files:
Capture_1.PNG  14 kb
Capture_2.PNG  18 kb
 
Luandre Ezra:

Hi everyone, I had a test with SAR indicator. Currently I want to test about SAR position on the chart. This is my code:

The problem is every position of SAR indicator it always return SELL.

First I thought that because of CheckOpenPosition function that I wrote but when I'm not using that function the problem still occur. 

Second I thought about the tick data that I used so instead using data from the tick data that I download so I tried to use data outside of that downloaded data and still the result is the same.

if anyone knows why this problem occur please kindly help me.

      double InpSARStep=0.0002;    // Step
      double InpSARMaximum=0.2;  // Maximum
      double SarsDeger1=iSAR(NULL,0,InpSARStep,InpSARMaximum,1);
      double SarsDeger2=iSAR(NULL,0,InpSARStep,InpSARMaximum,2);
      double SarsFark=((SarsDeger1-SarsDeger2))/Point;
      
      if(SarsDeger1>SarsDeger2 && SarsFark> 80) Alert("BUY");
      if(SarsDeger1<SarsDeger2 && SarsFark> 80) Alert("SELL");
      
 
if(SarsDeger1>SarsDeger2 

Print out your variables and you will find out why.

 
William Roeder #:

Print out your variables and you will find out why.

xddddd


The difference between the two sars.

 
double InpSARStep=0.0002;    // Step
double InpSARMaximum=0.2;  // Maximum
double SarsDeger1=iSAR(NULL,0,InpSARStep,InpSARMaximum,1);
double SarsDeger2=iSAR(NULL,0,InpSARStep,InpSARMaximum,2);
double SarsFark=((SarsDeger1-SarsDeger2))/Point;
      
if(SarsDeger1>SarsDeger2 && SarsFark> 80) Alert("BUY");
if(SarsDeger1<SarsDeger2 && SarsFark> 80) Alert("SELL");

Sorry I don't really understand what you want to tell me about this code. My problem lies in the alert result of the SAR indi position. The problem of the SAR indi position is already resolved but still there's a problem where the result is lagging with 1 bar.