Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1161

 
KolyanSNA:
Hello! How do I change the font size in the terminal? Can someone give me a hint?

It may help to change the zoom in the OS display settings if you have such a need.

 

Is there any way to determine from the OnChartEvent whether a button was pressed on the keyboard when the mouse clicked on the graphic?

 

Please help me to understand the indicators.

Here, at the opening of each new bar there should be a recalculation.

Logic - ZigZag should connect bottoms (low of last 3 bars below two adjacent ones) and tops (high of last 3 bars above two adjacent ones).

If a peak is followed by a new one, the indicator is redrawn on the new one, the old one is deleted, I think it's clear.

In fact, the indicator connects all highs and lows sequentially, what is the problem?

#property indicator_chart_window
#property indicator_buffers 2 
#property indicator_plots   1 
//--- plot ZigZag 
#property indicator_label1  "ZigZag" 
#property indicator_type1   DRAW_ZIGZAG 
#property indicator_color1  clrBlue 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- indicator buffers 
double         ZigZagBuffer1[]; 
double         ZigZagBuffer2[]; 
double a = 0;
int last = 0; //В последний раз была вершина или низина 
int lastN = 0; //Номер бара с экстремумом
//+------------------------------------------------------------------+ 
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- связывание массивов и индикаторных буферов 
   SetIndexBuffer(0,ZigZagBuffer1,INDICATOR_DATA); 
   SetIndexBuffer(1,ZigZagBuffer2,INDICATOR_DATA); 
//--- значение 0 (пустое значение) не будет участвовать в отрисовке 
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0); 
   ArrayInitialize(ZigZagBuffer1, EMPTY_VALUE);
   ArrayInitialize(ZigZagBuffer2, EMPTY_VALUE);
//--- значение 0 (пустое значение) не будет участвовать в отрисовке 
   PlotIndexSetString(0,PLOT_LABEL,"ZigZag1;ZigZag2"); 
//--- 
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| Custom indicator iteration function                              | 
//+------------------------------------------------------------------+ 
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(a != iOpen(_Symbol, PERIOD_CURRENT, 0))
   {
      a = iOpen(_Symbol, PERIOD_CURRENT, 0);
      for(int i = Bars(_Symbol, PERIOD_CURRENT) - 3; i > 0; i--)
      {
         if(high[i] <= high[i+1] && high[i+2] <= high[i+1] && low[i] >= low[i+1] && low[i+2] >= low[i+1]) //В обе стороны на одном баре
         {
            if(last == 0) continue; //Если с этого начинается расчет - пропускаем просто
            if(last == 1) //Если была вершина
            {
               if(high[lastN] > high[i+1])//Если новая верщина не перебила старую
               {
                  ZigZagBuffer1[i+1] = high[i+1];
                  ZigZagBuffer2[i+1] = low[i+1];
                  lastN = i+1;
                  continue;
               }
               //Если перебила
               ZigZagBuffer1[lastN] = EMPTY_VALUE;
               ZigZagBuffer1[i+1] = high[i+1];
               ZigZagBuffer2[i+1] = low[i+1];
               last = -1;
               lastN = i+1;
               continue;
            }
            if(last == -1)
            {
               if(low[lastN] < low[i+1])//Если новая низина не перебила старую
               {
                  ZigZagBuffer1[i+1] = high[i+1];
                  ZigZagBuffer2[i+1] = low[i+1];
                  lastN = i+1;
                  continue;
               }
               //Если перебила
               ZigZagBuffer2[lastN] = EMPTY_VALUE;
               ZigZagBuffer1[i+1] = high[i+1];
               ZigZagBuffer2[i+1] = low[i+1];
               last = 1;
               lastN = i+1;
               continue;
            }
         }
         if(high[i] <= high[i+1] && high[i+2] <= high[i+1]) //Новая вершина
         {
            if(last == 0)
            {
               last = 1;
               lastN = i+1;
               ZigZagBuffer1[i+1] = high[i+1];
               continue;
            }
            if(last == 1)
            {
               ZigZagBuffer1[lastN] = EMPTY_VALUE;
               ZigZagBuffer1[i+1] = high[i+1];
               lastN = i+1;
               continue;
            }
            if(last == -1)
            {
               ZigZagBuffer1[i+1] = high[i+1];
               lastN = i+1;
               last = 1;
               continue;
            }
         }
         if(low[i] >= low[i+1] && low[i+2] >= low[i+1])//Новая низина
         {
            if(last == 0)
            {
               last = -1;
               lastN = i+1;
               ZigZagBuffer2[i+1] = low[i+1];
               continue;
            }
            if(last == -1)
            {
               ZigZagBuffer2[lastN] = EMPTY_VALUE;
               ZigZagBuffer2[i+1] = low[i+1];
               lastN = i+1;
               continue;
            }
            if(last == 1)
            {
               ZigZagBuffer2[i+1] = low[i+1];
               lastN = i+1;
               last = -1;
               continue;
            }
         }
      }
   }
   
   return(rates_total); 
} 
Обработчик события "новый бар"
Обработчик события "новый бар"
  • www.mql5.com
Для создателей индикаторов и экспертов всегда был актуален вопрос написания экономичного кода с точки зрения времени выполнения. Можно подойти к решению этой задачи с разных сторон. Из этой обширной темы в данной статье будет затронут, казалось бы уже решенный вопрос: проверка появления нового бара. Это достаточно популярный способ ограничения...
 
Roman Sharanov:

Please help me to understand the indicators.

Here, at the opening of each new bar there should be a recalculation.

Logic - ZigZag should connect bottoms (low of last 3 bars below two adjacent ones) and tops (high of last 3 bars above two adjacent ones).

If a peak is followed by a new one, the indicator is redrawn on the new one, the old one is deleted, I think it's clear.

In fact, the indicator connects all HI and LH in series, what is the problem?

As far as I understand you have 2 drawing buffers. Both of them are drawn. Each buffer has its own line, you get one extremum. There are 3 buffers in the zigzag. Two calculated ones for maxima and minima separately, and one for drawing according to the sign that after the maxima have been written into the buffer, we are looking for the minimum and vice versa.

 
Question, is there a description of the standard libraries for MT4 or not?
 
Valeriy Yastremskiy:

I understand you have two drawing buffers. Both are drawn. Each buffer has its own line, you get one extreme. There are 3 buffers in the zigzag. Two calculation buffers are separate for maxima and minima, and one buffer is used for drawing following the last maximum written into the maxima buffer, and vice versa.

Help - zigzag is built using 2 buffers, example with 2 buffers too

 
Valeriy Yastremskiy:
Question, is there a description of the standard libraries for MT4 or not?
Документация по MQL5: Стандартная библиотека
Документация по MQL5: Стандартная библиотека
  • www.mql5.com
Стандартная библиотека MQL5 написана на языке MQL5 и предназначена для облегчения написания программ (индикаторов, скриптов, экспертов) конечным пользователям. Библиотека обеспечивает удобный доступ к большинству внутренних функций MQL5.
 
Artyom Trishkin:

MT4.

Kudos for the articles. Great work.

 
Roman Sharanov:

Help - zigzag is built over 2 buffers, example with 2 buffers too

Start of ZZ code.

//+------------------------------------------------------------------+
//|                                                       ZigZag.mq4 |
//|                   Copyright 2006-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "2006-2014, MetaQuotes Software Corp."
#property link      "http://www.mql4.com"
#property strict

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1  Red
//---- indicator parameters
input int InpDepth=12;     // Depth
input int InpDeviation=5;  // Deviation
input int InpBackstep=3;   // Backstep
//---- indicator buffers
double ExtZigzagBuffer[];
double ExtHighBuffer[];
double ExtLowBuffer[];

One drawing buffer. Three arrays.

Look at the code. The logic is complex. It's usually hard to do on the fly.

ZigZag code with translated comments into Russian
Files:
ZigZagRu.mq4  19 kb
 
Valeriy Yastremskiy:

MT4.

Kudos for the articles. Great work.

Doesn't matter - almost everything fits.

Thank you.

Reason: