AM2
Андрей

 
Уровень 34

  Торгую в компаниях:


Группа "Программирование на MQL"

Рейтинг 280



Как написать стрелочный индикатор для МТ5

Терминал МТ5 набирает все большую популярность. Если буквально год назад, заказов на MQL5 было единицы, то в настоящее время их число растет. В этом топике рассмотрим создание стрелочного индикатора на MQL5. Когда я делал запрос в поиске на эту тему, то не нашел ни одной статьи.

На экран мы будем выводить простую МА и стрелки, когда меняется направление средней. Для этого нам понадобится 3 буфера и 3 графических построения:


#property indicator_buffers 3
#property indicator_plots 3


Определим параметры линии и стрелок:


#property indicator_label1  "MA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2

#property indicator_label2  "Signal UP"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrLime
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2

#property indicator_label3  "Signal DN"
#property indicator_type3   DRAW_ARROW
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  2


Зададим период МА, хендл индикатора и буферы:

input int MAPeriod=22;

int ind=0;
double ma[],up[],dn[];


Сопоставляем буферы данных с буфером индикатора:

int OnInit()
  {
//--- indicator buffers mapping

   SetIndexBuffer(0,ma,INDICATOR_DATA);
   
   SetIndexBuffer(1,up,INDICATOR_DATA);
   PlotIndexSetInteger(1,PLOT_ARROW,233);

   SetIndexBuffer(2,dn,INDICATOR_DATA);
   PlotIndexSetInteger(2,PLOT_ARROW,234);

   ind=iMA(NULL,0,MAPeriod,0,0,0);
//---
   return(INIT_SUCCEEDED);
  }


Заполнили данными буфер индикатора:


CopyBuffer(ind,0,0,rates_total-1,ma);


Проверили линию на смену направления:

   for(int i=0; i<=rates_total-1; i++)
     {
      if(ma[i]>ma[i+1] && ma[i+1]<ma[i+2])
        {
         up[i]=low[i];
        }
      if(ma[i]<ma[i+1] && ma[i+1]>ma[i+2])
        {
         dn[i]=high[i];
        }
     }


И наш индикатор готов!


//+------------------------------------------------------------------+
//|                                                      Pointer.mq5 |
//|                                              Copyright 2020, AM2 |
//|                                      http://www.forexsystems.biz |
//+------------------------------------------------------------------+

#property copyright "Copyright 2020, AM2"
#property link      "http://www.forexsystems.biz"
#property version   "1.00"
#property indicator_chart_window

#property indicator_buffers 3
#property indicator_plots 3

#property indicator_label1  "MA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2

#property indicator_label2  "Signal UP"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrLime
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2

#property indicator_label3  "Signal DN"
#property indicator_type3   DRAW_ARROW
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  2

input int MAPeriod=22;

int ind=0;
double ma[],up[],dn[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

   SetIndexBuffer(0,ma,INDICATOR_DATA);
   
   SetIndexBuffer(1,up,INDICATOR_DATA);
   PlotIndexSetInteger(1,PLOT_ARROW,233);

   SetIndexBuffer(2,dn,INDICATOR_DATA);
   PlotIndexSetInteger(2,PLOT_ARROW,234);

   ind=iMA(NULL,0,MAPeriod,0,0,0);
//---
   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[])
  {
//---
   CopyBuffer(ind,0,0,rates_total-1,ma);

   for(int i=0; i<=rates_total-1; i++)
     {
      if(ma[i]>ma[i+1] && ma[i+1]<ma[i+2])
        {
         up[i]=low[i];
        }
      if(ma[i]<ma[i+1] && ma[i+1]>ma[i+2])
        {
         dn[i]=high[i];
        }
     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+





Скачать индикатор: www.opentraders.ru/downloads/2625/
  • +6
  • Просмотров: 6004
  • 11 апреля 2020, 09:50
  • AM2
Понравилcя материал? Не забудьте поставить плюс и поделиться в социальной сети!

Вступите в группу "Программирование на MQL", чтобы следить за обновлениями
ПРИСОЕДИНИТЬСЯ К ГРУППЕ
присоединиться
  Предыдущая запись в группе
Пишем простейший цветной индикатор для МТ5
Следующая запись в группе  
Экспорт сигналов из МТ4 в Telegram
10 апреля 2020
25 ноября 2020

Комментарии (0)


Зарегистрируйтесь или авторизуйтесь, чтобы оставить комментарий
Начать торговлю с Альпари