Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
0 Views
0 Downloads
0 Favorites
GapFinder
ÿþ//+------------------------------------------------------------------+

//|                                                    GapFinder.mq5 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                                                 https://mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2018, MetaQuotes Software Corp."

#property link      "https://mql5.com"

#property version   "1.00"

#property indicator_chart_window

#property indicator_buffers 6

#property indicator_plots   6

//--- plot LineToUpUP

#property indicator_label1  "Upper up area"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrDodgerBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot LineToUpDN

#property indicator_label2  "Lower up area"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrDarkOrange

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot LineToDnUP

#property indicator_label3  "Upper down area"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrDodgerBlue

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot LineToDnDN

#property indicator_label4  "Lower down area"

#property indicator_type4   DRAW_LINE

#property indicator_color4  clrDarkOrange

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1



//--- plot arrow up

#property indicator_label5  "Arrow up"

#property indicator_type5   DRAW_ARROW

#property indicator_color5  clrDodgerBlue

#property indicator_style5  STYLE_SOLID

#property indicator_width5  1

//--- plot arrow down

#property indicator_label6  "Arrow down"

#property indicator_type6   DRAW_ARROW

#property indicator_color6  clrRed

#property indicator_style6  STYLE_SOLID

#property indicator_width6  1

//--- enums

enum ENUM_INPUT_YES_NO

  {

   INPUT_YES   =  1, // Yes

   INPUT_NO    =  0  // No

  };

//--- input parameters

input uint              InpMinGapSize  =  2;                   // Minimum gap size (in points)

input ENUM_INPUT_YES_NO InpDrawArea    =  INPUT_YES;           // Whether to draw a gap area

input color             InpColorToUP   =  clrMediumTurquoise;  // Color of the gap area up

input color             InpColorToDN   =  clrGold;             // Color of the gap area down

//--- indicator buffers

double                  BufferLineToUpUP[];

double                  BufferLineToUpDN[];

double                  BufferLineToDnUP[];

double                  BufferLineToDnDN[];

double                  BufferArrowUP[];

double                  BufferArrowDN[];

//--- global variables

double                  min_gap_size;

string                  prefix;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- set global variables

   min_gap_size=(InpMinGapSize<1 ? 1 : InpMinGapSize)*Point();

   prefix=MQLInfoString(MQL_PROGRAM_NAME)+"_";

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferLineToUpUP,INDICATOR_DATA);

   SetIndexBuffer(1,BufferLineToUpDN,INDICATOR_DATA);

   SetIndexBuffer(2,BufferLineToDnUP,INDICATOR_DATA);

   SetIndexBuffer(3,BufferLineToDnDN,INDICATOR_DATA);

   SetIndexBuffer(4,BufferArrowUP,INDICATOR_DATA);

   SetIndexBuffer(5,BufferArrowDN,INDICATOR_DATA);

//--- setting a code from the Wingdings charset as the property of PLOT_ARROW

   PlotIndexSetInteger(4,PLOT_ARROW,241);

   PlotIndexSetInteger(5,PLOT_ARROW,242);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Gap finder("+(string)min_gap_size+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferLineToUpUP,true);

   ArraySetAsSeries(BufferLineToUpDN,true);

   ArraySetAsSeries(BufferLineToDnUP,true);

   ArraySetAsSeries(BufferLineToDnDN,true);

   ArraySetAsSeries(BufferArrowUP,true);

   ArraySetAsSeries(BufferArrowDN,true);

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator deinitialization function                       |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

   ObjectsDeleteAll(0,prefix);

   ChartRedraw();

  }

//+------------------------------------------------------------------+

//| 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[])

  {

//--- @>25@:0 =0 <8=8<0;L=>5 :>;85AB2> 10@>2 4;O @0AGQB0

   if(rates_total<4) return 0;

//--- #AB0=>2:0 <0AA82>2 1CD5@>2 :0: B09<A5@89

   ArraySetAsSeries(open,true);

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(close,true);

   ArraySetAsSeries(time,true);

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-5;

      BuffersInitialize();

     }

