This EA is not trading. Can anyone tell me why. Also, is this the correct way to code Take Profit and Stop Loss?

 

Can you help? I am learning MQL4 and working through the MQL4 tutorials. I have been working on the ROC EA example and have made some tweaks to the code so all it takes for a trade to be made - either opening or closing - is the crossing of one specialised MA(Moving Average) over another. The MA's are calculated via a separate indicator called rocseparate. The data of the two MA's are stored in Buffer 4 and Buffer 5 respectively.

The Stop Loss and Take Profit (TP) are set as external variables but the EA can close trades if the MA criteria is correct before it reaches TP.

The EA is NOT trading!! So my questions are: -

1) Why?

2) Why??

3) I calculated the StopLoss and TP as the External variable x Point. However, in order to get the correct figures I had to multiply the ponts by 10, is this the correct way of getting the orders placed correctly according to the parameters set in the external variables?

SL=Bid - StopLoss*(Point*10); // Calculating SL of opened

TP=Bid + TakeProfit*(Point*10); // Calculating TP of opened.

Any help, as ever, would be very much appreciated.

Thanks

W

extern double StopLoss  =100;          // SL for an opened order
extern double TakeProfit=35;           // TP for an opened order
extern double Lots      =0.1;          // Strictly set amount of lots
extern double Prots     =0.07;         // Percent of free margin
//-------------------------------------------------------------- 1a --
extern int Period_MA_1  =56;           // Period of calculation MA
extern int Bars_V       =34;           // Amount of bars for rate calculation
extern int Aver_Bars    =0;            // Amount of bars for smoothing
//-------------------------------------------------------------- 1b --
bool   Work=true;                      // EA will work.
string Symb;                           // Security name
//--------------------------------------------------------------- 2 --
int start()
  {
   int
   Total,                              // Amount of orders in a window
   Tip=-1,                             // Type of selected order (B=0,S=1)
   Ticket;                             // Order number
   double
   MA_1_t,                             // Current MA_1 value
   MA_2_t,                             // Current MA_2 value
   Lot,                                // Amount of lots in a selected order
   Lts,                                // Amount of lots in an opened order
   Min_Lot,                            // Minimal amount of lots
   Step,                               // Step of lot size change
   Free,                               // Current free margin
   One_Lot,                            // Price of one lot
   Price,                              // Price of a selected order
   SL,                                 // SL of a selected order 
   TP;                                 // TP of a selected order
   bool
   Ans  =false,                        // Server response after closing
   Cls_B=false,                        // Criterion for closing Buy
   Cls_S=false,                        // Criterion for closing Sell
   Opn_B=false,                        // Criterion for opening Buy
   Opn_S=false;                        // Criterion for opening Sell


   // Trading criteria
   int H= 1000;                   // Amount of bars in calc. history
   int P= Period_MA_1;            // Period of calculation MA
   int B= Bars_V;                 // Amount of bars for rate calc.
   int A= Aver_Bars;              // Amount of bars for smoothing
//-------------------------------------------------------------- 5a --
   double L_1=iCustom(NULL,0,"rocseparate",H,P,B,A,4,0);
   double L_5=iCustom(NULL,0,"rocseparate",H,P,B,A,5,0);
//-------------------------------------------------------------- 5b --
   if (L_1>L_5)
     {
      Opn_B=true;                               // Criterion for opening Buy
      Cls_S=true;                               // Criterion for closing Sell
     }
   if (L_1<L_5)
     {
      Opn_S=true;                               // Criterion for opening Sell
      Cls_B=true;                               // Criterion for closing Buy
     }
//--------------------------------------------------------------- 6 --
   // Closing orders
   while(true)                                  // Loop of closing orders
     {
      if (Tip==0 && Cls_B==true)                // Order Buy is opened..
        {                                       //and there is criterion to close
         Alert("Attempt to close Buy ",Ticket,". Waiting for response..");
         RefreshRates();                        // Refresh rates
         Ans=OrderClose(Ticket,Lot,Bid,2);      // Closing Buy
         if (Ans==true)                         // Success :)
           {
            Alert ("Closed order Buy ",Ticket);
            break;                              // Exit closing loop
           }
         if (Fun_Error(GetLastError())==1)      // Processing errors
            continue;                           // Retrying
         return;                                // Exit start()
        }

      if (Tip==1 && Cls_S==true)                // Order Sell is opened..
        {                                       // and there is criterion to close
         Alert("Attempt to close Sell ",Ticket,". Waiting for response..");
         RefreshRates();                        // Refresh rates
         Ans=OrderClose(Ticket,Lot,Ask,2);      // Closing Sell
         if (Ans==true)                         // Success :)
           {
            Alert ("Closed order Sell ",Ticket);
            break;                              // Exit closing loop
           }
         if (Fun_Error(GetLastError())==1)      // Processing errors
            continue;                           // Retrying
         return;                                // Exit start()
        }
      break;                                    // Exit while
     }

   // Opening orders
   while(true)                                  // Orders closing loop
     {
      if (Total==0 && Opn_B==true)              // No new orders +
        {                                       // criterion for opening Buy
         RefreshRates();                        // Refresh rates
         SL=Bid - StopLoss*(Point*10);               // Calculating SL of opened
         TP=Bid + TakeProfit*(Point*10);             // Calculating TP of opened.
         Alert("Attempt to open Buy. Waiting for response..");
         Ticket=OrderSend(Symb,OP_BUY,Lts,Ask,2,SL,TP);// Buy
         if (Ticket > 0)                        // Success :)           
           {
            Alert ("Opened order Buy ",Ticket);
            return;                             // Exit start()
           }
         if (Fun_Error(GetLastError())==1)      // Processing errors
            continue;                           // Retrying
         return;                                // Exit start()        
        }
      if (Total==0 && Opn_S==true)              // No new orders +
        {                                       // criterion for opening Sell
         RefreshRates();                        // Refresh rates
         SL=Ask + StopLoss*(Point*10);               // Calculating SL of opened
         TP=Ask - TakeProfit*(Point*10);             // Calculating TP of opened
         Alert("Attempt to open Sell. Waiting for response..");
         Ticket=OrderSend(Symb,OP_SELL,Lts,Bid,2,SL,TP);//Opening Sells
         if (Ticket > 0)                        // Success :)
           {
            Alert ("Opened oredr Buy ",Ticket);
            return;                             // Exit start()
           }
         if (Fun_Error(GetLastError())==1)      // Processing errors
            continue;                           // Retrying
         return;                                // Exit start()
        }
      break;                                    // Exit while
     }
 
Willforth:

Can you help? I am learning MQL4 and working through the MQL4 tutorials. I have been working on the ROC EA example and have made some tweaks to the code so all it takes for a trade to be made - either opening or closing - is the crossing of one specialised MA(Moving Average) over another. The MA's are calculated via a separate indicator called rocseparate. The data of the two MA's are stored in Buffer 4 and Buffer 5 respectively.

The Stop Loss and Take Profit (TP) are set as external variables but the EA can close trades if the MA criteria is correct before it reaches TP.

The EA is NOT trading!! So my questions are: -

1) Why?

2) Why??

3) I calculated the StopLoss and TP as the External variable x Point. However, in order to get the correct figures I had to multiply the ponts by 10, is this the correct way of getting the orders placed correctly according to the parameters set in the external variables?

SL=Bid - StopLoss*(Point*10); // Calculating SL of opened

TP=Bid + TakeProfit*(Point*10); // Calculating TP of opened.

Any help, as ever, would be very much appreciated.

Thanks

W


How is it not trading in strategietester or on a demo/real account not live trading

Check atleast inside terminal you have tab Experts and Journal read the messages inside this

 
while(true)                                  // Orders closing loop
    

while what is true ?

Should that not be while(Work) ?

Ok ignore that, I tested it in a script you can use while that way, I wasn't aware you could set the condition of while explicitly without some conditions ... you learn something new every day ... btw did you check your log file for errors ?

 

While you mess up ... :)

      // Closing orders
      if (Tip==0 && Cls_B==true)           
        {  
          while(Ans == false)                          
           {                                  

      if (Tip==1 && Cls_S==true)              
        {                                   
         while(Ans == false)                                
           { 

      // Opening orders
      if (Total==0 && Opn_B==true)           
        {        
        while(Ticket < 1)                      
           {                             

      if (Total==0 && Opn_S==true)     
        {                            
        while(Ticket < 1) 
 
Willforth:

The EA is NOT trading!! So my questions are: -

1) Why?

Why aren't you checking if your OrderSend() works or fails ? if it fails it returns -1 . . . so check if the ticket is -1 if it is report the error then you will know why it has failed . . .

Is your Broker an ECN Broker ? if they are you can't set SL & TP when you do the OrderSend(), you have to send with TP & SL at 0.0 and then do an OrderModify()

 
SL=Bid - StopLoss*(Point*10); // Calculating SL of opened
Adjust for 4/5 digit brokers (tp, sl, AND slippage.) Adjust for ECN brokers. Always test return codes.
//++++ These are adjusted for 5 digit brokers.
int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.015      0.0150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int     init(){                                                     OptParameters();
     if (Digits % 2 == 1){      // DE30=1/JPY=3/EURUSD=5 https://www.mql5.com/en/forum/135345
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//---- These are adjusted for 5 digit brokers.
    /* On ECN brokers you must open first and THEN set stops
    int ticket = OrderSend(..., 0,0,...)
    if (ticket < 0)
       Alert("OrderSend failed: ", GetLastError());
    else if (!OrderSelect(ticket, SELECT_BY_TICKET))
       Alert("OrderSelect failed: ", GetLastError());
    else if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0)
       Alert("OrderModify failed: ", GetLastError());
     */
 
As ever, thanks very much for all of your help. Just to let you know, the reason the EA was not trading was due to the indicator not passing relevant information for the EA to work. The moment I changed the indicator the orders were placed correctly. Thanks very much for the advice on the handling of symbols with different decimal places that was a great help and I have implemented that in the EA.
Reason: