Forex Algo Trader
Sign In Register
Market Resources Tools Autotrading Algo Jobs
Building a Breakout-Hunting Beast with Consolidation Ranges πŸ†
Resource

Building a Breakout-Hunting Beast with Consolidation Ranges πŸ†

Join us for a wild, detailed, and laugh-packed trek through our MQL5 code that automates breakout trading by spotting tight price ranges and pouncing on explosive moves. We’ll lay out every step with code blocks, keep the flow smoother than a jungle river, and toss in examples so you don’t doze offβ€”all while staying pro and fun! 😎
Allan Munene Mutiiria Verified
@allan_munene_mutiiria
1 year ago 410 views 0 comments 7 min read 2 files

Alright, trading warriors, grab your machetes and night-vision goggles, β€˜cause we’re diving into the forex jungle to build an MQL5 Expert Advisor (EA) that’s like a panther stalking breakout gold! This ain’t just codeβ€”it’s a heart-pounding hunt we’re crafting together, and we’re gonna map it out with a flow so slick it’s like gliding down a river. We’ll keep it pro, stuffed with detail to make you a coding beast, and spiced with humor and examplesβ€”like picturing you as a jungle tracker snaring pipsβ€”so you stay wide awake and grinning. Our mission? Build a bot that spots tight price ranges, marks them with a rectangle, and signals buy or sell breakouts when prices bust loose, all without trading logic yet (but ready for you to add the firepower). Let’s hack through the vines and unleash this breakout beast! 🏹

Step 1: Setting Up the Jungle Campβ€”Prepping Our Hunt 🌴

First, we’re pitching our jungle camp, the base where we sharpen our spears and prep our traps. This is like gearing up with a map, compass, and a trusty machete before stalking our prey.

#define RANGE "Range Rectangle"

int OnInit()
{
   datetime
         t1 = D'2023/09/05 06:00',
         t2 = D'2023.09.11 02:00';
   double
         p1 = 0.64000,
         p2 = 0.63650;
         
   ObjectDelete(0,"REC");
   ObjectCreate(0,"REC",OBJ_RECTANGLE,0,t1,p1,t2,p2);
   ObjectSetInteger(0,"REC",OBJPROP_COLOR,clrLime);
   ObjectSetInteger(0,"REC",OBJPROP_FILL,false);
   ObjectSetInteger(0,"REC",OBJPROP_WIDTH,7);

   return(INIT_SUCCEEDED);
}

We start by rigging our camp with a test rectangleβ€”think of it as a practice snare to mark our territory. We define a rectangle named β€œREC” with hardcoded dates (September 5 to 11, 2023) and prices (0.64000 to 0.63650), like sketching a mock trap on our map. We paint it lime green, unfilled, with a beefy 7-pixel width, like a glowing marker in the jungle. This rectangle is a one-time setup, drawn when we load the EA, to test our drawing skills before the real hunt. We clear any old β€œREC” first, ensuring a clean slate, and return a success signal to confirm our camp’s ready. It’s like saying, β€œTraps set, let’s hunt!” with a practice run to boost our confidence.

Step 2: Scouting the Clearingβ€”Spotting Tight Ranges 🧐

Camp set? Time to scout the jungle clearing, searching for a tight price range where the market’s chilling like a panther before a leap. This is where we hunt for consolidation zones.

int totalBars = 0;

void OnTick()
{
   double
      Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits),
      Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

   int 
      indexHighestBar = iHighest(_Symbol,_Period,MODE_HIGH,50,1),
      indexLowestBar = iLowest(_Symbol,_Period,MODE_LOW,50,1);
   double
      rangeHighPrice = iHigh(_Symbol,_Period,indexHighestBar),
      rangeLowPrice = iLow(_Symbol,_Period,indexLowestBar);
   datetime
      RangeStartTime = iTime(_Symbol,_Period,30),
      RangeEndTime = iTime(_Symbol,_Period,0);
   
   double currentRangeHeight = NormalizeDouble(rangeHighPrice - rangeLowPrice,_Digits);
   bool isInRange = currentRangeHeight <= 500*_Point;
   
   if (isInRange){
      drawRange(RangeStartTime,rangeHighPrice,RangeEndTime,rangeLowPrice);
   }
}

Every market tick is like a rustle in the bushes, so we grab the current ask and bid prices, like checking the wind direction. We scan the last 50 candles to find the highest and lowest prices, like spotting the tallest tree and deepest ditch in the clearing. If the range between them is 500 pips or less, we’ve found our consolidation zoneβ€”our panther’s pacing cage. We mark this range with timestamps from 30 candles back to the current candle, like setting a trap’s boundaries. If the range is tight, we call our drawing function to paint a red rectangle on the chart, like a neon sign shouting, β€œHere’s the quiet zone!” This is our β€œAha!” moment, like a tracker spotting fresh paw prints. If the range is too wide, we hold off, waiting for a tighter clearing, like a patient hunter avoiding a noisy jungle.

Step 3: Springing the Trapβ€”Signaling Breakouts πŸ”₯

Range spotted? Time to set the snare and pounce when the market breaks out, like a panther busting free from its cage.

