Viewing the resource: Bearish Engulfing EA: Spot Reversal Patterns with Precision

Bearish Engulfing EA: Spot Reversal Patterns with Precision

Allan Munene Mutiiria 2025-06-20 23:10:18 89 Views
Join our fun, newbie-friendly guide to the Bearish Engulfing EA MQL5 code! We’ll explain every fun...

Introduction

Hey there, future forex storm chaser! Picture the forex market as a turbulent sky, where a sudden bearish engulfing pattern looms like a dark cloud signaling a price reversal. The Bearish Engulfing EA is your weather radar, spotting this classic candlestick pattern and marking it with a red arrow for a clear sell signal. This article is your storm-chasing guide, walking you through the MQL5 code on MetaTrader 5 with vivid detail. We’ll explain every major function like you’re new to coding (no worries!), blending humor and flow like a storyteller by a campfire. By the end, you’ll be ready to let this Expert Advisor (EA) hunt reversals for you. Let’s ride the storm!

Strategy Blueprint

The Bearish Engulfing EA focuses on the bearish engulfing pattern, a two-candle formation signaling a potential top:

  • First Candle: A small bullish candle (open below close), showing buyers in control.

  • Second Candle: A larger bearish candle (open above close) that engulfs the first, with its open above the first’s close and close below the first’s open, indicating sellers overpowering buyers.

The EA identifies this pattern and places a red arrow above the bearish candle, signaling a sell. The strategy assumes prices will drop post-pattern, ideal for reversals in uptrends or at resistance levels. It returns -1 for a sell signal, enabling manual or automated trading. It’s like spotting a thundercloud and betting on a downpour. See below.

Code Implementation

Let’s dive into the MQL5 code like storm chasers tracking a cloud, crafting a narrative that flows seamlessly from setup to signal detection. We’ll present each major function in full, explaining every key function in detail for clarity, and quoting variables (e.g., "high1") and functions (e.g., "OnInit()") to keep it beginner-friendly. Our goal is to make the code as inviting as a clear forecast, guiding you through every step with ease.

Setting the Radar: Header and Standard Functions

We start by setting up the tracking system, like calibrating a weather radar before a storm.

//+------------------------------------------------------------------+
//|                                            Bearish Engulfing.mq5 |
//|                        Copyright 2025, Allan Munene Mutiiria      |
//|                            https://t.me/Forex_Algo_Trader         |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, Allan Munene Mutiiria"
#property link      "https://t.me/Forex_Algo_Trader"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   getBearishEngulfing();
}

This opening sets the stage. The header declares the EA as “Bearish Engulfing,” crafted by Allan in 2025, with a link to your Telegram channel and version 1.00.

  • "OnInit()": The EA’s startup function, called when you attach it to a chart. It returns INIT_SUCCEEDED, a constant signaling successful initialization, as no indicators or setup are needed here. It’s like switching on the radar with a green light.

  • "OnDeinit()": The cleanup function, called when the EA is removed. It’s empty, as no resources need freeing, like a radar that auto-shuts off.

  • "OnTick()": The EA’s heartbeat, running on every price tick. It calls "getBearishEngulfing()", the core function for pattern detection, like tuning the radar to scan for clouds.

These standard functions form the EA’s backbone, keeping it lightweight and focused on pattern spotting.

Scanning for Clouds: getBearishEngulfing Function

This is where the EA hunts for the bearish engulfing pattern.

// function TO GIVE THE CONDITIONS
int getBearishEngulfing(){
   datetime time = iTime(_Symbol,PERIOD_CURRENT,1);
   double high1 = iHigh(_Symbol,PERIOD_CURRENT,1);
   double open1 = iOpen(_Symbol,PERIOD_CURRENT,1);
   double low1 = iLow(_Symbol,PERIOD_CURRENT,1);
   double close1 = iClose(_Symbol,PERIOD_CURRENT,1);
   double open2 = iOpen(_Symbol,PERIOD_CURRENT,2);
   double high2 = iHigh(_Symbol,PERIOD_CURRENT,2);
   double low2 = iLow(_Symbol,PERIOD_CURRENT,2);
   double close2 = iClose(_Symbol,PERIOD_CURRENT,2);
   
   if (open1 > close1){
      if (open2 < close2){
         if (high1 > high2 && low1 < low2){
            if (close1 < open2 && open1 > close2){
               Print("BEARISH ENGULFING FOUND");
               createArrow(time,high1,218,clrRed);
               return -1;
            }
         }
      }
   }
   return 0;  
}

