PinBarTracker

Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
0 Views
0 Downloads
0 Favorites
PinBarTracker
ÿþ//+------------------------------------------------------------------+

//|                                                PinBarTracker.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 description "Pin Bar Tracker indicator"

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_plots   2

//--- plot PinUP

#property indicator_label1  "Upper Pin Bar"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot PinDN

#property indicator_label2  "Lower Pin Bar"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- enums

enum ENUM_INPUT_YES_NO

  {

   INPUT_YES   =  1, // Yes

   INPUT_NO    =  0  // No

  };

//--- input parameters

input uint              InpDepth       =  70;         // Depth of pinbars in percents

input uint              InpMaxRange    =  1000;       // Maximum range

input uint              InpMinRange    =  5;          // Minimum range

input ENUM_INPUT_YES_NO InpExtremum    =  INPUT_YES;  // Extremums only

input ENUM_INPUT_YES_NO InpUseAlerts   =  INPUT_YES;  // Use alerts

input ENUM_INPUT_YES_NO InpSendMail    =  INPUT_NO;   // Send mail

input ENUM_INPUT_YES_NO InpSendPush    =  INPUT_YES;  // Send push-notifications

//--- indicator buffers

double         BufferPinUP[];

double         BufferPinDN[];

//--- global variables

double         points;

int            digits;

int            depth_prc;

int            range_max;

int            range_min;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   depth_prc=int(InpDepth<1 ? 1 : InpDepth);

   range_max=int(InpMaxRange<1 ? 1 : InpMaxRange);

   range_min=int((int)InpMinRange>range_max ? range_max : InpMinRange);

   points=(Point()<0.001 ? 0.0001 : 0.01);

   digits=(int)SymbolInfoInteger(NULL,SYMBOL_DIGITS);

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferPinUP,INDICATOR_DATA);

   SetIndexBuffer(1,BufferPinDN,INDICATOR_DATA);

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

   PlotIndexSetInteger(0,PLOT_ARROW,159);

   PlotIndexSetInteger(1,PLOT_ARROW,159);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"PinBar Tracker ("+(string)depth_prc+","+(string)range_max+","+(string)range_min+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferPinUP,true);

   ArraySetAsSeries(BufferPinDN,true);

//---

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

  {

//--- #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 :>;8G5AB20 4>ABC?=KE 10@>2

   if(rates_total<4) return 0;

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

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-1;

      ArrayInitialize(BufferPinUP,EMPTY_VALUE);

      ArrayInitialize(BufferPinDN,EMPTY_VALUE);

     }



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

   static datetime last_time=0;

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

     {

      if(PinUp(time[i],open[i],high[i],low[i],close[i],depth_prc))

        {

         BufferPinUP[i]=high[i];

         if(i==0 && last_time!=time[0])

           {

            string message=Symbol()+", "+TimeframeToString(Period())+": Upper Pin Bar at "+TimeToString(TimeCurrent());

            if(InpUseAlerts) Alert(message);

            if(InpSendMail  && TerminalInfoInteger(TERMINAL_EMAIL_ENABLED)) SendMail("PinBar Tracker Signal",message);

            if(InpSendPush && TerminalInfoInteger(TERMINAL_NOTIFICATIONS_ENABLED)) SendNotification(message);

            last_time=time[0];

           }

        }

      if(PinDown(time[i],open[i],high[i],low[i],close[i],depth_prc))

        {

         BufferPinDN[i]=low[i];

         if(i==0 && last_time!=time[0])

           {

            string message=Symbol()+", "+TimeframeToString(Period())+": Lower Pin Bar at "+TimeToString(TimeCurrent());

            if(InpUseAlerts) Alert(message);

            if(InpSendMail  && TerminalInfoInteger(TERMINAL_EMAIL_ENABLED)) SendMail("PinBar Tracker Signal",message);

            if(InpSendPush && TerminalInfoInteger(TERMINAL_NOTIFICATIONS_ENABLED)) SendNotification(message);

            last_time=time[0];

           }

        }

     }



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

   return(rates_total);

  }

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

//|                                                                  |

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

bool PinUp(const datetime time,const double open,const double high,const double low,const double close,const int depth)

  {

   int range=(int)round((high-low)/points);

   int zone=(int)round(range*depth*0.01);

   int shift=BarShift(NULL,PERIOD_D1,time,true);

   double dayHidh=High(NULL,PERIOD_D1,shift);

   double level=NormalizeDouble(low+zone*points,digits);

   bool check=false;



   if(range>range_min && range<range_max)

     {

      if(!InpExtremum || (InpExtremum && high==dayHidh))

         check=true;

      if(check && open<=level && close<=level)

         return(true);

     }

   return(false);

  }

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

//|                                                                  |

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

bool PinDown(datetime time,double open,double high,double low,double close,int depth)

  {

   int range=(int)round((high-low)/points);

   int zone=(int)round(range*depth*0.01);

   int shift=BarShift(NULL,PERIOD_D1,time,true);

   double dayLow=Low(NULL,PERIOD_D1,shift);

   double level=NormalizeDouble(high-zone*points,digits);

   bool check=false;



   if(range>range_min && range<range_max)

     {

      if(!InpExtremum || (InpExtremum && low==dayLow))

         check=true;

      if(check && open>=level && close>=level)

         return(true);

     }

   return(false);

  }

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

//| >72@0I05B A<5I5=85 10@0 ?> 2@5<5=8                              |

//| https://www.mql5.com/ru/forum/743/page11#comment_7010041         |

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

int BarShift(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const datetime time,bool exact=false)

  {

   int res=Bars(symbol_name,timeframe,time+1,UINT_MAX);

   if(exact) if((timeframe!=PERIOD_MN1 || time>TimeCurrent()) && res==Bars(symbol_name,timeframe,time-PeriodSeconds(timeframe)+1,UINT_MAX)) return(WRONG_VALUE);

   return res;

  }

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

//| >72@0I05B F5=C Open                                             |

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

double High(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const int index)

  {

   double array[];

   return(CopyHigh(symbol_name,timeframe,index,1,array)==1 ? array[0] : 0);

  }  

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

//| >72@0I05B F5=C Low                                              |

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

double Low(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const int index)

  {

   double array[];

   return(CopyLow(symbol_name,timeframe,index,1,array)==1 ? array[0] : 0);

  }  

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

//| Timeframe to string                                              |

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

string TimeframeToString(const ENUM_TIMEFRAMES timeframe)

  {

   return StringSubstr(EnumToString(timeframe),7);

  }

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

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