Couldn't get the Stop Loss and Trailing Stop Loss functioning

 

Dear All,


Here is my personal usage EA code, it run's okay but can't get the stop loss and trailing stop loss to be functioning. Do you mind helping me. If possible please point out where should the instruction for 50 pip stop loss should be placed.

extern double lTakeProfit = 10;
extern double sTakeProfit = 10;
extern double lTrailingStop = 15;
extern double sTrailingStop = 15;
extern color clOpenBuy = Blue;
extern color clCloseBuy = Aqua;
extern color clOpenSell = Red;
extern color clCloseSell = Violet;
extern color clModiBuy = Blue;
extern color clModiSell = Red;
extern string Name_Expert = "persona non grata";
extern int Slippage = 2;
extern bool UseSound = TRUE;
extern string NameFileSound = "alert.wav";
extern double Lots = 0.10;


void deinit() {
   Comment("");
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start(){
   if(Bars<100){
      Print("bars less than 100");
      return(0);
   }
   if(lTakeProfit<10){
      Print("TakeProfit less than 10");
      return(0);
   }
   if(sTakeProfit<10){
      Print("TakeProfit less than 10");
      return(0);
   }

   double diClose0=iClose(NULL,5,0);
   double diMA1=iMA(NULL,5,7,0,MODE_SMA,PRICE_OPEN,0);
   double diClose2=iClose(NULL,5,0);
   double diMA3=iMA(NULL,5,6,0,MODE_SMA,PRICE_OPEN,0);

   if(AccountFreeMargin()<(1000*Lots)){
      Print("We have no money. Free Margin = ", AccountFreeMargin());
      return(0);
   }
   if (!ExistPositions()){

      if ((diClose0<diMA1)){
         OpenBuy();
         return(0);
      }

      if ((diClose2>diMA3)){
         OpenSell();
         return(0);
      }
   }
   TrailingPositionsBuy(lTrailingStop);
   TrailingPositionsSell(sTrailingStop);
   return (0);
}

bool ExistPositions() {
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol()) {
return(True);
}
} 
} 
return(false);
}
void TrailingPositionsBuy(int trailingStop) { 
   for (int i=0; i<OrdersTotal(); i++) { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { 
         if (OrderSymbol()==Symbol()) { 
            if (OrderType()==OP_BUY) { 
               if (Bid-OrderOpenPrice()>trailingStop*Point) { 
                  if (OrderStopLoss()<Bid-trailingStop*Point) 
                     ModifyStopLoss(Bid-trailingStop*Point); 
               } 
            } 
         } 
      } 
   } 
} 
void TrailingPositionsSell(int trailingStop) { 
   for (int i=0; i<OrdersTotal(); i++) { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { 
         if (OrderSymbol()==Symbol()) { 
            if (OrderType()==OP_SELL) { 
               if (OrderOpenPrice()-Ask>trailingStop*Point) { 
                  if (OrderStopLoss()>Ask+trailingStop*Point || 
OrderStopLoss()==0)  
                     ModifyStopLoss(Ask+trailingStop*Point); 
               } 
            } 
         } 
      } 
   } 
} 

//+------------------------------------------------------------------+
//| Modify StopLoss                                                  |
//+------------------------------------------------------------------+
void ModifyStopLoss(double ldStopLoss) { 
   bool fm;
   fm = OrderModify(OrderTicket(),OrderOpenPrice
(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE); 
   if (fm && UseSound) PlaySound(NameFileSound); 
}
//+------------------------------------------------------------------+ 

void OpenBuy() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 
   ldLot = GetSizeLot(); 
   ldStop = 0.5; 
   ldTake = GetTakeProfitBuy(); 
   lsComm = GetCommentForOrder(); 
   OrderSend(Symbol
(),OP_BUY,ldLot,Ask,Slippage,ldStop,ldTake,lsComm,0,0,clOpenBuy); 
   if (UseSound) PlaySound(NameFileSound); 
} 
void OpenSell() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 

   ldLot = GetSizeLot(); 
   ldStop = 0.5; 
   ldTake = GetTakeProfitSell(); 
   lsComm = GetCommentForOrder(); 
   OrderSend(Symbol
(),OP_SELL,ldLot,Bid,Slippage,ldStop,ldTake,lsComm,0,0,clOpenSell); 
   if (UseSound) PlaySound(NameFileSound); 
} 
string GetCommentForOrder() { return(Name_Expert); } 
double GetSizeLot() { return(Lots); } 
double GetTakeProfitBuy() { return(Ask+lTakeProfit*Point); } 
double GetTakeProfitSell() { return(Bid-sTakeProfit*Point); } 
 
It is EA by KimIV and it's working good. What is your problem? If you use it with Alpari broker you have to increase Trailingstops and TakeProfits to 10 times.
 
