Similar Candle

Author: Copyright © 2021, Vladimir Karputov
1 Views
0 Downloads
0 Favorites
Similar Candle
ÿþ//+------------------------------------------------------------------+

//|                                               Similar Candle.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 1

#property indicator_plots   1

//--- plot Found

#property indicator_label1  "Found"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrLawnGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- input parameters

input group             "VLine"

input string   InpVLineName      = "VLine Similar Candle";  // VLine name

input group             "Arrow"

input uchar    InpCode           = 162;                     // Arrow code for style DRAW_ARROW (font Wingdings)

input int      InpShift          = 10;                      // Vertical shift of arrows in pixels

input group             "Candle"

input double   InpTolerance      = 10;                      // Tolerance (in %)

input bool     InpType           = true;                    // Candle type

input bool     InpUpperShadow    = true;                    // Upper shadow

input bool     InpBottomShadow   = true;                    // Bottom shadow

//--- indicator buffers

double   FoundBuffer[];

//---

bool     m_stop=false;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,FoundBuffer,INDICATOR_DATA);

//--- define the symbol code for drawing in PLOT_ARROW

   PlotIndexSetInteger(0,PLOT_ARROW,InpCode);

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

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-InpShift);

//--- set as an empty value 0

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//---

   Comment("");

  }

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

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

  {

   int limit=prev_calculated-1;

   if(prev_calculated==0)

     {

      m_stop=false;

      limit=0;

     }

//---

   if(m_stop)

     {

      Comment("Similar Candle Stop: ",m_stop);

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

         FoundBuffer[i]=0.0;

      return(rates_total);

     }

   Comment("Similar Candle Stop: ",m_stop);

   if(ObjectFind(ChartID(),InpVLineName)<0)

     {

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

         FoundBuffer[i]=0.0;

      return(rates_total);

     }

   int vlime_type=ObjectGetInteger(ChartID(),InpVLineName,OBJPROP_TYPE);

   if(vlime_type!=OBJ_VLINE)

     {

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

         FoundBuffer[i]=0.0;

      return(rates_total);

     }

   datetime vlime_time=(datetime)ObjectGetInteger(ChartID(),InpVLineName,OBJPROP_TIME);

   if(vlime_time==D'1970.01.01 00:00')

     {

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

         FoundBuffer[i]=0.0;

      return(rates_total);

     }

//--- main loop

   double candle_open=0.0;

   double candle_high=0.0;

   double candle_low=0.0;

   double candle_close=0.0;

   int candle_type=0; // "-1";"0";"1"

   double candle_high_low=0.0;

   double candle_body=0.0; // percentage

   double candle_upper_shadow=0.0; // percentage

   double candle_bottom_shadow=0.0; // percentage

   for(int i=rates_total-1; i>=0; i--)

     {

      if(time[i]==vlime_time)

        {

         candle_open=open[i];

         candle_high=high[i];

         candle_low=low[i];

         candle_close=close[i];

         candle_type=(open[i]<close[i])?1:-1;

         //---

         candle_high_low=candle_high-candle_low; // 100%

         candle_body=(candle_type==1)?close[i]-open[i]:open[i]-close[i];

         candle_body=candle_body*100.0/candle_high_low;

         candle_upper_shadow=(candle_type==1)?high[i]-close[i]:high[i]-open[i];

         candle_upper_shadow=candle_upper_shadow*100.0/candle_high_low;

         candle_bottom_shadow=(candle_type==1)?open[i]-low[i]:close[i]-low[i];

         candle_bottom_shadow=candle_bottom_shadow*100.0/candle_high_low;

         break;

        }

     }

   for(int i=rates_total-1; i>=0; i--)

     {

      FoundBuffer[i]=0.0;

      bool bln_type=true;

      bool bln_upper_shadow=true;

      bool bln_bottom_shadow=true;

      double _type=(open[i]<close[i])?1:-1;

      double _high_low=high[i]-low[i]; // 100%

      if(InpType)

        {

         if(_type!=candle_type)

            bln_type=false;

        }

      if(InpUpperShadow)

        {

         double _upper_shadow=(_type==1)?high[i]-close[i]:high[i]-open[i];

         _upper_shadow=_upper_shadow*100.0/_high_low;  // percentage

         if(InpTolerance>0.0)

           {

            if(_upper_shadow<candle_upper_shadow-InpTolerance || _upper_shadow>candle_upper_shadow+InpTolerance)

               bln_upper_shadow=false;

           }

        }

      if(InpBottomShadow)

        {

         double _bottom_shadow=(_type==1)?open[i]-low[i]:close[i]-low[i];

         _bottom_shadow= _bottom_shadow*100.0/_high_low;  // percentage

         if(InpTolerance>0.0)

           {

            if(_bottom_shadow<candle_bottom_shadow-InpTolerance || _bottom_shadow>candle_bottom_shadow+InpTolerance)

               bln_bottom_shadow=false;

           }

        }

      if(bln_type && bln_upper_shadow && bln_bottom_shadow)

         FoundBuffer[i]=high[i];

     }

   m_stop=true;

   Comment("Similar Candle Stop: ",m_stop);

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

   return(rates_total);

  }

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

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