• Welcome back! Thank you for being a part of this Traders Community. Let's discuss and share :)
    Selamat datang kembali! Trimakasih telah menjadi bagian dari Komunitas Trader ini. Mari berdiskusi dan berbagi :)

MTF Trend-Following Pyramiding EA using D1/H4/M15

uuuuuu

New Member
Credits
69

MTF Trend-Following Pyramiding EA​


Using D1, H4 and M15 Timeframes​


This EA is a trend-following pyramiding Expert Advisor that uses multiple timeframes: D1, H4 and M15.


It enters in the direction of the higher-timeframe trend, adds positions as the trade moves into profit, and tightens the trailing stop as the number of positions increases. The idea is to ride strong trends while eventually closing all positions together through a synchronized stop-loss system.


1. Trading Environment​


Supported symbols:
No symbol restriction. The EA trades the symbol of the chart it is attached to.


Operating timeframe:
The EA logic is hardcoded to work mainly on M15 data, regardless of the chart timeframe.
It internally uses D1, H4 and M15 data for trend detection, entry and exit management.


2. Initial Entry Logic​


The EA opens the first position only when all conditions are met at the close of an M15 candle.


Buy Entry Conditions​


Higher-Timeframe Trend Filter​


The higher-timeframe trend must be bullish:


  • D1 short MA period 5 is above D1 long MA period 15
  • H4 short MA period 15 is above H4 long MA period 40

Pullback Filter​


The current price must not be too far away from the H4 short moving average.


By default, the allowed distance is:


M15 ATR × 10.0


This is used to avoid entering when the price is already too overextended from the main trend.


M15 Trigger​


The M15 trigger condition is a moving average breakout:


  • The previous M15 candle closed below the M15 trigger MA period 60
  • The current M15 candle closes above the trigger MA

Swing Stabilization Filter​


The EA also checks whether the recent swing low has stopped updating.


For a buy entry, the lowest low of the last 15 M15 candles must not have occurred within the most recent 1 candle.


This is meant to avoid entering while the market is still actively making new lows.


Sell Entry Conditions​


Sell conditions are the exact opposite of the buy conditions:


  • D1 and H4 trend filters must be bearish
  • Price must be close enough to the H4 short MA
  • M15 candle must cross below the trigger MA
  • The recent swing high must not have occurred within the most recent 1 candle

Initial Stop Loss​


For buy positions:
The initial SL is placed at the lowest low of the last 15 M15 candles.


For sell positions:
The initial SL is placed at the highest high of the last 15 M15 candles.


3. Pyramiding Logic​


When the trade moves in the trend direction, the EA adds positions.


Maximum Number of Positions​


Default value:


MaxPositions = 10


Add-on Entry Condition​


A new position is added when price moves at least:


M15 ATR × 0.8


from the open price of the most recently opened position.


Lot Size for Additional Positions​


The add-on lot size is calculated as:


FixedLot × PyramidScale


Default example:


0.01 × 2.0 = 0.02 lots


So the first position is 0.01 lots, and the second position onward will be 0.02 lots by default.


This is not a martingale system where the lot size keeps doubling every time.
After the first position, the add-on positions use a fixed scaled lot size.


Stop Loss for Additional Positions​


When a new position is added, its stop loss is synchronized with the safest existing stop-loss level among the current positions.


In other words, the new position does not receive an independent loose stop.
It joins the same shared SL structure as the existing positions.


4. Exit Logic: Dynamic Trailing Stop​


The EA uses a dynamic trailing stop system designed to let profits run while tightening the exit line as more positions are added.


All positions share the same synchronized SL level and are intended to be closed together.


Trailing Reference​


The trailing stop is based on the highest high or lowest low of the last 14 M15 candles.


Dynamic ATR Multiplier​


The ATR multiplier becomes smaller as the number of positions increases.


Formula:


Max Multiplier 8.0 - (Current Position Count - 1) × Step 1.0


Example:


  • 1 position: ATR × 8.0
  • 2 positions: ATR × 7.0
  • 3 positions: ATR × 6.0
  • And so on
  • Minimum multiplier: ATR × 2.0

