something wrong about moving stop, please help!!

 

hi everybody,

I'm a newbie about MT4

I wrote a EA, it work fine, but the part of "moving stop" isn't work, and I can't find out what's wrong.

anyone can help me? please!!!  thank you!!!

 

 

//check position, close or move stop loss
   if(OrdersTotal() != 0)
   {
      if(OrdersTotal() != 3)//close all position
      {
         CloseAll();//close all
      }
      MoveStopLoss(movingStopLoss);//move stop loss
      
   }

------

void MoveStopLoss(int movingStopLoss)
{
   int SLcnt;
   if(OrderSelect(OrdersTotal()-1, SELECT_BY_POS)==false)//chose current trades and orders
      return(0);
   if(OrdersTotal()>0)
   {
      for(SLcnt=OrdersTotal();SLcnt>=0;SLcnt--)
      {
         if(OrderSelect(SLcnt, SELECT_BY_POS)==false)
            continue;
         else
         {
            if(OrderType()==OP_BUY && ((Close[0]-OrderStopLoss())>movingStopLoss)) //long positions modify stop loss
               OrderModify(OrderTicket(), OrderOpenPrice(), Bid-movingStopLoss*pt, 0, 0);
               
            if(OrderType()==OP_SELL && ((OrderStopLoss()-Close[0])>movingStopLoss)) //short positions modify stop loss
               OrderModify(OrderTicket(), OrderOpenPrice(), Ask+movingStopLoss*pt, 0, 0);
         }
      }
   }
}
 
What error# do you get?
 
ubzen:
What error# do you get?


i don't seen any error, compile is no problem.

and the code is basically the same with the part of ordersend code,

the only two differences are the variables that I indicate to the function and orderModify() function's variable.  

 

I created the variables you didn't provide. If they're wrong the correct them below. The program gets to "Here==1" however it does not get to "Here==2" and thats where your order-modify is located. Any ideas why?

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int movingStopLoss=20;
double pt;
void init(){pt=Point;}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start(){

    while(OrdersTotal()!=3){int Magic=7;
        OrderSend(Symbol(),OP_BUY,1,Ask,0,0,0,"",Magic,0,Blue);
    }

    //check position, close or move stop loss
   if(OrdersTotal() != 0)
   {
      /*if(OrdersTotal() != 3)//close all position
      {
         CloseAll();//close all
      }*/
      MoveStopLoss(movingStopLoss);//move stop loss
   }

}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void MoveStopLoss(int movingStopLoss)
{
   int SLcnt;
   if(OrderSelect(OrdersTotal()-1, SELECT_BY_POS)==false)//chose current trades and orders
      return(0);
   if(OrdersTotal()>0)
   {
      for(SLcnt=OrdersTotal();SLcnt>=0;SLcnt--)
      {
         if(OrderSelect(SLcnt, SELECT_BY_POS)==false)
            continue;
         else
         {  
            Alert("Here===1");
            if(OrderType()==OP_BUY && ((Close[0]-OrderStopLoss())>movingStopLoss)){ //long positions modify stop loss
               Alert("Here===2");
               OrderModify(OrderTicket(), OrderOpenPrice(), Bid-movingStopLoss*pt, 0, 0);
            }
            if(OrderType()==OP_SELL && ((OrderStopLoss()-Close[0])>movingStopLoss)){ //short positions modify stop loss
               OrderModify(OrderTicket(), OrderOpenPrice(), Ask+movingStopLoss*pt, 0, 0);
            }
         }
      }
   }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23:19:28 Helping: loaded successfully
23:19:32 Helping test started
23:19:32 2012.01.02 06:01  Helping EURUSD,M5: open #1 buy 1.00 EURUSD at 1.2930 ok
23:19:32 2012.01.02 06:01  Helping EURUSD,M5: open #2 buy 1.00 EURUSD at 1.2930 ok
23:19:32 2012.01.02 06:01  Helping EURUSD,M5: open #3 buy 1.00 EURUSD at 1.2930 ok
23:19:32 2012.01.02 06:01  Helping EURUSD,M5: Alert: Here===1
23:19:32 2012.01.02 06:01  Helping EURUSD,M5: Alert: Here===1
23:19:32 2012.01.02 06:01  Helping EURUSD,M5: Alert: Here===1
23:19:43 2012.01.02 06:01  Helping EURUSD,M5: Alert: Here===1
 
ubzen:

I created the variables you didn't provide. If they're wrong the correct them below. The program gets to "Here==1" however it does not get to "Here==2" and thats where your order-modify is located. Any ideas why?

 


I had PM to you, thank you!!
 
jason3000d: I had PM to you, thank you!! 

The reason your ordermodify is not working is because 

Close[0]-OrderStopLoss())>movingStopLoss

close - orderstoploss ... would never be greater than movingStopLoss

movingStopLoss is 30 [within your codes]. where-as close is usually like 1.54321 and orderstoploss is also a price like 1.43210.

therefore something like 1.5 cannot be> than 30.0

Ps> I responded to your PM with additional info.
 
ubzen:

The reason your ordermodify is not working is because 

close - orderstoploss ... would never be greater than movingStopLoss

movingStopLoss is 30 [within your codes]. where-as close is usually like 1.54321 and orderstoploss is also a price like 1.43210.

therefore something like 1.5 cannot be> than 30.0

Ps> I responded to your PM with additional info.


thanks again. it's a stupid bug................
 
jason3000d:thanks again. it's a stupid bug................ 
No Problem :)