Introduction
Hey there, fellow traders! If you’re like most of us, you probably rely on your Expert Advisors (EAs) to manage your trades. But have you ever thought about how these EAs determine when to close orders at take profit? While many EAs stick to a simple pip-based approach, the EA Perceptron takes a different route—it focuses on your current profit instead. This makes managing take profit across multiple open positions so much easier, especially if you’re juggling several bots or EAs at once. Want to stay in the loop? Add me to your friends and follow my feed for updates!
By using this profit-based method, you can sidestep some of the common headaches that come with pip-based take profits. For instance, if your broker experiences slippage, it could seriously limit your potential profits. With a current profit-based system, you gain better control over your trades, ensuring you don’t leave money on the table.
If you’re curious about how to set up a take profit based on current profit, take a peek at the code for the EA SwingBot—it’s a great reference!
…
Total Orders
First things first, let’s look at the code that calculates the total number of open orders tied to the same magic number.
The magic number is a unique identifier that you or your EA assigns to each order. Here’s how it works:
The code kicks off by initializing a variable total_orders to zero. It then loops through all open orders using a for loop and selects each order with the OrderSelect() function. If the order is successfully selected, it bumps up the total_orders variable by one.
//----------------- int total_orders = 0; for(int i = 0; i < OrdersTotal(); i++) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if(OrderMagicNumber() == MagicNumber) { total_orders++; } } }
…
Calculating Current Profit
The next step involves initializing two variables: ProfittoMinimo and Profit. The ProfittoMinimo variable sets the threshold for activating take profit, and its value is expressed in your account’s currency. The Profit variable accumulates the current profit from all open positions with the same magic number. Additionally, we’ve got a StopLoss variable for your stop-loss settings.
The code employs a for loop to iterate through all open positions using the OrdersTotal() function. For each position, the corresponding order is selected with the OrderSelect() function. If the order is successfully selected and has the same magic number, the order’s profit is added to the Profit variable.
double ProfittoMinimo = 3; // target profit double Profit = 0; // current profit for(int i=0; i<OrdersTotal(); i++) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if(OrderMagicNumber() == MagicNumber) // For multiple EAs, you can remove the MagicNumber filter to manage total orders { Profit += OrderProfit(); } } }
You can set the minimum profit as an external variable in the EA options:

…
Closing Positions When Profit is Reached
Here’s where it gets interesting! The code uses a for loop to check all open orders with the OrdersTotal() function, starting from the last order and working backward. For each order, it selects the corresponding trade with the OrderSelect() function.
If the trade matches the current chart’s symbol, is of type OP_BUY, and has the same magic number, it checks if the Profit is greater than or equal to ProfittoMinimo. If it is, the trade is closed at the bid price using the OrderClose() function, and you’ll see a message confirming that the buy order has been closed.
On the flip side, if it’s a sell order (type OP_SELL), and it meets the criteria, the trade is closed at the ask price, with a message displayed for the closed sell order.
for(int e = OrdersTotal() - 1; e >= 0; e--) { if(OrderSelect(e, SELECT_BY_POS, MODE_TRADES)) { if(OrderSymbol() == Symbol() && OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber) // Only modify the order if the MagicNumber matches { if(Profit >= ProfittoMinimo) { OrderClose(OrderTicket(), OrderLots(), ND(OrderClosePrice()), 3); // Bid price Print("Buy order closed", Profit, " - Minimum stop loss: ",MarketInfo(Symbol(), MODE_STOPLEVEL)); } } if(OrderSymbol() == Symbol() && OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber) { if(Profit >= ProfittoMinimo) { OrderClose(OrderTicket(), OrderLots(), ND(OrderClosePrice()), 3); // Ask price Print("Sell order closed", Profit, " - Minimum stop loss: ",MarketInfo(Symbol(), MODE_STOPLEVEL)); } } } }
…
Conclusion
This code can be a game-changer for your position-closing strategies that focus on take profit. Plus, you can easily integrate this with a trailing stop based on your current profit. If you're running multiple Expert Advisors, consider leaving out the if condition on the MagicNumber to set universal take profit levels across all open positions. It’s a handy way to keep all your active EAs in check!
연관 포스트
- RRS Impulse: Your Go-To Scalping EA for MetaTrader 4
- Maximize Your Trading with Trickerless for MetaTrader 4
- Maximize Your Trading with the Babi Ngepet EA for MetaTrader 5
- Unlocking the Power of RRS Non-Directional EA for MetaTrader 4
- Unlocking the AK-47 Scalper EA: Your Ultimate MetaTrader 4 Companion