Tarea técnica
MT4 Direction Control EA — Developer Specification
1. Purpose
Create an MT4 Expert Advisor that allows the trader to control the permitted trade direction on the chart using three on-chart buttons:
- BUY ONLY
- SELL ONLY
- ALLOW ALL
The EA does not generate trades itself. Its primary function is to control and enforce the allowed trade direction.
2. Chart / Symbol Behaviour
When the EA is attached to a chart:
- It applies to the currency pair/symbol of that chart.
- Example:
- EA attached to EURUSD → controls EURUSD.
- EA attached to GBPUSD → controls GBPUSD.
- The trader can change the permitted direction by clicking the buttons.
3. Buttons
Display three buttons on the chart:
BUY ONLY
When selected:
- BUY trades are permitted.
- SELL trades are NOT permitted.
- Any existing SELL trade for that symbol should be closed immediately.
- If a new SELL trade is opened manually or by another EA, the Direction Control EA must detect it and close it immediately.
SELL ONLY
When selected:
- SELL trades are permitted.
- BUY trades are NOT permitted.
- Any existing BUY trade for that symbol should be closed immediately.
- If a new BUY trade is opened manually or by another EA, the Direction Control EA must detect it and close it immediately.
ALLOW ALL
When selected:
- Both BUY and SELL trades are permitted.
- The EA does not close either direction.
4. Immediate Opposite-Trade Protection
The EA must continuously monitor open trades for the chart symbol.
If the current mode is:
BUY ONLY
→ Any SELL position detected must be closed immediately.
SELL ONLY
→ Any BUY position detected must be closed immediately.
The monitoring must be as fast as practically possible within MT4 limitations.
The EA should check on:
- Every incoming tick
- Trade/order-state changes where possible
- EA initialisation
- Button mode changes
The developer should optimise the code to minimise the delay between detecting an unauthorised trade and sending the close request.
5. Existing Trades When Button Is Changed
If the trader changes from:
ALLOW ALL → BUY ONLY
→ Immediately close all existing SELL trades for that symbol.
If changing:
ALLOW ALL → SELL ONLY
→ Immediately close all existing BUY trades for that symbol.
If changing:
BUY ONLY → SELL ONLY
→ Immediately close all BUY trades.
If changing:
SELL ONLY → BUY ONLY
→ Immediately close all SELL trades.
6. New Trade Protection
The EA must also protect against trades opened after the direction has been selected.
Example:
BUY ONLY is selected.
Another EA opens a SELL trade on EURUSD.
The Direction Control EA detects the SELL trade and immediately attempts to close it.
This must work for:
- Manually opened trades
- Trades opened by another EA
- Market orders
7. Symbol Scope
Default behaviour:
Only control trades belonging to the chart symbol.
Example:
EA attached to EURUSD:
- Controls EURUSD trades.
- Does NOT control GBPUSD trades.
- Does NOT control XAUUSD trades.
8. Magic Number Behaviour
The EA should have an input option:
Control Manual Trades = true/false
Control Other EA Trades = true/false
Recommended default:
- Manual trades: TRUE
- Other EA trades: TRUE
The developer should provide filtering options if required by the MT4 implementation.
9. Initial Mode
Input parameter:
InitialMode
Options:
- BUY_ONLY
- SELL_ONLY
- ALLOW_ALL
Recommended default:
ALLOW_ALL
The EA should remember the currently selected mode while it remains attached to the chart.
10. Button Appearance
The currently active mode should be visually obvious.
Example:
BUY ONLY → highlighted/active
SELL ONLY → highlighted/active
ALLOW ALL → highlighted/active
The inactive buttons should remain visible but clearly indicate they are not selected.
Button position should be configurable.
11. EA Inputs
Suggested developer inputs:
- InitialMode
- ControlManualTrades
- ControlOtherEAs
- ControlCurrentSymbolOnly = true
- CheckOnEveryTick = true
- CloseRetryAttempts
- RetryDelayMilliseconds
- ButtonCorner
- ButtonX
- ButtonY
- ButtonWidth
- ButtonHeight
- ButtonFontSize
12. Important Requirement
The EA must NOT open trades.
It only:
- Allows the selected direction.
- Blocks/cleans up the opposite direction.
- Continuously monitors the symbol.
- Immediately closes unauthorised opposite trades.
The EA should be lightweight and designed for extremely fast execution.