Help with setting a timer on trades?

Mr.T0r0sTrades  

So I made this simple exponential and simple moving average expert advisor nothing crazy but I'm having trouble with giving a timer for every trade it enters whether it be long or short it updates the trailing stop loss every 20 minutes can someone help or at least provided some resource that can help?


void OnTick()
  {
  
  string signal = "";
  
   double SlowMovingAverage = iMA(NULL,0,5,0,MODE_SMA,PRICE_CLOSE,0);
   
   double LastSlowMovingAverage = iMA(NULL,0,5,0,MODE_SMA,PRICE_CLOSE,1);

   double FastMovingAverage = iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,0);

   double LastFastMovingAverage = iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,1);


   if ((LastFastMovingAverage < LastSlowMovingAverage)&&(FastMovingAverage > SlowMovingAverage))
      {
      signal = "Buy";
      }
      
      if(signal=="Buy" && OrdersTotal()==0)
      int Buyticket = OrderSend(Symbol(),OP_BUY,3,Ask,3,0,0,NULL,0,0,Green);
      //we count all the orders
      for (int b=OrdersTotal()-1;b>=0;b--)
       {
      //we select one of the orders
      if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
         //we check if the order belongs to the current chart
         if (OrderSymbol()==Symbol())
            //if it is a buy order
               if (OrderType()==OP_BUY)
               {
                  //if the stop loss is below 150 points
                  if (OrderStopLoss() < Ask - (1000* _Point))
                     //we modify the stop loss
                     OrderModify(
                                    OrderTicket(),  //for current order
                                    OrderOpenPrice(),  //opened for the openprice
                                    Ask - (1000* _Point),  //set stop loss
                                     OrderTakeProfit(),  //unchanged take profit
                                    0,                  // no experiation
                                    CLR_NONE             //no color
                     );                
          }
          
        }

   if ((LastFastMovingAverage > LastSlowMovingAverage)&&(FastMovingAverage < SlowMovingAverage))
      {
      signal = "Sell";
      }
       //we count all the orders
       if(signal=="Sell" && OrdersTotal()==0)
        int sellTicket = OrderSend(Symbol(),OP_SELL,3,Bid,3,0,0,NULL,0,0,Red);
         for (int s=OrdersTotal()-1;s>=0;s--)
         {
            //we select one of the orders
             if (OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
               //we check if the order belongs to the current chart
                  if (OrderSymbol()==Symbol())
                  //if it is a buy order
                     if (OrderType()==OP_SELL)
               {
                  //if the stop loss is below 150 points
                  if (OrderStopLoss()==0 || OrderStopLoss() > Bid + (1000* _Point))
                     //we modify the stop loss
                     OrderModify(
                                    OrderTicket(),  //for current order
                                    OrderOpenPrice(),  //opened for the openprice
                                    Bid + (1000* _Point),  //set stop loss
                                    OrderTakeProfit(),  //unchanged take profit
                                    0,                  // no experiation
                                    CLR_NONE             //no color
                     );                
               }

         }
   }



R4tna C  
Mr.T0r0sTrades:

So I made this simple exponential and simple moving average expert advisor nothing crazy but I'm having trouble with giving a timer for every trade it enters whether it be long or short it updates the trailing stop loss every 20 minutes can someone help or at least provided some resource that can help?


Here is a simple example of how you can create a delay - in this case 3 secs. You could adjust this so it executes every 20 mins and apply it to your code where you need it

//+------------------------------------------------------------------+
//|                                             429041-Timedelay.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//  
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+


#define  delayInSecs 3
datetime nextActionTime = 0;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {

   if(nextActionTime <= TimeCurrent())
     {
      Alert(StringFormat("Alert at %s", TimeToString(TimeCurrent(), TIME_SECONDS)));
      nextActionTime = TimeCurrent() + delayInSecs;
     }


  }
//+------------------------------------------------------------------+
Mr.T0r0sTrades  
R4tna C #:

Here is a simple example of how you can create a delay - in this case 3 secs. You could adjust this so it executes every 20 mins and apply it to your code where you need it

that totally worked dude appreciate the help thanks much appreciated

R4tna C  
Mr.T0r0sTrades #:

that totally worked dude appreciate the help thanks much appreciated

Great! Happy it worked for you

Reason: