Position Basket PL Closer MT4
- 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
An Expert Advisor for MetaTrader 4 that treats several open orders as one "basket" and closes the basket as a whole when the combined profit or loss gets to the level you have chosen. It works with orders from manual trading and from other EAs. On the chart, its panel carries the short name "Basket P/L Closer".
Please note what it does not do. It places no new trades on your real or demo account, offers no buy or sell signals and is not a trading system. Out of the box all four closing rules are switched off, so it only displays the baskets until you enable a rule. It does not stop losses from happening: it starts closing once a level has been passed, and the result can end up beyond that level.
MT4 SPECIFICS (PLEASE READ FIRST)
- One order at a time: MT4 sends a close request and waits for the reply before the next one. In our tests each close took about 0.3 s, so a basket of 20 orders needs roughly 6 seconds. Prices can move during that time.
- No comment on the close: MT4 cannot attach a comment when an order is closed. Which rule closed which order is written to the Experts tab (log), together with the order number.
- Commission: money and percent values use the swap and the commission that MT4 shows for each order (input, on by default).
- Panel lines longer than about 62 bytes are cut and end with "~" (an MT4 text limit). Japanese text is cut earlier because each character uses 2 bytes.
- Switch on the AutoTrading button in the toolbar and tick "Allow live trading" in the EA properties. Without them nothing can be closed; the panel then says why.
- All 34 inputs appear in one list (MT4 has no input groups).
HOW ORDERS BECOME BASKETS
- One basket for everything, a basket per symbol (default), per magic number, or per symbol and magic number.
- Buys and sells in the same basket (default), in separate baskets, or only buys / only sells.
- You may limit the EA to certain magic numbers (0 = manual orders) and to certain symbols, comma separated; leave empty for all. The symbol must be typed as MT4 shows it, suffix included (case does not matter). Orders outside these filters are left alone.
THE FOUR CLOSING RULES (off by default)
1. Profit target - the combined profit reaches your value.
2. Loss limit - the combined loss reaches your value (type it as a positive number).
3. Profit trailing - once the combined profit passes a start value, the EA remembers the best value and closes when the profit drops back by your distance.
4. Break-even - once the combined profit passes a start value, the EA closes if it falls back to the amount you want to keep.
Every rule can be measured in account currency, in percent of the balance or in pips. When the loss limit and another rule trigger together, the loss limit wins.
WHAT HAPPENS NEXT
- Keep watching (default).
- Pause only that basket for a number of minutes (60 by default). New orders that belong to it are closed as soon as they are found.
- Stop the EA. The first basket close stops the checking of every basket, not only the one that was closed. The stop stays until you click Resume on the panel or change the basket definition. Editing the rule values alone does not end it.
- Pending orders of the basket can be removed too (optional): at a close they are removed first, before the open orders are closed, and also during a pause.
KEEPS ITS MEMORY
Trailing peaks, armed break-evens, unfinished closes, pauses and the stop are stored in the terminal's global variables and survive a restart of MT4 or Windows; in our forced-shutdown tests nothing was lost. They stay in that terminal: another terminal, computer or virtual server starts without them. In the Strategy Tester nothing is written to the terminal.
PANEL
Status, combined result, number of orders, each basket with its P/L and the distance to its rules, closing or paused baskets on top (1-20 baskets shown), the reason when closing is impossible, Resume and collapse buttons. English or Japanese, following the terminal language or your choice. Popup alert after a close (on by default) and optional push notifications.
LIMITATIONS TO KNOW BEFORE BUYING
- A level is a trigger, not a promise. Closing uses market orders, and on MT4 one order after the other, so the result can be worse than your level. In one of our tests a loss limit of -60 ended at -64 to -89. Weekend gaps, fast markets, slippage, requotes or rejected orders and a closed market can make it worse.
- A basket that already meets a rule when the EA starts, or when you switch the rule on, is closed after about 3 seconds. Check the panel first.
- "Basket definition" means: grouping, buy/sell setting, magic and symbol filters, trailing unit, break-even unit, Points per pip, and the swap/commission switch. Editing any of them starts fresh: trailing peaks, armed break-evens, pauses and the stop are cleared. Other inputs can be edited without that reset.
- Pips are simply added up order by order; the lot size is not taken into account (0.01 and 1.00 lot count alike). The automatic pip is 10 points on 3- and 5-digit symbols and 1 point elsewhere, which means 1 pip = 1 point on many GOLD, index and BTC symbols. A Points per pip value you enter is used for every symbol. For mixed-symbol baskets, money or percent is usually the better unit.
- Percent always refers to the balance at the moment of the check, so it changes after every close.
- Pips ignore swap and commission.
- Run one copy per basket definition. A second copy with the same definition on the same account stands by. Copies with different definitions both work, and where their filters cover the same orders, both of them will try to close those orders. Two terminals on one account cannot detect each other.
- During a pause each new order is closed again, costing spread and commission each time. Other EAs are not prevented from opening orders.
- Nothing works while MT4 is closed or disconnected or AutoTrading is off.
- Not yet tested on a live account: symbols with a freeze level and closing orders larger than the symbol's maximum lot in parts.
- Strategy Tester: manual orders cannot be reproduced there, so a demonstration opens small orders (magic 73170) on the chart symbol. One run risks about 1.5% of the balance at most, and the demonstration ends when the balance drops under 95% of the start or after 20 runs. Its trades are random and their P/L means nothing; the report is not the result of a trading method. It never runs on a real or demo account.
FIRST START
1. Put the EA on one chart; the symbol of the chart does not matter.
2. Choose filters, grouping and buy/sell, and confirm on the panel that the baskets look right (all rules off = display only).
3. Enable the rules you need with their units and choose what happens after a close. Read the panel note and click OK.
4. Turn on AutoTrading and allow live trading. Practise on a demo account first.
PLEASE NOTE
- Profit is not promised, and a close exactly at your level cannot be guaranteed.
- What to trade and which levels to use remain your own decisions.
- While the panel is visible, "Chart on foreground" is switched off for that chart and switched back on when the EA is removed.
- Support by MQL5 message within 3 business days for installation, settings and bug reports. Questions about what or when to trade, or which levels to set, cannot be answered.
