theres a problem with the initial pendings orders, if the buy pending order hits SL it will contiune to open buy trades until a buy trade hits take profit, once the first take profit is hit the EA will now reverse all trades going forward. the same must happen for the first opening pending sell order
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is a minimal patch that fixes the behaviour you described.
The EA now:
Remembers which side (BUY or SELL) was triggered first (g_firstSide).
Keeps trading that same side until the first Take-Profit is reached.
Only then flips to the opposite side and continues reversing forever.
Replace the three blocks marked 🔧 in your current file with the code below.
🔧 1. Add these two global variables near the top
mql5
// NEW: track the very first triggered side
ENUM_POSITION_TYPE g_firstSide = WRONG_VALUE; // BUY or SELL
bool g_firstTPHit = false; // TP reached on first side?
🔧 2. Replace CheckForClosedPosition() with this version
mql5
void CheckForClosedPosition()
{
bool hasPos = false;
ulong curTicket = 0;
for(int i = PositionsTotal()-1; i >= 0; i--)
if(position.SelectByIndex(i) &&
position.Symbol() == _Symbol &&
position.Magic() == InpMagicNumber)
{
hasPos = true;
curTicket = position.Ticket();
break;
}
// position just closed?
if(g_lastPositionTicket > 0 && !hasPos)
{
if(HistorySelect(TimeCurrent()-86400, TimeCurrent()))
{
for(int i = HistoryDealsTotal()-1; i >= 0; i--)
{
ulong deal = HistoryDealGetTicket(i);
if(deal == 0) continue;
ulong posID = HistoryDealGetInteger(deal, DEAL_POSITION_ID);
if(posID != g_lastPositionTicket) continue;
ENUM_DEAL_ENTRY entry = (ENUM_DEAL_ENTRY)HistoryDealGetInteger(deal, DEAL_ENTRY);
if(entry != DEAL_ENTRY_OUT) continue;
ENUM_DEAL_TYPE dType = (ENUM_DEAL_TYPE)HistoryDealGetInteger(deal, DEAL_TYPE);
ENUM_POSITION_TYPE closedSide =
(dType == DEAL_TYPE_BUY) ? POSITION_TYPE_SELL : POSITION_TYPE_BUY;
double profit = HistoryDealGetDouble(deal, DEAL_PROFIT);
bool wasTP = profit > 0;
// --- FIRST side logic ---
if(g_firstSide == WRONG_VALUE) // very first trigger
g_firstSide = closedSide;
if(closedSide == g_firstSide && wasTP) // first TP on that side
g_firstTPHit = true;
// --- decide next trade ---
ENUM_POSITION_TYPE nextSide;
if(!g_firstTPHit) // still on first side
nextSide = g_firstSide;
else // now reverse forever
nextSide = (closedSide == POSITION_TYPE_BUY) ? POSITION_TYPE_SELL : POSITION_TYPE_BUY;
PlaceReversalTrade(nextSide);
break;
}
}
g_lastPositionTicket = 0;
}
if(hasPos) g_lastPositionTicket = curTicket;
}
🔧 3. Replace PlaceReversalTrade() with this version