Trebuchet Bracket Engine
- Experts
-
Syarief Azman Bin Rosli
Hello and welcome to my official MQL5 profile!
I am Syarief Azman Bin Rosli, an algorithmic trading systems developer and quantitative software engineer with over a decade of specialized experience creating robust automated trading solutions for MetaTrader 5 and MetaTrader 4. - Version: 1.0
- Activations: 5
Trebuchet Bracket Engine is a MetaTrader 4 Expert Advisor for daily range-expansion breakouts. At the anchor (the day open by default) it measures the recent N-day range, brackets the anchor with a buy stop above and a sell stop below at K x range, puts the stop loss at the anchor and closes everything at the day exit. At most one trade per side per day; no grid, martingale, averaging or overnight holding (unless you switch the day exit off).
Quick Start
- Attach the EA to one chart of the symbol (any timeframe; the logic uses daily bars and the clock) and enable AutoTrading plus "Allow live trading".
- Keep the defaults first: 4-day Dual Thrust range, triggers at 0.50 x range, stop at the anchor, entry cutoff 21:00 and day exit 22:00 server time, 1 % risk.
- After the anchor the panel shows BRACKET ARMED and two pending orders appear: a buy stop and a sell stop.
- Check: a visual Strategy Tester run on XAUUSD M15 over two weeks draws every bracket and trade.
- Push alerts: set your MetaQuotes ID (Tools > Options > Notifications) and Push Notifications = true.
How It Trades
Range: Range Basis Dual Thrust (max of HH-LC, HC-LL) uses the N completed days before today (Range Lookback): HH/LL are their highest high and lowest low, HC/LC their highest and lowest close, range = the larger of HH-LC and HC-LL. Average daily range = mean high-low of those days; Previous day range = yesterday's high-low.
Anchor: at Anchor Time the anchor price is fixed. 00:00 uses the daily open (the first tick of the server day). Another time uses the open of the first bar at or after it.
Triggers: buy trigger = anchor + Buy Trigger Distance x range, sell trigger = anchor - Sell Trigger Distance x range. If range / daily ATR (Daily ATR Period) is below Minimum Range or above Maximum Range there is no bracket that day (RANGE TOO NARROW / RANGE TOO WIDE).
Entry Orders:
- Pending stop orders: a buy stop and a sell stop are placed right after the anchor. If a trigger is closer than the broker stop level, that side is watched by the EA instead and entered at market (VIRTUAL).
- Market order on touch: no pending orders; the EA buys when Ask reaches the buy trigger and sells when Bid reaches the sell trigger (skipped while the spread exceeds Max Spread for Market Entries).
A side whose trigger was already passed when the bracket is set (for example after a restart) is MISSED and never chased. Each side trades at most once per day, and Max Trades per Day caps the total. Cancel the Opposite Order on Fill deletes the other side when one fills.
Stop Loss Placement:
- At the anchor: the stop sits at the anchor price.
- At the opposite trigger (stop and reverse): the long stop sits at the sell trigger and the short stop at the buy trigger, so a failed breakout can flip into the other side.
- Range fraction from entry: entry minus (plus for sells) Stop Distance for Range Fraction x range.
Stops are widened to the broker minimum when needed.
Exits: Take Profit sets a target at that R multiple of the stop distance (0 = no target). Breakeven Trigger moves the stop to the entry once the trade has gained that many R. At Entry Cutoff all pending orders are deleted; at Day Exit Time every position of the EA is closed, and a position from an earlier server day is closed at once (empty = hold overnight). Closes, deletes and stop changes are sent only outside the freeze distance and inside the trading session, otherwise retried.
Lot Sizing: Fixed lots trades Fixed Lots. Risk % of balance: lots = balance x Risk per Trade / 100 / loss per lot at the stop, rounded down to the lot step and capped by Max Lots. Below the broker minimum the minimum lot is used, so small accounts, or gold with wide daily ranges, may risk more than the set percent. Free margin is checked before each order (NO MARGIN).
Recommended: XAUUSD and GBPUSD. Low-volatility pairs such as EURUSD gave weak results with the defaults. Times are broker server time (designed for GMT+2/+3 servers). Worth optimizing: Range Lookback, both trigger distances, Stop Loss Placement, Day Exit Time and Maximum Range.
Panel Controls
- [-] / [+]: collapse to a pill (TREBUCHET plus the state) or expand.
- <>: move the panel to the next chart corner.
- PAUSE / RESUME: PAUSE deletes the pending orders and blocks new entries (open positions stay managed); RESUME re-places today's bracket if it is still valid. Saved in a global variable, so it survives restarts.
- BRACKET ON / BRACKET OFF: show or hide the chart lines.
- CLOSE ALL: the first click shows CONFIRM?, a second click within 4 seconds closes every position and deletes every pending order of this EA on the symbol.
Collapse, corner and bracket lines return to the inputs when the EA is re-attached.
Panel Readouts
- Header LED: green while the bracket is armed or a trade is live, amber when paused, grey otherwise.
- DAY STATE: badge WAITING FOR ANCHOR, BRACKET ARMED, LONG LIVE, SHORT LIVE, PAUSED, DAY QUOTA USED, DAY DONE (after the cutoff), BRACKET SPENT (both sides used), RANGE TOO NARROW, RANGE TOO WIDE or NO DAILY HISTORY; date; ANCHOR time and price; CUTOFF and EXIT times.
- RANGE ENGINE: lookback and basis; RANGE value with both trigger distances K; HH, LL, HC, LC; a meter of range / ATR from 0 to 3 with ticks at both limits; the ratio, the limits and OK or the reason.
- BRACKET: gauge with the sell trigger (coral), the anchor (bronze) and the buy trigger (teal); the marker shows Bid and turns teal or coral beyond a trigger. Below: SELL, ANCHOR and BUY prices and the distance to each trigger in points. Right caption: STOP AT ANCHOR, STOP+REVERSE or STOP x.xxR, and the target in R.
- Side lines LONG and SHORT: READY, BUY STOP AT / SELL STOP AT price, WATCHING price (VIRTUAL), FILLED - IN TRADE, TRADED TODAY, MISSED: PRICE ALREADY BEYOND, SKIPPED: NO MARGIN or ORDER FAILED, CANCELLED: OCO, CUTOFF, QUOTA, PAUSED, MANUAL or NEW DAY, NO BRACKET TODAY.
- POSITION: FLAT, or side, lots, entry and floating profit; SL, TP, R and BE ([x] once the stop is at breakeven). When flat: TODAY trades / maximum and DAY P/L, or NEW ENTRIES PAUSED.
- ACTIVITY: the last three events with time: BRACKET, NO BRACKET, BUY STOP / SELL STOP placed, BUY FILLED / SELL FILLED, STOP CANCELLED, MISSED, SKIPPED, ORDER FAILED, BREAKEVEN, CLOSED (day exit, previous day, manual), CLOSE ALL, PAUSED by user, RESUMED by user.
- Deck line: TRADES, WIN percent and NET closed profit of this EA on the symbol.
Chart Overlays
From the anchor to the day exit: a teal BUY TRIGGER line, a coral SELL TRIGGER line and a dotted bronze ANCHOR line, labelled for today; Past Days Drawn on Chart earlier brackets in thinner lines (grey when filtered). Shown on chart timeframes up to H4.
Alerts
Pop-up Alerts and Push Notifications fire when the bracket is set at the anchor, on each fill and on each close. Not in the Strategy Tester.
Side Effects
- Global variable TBE_PAUSE_<symbol>_<magic> = 1 while paused.
- Chart objects prefixed TBE_P_ (panel) and TBE_Z_ (overlays), removed with the EA. One-second timer. No files, no DLLs.
bool paused = GlobalVariableCheck("TBE_PAUSE_XAUUSD_26110901"); Input Parameters
Range Engine
- Range Lookback (4 days), Range Basis (Dual Thrust (max of HH-LC, HC-LL); or Average daily range, Previous day range).
- Buy Trigger Distance (0.50) and Sell Trigger Distance (0.50): x range.
- Daily ATR Period (14 days), Minimum Range (0.0 x ATR, 0 = off), Maximum Range (2.5 x ATR, 0 = off).
Schedule
- Anchor Time (00:00 = day open), Entry Cutoff (21:00), Day Exit Time (22:00, empty = off): server time, anchor before cutoff, cutoff not after the exit.
Orders and Exits
- Entry Orders (Pending stop orders; or Market order on touch).
- Stop Loss Placement (At the anchor; At the opposite trigger (stop and reverse); Range fraction from entry), Stop Distance for Range Fraction (0.30 x range).
- Take Profit (0.0 R = day exit only), Breakeven Trigger (0.0 R = off).
- Max Trades per Day (2), Cancel the Opposite Order on Fill (false).
Money Management
- Lot Sizing (Risk % of balance; or Fixed lots), Fixed Lots (0.10), Risk per Trade (1.0 %), Max Lots (5.0).
- Max Spread for Market Entries (0 points = off).
Execution
- Magic Number (26110901, one per chart on the same symbol), Max Slippage (30 points), Order Comment (Trebuchet).
Display and Alerts
- Show Panel (true), Draw Bracket Levels (true), Past Days Drawn on Chart (5), Start Collapsed (false), Panel Scale (1.0, 0.5-2.5).
- Pop-up Alerts (true), Push Notifications (false).
Typical Setups
- Classic daily bracket (XAUUSD, GBPUSD): defaults.
- Stop and reverse: Stop Loss Placement = At the opposite trigger (stop and reverse), Max Trades per Day 2.
- London bracket: Anchor Time 08:00, Entry Cutoff 16:00, Day Exit Time 21:00, Range Basis = Average daily range, both trigger distances 0.40.
- Target and protect: Take Profit 2.0, Breakeven Trigger 1.0, Cancel the Opposite Order on Fill true.
Notes
- After a restart the bracket is rebuilt from daily bars; existing orders keep their sides and a passed trigger is never chased.
- Pending orders never survive into a new day.
- An empty Day Exit Time is accepted only together with a Take Profit above 0; otherwise a trade could only close at its stop.
- The Strategy Tester uses the same rules; panel and lines appear only in visual mode. Test on a demo account first.
Risk Disclaimer
Trading forex and CFDs on margin carries a high risk of loss. Past or simulated results do not guarantee future results. This EA does not promise profit; use only capital you can afford to lose.
