Why is my StopLoss moving in the wrong direction?

To add comments, please log in or register
dkamerond
220
dkamerond  

I'm trying to build my first EA and honestly i'm mostly using copy and paste but i cannot see why this is not working the way i want it to. The idea is to move my stop loss up to the Low or High of the candle with a shift of 2 but once its placed it should not move back. In this example i have a Buy trade in and when it was placed the initial stop loss and first move were fine but then when the next candle posted the new low was lower than the previous without hitting my stop loss it moved my stop loss down and it should not have.


Here is the code i'm using:

void ManageTrades(){

   double low=iLow(NULL,PERIOD_CURRENT,shift);
   double high=iHigh(NULL,PERIOD_CURRENT,shift);  
   double bid = MarketInfo(OrderSymbol(),MODE_BID);
   double ask = MarketInfo(OrderSymbol(),MODE_ASK);
   int pos=0;
   for(pos = OrdersTotal()-1; pos >= 0 ; pos--){
      bool check = OrderSelect(pos, SELECT_BY_POS);
      double SL=OrderStopLoss();
      if(check &&OrderSymbol()==Symbol()){
         if(OrderType()==OP_BUY&&ask>SL&&ask>OrderOpenPrice()){
            OrderModify(OrderTicket(),OrderOpenPrice(),low,OrderTakeProfit(),0);
            
         }
         else if(OrderType()==OP_SELL&&bid<SL&&bid<OrderOpenPrice()){
            OrderModify(OrderTicket(),OrderOpenPrice(),high,OrderTakeProfit(),0);
                   
         }  
      }
   }
}

If i understand correctly (which i don't since its not working), it should only myve the stop loss if Ask is greater than SL which is stop loss as is highlighted above. Also i have not done anything to make this work in JPY pairs but it is only working on 5 digit pairs at the moment so if you have a suggesting that would fix that i'd appreciate that too.


Thanks in advance for any assistance/advice you can give!

Seng Joo Thio
1134
Seng Joo Thio  

I think the ask>SL condition is redundant because if Ask<=SL the pos would have been closed already. Instead, change it to low>SL instead to make sure the stoploss only move up rather than down.

Similar for bid<SL for OP_SELL - change to high<SL instead.

dkamerond
220
dkamerond  
Perfect thanks! That got it working.
To add comments, please log in or register