void OnTick()
{
   // ... (range detection code as above)
   if (isInRange){
      if (Ask > rangeHighPrice){
         int bars = iBars(_Symbol,_Period);
         if (totalBars == bars) return;
         totalBars = bars;
         Comment("BUY BREAKOUT");
         Print("BUY BREAKOUT");
         isInRange = false;
         return;
      }
      else if (Bid < rangeLowPrice){
         int bars = iBars(_Symbol,_Period);
         if (totalBars == bars) return;
         totalBars = bars;
         Comment("SELL BREAKOUT");
         Print("SELL BREAKOUT");
         isInRange = false;
         return;
      }
      return;
   }
}

If we’re in a tight range, we watch for the breakout. If the ask price surges above the range’s high, it’s like the panther leaping upward, triggering a β€œBUY BREAKOUT” signal. If the bid crashes below the low, it’s like a dive into the shadows, cueing a β€œSELL BREAKOUT.” We only act on new candles, checking the bar count to avoid spamming like a trigger-happy hunter. When a breakout hits, we log and display it on the chart, like shouting β€œGotcha!” and waving a flag. We then reset our range flag, like pulling up the snare for the next hunt. This is pure adrenaline, like spotting the panther’s leap and knowing it’s go-time!

Step 4: Painting the Trapβ€”Drawing the Range Rectangle 🎨

To make our hunt visual, we paint a red rectangle around the consolidation zone, like marking a trap with glowing paint.

void drawRange(datetime time1,double price1,datetime time2,double price2){
   int found = ObjectFind(0,RANGE);
   if (found < 0){
      ObjectCreate(0,RANGE,OBJ_RECTANGLE,0,time1,price1,time2,price2);
   }
   else{
      ObjectSetInteger(0,RANGE,OBJPROP_TIME,0,time1);
      ObjectSetDouble(0,RANGE,OBJPROP_PRICE,0,price1);
      ObjectSetInteger(0,RANGE,OBJPROP_TIME,1,time2);
      ObjectSetDouble(0,RANGE,OBJPROP_PRICE,1,price2);
      ObjectSetInteger(0,RANGE,OBJPROP_FILL,false);
      ObjectSetInteger(0,RANGE,OBJPROP_COLOR,clrRed);
      ObjectSetInteger(0,RANGE,OBJPROP_WIDTH,3);
      ObjectSetInteger(0,RANGE,OBJPROP_STYLE,STYLE_SOLID);
      ObjectSetInteger(0,RANGE,OBJPROP_BACK,true);
   }
}

We define a rectangle named β€œRange Rectangle” and check if it exists. If not, we create it, spanning from 30 candles back to the current candle, covering the range’s high to low. If it’s already there, we update its coordinates, painting it red, unfilled, with a 3-pixel width, like a bold outline in the jungle. This rectangle is our trap’s marker, making the consolidation zone pop on the chart like a glowing snare. It’s like saying, β€œHere’s where the panther’s hiding!” so we never miss the action. See below.

Step 5: Packing Up Campβ€”Cleaning Up Loose Ends πŸ•οΈ

When the hunt’s over, or we’re moving camp, we clean up to keep the jungle tidy.

void OnDeinit(const int reason)
{
}

Our cleanup is minimalβ€”no objects or indicators to clear, like a tracker leaving no trace. The test rectangle (β€œREC”) is set once, and the dynamic range rectangle persists, but we could add cleanup later if we want a spotless chart, like sweeping away campfire ashes. For now, it’s a light pack-up, ready for the next stalk.

Why This EA’s a Jungle Legend (and Keeps You Hooked!) 🀘

This EA is a breakout-hunting beast, spotting tight ranges like a tracker eyeing paw prints and signaling breakouts like a panther’s leap. It’s pro-grade trading with a jungle swagger, keeping you awake with its simplicity and potential. Imagine stalking EURUSD, seeing a 400-pip range on H1, and catching a 300-pip breakout when it busts looseβ€”that’s this bot’s vibe! It’s ready for you to add trading logic, like arming your trap with cannons, and the red rectangle makes ranges pop like a neon sign. Tweak the range size (500 pips) or candle window (50) to match your style, like sharpening your machete for bigger prey.

Putting It All Together

To unleash this EA:

  1. Fire up MetaEditor in MetaTrader 5 like you’re entering the jungle.

  2. Paste the code, compile (F5), and fix typosβ€”nobody wants a broken trap.

  3. Drop the EA on your chart, enable AutoTrading, and watch for red rectangles and breakout signals.

  4. Add trading logic (e.g., buy/sell orders) to pounce on signals, like arming your snare.

  5. Stay sharpβ€”test on a demo first, β€˜cause even trackers scout before a hunt!

Conclusion

We’ve built a breakout-hunting beast that spots consolidation ranges, paints them red, and signals explosive moves like a panther on the prowl. This MQL5 code is our jungle trap, explained with detail to make you a trading tracker, humor to keep you grinning, and flow to carry you like a river. Ready to stalk? Check our video guide on the website for a front-row seat to this jungle adventure. Now go snare those pips! πŸ†

Disclaimer: Trading’s like hunting a jungle beastβ€”thrilling but risky. Losses can exceed deposits. Test on a demo account before going live.

Attachments
CONSOLIDATION_RANGE_BREAKOUT_EA.ex5 Β· 6.8 KB
Download
CONSOLIDATION_RANGE_BREAKOUT_EA.mq5 Β· 3.9 KB
Download
410 views
Discussions 0

No discussions yet

Be the first to share your thoughts on this resource.

Sign in to take part
Sign in or register to comment and report.
0%
Uploading…
Please don’t close this page