Viewing the resource: Crafting a Hanging Man Cosmic Spotter with MQL5 🌠

Crafting a Hanging Man Cosmic Spotter with MQL5 🌠

Allan Munene Mutiiria 2025-06-21 19:16:27 101 Views
Step aboard, forex explorers! We’re embarking on a thrilling journey through the MQL5 code for the...

Introduction

Imagine you’re a cosmic storm spotter, standing on the bridge of your trading starship, scanning the forex galaxy for signs of a market shift. The Hanging Man EA is your high-tech radar, meticulously designed to detect the Hanging Man candlestick pattern—a single candle that signals a potential bearish reversal after an uptrend. Picture a candle with a small body (open and close near the high) and a long lower wick, like a star dangling precariously before it falls, warning that buyers are losing steam. This EA doesn’t place trades but marks these patterns with white arrows on your chart, like planting beacons to guide your manual trading decisions. In this article, I’ll take you on a seamless, professional journey through the code, weaving a narrative that flows like a starry night sky. Each section will build on the last, with detailed explanations tailored for beginners, vibrant examples to bring the concepts to life, and a touch of cosmic charm to keep you captivated. Whether you’re new to MQL5 or a seasoned trader, this guide will illuminate how the EA spots Hanging Man patterns and sets you up to trade with precision. Let’s power up the radar and start our mission to hunt those bearish signals!

Strategy Blueprint

Before we dive into the code, let’s chart the course of what this EA does, like plotting a star map for our trading expedition:

  • Pattern Detection: The EA identifies a Hanging Man candle, where the open is above the close (bearish), the upper wick (high minus open) is less than 10% of the candle’s size, and the lower wick (close minus low) is over 70% of the size, signaling indecision after an uptrend.

  • Visualization: It places a white arrow at the candle’s low on the chart, acting as a clear visual cue for traders to spot potential sell opportunities.

  • Purpose: Designed for manual trading, the EA highlights bearish reversal points without executing trades, like a radar guiding your strategy without taking the helm.

  • Features: Processes the latest completed candle for accuracy, with a lightweight design that’s easy to extend with trading logic if desired. This strategy is like a cosmic radar, pinpointing Hanging Man patterns to guide traders through market turning points with clarity and precision, making it an essential tool for spotting bearish reversals.

Code Implementation

Now, let’s step into the starship’s engineering bay and explore the MQL5 code that powers this cosmic spotter. I’ll guide you through each section like a mission commander, ensuring the narrative flows seamlessly from one part to the next, with professional polish and engaging details. We’ll cover setting up the radar, detecting the Hanging Man pattern, marking it on the chart, and cleaning up, using clear explanations and examples—like spotting a Hanging Man on EURUSD—to make it relatable for beginners. Each step will feel like a chapter in our cosmic adventure, keeping you hooked as we build this trading tool together.

Step 1: Powering Up the Cosmic Radar—Setting the Stage

Our journey begins by activating the starship’s radar system, laying the foundation for detecting Hanging Man patterns.

//+------------------------------------------------------------------+
//|                                                  hanging man.mq5 |
//|      Copyright 2024, ALLAN MUNENE MUTIIRIA. #@Forex Algo-Trader. |
//|                           https://youtube.com/@ForexAlgo-Trader? |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, ALLAN MUNENE MUTIIRIA. #@Forex Algo-Trader"
#property link      "https://youtube.com/@ForexAlgo-Trader?"
#property description "======= IMPORTANT =======\n"
#property description "1. This is a FREE EA."
#property description "2. To get the source code of the EA, follow the Copyright link."
#property description "3. Incase of anything, contact developer via the link provided."
#property description "Hope you will enjoy the EA Logic.\n"
#property description "*** HAPPY TRADING! ***"
#property version   "1.00"

int OnInit(){
   return(INIT_SUCCEEDED);
}

We start by powering up the radar with the #property header, proudly declaring the EA as a free offering by Allan in 2024, complete with a YouTube link for support, like engraving your starship’s mission badge. The "OnInit()" function is our launch sequence, where we flip the switch to get the EA ready. It’s simple but effective, returning INIT_SUCCEEDED to signal, “Radar’s online, ready to scan the galaxy!” This minimal setup keeps the EA lightweight, like a sleek spaceship built for speed, priming it to detect Hanging Man patterns without any extra baggage. Think of it as setting up your control panel with just the essentials, ready to focus on spotting those cosmic signals.

Step 2: Scanning the Starfield—Detecting the Hanging Man Pattern

With the radar humming, we turn our attention to scanning the forex galaxy for the Hanging Man pattern, like spotting a flickering star signaling a storm.

int getHangingMan(){
   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 - open < candleSize * 0.1){
         if (close - low > candleSize * 0.7){
            Print("Hanging man pattern formed, SELL SIGNAL");
            createOBJ(time,low,233,clrWhite);
         }
      }
   }
   return 0;
}

void OnTick(){
   getHangingMan();
}

Now, let’s move to the bridge of our starship, where the "OnTick()" function acts as our radar’s continuous scan, checking every price tick for action. It calls "getHangingMan()", which is the heart of our pattern detection, like tuning the radar to spot a specific cosmic signal. This function focuses on the latest completed candle (bar 1) to ensure the pattern is fully formed, avoiding false alerts.

