MQL5 Position closing function does not close positions as expected, where am I wrong?
MQL5 Position closing function does not close positions as expected, where am I wrong?
Loading saved threads...
Remisa Phillips · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Remisa Phillips
Original post: https://stackoverflow.com/questions/79553784
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I'm developing an EA in MQL5 that should automatically close open positions when the money profit reaches or exceeds a profit target of 20 or falls to -100. I have tried using both trade.PositionClose() and a fallback using OrderSend(), as well as manually calculating profit from entry price and current market data. However, none of the approaches seem to reliably close the positions.
What I expect:
If a position's money profit is >= 20, it should close immediately.
If a position's money profit is <= -100, it should close immediately.
What happens:
The EA prints debug messages showing the current POSITION_PROFIT, but even when it reaches the thresholds, the positions remain open.
void CheckClosePositions()
{
int total = PositionsTotal();
for(int i = total - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket == 0) continue;
if(!PositionSelectByTicket(ticket)) continue;
if(PositionGetString(POSITION_SYMBOL) != _Symbol) continue;
double moneyProfit = PositionGetDouble(POSITION_PROFIT);
PrintFormat("Ticket %I64u: MoneyProfit = %f", ticket, moneyProfit);
if(moneyProfit >= 20 || moneyProfit <= -100)
{
if(trade.PositionClose(ticket))
PrintFormat("Closed ticket %I64u, profit = %f", ticket, moneyProfit);
else
PrintFormat("Failed to close ticket %I64u, error = %d", ticket, GetLastError());
Sleep(500);
}
}
}
I've verified that auto‑trading is enabled and that the EA is attached to the correct symbol. I've also tried recalculating profit manually but with no success.
Question:
What might be causing the closing function not to work as expected? Are there any issues with how POSITION_PROFIT is updated by my broker, or is there something wrong with my logic? Any help would be appreciated.
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: Remisa Phillips Source score (net votes, not local likes): 1 Original post: https://stackoverflow.com/questions/79553784 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I'm developing an EA in MQL5 that should automatically close open positions when the money profit reaches or exceeds a profit target of 20 or falls to -100. I have tried using both trade.PositionClose() and a fallback using OrderSend(), as well as manually calculating profit from entry price and current market data. However, none of the approaches seem to reliably close the positions. What I expect: If a position's money profit is >= 20, it should close immediately. If a position's money profit is <= -100, it should close immediately. What happens: The EA prints debug messages showing the current POSITION_PROFIT, but even when it reaches the thresholds, the positions remain open. void CheckClosePositions() { int total = PositionsTotal(); for(int i = total - 1; i >= 0; i--) { ulong ticket = PositionGetTicket(i); if(ticket == 0) continue; if(!PositionSelectByTicket(ticket)) continue; if(PositionGetString(POSITION_SYMBOL) != _Symbol) continue; double moneyProfit = PositionGetDouble(POSITION_PROFIT); PrintFormat("Ticket %I64u: MoneyProfit = %f", ticket, moneyProfit); if(moneyProfit >= 20 || moneyProfit <= -100) { if(trade.PositionClose(ticket)) PrintFormat("Closed ticket %I64u, profit = %f", ticket, moneyProfit); else PrintFormat("Failed to close ticket %I64u, error = %d", ticket, GetLastError()); Sleep(500); } } } I've verified that auto‑trading is enabled and that the EA is attached to the correct symbol. I've also tried recalculating profit manually but with no success. Question: What might be causing the closing function not to work as expected? Are there any issues with how POSITION_PROFIT is updated by my broker, or is there something wrong with my logic? Any help would be appreciated.
Checking account access…