Roger:
It is EA by KimIV and it's working good. What is your problem? If you use it with Alpari broker you have to increase Trailingstops and TakeProfits to 10 times.

the problem is the stop loss and/or trailing stop loss is never functioning. That's is the main problem.

 
do anyone have any idea to resolve the issue of non active stop loss and/or trailing stop loss on this EA?
 
anyone can help with this code.... until now I can't get this EA to run STOP LOSS function... could anyone point the error on the code?
 
sysop:
anyone can help with this code.... until now I can't get this EA to run STOP LOSS function... could anyone point the error on the code?

Hi sysop


The code is fine.

1. The reason your trailing stop doesn't work is because the take profit you have set (10) is closer than the trailing stop (15). If you set LTakeprofit and STakeProfit much larger it works fine.

2. You do need to multiply all your point values by 10 for it to work properly with a 5-digit broker.

3. It won't place SELL orders because your initial stop loss (ldStop = 0.5;) is below the entry price. If you set it to 2.5, for example, you get short positions as well.


Cheers

Jellybean

 
Jellybean:

Hi sysop


The code is fine.

1. The reason your trailing stop doesn't work is because the take profit you have set (10) is closer than the trailing stop (15). If you set LTakeprofit and STakeProfit much larger it works fine.

2. You do need to multiply all your point values by 10 for it to work properly with a 5-digit broker.

3. It won't place SELL orders because your initial stop loss (ldStop = 0.5;) is below the entry price. If you set it to 2.5, for example, you get short positions as well.


Cheers

Jellybean

I've follow your guidelines but still the stop loss can't get to the exact point (i.e if buy GBP/JPY at 153.22) ... then if I set up ldStop at 2.5 and TP at 25 pips, the value I get on the MT Platform is like this..


Buy: 153.22

SL: 2.50

TP: 153.47


What's wrong with the ldStop?

 
Put in the begining:
extern double lStopLoss = 50;
extern double sStopLoss = 50;
then replace
void OpenBuy() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 
   ldLot = GetSizeLot(); 
   ldStop = 0.5; 
   ldTake = GetTakeProfitBuy(); 
   lsComm = GetCommentForOrder(); 
   OrderSend(Symbol
(),OP_BUY,ldLot,Ask,Slippage,ldStop,ldTake,lsComm,0,0,clOpenBuy); 
   if (UseSound) PlaySound(NameFileSound); 
} 
void OpenSell() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 

   ldLot = GetSizeLot(); 
   ldStop = 0.5; 
   ldTake = GetTakeProfitSell(); 
   lsComm = GetCommentForOrder(); 
   OrderSend(Symbol
(),OP_SELL,ldLot,Bid,Slippage,ldStop,ldTake,lsComm,0,0,clOpenSell); 
   if (UseSound) PlaySound(NameFileSound); 
} 
to
void OpenBuy() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 
   ldLot = GetSizeLot(); 
   ldStop = Ask-lStopLoss*Point; 
   ldTake = GetTakeProfitBuy(); 
   lsComm = GetCommentForOrder(); 
   OrderSend(Symbol
(),OP_BUY,ldLot,Ask,Slippage,ldStop,ldTake,lsComm,0,0,clOpenBuy); 
   if (UseSound) PlaySound(NameFileSound); 
} 
void OpenSell() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 

   ldLot = GetSizeLot(); 
   ldStop = Bid+sStopLoss*Point;
   ldTake = GetTakeProfitSell(); 
   lsComm = GetCommentForOrder(); 
   OrderSend(Symbol
(),OP_SELL,ldLot,Bid,Slippage,ldStop,ldTake,lsComm,0,0,clOpenSell); 
   if (UseSound) PlaySound(NameFileSound); 
} 
 
Roger:
Put in the begining:
then replace
to

Done... it work like what it should be.. thanks..

 
sysop:

I've follow your guidelines but still the stop loss can't get to the exact point (i.e if buy GBP/JPY at 153.22) ... then if I set up ldStop at 2.5 and TP at 25 pips, the value I get on the MT Platform is like this..


Buy: 153.22

SL: 2.50

TP: 153.47


What's wrong with the ldStop?

Hi sysop

Further to Roger's reply above, you must also make sure that lTrailingStop is less than lTakeProfit and sTrailingStop is less than sTakeProfit. The reason is that this code does not trail a stop-loss until the price has moved beyond the entry price by the amount of the trailing stop. If the take profit is closer, the order will close before it gets a chance to trail the stop.

Cheers

Jellybean

 
Jellybean:

Hi sysop

Further to Roger's reply above, you must also make sure that lTrailingStop is less than lTakeProfit and sTrailingStop is less than sTakeProfit. The reason is that this code does not trail a stop-loss until the price has moved beyond the entry price by the amount of the trailing stop. If the take profit is closer, the order will close before it gets a chance to trail the stop.

Cheers

Jellybean

Yup... done that also... thanks for the reminder...

Reason: