Close all Buy or Sell Orders at once

 

Hi,

I want to run a cicle or something like that,

to identify last Buy/Sell closed order by magicnumber, and then

if it finds any remaining open Buy/Sell orders from a list of magic numbers, it must close all the Buy or Sell orders depending of the type of last closed.

I already tryed the following code, but it isn´t working as I want to.

int FecharCompra=0, FecharVenda=0;

for(int j=OrdersHistoryTotal()-1; j>=0; j--)
if ( OrderSelect(j,SELECT_BY_POS,MODE_HISTORY) && OrderSymbol() == Symbol() && OrderComment()==comenta
&& (OrderMagicNumber()==magico_0 || OrderMagicNumber()==magico_1 || OrderMagicNumber()==magico_2 || OrderMagicNumber()==magico_3 || OrderMagicNumber()==magico_4 || OrderMagicNumber()==magico_5 || OrderMagicNumber()==magico_6 || OrderMagicNumber()==magico_7 || OrderMagicNumber()==magico_8 || OrderMagicNumber()==magico_9 || OrderMagicNumber()==magico_10) )
{


if (OrderMagicNumber()==magico_1 || OrderMagicNumber()==magico_2 || OrderMagicNumber()==magico_3 || OrderMagicNumber()==magico_4 || OrderMagicNumber()==magico_5 || OrderMagicNumber()==magico_6 || OrderMagicNumber()==magico_7 || OrderMagicNumber()==magico_8 || OrderMagicNumber()==magico_9 || OrderMagicNumber()==magico_10)
{
if (OrderType()==OP_BUY) FecharCompra=1;
if (OrderType()==OP_SELL) FecharVenda=1;
}


} // end of ORDERSELECT

//...+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+***+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+**+*+*+



// FILTER TO CLOSE OPEN ORDERS

int Total1 = OrdersTotal(),
n;
for(n=0;n<Total1;n++)
{
OrderSelect(n,SELECT_BY_POS,MODE_TRADES);
if ( OrderSymbol()==Symbol() && OrderComment()==comenta
&& (OrderMagicNumber()==magico_0 || OrderMagicNumber()==magico_1 || OrderMagicNumber()==magico_2 || OrderMagicNumber()==magico_3 || OrderMagicNumber()==magico_4 || OrderMagicNumber()==magico_5 || OrderMagicNumber()==magico_6 || OrderMagicNumber()==magico_7 || OrderMagicNumber()==magico_8 || OrderMagicNumber()==magico_9 || OrderMagicNumber()==magico_10) )

{


if (OrderType()==OP_BUY)
{
if (FecharCompra==1)
{
if ( OrderMagicNumber()==magico_9 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_8 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_7 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_6 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_5 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_4 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_3 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_2 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_1 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_0 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
}
}
if (OrderType()==OP_SELL)
{
if (FecharVenda==1)
{
if ( OrderMagicNumber()==magico_9 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_8 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_7 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_6 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_5 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_4 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_3 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_2 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_1 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
if ( OrderMagicNumber()==magico_0 ) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Red);
}
}

}
}

Any sugestions?

Regards

 


OrdersHistoryTotal()-1
Select-(The Last would be 1st)
-(If you want Bullet-Proof check Highest Time)
-(Compromised Bullet-Proof check Highest Order#)
Magic=OrderMagic# break

OrdersTotal()-1
if(OrderMagic#()==Magic)
...Close
 
ubzen:




Hello,

Thanks for your help, but I can´t figure out to do it.

I already change it as you suggested, but I don´t know why it still not closing the open orders.

Maybe, I'm doing something wrong.

Can you please, copy here all the provided code with the changes that you would do on it?

Thank you very much.

 
Remember to count DOWN from hi-index to 0 maybe?