//---  0AGQB 8=48:0B>@0

   for(int i=limit; i>=0 && !IsStopped(); i--)

     {

   //--- M? 225@E

      if(low[i]-high[i+1]>=min_gap_size)

        {

         BufferArrowUP[i]=high[i+1];

         //--- A;8 =0 ?@>H;>< 10@5 5ABL AB@5;:0

         if(BufferArrowUP[i+1]!=EMPTY_VALUE)

           {

            double up=fmin(open[i+1],close[i+1]);

            double dn=fmax(open[i+2],close[i+2]);

            //--- !B5@5BL ;8=88 A>A54=53> 3M?0

            SetGapToUP(0,0,i+2);

           }

         //--- K25AB8 3M?

         double up=fmin(open[i],close[i]);

         double dn=fmax(open[i+1],close[i+1]);

         SetGapToUP(up,dn,i);

         DrawArea(i,up,dn,time,InpColorToUP,1);

        }

      else

        {

         BufferArrowUP[i]=EMPTY_VALUE;

         if(BufferArrowUP[i+1]==EMPTY_VALUE)

            SetGapToUP(0,0,i);

        }

   //--- M? 2=87

      if(low[i+1]-high[i]>=min_gap_size)

        {

         BufferArrowDN[i]=low[i+1];

         //--- A;8 =0 ?@>H;>< 10@5 5ABL AB@5;:0

         if(BufferArrowDN[i+1]!=EMPTY_VALUE)

           {

            double up=fmin(open[i+2],close[i+2]);

            double dn=fmax(open[i+1],close[i+1]);

            //--- !B5@5BL ;8=88 A>A54=53> 3M?0

            SetGapToDN(0,0,i+2);

           }

         //--- K25AB8 3M?

         double up=fmin(open[i+1],close[i+1]);

         double dn=fmax(open[i],close[i]);

         SetGapToDN(up,dn,i);

         DrawArea(i,up,dn,time,InpColorToDN,0);

        }

      else

        {

         BufferArrowDN[i]=EMPTY_VALUE;

         if(BufferArrowDN[i+1]==EMPTY_VALUE)

            SetGapToDN(0,0,i);

        }

     }

   

//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+

//| =8F80;870F8O 1CD5@>2                                            |

//+------------------------------------------------------------------+

void BuffersInitialize(void)

  {

   ArrayInitialize(BufferLineToUpUP,EMPTY_VALUE);

   ArrayInitialize(BufferLineToUpDN,EMPTY_VALUE);

   ArrayInitialize(BufferLineToDnUP,EMPTY_VALUE);

   ArrayInitialize(BufferLineToDnDN,EMPTY_VALUE);

   ArrayInitialize(BufferArrowUP,EMPTY_VALUE);

   ArrayInitialize(BufferArrowDN,EMPTY_VALUE);

  }

//+------------------------------------------------------------------+

//| #AB0=02;8205B 7=0G5=8O "3M? 225@E"                               |

//+------------------------------------------------------------------+

void SetGapToUP(const double price_up,const double price_dn,const int shift)

  {

   BufferLineToUpUP[shift]=BufferLineToUpUP[shift+1]=(price_up>0 ? price_up : EMPTY_VALUE);

   BufferLineToUpDN[shift]=BufferLineToUpDN[shift+1]=(price_dn>0 ? price_dn : EMPTY_VALUE);

  }

//+------------------------------------------------------------------+

//| #AB0=02;8205B 7=0G5=8O "3M? 2=87"                                |

//+------------------------------------------------------------------+

void SetGapToDN(const double price_up,const double price_dn,const int shift)

  {

   BufferLineToDnUP[shift]=BufferLineToDnUP[shift+1]=(price_up>0 ? price_up : EMPTY_VALUE);

   BufferLineToDnDN[shift]=BufferLineToDnDN[shift+1]=(price_dn>0 ? price_dn : EMPTY_VALUE);

  }

//+------------------------------------------------------------------+

//|  8AC5B >1;0ABL                                                   |

//+------------------------------------------------------------------+

void DrawArea(const int index, const double price_up,const double price_dn,const datetime &time[],const color color_area,const char dir)

  {

   if(!InpDrawArea) return;

   string name=prefix+(dir>0 ? "up_" : "dn_")+TimeToString(time[index]);

   if(ObjectFind(0,name)<0)

      ObjectCreate(0,name,OBJ_RECTANGLE,0,0,0,0);

   ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);

   ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);

   ObjectSetInteger(0,name,OBJPROP_FILL,true);

   ObjectSetInteger(0,name,OBJPROP_BACK,true);

   ObjectSetString(0,name,OBJPROP_TOOLTIP,"\n");

   //---

   ObjectSetInteger(0,name,OBJPROP_COLOR,color_area);

   ObjectSetInteger(0,name,OBJPROP_TIME,0,time[index+1]);

   ObjectSetInteger(0,name,OBJPROP_TIME,1,time[index]);

   ObjectSetDouble(0,name,OBJPROP_PRICE,0,price_up);

   ObjectSetDouble(0,name,OBJPROP_PRICE,1,price_dn);

  }

//+------------------------------------------------------------------+

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 ---