Viewing the resource: Bullish Hammer EA: Nail Reversal Patterns with Precision

Bullish Hammer EA: Nail Reversal Patterns with Precision

Allan Munene Mutiiria 2025-06-21 00:10:31 72 Views
Join our fun, beginner-friendly guide to the Bullish Hammer EA MQL5 code! We’ll explain every func...

Introduction

Hey there, future forex blacksmith! Picture the forex market as a roaring forge, where a bullish hammer candlestick glows like a freshly struck nail, signaling a reversal from bearish to bullish flames. The Bullish Hammer EA is your master smith, spotting this classic pattern and marking it with a red arrow (likely meant to be green, see notes) for a clear buy signal. This article is your forge blueprint, 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 smith’s tale by the fire. By the end, you’ll be ready to let this Expert Advisor (EA) hammer out trades for you. Let’s stoke the flames!

Strategy Blueprint

The Bullish Hammer EA targets the bullish hammer pattern, a single-candle signal of a potential bottom:

  • Candle Anatomy: A small bullish body (close above open), a short upper wick (high near close, <10% of candle range), and a long lower wick (open far above low, >80% of range), shaped like a hammer.

  • Market Context: Appears after a downtrend, showing buyers rejected lower prices, pushing the close higher.

The EA detects the pattern using customizable ratios (short body: 0.1, long wick: 0.8), places a red arrow (intended as green) below the candle, and returns 0 (no trade execution). The strategy assumes prices will rise, ideal for reversals at support or in downtrends. It’s like spotting a spark in the forge and betting on a blazing rally. See below.

Code Implementation

Let’s forge through the MQL5 code like a smith shaping molten metal, unfolding the logic as a seamless craft. Our journey starts with setting up the EA, moves to scanning for the hammer pattern, and ends with marking it visually. Each function builds on the last, with transitions that keep the narrative flowing like a hammer’s steady strikes. We’ll explain every major function in detail, quoting variables (e.g., "open") and functions (e.g., "OnInit()") for clarity, keeping it beginner-friendly with a fiery forge theme.

Forging the Foundation: Header and Standard Functions

Our craft begins by heating the forge, preparing the EA to shape market signals.

//+------------------------------------------------------------------+
//|                                               bullish_hammer.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"
int OnInit()
{
   return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
}
void OnTick()
{
   getHammer(0.1,0.8);
}

The header declares the EA as “bullish_hammer,” crafted by Allan in 2025, with a link to your Telegram channel and version 1.00, setting the forge aglow.

  • "OnInit()": The EA’s spark, called when attached to a chart. It returns INIT_SUCCEEDED, a constant signaling a successful start, as no indicators or setup are needed. It’s like stoking the forge with a steady flame.

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

  • "OnTick()": The EA’s hammer strike, running on every price tick. It calls "getHammer()" with parameters 0.1 (short body ratio) and 0.8 (long wick ratio), directing the craft toward pattern detection, like swinging a hammer at molten metal.

These functions lay the anvil, preparing the EA to forge bullish hammer signals.

Shaping the Pattern: getHammer Function

With the forge heated, we swing into the heart of the EA, shaping the bullish hammer pattern—the glowing nail signaling a buy.

int getHammer(double shortBodyRatio, double longBodyRatio){
   datetime time = iTime(Symbol(),PERIOD_CURRENT,1);
   double open = iOpen(_Symbol,PERIOD_CURRENT,1);
   double high = iHigh(_Symbol,PERIOD_CURRENT,1);
   double low = iLow(_Symbol,PERIOD_CURRENT,1);
   double close = iClose(_Symbol,PERIOD_CURRENT,1);
   
   double candleSize = high - low;
   
   if (open < close){
      if (high - close < candleSize * shortBodyRatio){
         if (open - low > candleSize * longBodyRatio){
            Print("Bullish candle pattern formed");
            createOBJ(time,low,217,clrRed,"hammer pattern");
         }
      }
   }
   return 0;
}

The "getHammer()" function is the EA’s hammer swing, forging the pattern with precision. Here’s how it flows:

  • Inputs: Takes "shortBodyRatio" (0.1) for the upper wick/body and "longBodyRatio" (0.8) for the lower wick, defining the hammer’s shape.

  • "iTime()": Fetches the timestamp of the previous candle (index 1) for the current symbol ("Symbol()", equivalent to "_Symbol") and timeframe ("PERIOD_CURRENT"), stored in "time" for arrow placement, like noting when the nail is struck.

  • "iOpen()", "iHigh()", "iLow()", "iClose()": Retrieve the open ("open"), high ("high"), low ("low"), and close ("close") prices of the previous candle (index 1). These define the candle’s structure, like measuring the metal’s form.

  • Candle Size: Calculates "candleSize" as "high - low", the total range, setting the scale for ratios.

  • Pattern Logic: Checks if:

    • The candle is bullish ("open < close").

    • The upper wick is short ("high - close < candleSize * shortBodyRatio", <10% of range).

    • The lower wick is long ("open - low > candleSize * longBodyRatio", >80% of range). If true, it logs “Bullish candle pattern formed” with "Print()", calls "createOBJ()", and shapes the signal. Returns 0 (no trade execution).

  • "Print()": Logs a message to the MetaTrader 5 journal, like shouting “Nail forged!” in the forge.

This function shapes the hammer pattern, striking the metal to form a buy signal, like crafting a glowing nail.

Polishing the Signal: createOBJ Function

With the hammer forged, the EA polishes it with a red arrow (likely meant green), marking the buy signal like a shining beacon.

void createOBJ(datetime time, double price, int arrawCode, color clr, string text){
   string objNAME = " ";
   if (ObjectCreate(0,objNAME,OBJ_ARROW,0,time,price)){
      ObjectSetInteger(0,objNAME,OBJPROP_ARROWCODE,arrawCode);
      ObjectSetInteger(0,objNAME,OBJPROP_COLOR,clr);
      ObjectSetInteger(0,objNAME,OBJPROP_WIDTH,3);      
   }
}

The "createOBJ()" function is the EA’s polish, placing an arrow below the hammer candle:

  • Inputs: Takes "time", "price", "arrawCode" (217, upward arrow), "clr" ("clrRed", likely meant "clrGreen"), and "text" (“hammer pattern”, unused in rendering).

  • "ObjectCreate()": Creates a graphical arrow ("OBJ_ARROW") on the chart (window 0) at "time" and "price". The object’s name ("objNAME") is a blank string, which may cause overlap issues (see notes). Returns true if successful, like polishing a nail to shine.

  • "ObjectSetInteger()": Sets arrow properties: style ("OBJPROP_ARROWCODE", 217), color ("OBJPROP_COLOR", "clrRed"), and width ("OBJPROP_WIDTH", 3 for visibility).

This function polishes the signal, marking the hammer with an arrow, like a gleaming nail ready for trading. See below.

Putting It All Together

To fire up 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 (likely meant green) signaling buy opportunities.

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

Conclusion

The Bullish Hammer EA is your blacksmith, forging bullish hammer patterns with arrows for clear buy signals. We’ve hammered through its MQL5 code with clear, detailed explanations, so you understand every strike like a master smith. 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 forging in a blaze—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!