104 lines
No EOL
3.5 KiB
Markdown
104 lines
No EOL
3.5 KiB
Markdown
# Close at Profit / Loss — EA for MetaTrader 5
|
|
|
|
**Version:** 1.10
|
|
**Platform:** MetaTrader 5
|
|
**Type:** Expert Advisor (utility)
|
|
**Symbol:** any
|
|
**Timeframe:** any
|
|
|
|
---
|
|
|
|
## Description
|
|
|
|
A utility Expert Advisor that automatically closes all open positions (and optionally pending orders) when a defined profit or loss threshold is reached — across the current symbol or all symbols simultaneously.
|
|
|
|
Designed for traders who want a simple, reliable account-level safety net without manual intervention. Key features:
|
|
|
|
- unified `CloseAll()` logic with configurable symbol scope and pending order handling
|
|
- dynamic profit/loss color coding on the info panel (green / red)
|
|
- panel displays active TP and SL thresholds at a glance
|
|
- aligned two-column panel layout with visual separators
|
|
- proper cleanup of all panel objects on `OnDeinit()`
|
|
- `ChartRedraw()` called after each panel update
|
|
|
|
---
|
|
|
|
## Parameters
|
|
|
|
| Parameter | Default | Description |
|
|
|---|---|---|
|
|
| `useProfitToClose` | `true` | Enable closing at profit threshold |
|
|
| `profitToClose` | `200` | Profit threshold in account currency ($) |
|
|
| `useLossToClose` | `false` | Enable closing at loss threshold |
|
|
| `lossToClose` | `100` | Loss threshold in account currency ($) |
|
|
| `AllSymbols` | `false` | Apply to all symbols (true) or current symbol only (false) |
|
|
| `PendingOrders` | `true` | Also delete pending orders when closing |
|
|
| `MaxSlippage` | `3` | Maximum allowed slippage in pips |
|
|
| `showMenu` | `true` | Display the info panel on chart |
|
|
| `menuColor` | `LightSteelBlue` | Panel header color |
|
|
| `valueColor` | `Teal` | Panel value color (neutral) |
|
|
| `profitColor` | `LimeGreen` | Panel value color when in profit |
|
|
| `lossColor` | `Tomato` | Panel value color when at a loss |
|
|
| `fontSize` | `10` | Panel font size |
|
|
| `Magic` | `-1` | Magic number filter (-1 = all EAs) |
|
|
| `PanelX` | `10` | Panel X offset (px) |
|
|
| `PanelY` | `20` | Panel Y offset (px) |
|
|
| `PanelCorner` | `CORNER_LEFT_UPPER` | Panel anchor corner |
|
|
|
|
---
|
|
|
|
## Info Panel
|
|
|
|
The on-chart panel shows in real time:
|
|
|
|
```
|
|
Close at Profit / Loss
|
|
--------------------------------
|
|
Positions (S / B): 2 / 1
|
|
Lots (S / B): 0.10 / 0.20
|
|
Profit (S / B): -12.50 / 34.80
|
|
--------------------------------
|
|
Take Profit: 200.00 $
|
|
Stop Loss: disabled
|
|
```
|
|
|
|
The profit row changes color dynamically: **green** when the combined P&L is positive, **red** when negative.
|
|
|
|
---
|
|
|
|
## How It Works
|
|
|
|
On every tick the EA:
|
|
|
|
1. Calculates the combined floating P&L (profit + swap) of all matching positions.
|
|
2. If P&L exceeds `profitToClose` → closes all matching positions (and optionally pending orders).
|
|
3. If P&L drops below `-lossToClose` → same action.
|
|
4. If a close attempt fails, it retries on the next tick until successful.
|
|
|
|
---
|
|
|
|
## Installation
|
|
|
|
1. Copy `_Close_at_PRofit.mq5` to `MQL5/Experts/`.
|
|
2. Compile in MetaEditor (F7) — should produce zero errors/warnings.
|
|
3. Attach to any chart. The EA does not open positions on its own.
|
|
4. Configure `profitToClose` / `lossToClose` to match your risk management rules.
|
|
|
|
> **Tip:** Set `Magic = -1` to manage all positions regardless of which EA opened them, or enter a specific magic number to manage only one strategy's trades.
|
|
|
|
---
|
|
|
|
## Changelog
|
|
|
|
| Version | Date | Changes |
|
|
|---|---|---|
|
|
| 1.10 | 2025 | Unified `CloseAll()`, dynamic P&L color, TP/SL rows on panel, `ChartRedraw()`, proper `OnDeinit()` cleanup |
|
|
| 1.00 | 2025 | Initial release |
|
|
|
|
---
|
|
|
|
## License
|
|
|
|
© 2025 [sunokfx](https://www.mql5.com/en/users/sunokfx)
|
|
|
|
Published for free use. Redistribution with attribution permitted. |