Position Basket PL Closer
- Utilities
-
Kazuma Uezono
MQL5 / MQL4 / Pine Script v6 developer and active trader.
Seller on the MQL5 Market (indicators for MT4/MT5).
Services:
- TradingView Pine Script to MT5/MT4 conversion (bar-by-bar matching, with tester report)
- MT4 to MT5 migration - Version: 1.2
- Activations: 5
This utility Expert Advisor for MetaTrader 5 groups the open positions of your account into baskets and closes a whole basket when its total profit or loss reaches a level you set. The positions can come from your manual trading or from other EAs. The panel on the chart shows the short name "Basket P/L Closer".
What it is not: it never opens trades on a real or demo account, it gives no buy or sell signals and it is not a trading strategy. All four close conditions are OFF by default, so after you attach it, it only shows the baskets until you turn a condition on. It cannot prevent losses: it sends market close orders after a level has been reached, and the result can be worse than the level (see the limits below).
BASKETS
- Grouping: one basket for all positions, one basket per symbol (default), one per magic number, or one per symbol + magic number.
- Buy and sell: together in one basket (default), as separate baskets, buy positions only, or sell positions only.
- Optional filters: only the listed magic numbers (0 = manual trades) and only the listed symbols, comma separated. Empty = all. Write symbol names exactly as in Market Watch, including any suffix (upper or lower case does not matter).
- Positions outside the filters are never touched.
CLOSE CONDITIONS (all OFF by default)
- Profit target: the basket is closed when its total profit reaches the value.
- Loss limit: the basket is closed when its total loss reaches the value (entered as a positive number).
- Profit trailing: starts when the total profit reaches a start value, follows the highest value, and closes the basket when the profit falls back from that peak by the distance you set.
- Break-even: arms when the total profit reaches a start value, then closes the basket if the total profit falls back to the value you want to keep.
- Each condition has its own unit: money (account currency), % of balance, or pips.
- If the loss limit and another condition are met at the same moment, the loss limit is used.
AFTER A BASKET IS CLOSED
- Nothing: keep watching (default).
- Pause that basket for N minutes (default 60): during the pause every new position that falls into that basket is closed as soon as the EA sees it.
- Stop the EA: after the first basket close, the EA stops checking the conditions of ALL baskets. This lasts until you press Resume on the panel or change the basket definition (see the limits). Changing only the condition values (target, limit, trailing, break-even) does not release the stop.
- Optional: also delete the basket's pending orders when it is closed and while it is paused. Pending orders are deleted before the positions are closed.
PANEL AND ALERTS
- Status, total P/L and number of positions, and for each basket its P/L and how far it is from each condition (trailing peak, break-even level). Baskets that are closing or paused are listed first; the number of baskets shown is adjustable (1-20).
- The reason is shown when the EA cannot close (for example Algo Trading is off).
- Resume button after a stop, collapse button, English or Japanese (automatic from the terminal language, or chosen).
- Popup alert when a basket is closed (default on) and push notifications (optional).
- Each close is written to the Experts log with its reason. The close orders carry a short comment "BKC" and the reason.
STATE AFTER A RESTART
- The trailing peak, an armed break-even, a close in progress, a pause and the stop are saved in the terminal and continue after the terminal or the computer is restarted. In our tests no state was lost in 5 forced shutdowns of the terminal.
- The state is saved in the terminal where the EA runs. If you move to another terminal or computer (for example a virtual server), it does not move with it.
MEASURED IN OUR TESTS (demo account, one computer)
- A basket of 2 positions and 1 pending order was closed and deleted about 0.3 s after its condition was met.
- A new position opened during a pause was closed about 0.2 s after it appeared.
Your times depend on your broker, your connection and your computer.
LIMITS YOU SHOULD KNOW BEFORE BUYING
- Not a guarantee. Baskets are closed with market orders, so the final result can be worse than the level you set. In one of our tests with a loss limit of -60, the baskets were closed at -64 to -89. Gaps (for example at the weekly open), fast markets, slippage, rejected or delayed orders and a closed market can make the difference larger.
- If a condition is already met when you attach the EA or turn the condition on, the basket is closed about 3 seconds later. Look at the panel values before you turn a condition on.
- The basket definition consists of: the grouping, the buy/sell setting, the magic number and symbol filters, the trailing unit, the break-even unit, Points per pip, and whether swap and commission are included. Changing any of these resets the trailing peaks, the armed break-evens, the pauses and the stop. Changing other inputs keeps them.
- Pips: the pips of a basket are the simple sum of the pips of its positions, not weighted by lot size (a 0.01 lot and a 1.00 lot position count the same). Automatic pip size: 10 points on 3- and 5-digit symbols, otherwise 1 point, so on many GOLD, index and BTC symbols 1 pip = 1 point. The Points per pip input, if set, applies to all symbols. For baskets with mixed symbols, money or % is usually clearer.
- %: percent of the account balance at the moment of each check. After closes the balance changes, so the same percent becomes a different amount.
- Money and % include swap and commission (input, on by default). On MT5 only the commission charged at entry is included; a commission charged at exit is not. Pips do not include swap or commission.
- One copy per basket definition: a second copy with the same basket definition on the same account waits on standby. Copies with different definitions both run, and if their baskets overlap (different filters on the same positions), both will try to close them. Copies in different terminals on the same account cannot see each other.
- During a pause, each new position is closed again, and each one costs spread and commission. The EA cannot stop another EA from opening trades.
- The EA works only while MetaTrader 5 is running and connected and Algo Trading is ON (toolbar button) and allowed in the EA settings.
- Not tested on a live account yet: netting accounts, symbols with a freeze level, and closing a position larger than the maximum lot of the symbol in parts. Tested on hedging accounts.
- Strategy Tester: manual trades cannot be simulated there, so the EA runs a demonstration. It opens small positions with magic number 73170 on the chart symbol, risks at most about 1.5% of the balance per run, and stops after the balance falls below 95% of the start or after 20 runs. The demonstration trades are random and their P/L has no meaning; the tester report is not a trading result. The demonstration never runs on a real or demo account.
SETUP
1. Attach the EA to any one chart. The chart symbol does not limit what is watched.
2. Set the filters, the grouping and the buy/sell setting, and check that the panel shows the baskets you expect (with all conditions OFF it only watches).
3. Turn on the conditions you want, choose their units and the action after a close. Read the note on the panel and press OK.
4. Turn on Algo Trading and allow it in the EA settings. Try everything on a demo account first.
IMPORTANT
- No profit is promised, and closing exactly at a level is not guaranteed.
- The EA never decides what or when to trade; the positions and the levels are your decisions.
OTHER
- 34 inputs in 8 groups. The maximum slippage for closing is adjustable (points); on market-execution accounts the broker may not use it.
- While the panel is shown, the EA turns off "Chart on foreground" for its chart so that the panel stays readable, and turns it back on when you remove the EA.
- Support through MQL5 messages, reply within 3 business days: installation, settings and bug reports. Questions about what or when to trade, or which levels to use, cannot be answered.
