AutoCloseOnProfitLoss Expert - Automatically Close All Positions on Profit/Loss
Duy Van Nguy, 2025.05.14 04:17
The AutoCloseOnProfitLoss Expert Advisor (EA) is a powerful automation tool for MetaTrader 5, designed to close all open positions when predefined profit or loss targets are reachedYes, that’s possible. You just need an EA that monitors all open positions on the same symbol, includes swap and commission in the total, and closes everything once the overall profit reaches breakeven plus your set target.
Basically, it acts like a shared Take Profit for all trades. It’s quite easy to code in MT5 using PositionGetDouble() for profit, swap, and commission, and CTrade.PositionClose() to close all orders together.
Hi
There is also possibility to calculate the level when this assumed profit is reached for all positions and modify all opened positions with this calculated price level. This way all opened trades would have the same take profit level.
It’s easier to calculate such level, if all trades would be in the same direction, but it can be done also with mixed types of trades. Of course, if you set up take profit too close to current price or it cannot be calculated from other reasons (for example equal lots for buys and sells), the EA might miss the chance for modifying TP and not close trades on time – but if you use proper setup, it should work like you wished.
You can calculate common TP level like this:
Double takeProfitCurrency = 100;//100$ CPositionInfo m_position = new CPositionInfo(); double sumLotsB = 0; double sumPL=0; double sumLots = 0; double beLevel =0 ; for(int i=0;i<PositionsTotal();i++){ if(m_position.SelectByIndex(i)){ if(m_position.Magic()==MagicNumber){ if(m_position.Symbol()==_Symbol){ double oprof =m_position.Profit()+m_position.Swap()+m_position.Commission(); if(m_position.PositionType()==POSITION_TYPE_BUY){ sumLots+=m_position.Volume(); sumPL+=oprof; } else if(m_position.PositionType()==POSITION_TYPE_SELL){ sumLots-=m_position.Volume(); sumPL+=oprof; } } } } } MqlTick lastTick; ResetLastError(); if(SymbolInfoTick(_Symbol, lastTick)){ double pv = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE)/(SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE)); double tpLevel = 0; if(sumLots!=0 && pv!=0){ if(sumLots > 0) tpLevel = lastTick.bid - (sumPL-takeProfitCurrency)/(sumLots*pv); else tpLevel = lastTick.ask - (sumPL-takeProfitCurrency)/(sumLots*pv); } }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi everyone,
I’m using a hedging account in MetaTrader 5, and I often have several open positions on the same symbol at different prices.
I want to set one common Take Profit that will close all positions together — either when price reaches a certain level or when the total profit in USD hits a target.
I know that I can click the three dots (...) → “Close all positions”, but many times the price moves too fast, which causes slippage and makes the closing price worse than expected.
Is there any script or EA that can handle this automatically with one shared Take Profit for all current trades?
If anyone has an example or existing tool that does this, please share.
Thanks in advance.