error on a weekly array

 

HI all, I'm trying to attach this code to a daily chart, but it doesn't work.

The code returns a different value than the one I see if I apply ATR indicator on a weekly chart. 

Could you tell me where is my mistake? Thank you

//+------------------------------------------------------------------+ 
//| CHECK ATR                                                        | 
//+------------------------------------------------------------------+ 

int handle_ATR_W;
double ATR_W[];

int Barre_W=Bars(_Symbol,PERIOD_W1);
  
int OnInit() 
  {
  ArraySetAsSeries(ATR_W,true);
    
  handle_ATR_W = iATR(Symbol(),PERIOD_W1,4);
      
  return(INIT_SUCCEEDED); 
  }

  
void OnTick() 
  {
   
   CopyBuffer(handle_ATR_W,PERIOD_W1,0,Barre_W,ATR_W);

   Print(ATR_W[0]);
    

  } 
            

//+-------------------------------------+
//| Expert deinitialization function    |
//+-------------------------------------+

void OnDeinit(const int reason) 
     {
     //---
     }
 
Alberto Tortella:

HI all, I'm trying to attach this code to a daily chart, but it doesn't work.

The code returns a different value than the one I see if I apply ATR indicator on a weekly chart. 

Could you tell me where is my mistake? Thank you

Just wrap this in a custom function and call it from within OnTick()--don't forget to change the period to W1:

Forum on trading, automated trading systems and testing trading strategies

most efficient way to call daily ATR from lower timeframes?

Mladen Rakic, 2017.12.25 21:12

Using handles to indicators like that tends to be clumsy (what if we decide to change the time frame or the period "on the fly" - new handle? ... yeeah ...)

More or less mt5 is making us code a bit more to get better results - ATR is a simple case (excluding the time frame and the period, it is not so much lines of code after all, and allows us flexibility that handle usage would not let us). I did not bother with the case explanation when there is less data available than the ATR desired period+1 rates available in the target time frame data - that is the case that built in ATR does not solve intuitively, and this simple code does that better (at least that is my opinion ...). Making this a function is no big deal either and will work better, faster, and in more flexible way than the built in ATR ...

ENUM_TIMEFRAMES _atrTimeFrame = PERIOD_D1;
int             _atrPeriod    = 20;
double          _atrValue     = 0;
   MqlRates _rates[]; int _ratesCopied = CopyRates(_Symbol,_atrTimeFrame,0,_atrPeriod+1,_rates);
                      if (_ratesCopied>0)
                           for (int i=1; i<_ratesCopied; i++) _atrValue += MathMax(_rates[i].high,_rates[i-1].close)-MathMin(_rates[i].low,_rates[i-1].close);
                                                              _atrValue /= MathMax(_ratesCopied,1);

If you add inputs to your EA code, here's the custom function. Call it by putting iATRcustom() in OnTick() and it will return the ATR value. ATRtimeframe and ATRperiod are inputs.

//+------------------------------------------------------------------+
//| Custom iATR function                                             |
//+------------------------------------------------------------------+
double iATRcustom()
  {
   ENUM_TIMEFRAMES _atrTimeFrame = ATRtimeframe;
   int             _atrPeriod    = ATRperiod;
   double          _atrValue     = 0;
      MqlRates _rates[]; int _ratesCopied = CopyRates(_Symbol,_atrTimeFrame,0,_atrPeriod+1,_rates);
                         if (_ratesCopied>0)
                              for (int i=1; i<_ratesCopied; i++) _atrValue += MathMax(_rates[i].high,_rates[i-1].close)-MathMin(_rates[i].low,_rates[i-1].close);
                                                                 _atrValue /= MathMax(_ratesCopied,1);
   return _atrValue;
  }
 
Thank you very much!