simplezigzag_v1

Author: Copyright 2014, Andrey Litvichenko, MetaQuotes Software Corp.
0 Views
0 Downloads
0 Favorites
simplezigzag_v1
//+------------------------------------------------------------------+
//|                                                 simplezigzag.mq5 |
//|    Copyright 2014, Andrey Litvichenko, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, Andrey Litvichenko, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot ZZline
#property indicator_label1  "ZZline"
#property indicator_type1   DRAW_ZIGZAG
#property indicator_color1  clrTeal
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//+------------------------------------------------------------------+
//|  Structure declaration SDot | Îáúÿâëåíèå ñòðóêòóðû SDot          |
//+------------------------------------------------------------------+
struct SDot
  {
   double            price;
   datetime          time;
  };
//+-------------------------------------------------------------------------------------+
//|Enumeration declaration ETrendStatus | Îáúÿâëåíèå ïåðå÷èñëåíèÿ ETrendStatus          |
//+-------------------------------------------------------------------------------------+
enum ETrendStatus
  {
   up,
   down
  };
//--- input parameters
//---âõîäíûå ïàðàìåòðû
input int      min_delta=100;    //minimum range in points/ìèíèìàëüííûé äèàïàçîí â ïóíêòàõ
//--- indicator buffers
//--- îáúÿâëåíèå ìàññèâîâ èíäèêàòîðíûõ áóôôåðîâ
double         ZZHigh[];
double         ZZLow[];
//---declaration of variables
//---îáúÿâëåíèå ïåðåìåííûõ
double         delta;
SDot           last_dot;
ETrendStatus   state;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
//--- èíèöèàëèçàöèÿ èíäèêàòîðíûõ áóôôåðîâ
   SetIndexBuffer(0,ZZHigh,INDICATOR_DATA);
   SetIndexBuffer(1,ZZLow,INDICATOR_DATA);
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);
//--- installation indicator's short name
//--- óñòàíîâêà êîðîòêîãî èìåíè èíäèêàòîðà
   IndicatorSetString(INDICATOR_SHORTNAME,"SimpleZigZag"+"("+string(min_delta)+")");
//--- setting accuracy indicator
//--- óñòàíîâêà òî÷íîñòè èíäèêàòîðà
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//---initialize variables
//---èíèöèàëèçàöèÿ ïåðåìåííûõ
   delta=min_delta*_Point;
//---
   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[])
  {
//---declaration of variables
//---îáúÿâëåíèå ïåðåìåííûõ
   int         first;
   int         bar;

   if(prev_calculated>rates_total || prev_calculated<=0) // Checking the first start of the indicator calculation
     {                                                   // ïðîâåðêà íà ïåðâûé ñòàðò ðàñ÷åòà èíäèêàòîðà
      for(int i=0;i<rates_total;i++)
        {
         ZZHigh[bar]=0;
         ZZLow[bar]=0;
        }
      first=1;
      if(close[first]>open[first]) // initialize variable state|èíèöèàëèçàöèè ïåðåìåííîé state
         state=up;
      else
         state=down;
      last_dot.price=close[first];                       // initialize variable last_dot|èíèöèàëèçàöèè ïåðåìåííîé last_dot
      last_dot.time=time[first];
     }
   else
     {
      first=prev_calculated-1;                           // starting number for the calculation of new bars|ñòàðòîâûé íîìåð äëÿ ðàñ÷åòà íîâûõ áàðîâ
     }
   
   for(bar=first;bar<rates_total;bar++)//main loop calculation of the indicator|îñíîâíîé öèêë ðàñ÷åòà èíäèêàòîðà
     {
      if(bar==rates_total-1 && prev_calculated<rates_total)
        {
         ZZHigh[bar]=0;
         ZZLow[bar]=0;
        }
      switch(state)
        {
         case up:
           {
            if(high[bar]>last_dot.price)//check the trend continuation|ïðîâåðêà ïðîäîëæåíèÿ òðåíäà
              {
               if(time[bar]>last_dot.time)
                  ZZHigh[ArrayBsearch(time,last_dot.time)]=0;//removing unnecessary vertices|óäàëåíèå íåíóæíûõ âåðøèí 
               last_dot.price=high[bar];
               last_dot.time=time[bar];
               ZZHigh[bar]=high[bar];
              }
            if(low[bar]<last_dot.price-delta && (time[bar]>last_dot.time || high[bar-1]-low[bar]>delta))//check the appearance of the new knee|ïðîâåðêà ïîÿâëåíèÿ íîâîãî êîëåíà
              {
               last_dot.time=time[bar];
               last_dot.price=low[bar];
               state=down;
               ZZLow[bar]=low[bar];
               CheckInsideBar(bar,time,open,high,low,close);
              }
           }
         break;
         case down:
           {
            if(low[bar]<last_dot.price)//check the trend continuation|ïðîâåðêà ïðîäîëæåíèÿ òðåíäà
              {
               if(time[bar]>last_dot.time)
                  ZZLow[ArrayBsearch(time,last_dot.time)]=0;//removing unnecessary vertices|óäàëåíèå íåíóæíûõ âåðøèí 
               last_dot.price=low[bar];
               last_dot.time=time[bar];
               ZZLow[bar]=low[bar];
              }
            if(high[bar]>last_dot.price+delta && (time[bar]>last_dot.time || high[bar]-low[bar-1]>delta))//check the appearance of the new knee|ïðîâåðêà ïîÿâëåíèÿ íîâîãî êîëåíà
              {
               last_dot.time=time[bar];
               last_dot.price=high[bar];
               state=up;
               ZZHigh[bar]=high[bar];
               CheckInsideBar(bar,time,open,high,low,close);
              }
           }
         break;
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CheckInsideBar(int bar,
                    const datetime &time[],
                    const double &open[],
                    const double &high[],
                    const double &low[],
                    const double &close[])
  {
   if(high[bar-1]>high[bar] && low[bar-1]<low[bar])
     {
      switch(state)
        {
         case up:
           {
            if(low[bar]<high[bar]-delta)
              {
               last_dot.time=time[bar];
               last_dot.price=low[bar];
               state=down;
               ZZLow[bar]=low[bar];
              }
           }
         break;
         case down:
           {
            if(high[bar]>low[bar]+delta)
              {
               last_dot.time=time[bar];
               last_dot.price=high[bar];
               state=up;
               ZZHigh[bar]=high[bar];
              }
           }
         break;
        }
     }
  }
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---