Close All Order when Profit....

 

HI,

I try to program Metatrader EA which will able close all the order when the profit is hit. This is for hedging correlation pair trade.

But my code do not cloase all the order, it only close 1 order.

Below is my code:

Can anyone help to improve the code?

int start()

{

double profit;

int i;

//----

profit=AccountProfit();

Comment("PROFIT & Loss now=",profit);

if(profit>=100)

{

for (i=0;i<OrdersTotal();i++)

{

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

if(OrderType()==OP_BUY )

{

OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);

}

if(OrderType()==OP_SELL )

{

OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);

}

}

}

return(0);

}

 
FXfish:
HI,

I try to program Metatrader EA which will able close all the order when the profit is hit. This is for hedging correlation pair trade.

But my code do not cloase all the order, it only close 1 order.

Below is my code:

Can anyone help to improve the code?

int start()

{

double profit;

int i;

//----

profit=AccountProfit();

Comment("PROFIT & Loss now=",profit);

if(profit>=100)

{

for (i=0;i<OrdersTotal();i++)

{

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

if(OrderType()==OP_BUY )

{

OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);

}

if(OrderType()==OP_SELL )

{

OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);

}

}

}

return(0);

}

Check Codersguru article http://www.metatrader.info/node/99

As i understand it is about how to close all the orders in Metatrader if the profit will rich, let's say, 200 pips, or any.

Reason: