Ignore above. Sorry had the logic in the wrong function. Try replace the OpenPosition function with the one below. The CArrayString SymbolList is slightly redundant as I assume you are only running this on a single pair. You may want to remove the Magic and Symbol check depending on your use-case.
Compiled, untested.
#include <arrays/arraystring.mqh>
#define SymbolOrderLimit 2
void OpenPosition(int command)
{
int total = OrdersTotal();
CArrayString SymbolList;
int SymbolListOrderCount[];
int ExistingIndex = 0;
for(int pos=total; pos>=0; pos--)
{
if(OrderSelect(pos,SELECT_BY_POS))
{
ExistingIndex = SymbolList.SearchLinear(OrderSymbol());
if (ExistingIndex == -1)
{
SymbolList.Add(OrderSymbol());
if (ArrayRange(SymbolListOrderCount, 0) < SymbolList.Total()) ArrayResize(SymbolListOrderCount, SymbolList.Total());
ExistingIndex = SymbolList.Total() - 1;
SymbolListOrderCount[ExistingIndex] = 1;
}
else
{
SymbolListOrderCount[ExistingIndex]++;
}
}
}
if (!(SymbolListOrderCount[ExistingIndex] < SymbolOrderLimit &&
OrderSymbol()==_Symbol &&
OrderMagicNumber()==Magic_Number)) return;
for(int attempt=0; attempt<TRADE_RETRY_COUNT; attempt++)
{
int ticket = 0;
int lastError = 0;
bool modified = false;
double stopLoss = GetStopLoss(command);
double takeProfit = GetTakeProfit(command);
string comment = IntegerToString(Magic_Number);
color arrowColor = command==OP_BUY ? clrGreen : clrRed;
if(IsTradeContextFree())
{
double price=MarketInfo(_Symbol,command==OP_BUY ? MODE_ASK : MODE_BID);
if(setProtectionSeparately)
{
ticket=OrderSend(_Symbol,command,Entry_Amount,price,10,0,0,comment,Magic_Number,0,arrowColor);
if(ticket>0 && (Stop_Loss>0 || Take_Profit>0))
{
modified=OrderModify(ticket,0,stopLoss,takeProfit,0,clrBlue);
}
}
else
{
ticket=OrderSend(_Symbol,command,Entry_Amount,price,10,stopLoss,takeProfit,comment,Magic_Number,0,arrowColor);
lastError=GetLastError();
if(ticket<=0 && lastError==130)
{
ticket=OrderSend(_Symbol,command,Entry_Amount,price,10,0,0,comment,Magic_Number,0,arrowColor);
if(ticket>0 && (Stop_Loss>0 || Take_Profit>0))
{
modified=OrderModify(ticket,0,stopLoss,takeProfit,0,clrBlue);
}
if(ticket>0 && modified)
{
setProtectionSeparately=true;
Print("Detected ECN type position protection.");
}
}
}
}
if(ticket>0)
break;
lastError=GetLastError();
if(lastError!=135 && lastError!=136 && lastError!=137 && lastError!=138)
break;
Sleep(TRADE_RETRY_WAIT);
Print("Open Position retry no: "+IntegerToString(attempt+2));
}
}