Trabalho concluído
Termos de Referência
I need a simple but reliable safety EA for MetaTrader 5 (MQL5) to protect my account from margin calls while running multiple Dark Gold EAs on Pepperstone UK.
My setup
– Broker: Pepperstone UK
– Platform: MT5
– Stop-out: 50%
– Leverage: 30:1
– Strategy: Several Dark Gold EAs (around 6 charts). Each uses a fixed grid with no martingale, for example: 0.01, 0.01, 0.01 … up to about 15 positions. All grid positions are equal lot size.
Goal of the EA
I want a separate safety EA that I attach to ONE chart only. It does not open trades. It only:
– Monitors account margin level
– When margin level gets too low (for example 80%), it closes exactly ONE trade: the oldest losing trade
– Waits a configurable cooldown time
– Repeats if needed, until margin improves or there are no more trades
Reasoning
Because I use a fixed grid with equal lot size, the oldest trades are usually the ones furthest away from the current price and doing the most damage to the basket’s average entry and take profit. Closing the oldest losing trade removes the “tail” of the grid and pulls the basket TP closer to current price, so a smaller retrace can clean up the rest.
Functional requirements
-
Attachment and scope
– EA runs on exactly one chart (any symbol).
– It monitors all open positions on the whole account, not just the chart symbol.
– It must be compatible with running multiple Dark Gold EAs in parallel. -
Margin level trigger
– On every tick, the EA reads ACCOUNT_MARGIN_LEVEL (percentage).
– Input: MarginLevelThreshold (double, default 80.0).
– If margin level > threshold: do nothing.
– If margin level <= threshold: trigger the close logic. -
Trade selection logic
– EA scans all open positions.
– Optional filter by Magic Number:
– Input: MagicFilter (long, default -1).
– If MagicFilter = -1: manage all trades.
– If MagicFilter = some value: manage only trades with that magic.
– Optional filter “only losing trades”:
– Input: OnlyCloseLosing (bool, default true).
– If true: only trades with POSITION_PROFIT < 0 are considered.
– From the remaining eligible trades, select the oldest by POSITION_TIME.
– Close exactly that one position. -
Closing and cooldown
– After closing one position successfully, the EA must wait a minimum cooldown time before closing another, even if margin is still low.
– Input: CooldownSeconds (int, default 5).
– The idea is a “slow bleed” of risk, not an instant flush of the whole account. -
Notifications
– Input: SendPushNotifications (bool, default true).
– If enabled, when the EA closes a position it should send SendNotification() with: ticket, symbol, volume, and margin level at the moment of closing.
– Also log this clearly in the Experts tab. -
Safety conditions
– The EA must not open trades.
– It must not modify SL/TP of existing trades.
– It must only close trades as per the margin logic above.
– It must not freeze or block the terminal (no heavy loops).
Inputs (must be configurable in EA parameters)
– MarginLevelThreshold (double, default 80.0)
– OnlyCloseLosing (bool, default true)
– CooldownSeconds (int, default 5)
– MagicFilter (long, default -1)
– SendPushNotifications (bool, default true)
– Optional: BrokerStopOutLevel (double, default 50.0) just for logging / information
Example behaviour
– Margin level at 300%: EA does nothing.
– Margin level falls to 82%: still above 80%, EA does nothing.
– Margin level drops to 80% or below:
– EA scans eligible trades (respecting MagicFilter and OnlyCloseLosing).
– Picks the oldest losing trade by POSITION_TIME.
– Closes this single trade.
– Logs it and optionally sends a push notification.
– Starts a cooldown (for example 5 seconds).
– After the cooldown, if margin is still <= threshold, it repeats and closes the next oldest losing trade.
– This continues until margin recovers above threshold or there are no more eligible positions.
Deliverables
– MQL5 source code (.mq5) with clear comments.
– Compiles and runs cleanly on latest MT5 build.
– Brief explanation in comments or text where:
– Margin level is checked
– Oldest losing trade is selected
– Cooldown is managed
– MagicFilter is applied