In "getHangingMan()", we grab the candle’s data using "iTime()", "iOpen()", "iHigh()", "iLow()", and "iClose()", like pulling up a star’s coordinates. We calculate the candle’s size ("candleSize = high - low")—think of this as measuring the star’s brightness range. The Hanging Man pattern requires three conditions:

  1. The candle is bearish ("open > close"), meaning the price fell during the period, like a star dimming.

  2. The upper wick is tiny ("high - open < candleSize * 0.1"), less than 10% of the candle’s size, showing little upward push.

  3. The lower wick is long ("close - low > candleSize * 0.7"), over 70% of the size, indicating buyers tried to push up but failed.

If all conditions are met, we log the discovery with "Print("Hanging man pattern formed, SELL SIGNAL")", like sending an alert to mission control, and call "createOBJ()" to mark the pattern. We return 0, like reporting “no storm” if the pattern isn’t found. For example, on a EURUSD H1 chart, if a candle opens at 1.2005, closes at 1.2000, with a high of 1.2006 and low of 1.1980 (candle size 26 pips), it qualifies (upper wick 1 pip < 2.6 pips, lower wick 20 pips > 18.2 pips), triggering a “SELL SIGNAL” alert. This is like spotting a flickering star at a resistance level, ready for your trading decision.

Step 3: Planting the Beacon—Marking the Hanging Man Pattern

Once we’ve detected a Hanging Man, we mark it on the chart with a white arrow, like placing a beacon to signal a cosmic storm.

void createOBJ(datetime time, double price, int arrawcode, color clr){
   string OBJName = " ";
   if (ObjectCreate(0,OBJName,OBJ_ARROW,0,time,price)){
      ObjectSetInteger(0,OBJName,OBJPROP_ARROWCODE,arrawcode);
      ObjectSetInteger(0,OBJName,OBJPROP_COLOR,clr);
   }
}

Stepping into the visualization hub, the "createOBJ()" function is our tool for marking the pattern, like planting a glowing beacon on the chart. It creates a white arrow (code 233) at the candle’s low ("price") using "ObjectCreate(OBJ_ARROW)", setting its position with "time" and styling it with "ObjectSetInteger()" for the arrow code ("OBJPROP_ARROWCODE") and color ("OBJPROP_COLOR", white). However, there’s a catch: the "OBJName" is set to a blank space (" "), which means all arrows overwrite each other at the same name, potentially causing issues (we’ll address this in the notes).

Imagine this on GBPUSD: a Hanging Man forms at a high of 1.3505, low of 1.3475, open at 1.3503, and close at 1.3490. The EA places a white arrow at 1.3475, like a beacon flashing “Sell alert!” at a resistance level. This visual cue makes it easy to spot the pattern, empowering you to decide whether to enter a sell trade or wait for confirmation, like a storm spotter assessing the sky before acting.

Step 4: Docking the Starship—Cleaning Up the Radar

As our mission nears its end, we prepare to dock the starship, ensuring the radar system shuts down cleanly.

void OnDeinit(const int reason){
}

In the docking bay, the "OnDeinit()" function is our final stop, but it’s currently empty, like leaving the radar’s beacons glowing on the chart. This means the white arrows remain when you remove the EA, which could clutter your workspace. To tidy up, you could add "ObjectsDeleteAll()" to clear all arrows, like powering down your ship’s displays:

ObjectsDeleteAll(0, -1, -1);
ChartRedraw();

For now, the empty "OnDeinit()" keeps things simple, like leaving your starship idling, but adding cleanup would ensure a pristine chart for your next mission. This lightweight approach lets you focus on spotting patterns without worrying about stray objects, setting the stage for future enhancements.

Why This EA’s a Cosmic Legend (and Keeps You Engaged!)

The Hanging Man EA is a cosmic legend, acting like a radar that spots bearish reversal patterns with precision, marking them with white arrows to guide your manual trades. Its simple design is perfect for beginners, while its potential for adding trading logic excites pros. Picture spotting a Hanging Man on EURUSD at 1.2000 after a rally, with a white arrow signaling a sell opportunity at a resistance level—pure cosmic gold! The EA’s clear visuals and focused logic make it a must-have for traders hunting reversals, offering a foundation you can build on to conquer the forex galaxy.

Putting It All Together

To launch this EA:

  1. Open MetaEditor in MetaTrader 5, like stepping onto your starship’s bridge.

  2. Copy the code, compile with F5, and check for errors—no commander wants a faulty radar!

  3. Drop the EA on your chart and watch for white arrows marking Hanging Man patterns, like beacons in the galaxy.

  4. Use the signals to inform manual trades, pairing them with market analysis, like plotting a course through the stars.

  5. Test on a demo account first—real pips deserve a practice mission!

Conclusion

We’ve crafted a Hanging Man Cosmic Spotter that lights up bearish reversals with white arrows, guiding your manual trading decisions with stellar precision. This MQL5 code is your cosmic radar, brought to life with a seamless, professional narrative that flows like a starry night, packed with clear explanations and vivid examples to spark your trading passion. Whether you’re just starting out or charting new galaxies, this EA empowers you to hunt market signals like a pro. Ready to explore further? Check our video guide on the website for a front-row seat to this cosmic mission. Now, go chase those bearish stars! 🌠

Disclaimer: Trading is like spotting a cosmic storm—thrilling but risky. Losses can exceed deposits. Test strategies 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!