Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
I have developed the following codes and it is working fine. The code is developed to limit one order in a pair if the order is opened manually.
void OnTick()
{
//---
for(int cnt = OrdersTotal()-1; cnt >=0 ; cnt--)
{
bool result=false;
if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
{
RefreshRates();
string symbol = OrderSymbol();
int ticket = OrderTicket();
Print(OrdersTotal());
if(OrdersTotal()>2)
{
for(int i=OrdersTotal()-2;i>=0;i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == symbol && ticket !=OrderTicket() )
{
Print("Symbol: "+symbol+"; OrderSymbol: "+OrderSymbol());
if(OrderType()== OP_BUY)
result=OrderClose(OrderTicket(),OrderLots( ) ,MarketInfo(OrderSymbol( ),MODE_BID) ,50, CLR_NONE);
if(OrderType()== OP_SELL)
result=OrderClose(OrderTicket(),OrderLots( ) ,MarketInfo(OrderSymbol( ),MODE_ASK) ,50,CLR_NONE);
////Print("Symbol: "+symbol+"; OrderSymbol: "+OrderSymbol());
break;
}
else continue;
}
}
}
int Err=GetLastError();
if(Err!= 0)
Print ("Error Closing Trade " , Err);
}
}
}
//+------------------------------------------------------------------+