Viewing the resource: Bullish Engulfing EA: Capture Reversal Patterns with Precision

Bullish Engulfing EA: Capture Reversal Patterns with Precision

Allan Munene Mutiiria 2025-06-20 23:57:45 94 Views
Join our fun, beginner-friendly guide to the Bullish Engulfing EA MQL5 code! We’ll explain every f...

Introduction

Ahoy, future forex sailor! Picture the forex market as a vast, stormy sea, where a sudden bullish engulfing pattern shines like a beacon, signaling a tide turning from bearish to bullish. The Bullish Engulfing EA is your trusty lighthouse, spotting this classic candlestick pattern and marking it with a green arrow for a clear buy signal. This article is your nautical chart, guiding you through the MQL5 code on MetaTrader 5 with vivid detail. We’ll unpack every major function with clarity for beginners, weaving humor and flow like a sea shanty sung by a crew. By the end, you’ll be ready to let this Expert Advisor (EA) steer your trades toward bullish shores. Let’s hoist the sails!

Strategy Blueprint

The Bullish Engulfing EA focuses on the bullish engulfing pattern, a two-candle formation signaling a potential bottom:

  • First Candle: A small bearish candle (open above close), showing sellers in control.

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

The EA identifies this pattern, places a green arrow below the bullish candle, and returns 1 to signal a buy. The strategy assumes prices will rise post-pattern, ideal for reversals in downtrends or at support levels. It’s like spotting a clearing storm and betting on smooth sailing ahead. See below.

Code Implementation

Let’s sail through the MQL5 code like a ship cutting through waves, unfolding the logic as a seamless voyage. Our journey starts with setting up the EA, moves to scanning for the pattern, and ends with marking it visually. Each function builds on the last, with transitions that keep the narrative flowing like a steady current. We’ll explain every major function in detail, quoting variables (e.g., "open1") and functions (e.g., "OnInit()") for clarity, keeping it beginner-friendly with a playful sea theme.

Setting Sail: Header and Standard Functions

Our adventure begins by rigging the ship, preparing the EA to scan the market’s tides.

//+------------------------------------------------------------------+
//|                                            Bullish 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()
{
   getBullishEngulfing();
}

The header declares the EA as “Bullish Engulfing,” crafted by Allan in 2025, with a link to your Telegram channel and version 1.00, setting the course for our journey.

  • "OnInit()": The EA’s launch, called when attached to a chart. It returns INIT_SUCCEEDED, a constant signaling a smooth start, as no indicators or setup are needed. It’s like raising the anchor with clear skies ahead.

  • "OnDeinit()": The cleanup function, called when removed. It’s empty, like leaving the harbor pristine, as no resources need freeing.

  • "OnTick()": The EA’s heartbeat, running on every price tick. It calls "getBullishEngulfing()", the core function for pattern detection, like scanning the horizon for a beacon.

These functions rig the ship, preparing it to sail toward bullish engulfing patterns.

Charting the Waves: getBullishEngulfing Function

With the ship ready, we steer into the heart of the EA, scanning for the bullish engulfing pattern—the beacon signaling a rising tide.

// FUNCTION PROTOTYTE
int getBullishEngulfing(){
   datetime time = iTime(_Symbol,PERIOD_CURRENT,1);
   double open1 = iOpen(_Symbol,PERIOD_CURRENT,1);
   double high1 = iHigh(_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("Bullish Engulfing Found");
               createArrow(time,low1,233,clrGreen);
               return 1;
            }
         }
      }
   }
   
   return 0;
}

The "getBullishEngulfing()" function is the EA’s lookout, scanning for the pattern. Here’s how it flows:

  • "iTime()": Fetches 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, like noting when a beacon appears.

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

  • Pattern Logic: Checks if:

    • The first candle is bullish ("open1 < close1").

    • The second candle is bearish ("open2 > close2").

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

    • The first candle’s close is above the second’s open ("close1 > open2") and its open is below the second’s close ("open1 < close2"). If true, it logs “Bullish Engulfing Found” with "Print()", calls "createArrow()", and returns 1 (buy signal). Otherwise, it returns 0 (no signal).

  • "Print()": Logs a message to the MetaTrader 5 journal, like shouting “Beacon spotted!” from the crow’s nest.

This function charts the market’s waves, locking onto the bullish engulfing pattern like a lighthouse beam.

Signaling the Tide: createArrow Function

With the pattern spotted, the EA paints a green arrow to mark the buy signal, guiding traders like a beacon.

// function to create arrow
void createArrow (datetime time, double price, int arrCode, color clr){
   string arrName = " ";
   if (ObjectCreate(0,arrName,OBJ_ARROW,0,time,price)){
      ObjectSetInteger(0,arrName,OBJPROP_ARROWCODE,arrCode);
      ObjectSetInteger(0,arrName,OBJPROP_COLOR,clr);
      ObjectSetInteger(0,arrName,OBJPROP_WIDTH,3);
   }
}

The "createArrow()" function is the EA’s signal flare, placing a green arrow below the bullish candle:

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

  • "ObjectSetInteger()": Sets arrow properties: style ("OBJPROP_ARROWCODE", 233 for an upward arrow), color ("OBJPROP_COLOR", "clrGreen"), and width ("OBJPROP_WIDTH", 3 for visibility).

This function lights the way, marking the pattern with a bold green arrow, like a flare signaling clear skies ahead. See below.

Putting It All Together

To launch 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 green arrows signaling buy opportunities.

  5. Trade smart—don’t bet your ship on one pattern! Manually place buy trades based on arrows or add trade execution code (e.g., using "CTrade").

Conclusion

The Bullish Engulfing EA is your lighthouse, spotting bullish engulfing patterns with green arrows for clear buy signals. We’ve sailed through its MQL5 code with clear, detailed explanations, so you understand every move like a seasoned sailor. Now you’re set to automate pattern detection and trade reversals like a pro. Want to see it in action? Check our video guide on the website!

Disclaimer: Trading’s like navigating a stormy sea—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!