Profit based on your current profits - MetaTrader 4 Expert | MT4 EA Download - MetaTrader 4 Resources

Most EAs tend to close take profit orders based on the spread distance from the purchase price. However, the code used by EA Perceptron is mainly based on current profits. This method allows you to easily manage Take Profit for multiple open positions and monitor the current total profit based on a magic number, in case you are using multiple robot instances or different EAs at the same time. Add me as your friend and follow my feed for the latest news!
Using this code can also have a positive impact on some problems that may arise when using point-based take profit. For example, a pip-based take-profit may change based on the broker's slippage, limiting profits. By using tickers based on current profits, you can avoid this problem and have better control over your trades.
If you want to know more about how to set a take profit based on the current profit, you can use the code of EA SwingBot as a reference.
Let's start with the code that calculates the total number of open orders with the same magic number.
The magic number is a unique identifier assigned to an order by a trader or EA .
The code initializes a variable with a total order count of zero. It then uses a for loop to loop through all open orders and uses the order select() function. If the order is successfully selected, the order total variable is incremented and minus one .
//----------------- Integer total number of orders = 0 ; for ( integer i = 0 ; i < total number of orders (); i++) { if ( order select (i, select by POS, MODE_TRADES)) { if (OrderMagicNumber() == MagicNumber) { Total number of orders++; } } }
…
This code initializes two variables: profit minimization and profit . The variable Profit Minimization is used to activate Take Profit at this level, the value of which is expressed in the currency of the account. variable Profit is used to accumulate the current profit of all open positions with the same magic number. Variable Stop Loss is used to stop losses .
The code uses a for loop to iterate over all open positions, using the Total Orders() function. For each open position, select the corresponding order using the orderSelect() function. If the order is successfully selected and has the same magic number, the profit of this order will be added to Profits are variable .
Double minimum profit = 3 ; // Target profit Double profit = 0 ; // Current profit for ( integer i = 0 ; i < total number of orders (); i++) { if ( order select (i, select by POS, MODE_TRADES)) { if (OrderMagicNumber() == MagicNumber) // If there are multiple EAs, you can remove the MagicNumber filter to maintain the functionality of total orders { Profit+=Order Profit(); } } }
Minimum profit can be set as an external variable and configured in the EA options:

The code uses a for loop to iterate over all open orders, using the OrderTotal() function. The loop starts from the last order and goes to the first order. For each order, use the following command to select the corresponding trade order select() function .
If the selected trade has the same symbol as the current chart, is of type OP_Buy and has the same magic number as specified in the code, it checks if profit Trading volume is greater than or equal to profit minimization . If so, then end the trade with the buy price using the orderClose() function and print a message stating that the buy order has been closed .
Likewise, if the selected trade has the same symbol as the current chart, is of type OP_Sell and has the same magic number as specified in the code, it checks if profit Trading volume is greater than or equal to profit minimization . If yes, then use the following command to close the trade at the ask price using the orderClose() function and print a message indicating that the sell order has been closed.
for ( integer e = total number of orders ()- 1 ; e >= 0 ; e--) { if ( order select (e, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol() == Symbol () && OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber) // l'ordine viene modificato alone se il MagicNumber corresponds to quello dell'ordine in corso. { if (Profit>= ProfittoMinimo) { OrderClose(OrderTicket(), OrderLots(), ND(OrderClosePrice()), 3 ); // Buy price print ( "Buy order has been closed" , Profit, "-Minimum Stop Loss:" , Market Information( Symbol (), MODE_STOPLEVEL)); } } if (OrderSymbol() == Symbol () && OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber) { if (Profit>= ProfittoMinimo) { OrderClose(OrderTicket(), OrderLots(), ND(OrderClosePrice()), 3 ); //Sell price print ( "Sell order has been closed" , Profit, "-Minimum Stop Loss:" , Market Information( Symbol (), MODE_STOPLEVEL)); } } } }
This code can be useful for all take profit based closing strategies, but it can also be combined with a trailing stop based on an increase in current profit. This system is also useful in the presence of several Expert Advisors. If you exclude the if condition on MagicNumber, you can set a general take profit level to control all open positions of all active EAs at the same time
Attachment download
📎 swingbot_take_profit.mq4 (4.42 KB)
Source: MQL5 #47010
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •