Hammer and Hanging Man Candlestick

Author: Copyright © 2022, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Hammer and Hanging Man Candlestick
ÿþ//+------------------------------------------------------------------+

//|                           Hammer and Hanging Man Candlestick.mq5 |

//|                              Copyright © 2022, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

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

#property copyright "Copyright © 2022, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_plots   2

//--- plot Hammer

#property indicator_label1  "Hammer"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot HangingMan

#property indicator_label2  "HangingMan"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- input parameters

input group             "Hammer"

sinput uchar               InpHammerCode              = 172;         // Hammer: code for style DRAW_ARROW (font Wingdings)

sinput int                 InpHammerShift             = 10;          // Hammer: vertical shift of arrows in pixels

input group             "Hanging Man"

sinput uchar               InpHangingManCode          = 172;         // Hanging Man: code for style DRAW_ARROW (font Wingdings)

sinput int                 InpHangingManShift         = 10;          // Hanging Man: vertical shift of arrows in pixels

//--- indicator buffers

double   HammerBuffer[];

double   HangingManBuffer[];

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,HammerBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,HangingManBuffer,INDICATOR_DATA);

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

   PlotIndexSetInteger(0,PLOT_ARROW,InpHammerCode);

   PlotIndexSetInteger(1,PLOT_ARROW,InpHangingManCode);

//--- set the vertical shift of arrows in pixels

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,InpHammerShift);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,-InpHangingManShift);

//--- set as an empty value 0.0

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,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[])

  {

   if(rates_total<3)

      return(0);

//---

   int limit=prev_calculated-1;

   if(prev_calculated==0)

     {

      limit=1;

      HammerBuffer[0]=0.0;

      HangingManBuffer[0]=0.0;

     }

   for(int i=limit; i<rates_total; i++)

     {

      HammerBuffer[i]=0.0;

      HangingManBuffer[i]=0.0;

      if(i>0)

        {

         double upper_shadow=0.0;

         double body=0.0;

         double lower_shadow=0.0;

         if(open[i]>close[i])

           {

            upper_shadow= high[i]-open[i];

            body        = open[i]-close[i];

            lower_shadow= close[i]-low[i];

           }

         else

           {

            upper_shadow= high[i]-close[i];

            body        = close[i]-open[i];

            lower_shadow= open[i]-low[i];

           }

         //--- Hammer

         if(open[i-1]>close[i-1])

           {

            if(lower_shadow/2.0>body)

               if(upper_shadow<body)

                  HammerBuffer[i]=low[i];

           }

         else // Hanging Man

           {

            if(lower_shadow/2.0>body)

               if(upper_shadow<body)

                  HangingManBuffer[i]=high[i];

           }

        }

     }

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

   return(rates_total);

  }

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

Comments