Download MetaTrader 5
To add comments, please log in or register
Trade with MetaTarder 5. Download it right now!
Carl Schreiber
5938
Carl Schreiber 2014.08.29 15:01 

Hi

I modified the default ATR.mq4 just a bit (4 lines, marked green and: // NEW):

//+------------------------------------------------------------------+
//|                                                          ATR.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "https://www.mql4.com"
#property description "Average True Range"
#property strict

//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1  DodgerBlue
//--- input parameter
input int InpAtrPeriod=14; // ATR Period
//--- buffers
double ExtATRBuffer[];
double ExtTRBuffer[];
double LotValue;              //NEW
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   string short_name;
   LotValue = MarketInfo(Symbol(),MODE_TICKVALUE) / MarketInfo(Symbol(), MODE_TICKSIZE); //NEW
//--- 1 additional buffer used for counting.
   IndicatorBuffers(2);
   IndicatorDigits(3);
//--- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtATRBuffer);
   SetIndexBuffer(1,ExtTRBuffer);
//--- name for DataWindow and indicator subwindow label
   short_name="ATR$("+IntegerToString(InpAtrPeriod)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//--- check for input parameter
   if(InpAtrPeriod<=0)
     {
      Print("Wrong input parameter ATR Period=",InpAtrPeriod);
      return(INIT_FAILED);
     }
//---
   SetIndexDrawBegin(0,InpAtrPeriod);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Average True Range                                               |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   int i,limit;
//--- check for bars count and input parameter
   if(rates_total<=InpAtrPeriod || InpAtrPeriod<=0)
      return(0);
//--- counting from 0 to rates_total
   ArraySetAsSeries(ExtATRBuffer,false);
   ArraySetAsSeries(ExtTRBuffer,false);
   ArraySetAsSeries(open,false);
   ArraySetAsSeries(high,false);
   ArraySetAsSeries(low,false);
   ArraySetAsSeries(close,false);
//--- preliminary calculations
   if(prev_calculated==0)
     {
      ExtTRBuffer[0]=0.0;
      ExtATRBuffer[0]=0.0;
      //--- filling out the array of True Range values for each period
      for(i=1; i<rates_total; i++)
         ExtTRBuffer[i]=LotValue*(MathMax(high[i],close[i-1])-MathMin(low[i],close[i-1])); //  NEW: LotValue* 
      //--- first AtrPeriod values of the indicator are not calculated
      double firstValue=0.0;
      for(i=1; i<=InpAtrPeriod; i++)
        {
         ExtATRBuffer[i]=0.0;
         firstValue+=ExtTRBuffer[i];
        }
      //--- calculating the first value of the indicator
      firstValue/=InpAtrPeriod;
      ExtATRBuffer[InpAtrPeriod]=firstValue;
      limit=InpAtrPeriod+1;
     }
   else
      limit=prev_calculated-1;
//--- the main loop of calculations
   for(i=limit; i<rates_total; i++)
     {
      ExtTRBuffer[i]=LotValue*(MathMax(high[i],close[i-1])-MathMin(low[i],close[i-1]));  //  NEW: LotValue*  
      ExtATRBuffer[i]=ExtATRBuffer[i-1]+(ExtTRBuffer[i]-ExtTRBuffer[i-InpAtrPeriod])/InpAtrPeriod;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


The Buffer-Results are only multiplied by LotValue. (This way I see how much money I may be able to make)

Now I apply this indi to the chart twice: 1) InpAtrPeriod=14 and 2) 1) InpAtrPeriod=7 in the same sub-window.

Now after some time both ATR$ aren't painted and the values in the MarketWatch are 0.00.

Only after I open the parameter-window of each of them and press ok, they are painted again. In the expert tab I don't see anything than:

2014.08.29 16:49:09.685 ATR$ GBPUSD,M15: initialized
2014.08.29 16:49:09.595 ATR$ GBPUSD,M15: uninit reason 5
2014.08.29 16:47:28.596 ATR$ GBPUSD,M15: initialized
2014.08.29 16:47:28.496 ATR$ GBPUSD,M15: uninit reason 5

No error, no warning, just the inf. they have been initialized and then after a while both disappear?

Any idea?

Beside that, despite the fact mt4 offers to apply e.g. both ATR$ in the same window it can happen that the ATR$(7) is painted above the ATR$(14) but the value of ATR(7) is lower than the ATR$(14)!!

I mentioned that at the service desk - no reaction, it seems to be a feature not a bug.

Gooly

Alain Verleyen
Moderator
28537
Alain Verleyen 2014.08.29 23:22  
/
To add comments, please log in or register