The "getBearishEngulfing()" function is the EA’s radar, scanning for the bearish engulfing pattern. Let’s unpack its key functions:

  • "iTime()": Retrieves the timestamp of the previous candle (index 1) for the current symbol ("_Symbol", e.g., EURUSD) and timeframe ("PERIOD_CURRENT", e.g., H1), stored in "time" for arrow placement.

  • "iHigh()", "iOpen()", "iLow()", "iClose()": Fetch the high, open, low, and close prices of the previous (index 1, "high1", "open1", "low1", "close1") and second-previous (index 2, "high2", "open2", "low2", "close2") candles. These define the pattern’s candles.

  • "Print()": Logs a message to the MetaTrader 5 journal, here announcing “BEARISH ENGULFING FOUND” when the pattern is detected.

  • Pattern Logic: Checks if:

    • The first candle is bearish ("open1 > close1").

    • The second candle is bullish ("open2 < close2").

    • The first candle engulfs the second’s range ("high1 > high2 && low1 < low2").

    • The first candle’s close is below the second’s open ("close1 < open2") and its open is above the second’s close ("open1 > close2"). If true, it calls "createArrow()", logs the find, and returns -1 (sell signal). Otherwise, it returns 0 (no signal).

  • "createArrow()": A custom function (defined below) that places a red arrow on the chart.

This function flows from fetching candle data to validating the pattern and signaling a sell, like a radar locking onto a storm cloud.

Marking the Storm: createArrow Function

This function paints the sell signal on the chart.

// CREATE ARROW
void createArrow(datetime time, double price, int code, color clr){
   string arrName = " ";
   if (ObjectCreate(0,arrName,OBJ_ARROW,0,time, price)){
      ObjectSetInteger(0,arrName,OBJPROP_ARROWCODE,code);
      ObjectSetInteger(0,arrName,OBJPROP_COLOR,clr);
      ObjectSetInteger(0,arrName,OBJPROP_WIDTH,3);
   }
}

The "createArrow()" function is the EA’s artist, placing a red arrow to mark the pattern. Key functions:

  • "ObjectCreate()": Creates a graphical object (an arrow, "OBJ_ARROW") on the chart (window 0) at the specified "time" and "price". The object’s name ("arrName") is a blank string, which may cause issues (see notes), but it works here. Returns true if successful.

  • "ObjectSetInteger()": Sets integer properties for the object, like arrow style ("OBJPROP_ARROWCODE", 218 for a downward arrow), color ("OBJPROP_COLOR", "clrRed"), and width ("OBJPROP_WIDTH", 3 for visibility).

This function places a bold red arrow above the bearish candle, like a flashing storm warning.

Putting It All Together

To unleash this EA:

  1. Open MetaEditor in MetaTrader 5.

  2. Paste the code into a new Expert Advisor file.

  3. Compile (F5). If errors appear, double-check your copy-paste.

  4. Drag the EA onto your chart, enable AutoTrading, and watch for red arrows signaling sell opportunities.

  5. Trade smart—don’t bet your raincoat on one pattern! Manually place sell trades based on arrows or add trade execution code.

Conclusion

The Bearish Engulfing EA is your storm-chasing companion, spotting bearish engulfing patterns with red arrows for clear sell signals. We’ve explored its MQL5 code with clear, detailed explanations, so you understand every move like a seasoned trader. Now you’re set to automate pattern detection and trade reversals like a pro. Want to see it in action? Check our video tutorial on the website!

Disclaimer: Trading’s like chasing a thunderstorm—exciting but risky. Losses can exceed deposits. Test on a demo account before going live.

Disclaimer: The ideas and strategies presented in this resource are solely those of the author and are intended for informational and educational purposes only. They do not constitute financial advice, and past performance is not indicative of future results. All materials, including but not limited to text, images, files, and any downloadable content, are protected by copyright and intellectual property laws and are the exclusive property of Forex Algo-Trader or its licensors. Reproduction, distribution, modification, or commercial use of these materials without prior written consent from Forex Algo-Trader is strictly prohibited and may result in legal action. Users are advised to exercise extreme caution, perform thorough independent research, and consult with qualified financial professionals before implementing any trading strategies or decisions based on this resource, as trading in financial markets involves significant risk of loss.

Recent Comments

Go to discussion to Comment or View other Comments

No comments yet. Be the first to comment!