Introduction
Yo, trading tribe, lace up your safari boots and grab your binoculars, βcause weβre trekking into the forex savanna to build an MQL5 Expert Advisor (EA) thatβs like a spring-loaded trap for snaring breakout gold! This isnβt just codeβitβs a heart-racing hunt weβre crafting together, and weβre gonna lay it out with a flow so slick itβs like gliding across a jungle river. Weβll keep it pro, packed with detail to turn you into a coding tracker, and spiced with humor and examplesβlike picturing you as a safari pro nabbing pipsβso you stay glued to the adventure without yawning. Our quest? Build a bot that spots tight price ranges, paints them blue, and flags buy or sell breakouts when prices bolt, ready for you to add trading firepower. Letβs hack through the vines and unleash this breakout beast! πΉ See below.

Step 1: Pitching the Safari CampβSetting Up Our Hunt ποΈ
First, weβre setting up our safari camp, the base where we sharpen our spears and prep our traps. Think of this as rolling out your tent, stocking supplies, and checking your map before stalking the savanna.
#define rangeNAME "CONSOLIDATION RANGE"
int OnInit()
{
return(INIT_SUCCEEDED);
}We kick off by defining a rectangle name, βCONSOLIDATION RANGE,β like naming our trap to mark our territory. Our camp setup is leanβwe donβt need much gear yet, just a green light to start the hunt. The initialization function is a quick nod, returning a success signal to confirm our campβs ready, like a tracker saying, βTents up, letβs roll!β Itβs a simple start, but itβs the foundation for our jungle mission, keeping things light so we can focus on the prey.
Step 2: Stalking the CorralβSpotting Tight Ranges π§
Camp pitched? Time to stalk the savanna corral, scouting for a tight price range where the marketβs grazing like a herd of gazelles before a stampede.
int rangeBars = 20;
int rangeSizePoints = 500;
void OnTick(){
int currBars = iBars(_Symbol,_Period);
static int prevBars = currBars;
static bool isNewBar = false;
if (prevBars == currBars) {isNewBar = false;}
else {isNewBar = true; prevBars = currBars;}
if (isRangeExist == false && isNewBar){
TIME1_X1 = iTime(_Symbol,_Period,rangeBars);
int highestHigh_BarIndex = iHighest(_Symbol,_Period,MODE_HIGH,rangeBars,1);
PRICE1_Y1 = iHigh(_Symbol,_Period,highestHigh_BarIndex);
TIME2_X2 = iTime(_Symbol,_Period,0);
int lowestLow_BarIndex = iLowest(_Symbol,_Period,MODE_LOW,rangeBars,1);
PRICE2_Y2 = iLow(_Symbol,_Period,lowestLow_BarIndex);
isInRange = (PRICE1_Y1 - PRICE2_Y2)/_Point <= rangeSizePoints;
if (isInRange){
Print("WE ARE IN RANGE, PLOT THE RANGE ASAP");
plotConsolidationRange(rangeNAME,TIME1_X1,PRICE1_Y1,TIME2_X2,PRICE2_Y2);
isRangeExist = true;
}
}
}Every market tick is like a rustle in the grass, so we check for new candles to avoid spamming like a trigger-happy poacher. On a new candle, if no range exists, we scan the last 20 candles to find the highest and lowest prices, like spotting the tallest acacia and lowest ditch in the corral. If the range is 500 pips or less, weβve found our consolidation zoneβour herdβs grazing patch. We mark it with a start time (20 candles back) and end time (current candle), logging the high and low prices, like staking out the corralβs fences. If itβs tight, we shout βWE ARE IN RANGE!β and paint a blue rectangle on the chart, like a glowing snare marking our trap. This is our βGotcha!β moment, like a tracker spotting fresh hoofprints in the dirt.
Step 3: Springing the SnareβSignaling Breakouts π₯
Range corralled? Time to rig the snare and pounce when the market stampedes, like a gazelle busting out of the herd.
void OnTick(){
// ... (range detection code as above)
double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
if (isRangeExist && isInRange){
double R_HighBreak_Prc = (PRICE2_Y2+rangeSizePoints*_Point);
double R_LowBreak_Prc = (PRICE1_Y1-rangeSizePoints*_Point);
if (Ask > R_HighBreak_Prc){
Print("BUY BREAKOUT, L = ",PRICE2_Y2,", H BRK PRC = ",R_HighBreak_Prc);
isRangeExist = false; isInRange = false;
return;
}
else if (Bid < R_LowBreak_Prc){
Print("SELL BREAKOUT, H = ",PRICE1_Y1,", L BRK PRC = ",R_LowBreak_Prc);
isRangeExist = false; isInRange = false;
return;
}
}
}If weβre in a tight range, we watch for the breakout. If the ask price rockets above the rangeβs high (plus 500 pips, to confirm the leap), itβs like a gazelle soaring upward, triggering a βBUY BREAKOUTβ signal. If the bid crashes below the low (minus 500 pips), itβs like a dive into the grass, cueing a βSELL BREAKOUT.β We log these with the rangeβs boundaries, like shouting βStampede!β and waving a flag. We reset our range flags, like pulling up the snare for the next hunt, ensuring we donβt double-dip on old breakouts. This is pure safari thrill, like spotting the herdβs bolt and knowing itβs go-time!
Step 4: Adjusting the TrapβUpdating the Range Dynamically π
To keep our trap fresh, we tweak the range if prices nudge new highs or lows within it, like a gazelle stretching the corralβs edges.
void OnTick(){
// ... (breakout signaling code as above)
if (isRangeExist && isInRange){
if (Ask > PRICE1_Y1){
PRICE1_Y1 = Ask;
TIME2_X2 = iTime(_Symbol,_Period,0);
Print("ASK > PREVIOUS HIGH, REPLOT THE RECTANGE RANGE");
plotConsolidationRange(rangeNAME,TIME1_X1,PRICE1_Y1,TIME2_X2,PRICE2_Y2);
}
else if (Bid < PRICE2_Y2){
PRICE2_Y2 = Bid;
TIME2_X2 = iTime(_Symbol,_Period,0);
Print("BID < PREVIOUS LOW, REPLOT THE RECTANGE RANGE");
plotConsolidationRange(rangeNAME,TIME1_X1,PRICE1_Y1,TIME2_X2,PRICE2_Y2);
}
else {
if (isNewBar){
TIME2_X2 = iTime(_Symbol,_Period,1);
plotConsolidationRange(rangeNAME,TIME1_X1,PRICE1_Y1,TIME2_X2,PRICE2_Y2);
}
}
}
}If the ask price creeps above the rangeβs high, we update the high and end time, like stretching the corralβs top fence. If the bid dips below the low, we adjust the low, like lowering the bottom fence. We repaint the blue rectangle to reflect these shifts, logging βREPLOT THE RECTANGLE RANGEβ like a tracker redrawing the map. On new candles, we nudge the end time forward, keeping the trap current, like tightening the snareβs ropes. This dynamic tweaking keeps our range relevant, like a hunter adapting to the herdβs fidgeting.
Step 5: Painting the SnareβDrawing the Range Rectangle π¨
To make our hunt pop, we paint a blue rectangle around the consolidation zone, like marking a trap with neon glow.
void plotConsolidationRange(string rangeName,datetime time1_x1,double price1_y1,
datetime time2_x2,double price2_y2){
if (ObjectFind(0,rangeName) < 0){
ObjectCreate(0,rangeName,OBJ_RECTANGLE,0,time1_x1,price1_y1,time2_x2,price2_y2);
ObjectSetInteger(0,rangeName,OBJPROP_COLOR,clrBlue);
ObjectSetInteger(0,rangeName,OBJPROP_FILL,true);
ObjectSetInteger(0,rangeName,OBJPROP_WIDTH,5);
}
else {
ObjectSetInteger(0,rangeName,OBJPROP_TIME,0,time1_x1);
ObjectSetDouble(0,rangeName,OBJPROP_PRICE,0,price1_y1);
ObjectSetInteger(0,rangeName,OBJPROP_TIME,1,time2_x2);
ObjectSetDouble(0,rangeName,OBJPROP_PRICE,1,price2_y2);
}
ChartRedraw(0);
}We name our rectangle βCONSOLIDATION RANGEβ and check if it exists. If not, we create it, spanning from the rangeβs start (20 candles back) to the current candle, covering high to low, painted blue, filled, with a 5-pixel width, like a bold glow in the savanna. If itβs already there, we update its coordinates, keeping it sharp. This rectangle is our trapβs beacon, making the range pop on the chart like a neon corral. Itβs like shouting, βHereβs where the gazelles are grazing!β
Step 6: Packing Up CampβCleaning Up Tracks ποΈ
When the huntβs done, or weβre moving camp, we tidy up to leave no tracks behind.
void OnDeinit(const int reason)
{
}Our cleanup is barebonesβno objects or indicators to clear, like a tracker vanishing without a trace. The dynamic rectangle persists, but we could add cleanup in the future to wipe it, like erasing campfire embers. For now, itβs a light pack-up, ready for the next stalk, keeping our savanna pristine.
Why This EAβs a Safari Legend (and Keeps You Stoked!) π₯
This EA is a breakout-snaring legend, spotting tight ranges like a tracker reading hoofprints and signaling stampedes like a gazelleβs bolt. Itβs pro-grade trading with a safari swagger, keeping you hooked with its dynamic range tweaks and visual flair. Picture stalking GBPUSD on H1, catching a 400-pip range, and nailing a 200-pip breakout when it surgesβthatβs this botβs vibe! Itβs primed for you to add trading logic, like loading your snare with cannons, and the blue rectangle makes ranges glow like a jungle beacon. Tweak the range size (500 pips) or window (20 candles) to match your hunt, like sharpening your spear for bigger game.
Putting It All Together
To unleash this EA:
Crack open MetaEditor in MetaTrader 5 like youβre storming the savanna.
Paste the code, compile (F5), and fix typosβno tracker wants a busted trap.
Drop the EA on your chart, enable AutoTrading, and watch for blue rectangles and breakout signals.
Add trading logic (e.g., buy/sell orders) to strike on signals, like arming your snare.
Stay sharpβtest on a demo first, βcause even trackers scout before a big hunt!
Conclusion
Weβve forged a breakout-snaring powerhouse that traps consolidation ranges, paints them blue, and flags explosive moves like a gazelle on the run. This MQL5 code is our safari snare, explained with detail to make you a trading tracker, humor to keep you chuckling, and flow to carry you like a savanna wind. Ready to hunt? Check our video guide on the website for a front-row seat to this wild adventure. Now go snare those pips! π¦
Disclaimer: Tradingβs like hunting a savanna beastβthrilling but risky. Losses can exceed deposits. Test on a demo account before going live.