Need Help with Move to Breakeven Function

 

Hi,

I wrote this simple function to move two open orders to breakeven when certain criteria are met.

The problem I'm having is that it moves one trade to breakeven but gives me a modify error 1 for the second.

Any ideas?

Thank You

void MoveToBreakEven()

{

for (int i=OrdersTotal()-1; i >= 0; i--)

{

if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))return;

if (!OrderSymbol() == Symbol())continue;

if (OrderMagicNumber() == MagicNumber1 || OrderMagicNumber() == MagicNumber2)

{

if (OrderType() == OP_BUY)

{

if (Bid > (OrderOpenPrice() +( OrderOpenPrice() - OrderStopLoss())))

if (!OrderModify (OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(PipsToLockIn*pips),OrderTakeProfit(),0,CLR_NONE))

Print ("Error modifying order on line: ",__LINE__);

}

else if (OrderType() == OP_SELL)

{

if (Ask < (OrderOpenPrice() -( OrderStopLoss() - OrderOpenPrice())))

if (!OrderModify (OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(PipsToLockIn*pips),OrderTakeProfit(),0,CLR_NONE))

Print ("Error modifying order on line: ",__LINE__);

}

}

}

}

 
ThemBonez:
Hi,

I wrote this simple function to move two open orders to breakeven when certain criteria are met.

The problem I'm having is that it moves one trade to breakeven but gives me a modify error 1 for the second.

Any ideas?

Thank You

void MoveToBreakEven()

{

for (int i=OrdersTotal()-1; i >= 0; i--)

{

if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))return;

if (!OrderSymbol() == Symbol())continue;

if (OrderMagicNumber() == MagicNumber1 || OrderMagicNumber() == MagicNumber2)

{

if (OrderType() == OP_BUY)

{

if (Bid > (OrderOpenPrice() +( OrderOpenPrice() - OrderStopLoss())))

if (!OrderModify (OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(PipsToLockIn*pips),OrderTakeProfit(),0,CLR_NONE))

Print ("Error modifying order on line: ",__LINE__);

}

else if (OrderType() == OP_SELL)

{

if (Ask < (OrderOpenPrice() -( OrderStopLoss() - OrderOpenPrice())))

if (!OrderModify (OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(PipsToLockIn*pips),OrderTakeProfit(),0,CLR_NONE))

Print ("Error modifying order on line: ",__LINE__);

}

}

}

}

Try to round prices.

So change this: OrderOpenPrice()-(PipsToLockIn*pips)

to this: NormalizeDouble(OrderOpenPrice()-(PipsToLockIn*pips),Digits)