Bearish Bullish Engulfing

Download script
Author: Copyright © 2021, Vladimir Karputov

This tool is designed to identify and visualize specific candlestick patterns on a trading chart—specifically Bearish and Bullish Engulfing patterns. These are popular technical analysis signals used by traders to spot potential reversals in price momentum.

How the Logic Works

The script scans your price history bar-by-bar to detect two distinct scenarios:

1. Bearish Engulfing Detection (Potential Downward Turn) The script looks for a specific transition between two consecutive candles:

  • Previous Candle: Must be a "bullish" candle (the price closed higher than it opened).
  • Current Candle: Must be a "bearish" candle (the price closed lower than it opened).
  • The "Engulfing" Criteria: The current bearish candle must be physically larger than the previous one in such a way that its opening and closing prices completely "swallow" or overlap the body of the previous bullish candle.
  • Visual Output: When these conditions are met, the script places a Red Arrow on your chart to signal a potential shift toward a downward trend.

2. Bullish Engulfing Detection (Potential Upward Turn) Conversely, the script identifies signals for an upward move:

  • Previous Candle: Must be a "bearish" candle (closed lower than it opened).
  • Current Candle: Must be a "bullish" candle (closed higher than it opened).
  • The "Engulfing" Criteria: The current bullish candle must be large enough to completely cover the body of the previous bearish candle.
  • Visual Output: When these conditions are met, the script places a Blue Arrow on your chart to signal a potential shift toward an upward trend.

Key Features for the User

  • Visual Clarity: Instead of just highlighting bars, the tool uses arrows. It is programmed to offset these arrows vertically (above or below the candle) so they do not clutter the actual price action, making them easier to see at a glance.
  • Real-Time Updates: The script calculates these patterns automatically as new price data arrives, allowing you to see signals develop on your chart dynamically.
  • Customizable Positioning: You can adjust the vertical "shift" (in pixels) of the arrows if you find they are too close to or too far from the candles based on your specific screen resolution and zoom level.
Price Data Components
12 Views
0 Downloads
0 Favorites
Bearish Bullish Engulfing
ÿþ//+------------------------------------------------------------------+

//|                                    Bearish Bullish Engulfing.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

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

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

#property copyright "Copyright © 2021, 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 BearishHarami

#property indicator_label1  "Bearish Engulfing"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot BullishHarami

#property indicator_label2  "Bullish Engulfing"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- input parameters

input uchar                Inp_Bearish_Code        = 164;         // Bearish Engulfing: symbol code from the Wingdings font

input uchar                Inp_Bullish_Code        = 164;         // Bullish Engulfing: vertical shift of arrows in pixels

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

//--- indicator buffers

double   BearishEngulfingBuffer[];

double   BullishEngulfingBuffer[];

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,BearishEngulfingBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,BullishEngulfingBuffer,INDICATOR_DATA);

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

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

   PlotIndexSetInteger(0,PLOT_ARROW,Inp_Bearish_Code);

   PlotIndexSetInteger(1,PLOT_ARROW,Inp_Bullish_Code);

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

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,Inp_Arrow_Shift);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,-Inp_Arrow_Shift);

//--- set as an empty value 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)

     {

      BearishEngulfingBuffer[0]=0.0;

      BullishEngulfingBuffer[0]=0.0;

      limit=1;

     }

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

     {

      BearishEngulfingBuffer[i]=0.0;

      BullishEngulfingBuffer[i]=0.0;

      //--- Bearish Engulfing

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

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

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

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

                  BearishEngulfingBuffer[i]=low[i];

      //--- Bullish Engulfing

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

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

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

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

                  BullishEngulfingBuffer[i]=high[i];

     }

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

   return(rates_total);

  }

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

Comments