So when there is only one position, the stop is loose and gives the trend room to develop.


As more positions are added, the stop becomes tighter to protect accumulated profit.


Buy Trailing Stop Update​


For buy positions, the EA calculates:


Highest high of the last 14 M15 candles - ATR × dynamic multiplier


If this new SL level is higher than the current SL, the EA raises the stop loss of all buy positions to that level.


For sell positions, the same logic is applied in reverse.


5. Safety and Protection Features​


This EA includes several protection mechanisms for practical live trading.


Stop-Loss Modification Retry System​


If the EA fails to modify a stop loss due to broker-side errors, fast price movement or temporary trading restrictions, it remembers the failed ticket number.


The EA then retries the SL modification on every tick until it succeeds.


Broker Stop-Level Adjustment​


If the requested SL is too close to the current market price and violates the broker’s minimum stop-level rule, the EA automatically adjusts the SL to the closest valid level allowed by the broker.


Spread Filter​


The EA has a spread filter to avoid entries during bad trading conditions.


If the current spread is above:


MaxSpread = 50 points


the EA skips new entries and pyramiding entries to reduce slippage and poor execution risk.


State Recovery After Restart​


When MT5 or the EA is restarted, the EA scans the currently open positions and restores its internal state.


It automatically detects whether it is currently in buy mode or sell mode based on the existing positions.




Summary​


This EA is designed as a multi-timeframe trend-following pyramiding system.


It uses D1 and H4 moving averages to define the main trend, M15 moving average breakout conditions for entry timing, ATR-based distance filters to avoid overextended entries, and a synchronized dynamic trailing stop to manage multiple positions as one basket.


The main concept is:


  • Enter only in the direction of the higher-timeframe trend
  • Add positions only when the market moves in favor of the trade
  • Tighten the trailing stop as exposure increases
  • Close all positions together through a shared stop-loss system
  • Avoid bad execution through spread and stop-level protection
  • Recover existing trade state after EA or MT5 restart

This is not a grid or martingale EA.
It is a trend-following pyramiding EA that increases exposure only when the trade is already moving in the intended direction.
 

Attachments

An interesting EA.
However, for XAUUSD, it should never be run "raw."

It requires:

stronger volatility filters
fewer open positions
smarter trailing stops
news filters
session filters
momentum blockers
improved entry logic

With these additions, it could indeed evolve into a very good Gold EA.

Unlike many other concepts, this one possesses at least one genuine statistical edge:
"Scale into winners rather than trying to rescue losers."

And that is precisely the approach that, in the long run, usually proves to be the right one.
 
it is interesting approach, my mt5 froze, any way Ronny you mention stronger volatility filters, anything particular ? I use choppynes index, still looking for right settings.
 
it is interesting approach, my mt5 froze, any way Ronny you mention stronger volatility filters, anything particular ? I use choppynes index, still looking for right settings.
CHOP alone is NOT enough.

Because a low CHOP can also mean:

the market is already completely overextended

that's exactly where stop-loss cascades occur. A truly effective combination for an Expert Advisor would be, for example:

1. CHOP Filter

Filters out sideways trading activity.

2. ATR Expansion Filter

Average True Range

Detects:

* sudden surge in volatility
* news-like movements
* candles that become "abnormally" large.

3. Candle Velocity Filter

Not just candle size.

But:

"How fast is gold currently moving?" Then:

* no new orders
* no adding to positions
* only basket management is active

This is often much better than standard candle filters.
 
ATR , did try few times, never worked in my favor, maybe a bit slow, or maybe wrong settings, haven't try Candle Velocity filter, did try scalping on candle size, hit and miss, maybe will get one of those CV fiters and try to incorporate, apparantlly is excellent for entry/exit, just did some reading about it. You recomend any, would you use it with trend ? Something else, what kind of trailling you use, guys that did program waygrow ea did very good job in designing trailling, some kind of hybrid trailling system, I'm trying with that one. I ain't programming guy, for that AI's are so helpfull, as long as one have decent idea.
 
Back
Top