MT3 code review request

 
This ModifyOrder code in MT3 Demo returns a "Modify failed [invalid s/l or t/p)" in the Journal. This same code works OK in the MT3 Backtest.

Could someone advise if they see something wrong in this code.

Thank you,
Wackena

defines: Gap(20),factor(1);
var: ts(0);
for cnt=1 to TotalTrades
{
mode=OrderValue(cnt,VAL_TYPE);
If OrderValue(cnt,VAL_SYMBOL)=Symbol then   
{
if mode=OP_BUY then
{ 
If OrderValue(cnt,VAL_CLOSEPRICE)-OrderValue(cnt,VAL_OPENPRICE)>(Gap*factor)*Point then
{
ts=OrderValue(cnt,VAL_OPENPRICE)+(Gap*factor)*Point;
factor=factor+1;
}
If ts>OrderValue(cnt,VAL_STOPLOSS) then
{   
ModifyOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_OPENPRICE),ts,OrderValue(cnt,VAL_TAKEPROFIT),Red);
}   
}

If mode=OP_SELL then   
{
If OrderValue(cnt,VAL_OPENPRICE)-OrderValue(cnt,VAL_CLOSEPRICE)>(Gap*factor)*Point then
{
ts=OrderValue(cnt,VAL_OPENPRICE)-(Gap*factor)*Point;
factor=factor+1;
} 
If ts<OrderValue(cnt,VAL_STOPLOSS) then 
{
ModifyOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_OPENPRICE),ts,OrderValue(cnt,VAL_TAKEPROFIT),Red);
}
}
}
  }
Reason: