Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1541

 
Artyom Trishkin #:
What's the point? What's the point?

I'm telling you, I wrote it in my sleep. )) Corrected.

Regards, Vladimir.

 

Corrected version:

//+------------------------------------------------------------------+
//| Input variables                                                  |
//+------------------------------------------------------------------+
input ushort Averaging_Period=14; // Период усреднения индикатора ATR

int Handle_ATR;
double atr_array_value[]; // создадим массив для значений индикатора ATR
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   Handle_ATR=iATR(_Symbol, PERIOD_CURRENT, Averaging_Period); // получим хэндл индикатора ATR
   if(Handle_ATR==INVALID_HANDLE)
     {
      Print("Некорректный хэндл индикатора ATR!");
      return(INIT_FAILED);
     }
   ArraySetAsSeries(atr_array_value, true); // зададим индексацию элементов массива, как в таймсериях
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(Handle_ATR);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   int copy_buffer = CopyBuffer(Handle_ATR, 0, 0, 3, atr_array_value); // копируем в массив данные указанного буфера индикатора ATR
   if(copy_buffer > 0) // если скопированные данные больше нуля
      Print("Значение ATR = ", DoubleToString(atr_array_value[0], 5)); // выведем на печать
  }
//+------------------------------------------------------------------+

With respect, Vladimir.

 

Isn't it true that SYMBOL_PRICE_VOLATILITY and ATR are different things in general?

like in school, check what is measured and what is counted from....


the first one is a reference parameter from the specification (or translated by DC), not related to timeframes and measured in %.

the second is statistical for an arbitrary period of a specific timeframe, measured in quote units.


for familiar Forex SYMBOL_PRICE_VOLATILITY is not used and is not broadcasted. But we have not only Forex here.

For example, exchange instruments have a similar thing in their specifications and general conditions - the upper limit of price change per session, after which trading will be automatically suspended. It is large, rare, but it is there

 
Maxim Kuznetsov #:
but is it ok that SYMBOL_PRICE_VOLATILITY and ATR are different things in general ?

Maxim, it's good that you reacted to this topic. Frankly speaking, I am far away from the volatility topic and I still can't understand why this volatility is needed and why this topic is raised many times but is not fully disclosed. The only thing I have understood is that there is supposedly a dependence of price chart reversal at a significant increase in volatility. Besides, I have read several topics related to the application of the ATR volatility indicator, but having installed it on the chart, I have not seen anything so valuable. Could you tell me in a concise form what is so valuable for a trader? I would be very grateful.

Regards, Vladimir.

 
MrBrooklin #:
The only thing I understood was that there is supposedly a dependence of price chart reversal at a significant increase in volatility. Besides, I have read several topics related to the application of the ATR volatility indicator, but having installed it on the chart, I have not seen anything so valuable. Could you tell me in a concise form what is so valuable for a trader? I would be very grateful.

If you write a multi-volume book about Forex (or about TA in general), 2/3 of volumes will be about volatility in one way or another :-)

The simplest practical application of ATR is to add the "shift" parameter to the code you have given, because the "shift" parameter will be added to the code.

And everything will be the same as in the old days:
Night, the icy ripples of the canal,
The pharmacy, thestreet, thestreetlight.

The market is cyclical, periodic and the ATR (average true range, amplitude of volatility) repeats itself time after time.

Intraday: shift your indicator reading to the right by PeriodSeconds(PERIOD_D1)/PeriodSeconds(PERIOD_CURRENT)-ATR_PERIOD/2. That is, by the number of bars for 1 day minus the "delay" from ATR averages.

get the average range (distance from Open to the farthest high/low, price spread) as it was yesterday at the same time. Now you can compare the current candlestick size with it and draw conclusions whether trading is more active or vice versa. It is already good

In passing, purely technical: in 99% of cases in TA we use window functions, i.e. averages, comparisons for some period. They all fail in three cases: when small values within windows are almost identical, when values at the ends of windows are too different, and when values are large but "all over the place". This roughly correlates with small and significant ATR and its reversals. That is, when the range is small, the indicators are too sensitive and react to the slightest noise, and when it rises sharply (or falls sharply), it is also nonsense

you can go on and on... :-)

 
Maxim Kuznetsov #:
Within a day: shift your indicator readings to the right by PeriodSeconds(PERIOD_D1)/PeriodSeconds(PERIOD_CURRENT)-ATR_PERIOD/2. That is, by the number of bars for 1 day minus the "delay" from ATR averages ...

Thank you, Maxim, for your attention and answer! Now I have something to think about.

Regards, Vladimir.

 
MrBrooklin #:

Corrected version:

With respect, Vladimir.

Thank you. However, no one has answered what the function of the indicator with the number of periods 0 produces and why only 3 elements are copied and how, if necessary, and whether it is necessary to copy all of them:
int copy_buffer = CopyBuffer(Handle_ATR, 0, 0, 3, atr_array_value);

Such a case...

I must say that I have become more aware of the essence of volatility. I used to think it was the deviation of prices from the average price. But both ATR and theory say it's a corridor of prices.
 
maxvoronin74 #:
Thank you. However, no one has answered what the function of the indicator with the number of periods 0 produces and why only 3 elements are copied and how to copy all of them, if necessary, and whether it is necessary:

Such a case...

I must say that I have become more aware of the essence of volatility. I used to think it was the deviation of prices from the average price. But both ATR and theory say it's a corridor of prices.

It doesn't give anything away. The minimum can be 1:

//+------------------------------------------------------------------+
//| 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[])
  {
   if(rates_total<=ExtPeriodATR)
      return(0);

   int i,start;
//--- 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 && !IsStopped(); i++)
         ExtTRBuffer[i]=MathMax(high[i],close[i-1])-MathMin(low[i],close[i-1]);
      //--- first AtrPeriod values of the indicator are not calculated
      double firstValue=0.0;
      for(i=1; i<=ExtPeriodATR; i++)
        {
         ExtATRBuffer[i]=0.0;
         firstValue+=ExtTRBuffer[i];
        }
      //--- calculating the first value of the indicator
      firstValue/=ExtPeriodATR;
      ExtATRBuffer[ExtPeriodATR]=firstValue;
      start=ExtPeriodATR+1;
     }
   else
      start=prev_calculated-1;
//--- the main loop of calculations
   for(i=start; i<rates_total && !IsStopped(); i++)
     {
      ExtTRBuffer[i]=MathMax(high[i],close[i-1])-MathMin(low[i],close[i-1]);
      ExtATRBuffer[i]=ExtATRBuffer[i-1]+(ExtTRBuffer[i]-ExtTRBuffer[i-ExtPeriodATR])/ExtPeriodATR;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }


If you enter a value less than 1, it will default to 14:

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- check for input value
   if(InpAtrPeriod<=0)
     {
      ExtPeriodATR=14;
      PrintFormat("Incorrect input parameter InpAtrPeriod = %d. Indicator will use value %d for calculations.",InpAtrPeriod,ExtPeriodATR);
     }
   else
      ExtPeriodATR=InpAtrPeriod;
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtATRBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtTRBuffer,INDICATOR_CALCULATIONS);
//---
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpAtrPeriod);
//--- name for DataWindow and indicator subwindow label
   string short_name=StringFormat("ATR(%d)",ExtPeriodATR);
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
   PlotIndexSetString(0,PLOT_LABEL,short_name);
  }


This is how it is done in the indicator. Of course, you can do anything you want, but a period less than 1 will not give anything in the calculations.

Why do you need a zero calculation period? What is the point?

 
Artyom Trishkin #:

No output. The minimum can be 1:

If you enter a value less than 1, the default value of 14 will be set:

Thank you. Now it is clear.

Artyom Trishkin #:

Why do you need a zero calculation period? What is the point?

I was wondering how to get the volatility of the current candle using the indicator's standard tools.

As for the second part of the question,"why only 3 elements are copied and how, if necessary, and whether it is necessary to copy all of them", is there any explanation?
 
maxvoronin74 #:

Thank you. I see now.

I was wondering how to get the volatility of the current candle using the indicator's standard tools.

As for the second part of the question,"why only 3 elements are copied and how, if necessary, and whether it is necessary to copy all of them", is there any explanation?
The person wanted to show in the example how to copy three bars. You can copy as many bars as you need. But for an Expert Advisor, as well as usually, it is enough to know the current volatility. This is only one zero, current bar.