Variables and Preparation
Define the variables needed to identify the oldest trade or the one furthest from the current price:
// Variables for tracking the furthest or oldest losing trade
double max_distance = 0; // Max distance from current price, initialized to zero
double lot_to_close = 0.1; // Lot size to close partially
int selected_ticket = -1; // Ticket ID of the selected trade for partial close
// Loop through all open trades to find the oldest or furthest losing trade
for (int i = OrdersTotal() - 1; i >= 0; i--) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
// Only consider trades on the current symbol and open positions
if (OrderSymbol() == Symbol() && (OrderType() == OP_BUY || OrderType() == OP_SELL)) {
double current_price = (OrderType() == OP_BUY) ? Bid : Ask; // Determine current price based on trade type
double order_price = OrderOpenPrice();
double distance = MathAbs(current_price - order_price); // Calculate distance from current price
// Check if the trade is in loss
if (OrderProfit() < 0) {
// Select the oldest or furthest trade in loss
if (distance > max_distance || selected_ticket == -1) {
max_distance = distance;
selected_ticket = OrderTicket();
}
}
}
}
}
// If a trade was found, proceed to partially close it
if (selected_ticket != -1) {
// Select the trade by ticket ID
if (OrderSelect(selected_ticket, SELECT_BY_TICKET, MODE_TRADES)) {
double remaining_lots = OrderLots() - lot_to_close; // Calculate remaining lots after partial close
// Check if the remaining lot size is above the minimum allowed
if (remaining_lots >= MarketInfo(Symbol(), MODE_MINLOT)) {
bool close_result = OrderClose(selected_ticket, lot_to_close, OrderClosePrice(), Slippage, clrRed);
if (close_result) {
Print("Partial close successful for trade with ticket: ", selected_ticket);
} else {
Print("Error attempting partial close. Error code: ", GetLastError());
}
} else {
Print("Cannot partially close; remaining size would be below minimum lot size.");
}
}
} else {
Print("No eligible trade found for partial close.");
}
Function for Partial Close on Oldest/Furthest Losing Trade
To make this process reusable, hereβs a function that you can call whenever you want to perform this action.
void PartialCloseOldestFurthestLoss(double lot_to_close, int slippage) {
double max_distance = 0; // Max distance from current price
int selected_ticket = -1; // Ticket ID of the selected trade for partial close
// Loop through all open trades to find the oldest or furthest losing trade
for (int i = OrdersTotal() - 1; i >= 0; i--) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
// Only consider trades on the current symbol and open positions
if (OrderSymbol() == Symbol() && (OrderType() == OP_BUY || OrderType() == OP_SELL)) {
double current_price = (OrderType() == OP_BUY) ? Bid : Ask; // Determine current price based on trade type
double order_price = OrderOpenPrice();
double distance = MathAbs(current_price - order_price); // Calculate distance from current price
// Check if the trade is in loss
if (OrderProfit() < 0) {
// Select the oldest or furthest trade in loss
if (distance > max_distance || selected_ticket == -1) {
max_distance = distance;
selected_ticket = OrderTicket();
}
}
}
}
}
// If a trade was found, proceed to partially close it
if (selected_ticket != -1) {
// Select the trade by ticket ID
if (OrderSelect(selected_ticket, SELECT_BY_TICKET, MODE_TRADES)) {
double remaining_lots = OrderLots() - lot_to_close; // Calculate remaining lots after partial close
// Check if the remaining lot size is above the minimum allowed
if (remaining_lots >= MarketInfo(Symbol(), MODE_MINLOT)) {
bool close_result = OrderClose(selected_ticket, lot_to_close, OrderClosePrice(), slippage, clrRed);
if (close_result) {
Print("Partial close successful for trade with ticket: ", selected_ticket);
} else {
Print("Error attempting partial close. Error code: ", GetLastError());
}
} else {
Print("Cannot partially close; remaining size would be below minimum lot size.");
}
}
} else {
Print("No eligible trade found for partial close.");
}
}
Explanation of the Function
Function Parameters:
lot_to_close: The lot size you want to close from the selected trade.
slippage: The allowable slippage for the close operation.
Selecting the Trade:
Loops through all open trades and finds the trade with the maximum distance from the current price that is also in loss.
Updates selected_ticket with the ticket ID of this trade.
Partial Close Execution:
If a trade is found, the function attempts a partial close by calling OrderClose.
Checks if the remaining lot size will be above the minimum allowed to ensure the trade remains valid.
Usage Example
To use this function, you can simply call it with the desired lot size to close and slippage:
PartialCloseOldestFurthestLoss(0.1, 3); // Partially closes 0.1 lots with 3 pips of allowable slippage

existe este (x>) pero nunca lo he usado. Es por esa razon que recibes notifiaciones siempre, por que quizas no esta bien condicionado
Sorry for the delay, I didn't quite understand when you wanted it to activate.
Look, open your project and share the link to the project you have, so we can help you.
Maximize Your EAs with fxDreema: Open Q&A Session
I would like you to review it and if you understand its logic, I hope for your collaboration if you like 





I understand and respect the diversity of approaches within our community. Personally, I choose to offer help and information for free, without expecting anything in return. For those users looking to accelerate the development of an EA or deepen their learning, I am available to provide personalized assistance.