Need help to unlocke my EA - Trailing Stop / Order

 

Hi everyone, 

I tried to code my EA but I meet differents problems, 

I don't understand why this EA send many trade at the same time (I try to block the code with 1 trade by 1), also How can I add a pause of 5 mins by exemple after an orderclose ? 


Thanks in advance for your time and your help. 

Take care of you. 

#property strict

extern int TakeProfit = 7000; // 70 pts 
extern int StopLoss = 2500; // 25 pts 
extern double LotSize = 1.00;
extern int period_RSI = 14;
extern int slipage = 700;
extern int Magic = 18042019;

extern int iHeureDebut = 8 ; 
extern int iMinutesDebut = 00 ; 
extern int iHeureFin = 22 ; 
extern int iMinutesFin = 00 ; 
extern int iTimeDebut ; 
extern int iTimeFin ; 
extern double TrailingStart=2500 ; // Trailing starting after +25 pts  
extern double TrailingStop=2000 ; // Break Even
extern double TrailingStep=100; // SL up 1 pt by 1 pt 

int i;

//+-----------------------------------------------------------------+
//|     OnTick function                                                                                                 |
//+-----------------------------------------------------------------+
int init()
{
iTimeDebut=iHeureDebut*3600+iMinutesDebut*60;
iTimeFin=iHeureFin*3600+iMinutesFin*60;
return (0) ;
}

void OnTick() 
{
   // Indicator M1 
   double RMA5 = iMA(Symbol(),1,5,0,MODE_SMA,PRICE_CLOSE,0) ; // MM 5 périod 1 min 
   double RMA10 = iMA(Symbol(),1,10,0,MODE_SMA,PRICE_CLOSE,0) ; // MM 10 périod 1 min 
   double Stock1 = iStochastic(Symbol(),1,5,3,3,MODE_SMA,0,MODE_MAIN,1) ; // Main Line - green 
   double Stock2 = iStochastic(Symbol(),1,5,3,3,MODE_SMA,0,MODE_SIGNAL,1) ; // Signal line - red
   double RSI1 = iRSI(Symbol(),1, period_RSI, PRICE_CLOSE, 1); // RSI 1 

   double SL_Buy = NormalizeDouble(Bid- StopLoss * Point, Digits);
   double TP_Buy = NormalizeDouble(Bid+ TakeProfit * Point, Digits);
   double SL_Sell = NormalizeDouble(Ask + StopLoss * Point, Digits);
   double TP_Sell = NormalizeDouble(Ask - TakeProfit * Point, Digits);
        bool NumPos = false; // Number of position 
        bool openbuy = false;
        bool opensell = false;
   int iTimeSeconds ; 
   iTimeSeconds = Hour()*3600+Minute()*60+Seconds();
   long VOL = iVolume (Symbol(),0,1);


        // ---------------------- Ordre Buy
        // if RMA5 is more than RMA10 
        // if Stock1 is > Stock2 
        // RSI > 45
        // One trade by one trade 
        for (i = OrdersTotal() - 1; i < OrdersTotal(); i++) 
           {
                if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) 
                   {
                        continue; // exit the cycle 
                NumPos++;
                   }
           }
        if (!NumPos && (iTimeSeconds>=iTimeDebut&&iTimeSeconds<iTimeFin)) 
           {
                if (RMA5 >= RMA10 
                && Stock1 > Stock2 
                && RSI1 >= 45
                ) 
                   {
                        openbuy = OrderSend(Symbol(), OP_BUY, LotSize, Ask, slipage, SL_Buy, TP_Buy, "BUY", Magic, 0, clrGreen);
                   }
           }
 // Order Sell 
// RSI < 50 
// RMA5 < RMA10 
// Stock1 < Stock2

                 for (i = OrdersTotal() - 1; i < OrdersTotal(); i++) 
           {
                if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) 
                   {
                        continue; // exit the cycle 
                   }
                NumPos++;
           }
           
        if (!NumPos && (iTimeSeconds>=iTimeDebut&&iTimeSeconds<iTimeFin)) 
    {
                if ( RMA5 < RMA10 && RSI1 < 50  ) 
                   {
                        opensell = OrderSend(Symbol(), OP_SELL, LotSize, Bid, slipage, SL_Sell, TP_Sell, "SELL", Magic, 0, clrYellow);
                   }
         }
 // Trailing stop 
 
 double tStopLoss = NormalizeDouble(OrderStopLoss(),Digits);
 double sl = OrderStopLoss(); 
 bool ticket = false ; 
 
RefreshRates();
      if (OrdersTotal()>0)
         {
         for (i=OrdersTotal(); i >= 0 ; i--) 
               {
               if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)== true)
                  if (OrderType()==OP_BUY)
                        {
                  if (Ask>NormalizeDouble(OrderOpenPrice()+TrailingStart*Point,Digits) && tStopLoss < NormalizeDouble (Bid - (TrailingStop+TrailingStep)*Point,Digits))
                              {
                        tStopLoss = NormalizeDouble (Bid-TrailingStop*Point,Digits); 
                      ticket = OrderModify (OrderTicket(),OrderOpenPrice(),tStopLoss, OrderTakeProfit(),0,White);
                       
                             }
                          } 
                   if (OrderType()==OP_SELL) 
              {
                  if (Bid < NormalizeDouble (OrderOpenPrice()-TrailingStart*Point,Digits) && (sl > (NormalizeDouble (Ask+(TrailingStop+TrailingStep)*Point,Digits)))
)
   {
tStopLoss = NormalizeDouble(Ask+TrailingStop*Point,Digits);
ticket = OrderModify(OrderTicket(),OrderOpenPrice(),tStopLoss,OrderTakeProfit(),0,Red);

                 
         }
    }
   }
   }
           
         
        for (i = OrdersTotal(); i >= 0; i--) {
                // ------------------ Order Buy Close 
                // if the candle 0 (Close ) is lower than the candle 2 (Close)
                // if the RSI [1] is lower than 40   
                if (OrderSelect(OrderTicket(), SELECT_BY_TICKET, MODE_TRADES) == true) {
                        if (OrdersTotal() >= 1 && OrderType() == OP_BUY &&  RSI1 <= 40  ) {
                                int OrderBuyClose = OrderClose(OrderTicket(), LotSize, Ask, slipage, Red);
                        }
                }

        }
        // Order Sell Close 
// if the candle 0 (Close ) is upper than the candle 2 (Close)
// if RSI [1] > 57 
                if (OrderSelect(OrderTicket(), SELECT_BY_TICKET) == true) {
                        if (OrdersTotal() >= 1 && OrderType() == OP_SELL && RSI1 >= 57) {
                                int OrderSellClose = OrderClose(OrderTicket(), LotSize, Bid, slipage, Orange);
                        }
                }
}  
 

Won't try to understand your code, I'm not that bright...

But if you're trying to only make one trade at a time, you just need to read existing orders, and if there is a trade already, do something...

This function would tell me if there are any existing orders for the chart symbol. You might need to add in other stuff like pending orders, magicnumber etc.

int ticket;
void OnTick(){
// other code
getexistingticket(ticket);
 if(ticket!=-1)return;
}

void getexistingticket(int &ticket){
ticket=-1;
 for(int i=OrdersTotal()-1;i>=0;i--)
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)&&OrderSymbol()==_Symbol){
       ticket=OrderTicket();
        return;}
      
}
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Requests to execute trade operations are formalized as orders. Each order has a variety of properties for reading. Information on them can be obtained using functions Position identifier that is set to an order as soon as it is executed. Each executed order results in a deal that opens or modifies an already existing position. The...
 
andrew:

Won't try to understand your code, I'm not that bright...

But if you're trying to only make one trade at a time, you just need to read existing orders, and if there is a trade already, do something...

This function would tell me if there are any existing orders for the chart symbol. You might need to add in other stuff like pending orders, magicnumber etc.

Hi Andrew, 


Thanks for you reply. 

I will try to change it asap. 

Reason: