Download MetaTrader 5
To add comments, please log in or register
Sign up for MQL5.community to leave your comments!
fly7680
307
fly7680 2016.07.25 13:07 

hello, I plugged in my code a time lock but I have problems. I have duplicate this code, but in the time 14:30 20:00 signals that I receive are all wrong:


//----------------------------------------------------------------------------------------------------//
int Ora_From_Hour = 08; //time of the day                                                             //
int Min_From_Min  = 00; //time of the day                                                             //
int Ora_To_Hour   = 12; //time of the day                                                             //
int Min_To_Min    = 30; //time of the day                                                             //
bool inTimeInterval(datetime t, int TOD_From_Hour, int TOD_From_Min, int TOD_To_Hour, int TOD_To_Min) //
  {                                                                                                   //
   string TOD      = TimeToString(t, TIME_MINUTES);                                                   //
   string TOD_From = StringFormat("%02d", TOD_From_Hour)+":"+StringFormat("%02d", TOD_From_Min);      //
   string TOD_To   = StringFormat("%02d", TOD_To_Hour)+":"+StringFormat("%02d", TOD_To_Min);          //
   return((StringCompare(TOD, TOD_From)  >= 0 && StringCompare(TOD, TOD_To) <= 0)                     //
     ||  (StringCompare(TOD_From, TOD_To)>  0                                                         //
     &&  ((StringCompare(TOD, TOD_From)  >= 0 && StringCompare(TOD, "23:59")<= 0)                     //
     ||  (StringCompare(TOD, "00:00")    >= 0 && StringCompare(TOD, TOD_To) <= 0))));                 //
  }                                                                                                   //
//----------------------------------------------------------------------------------------------------//

//----------------------------------------------------------------------------------------------------//
int Ora_From_Hour2 = 14; //time of the day                                                            //
int Min_From_Min2  = 30; //time of the day                                                            //
int Ora_To_Hour2   = 20; //time of the day                                                            //
int Min_To_Min2    = 00; //time of the day                                                            //
bool inTimeInterval2(datetime t, int TOD_From_Hour, int TOD_From_Min, int TOD_To_Hour, int TOD_To_Min)//
  {                                                                                                   //
   string TOD      = TimeToString(t, TIME_MINUTES);                                                   //
   string TOD_From = StringFormat("%02d", TOD_From_Hour)+":"+StringFormat("%02d", TOD_From_Min);      //
   string TOD_To   = StringFormat("%02d", TOD_To_Hour)+":"+StringFormat("%02d", TOD_To_Min);          //
   return((StringCompare(TOD, TOD_From)  >= 0 && StringCompare(TOD, TOD_To) <= 0)                     //
     ||  (StringCompare(TOD_From, TOD_To)>  0                                                         //
     &&  ((StringCompare(TOD, TOD_From)  >= 0 && StringCompare(TOD, "23:59")<= 0)                     //
     ||  (StringCompare(TOD, "00:00")    >= 0 && StringCompare(TOD, TOD_To) <= 0))));                 //
  }                                                                                                   //
//----------------------------------------------------------------------------------------------------//




// After condition i insert this code for time block

        {
         if (!inTimeInterval(Time[i], Ora_From_Hour, Min_From_Min, Ora_To_Hour, Min_To_Min))     continue;          
         Buffer1[i] = High[i] + 1 * myPoint; 
        }
        else
        {
         if (!inTimeInterval2(Time[i], Ora_From_Hour2, Min_From_Min2, Ora_To_Hour2, Min_To_Min2)) continue;
         Buffer1[i] = High[i] + 1 * myPoint; 
        }
fly7680
307
fly7680 2016.08.04 05:28  
I'm doing more testing, I leave no errors but I think is not working properly, the correct code is written in your opinion?

Thank you


//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 Yellow
#property indicator_label1 "Sell"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 1
#property indicator_color2 Yellow
#property indicator_label2 "Buy"

//--- indicator buffers
double Buffer1[];
double Buffer2[];




extern string RSI       =" Setting RSI";
extern int Periodo      = 2;
extern double RSI_Max   = 70;
extern double RSI_Min   = 30;

datetime time_alert; //used when sending alert
extern bool Audible_Alerts = true;
double myPoint; //initialized in OnInit

//----------------------------------------------------------------------------------------------------//
int Ora_From_Hour = 08; //time of the day                                                             //
int Min_From_Min  = 00; //time of the day                                                             //
int Ora_To_Hour   = 12; //time of the day                                                             //
int Min_To_Min    = 30; //time of the day                                                             //
bool inTimeInterval(datetime t, int TOD_From_Hour, int TOD_From_Min, int TOD_To_Hour, int TOD_To_Min) //
  {                                                                                                   //
   string TOD      = TimeToString(t, TIME_MINUTES);                                                   //
   string TOD_From = StringFormat("%02d", TOD_From_Hour)+":"+StringFormat("%02d", TOD_From_Min);      //
   string TOD_To   = StringFormat("%02d", TOD_To_Hour)+":"+StringFormat("%02d", TOD_To_Min);          //
   return((StringCompare(TOD, TOD_From)  >= 0 && StringCompare(TOD, TOD_To) <= 0)                     //
     ||  (StringCompare(TOD_From, TOD_To)>  0                                                         //
     &&  ((StringCompare(TOD, TOD_From)  >= 0 && StringCompare(TOD, "23:59")<= 0)                     //
     ||  (StringCompare(TOD, "00:00")    >= 0 && StringCompare(TOD, TOD_To) <= 0))));                 //
  }                                                                                                   //
//----------------------------------------------------------------------------------------------------//

//----------------------------------------------------------------------------------------------------//
int Ora_From_Hour2 = 14; //time of the day                                                            //
int Min_From_Min2  = 30; //time of the day                                                            //
int Ora_To_Hour2   = 20; //time of the day                                                            //
int Min_To_Min2    = 00; //time of the day                                                            //
bool inTimeInterval2(datetime t, int TOD_From_Hour, int TOD_From_Min, int TOD_To_Hour, int TOD_To_Min)//
  {                                                                                                   //
   string TOD      = TimeToString(t, TIME_MINUTES);                                                   //
   string TOD_From = StringFormat("%02d", TOD_From_Hour)+":"+StringFormat("%02d", TOD_From_Min);      //
   string TOD_To   = StringFormat("%02d", TOD_To_Hour)+":"+StringFormat("%02d", TOD_To_Min);          //
   return((StringCompare(TOD, TOD_From)  >= 0 && StringCompare(TOD, TOD_To) <= 0)                     //
     ||  (StringCompare(TOD_From, TOD_To)>  0                                                         //
     &&  ((StringCompare(TOD, TOD_From)  >= 0 && StringCompare(TOD, "23:59")<= 0)                     //
     ||  (StringCompare(TOD, "00:00")    >= 0 && StringCompare(TOD, TOD_To) <= 0))));                 //
  }                                                                                                   //
//----------------------------------------------------------------------------------------------------//


void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | RSIalert @ "+Symbol()+","+Period()+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      if(Audible_Alerts) Alert(type+" | RSIalert @ "+Symbol()+","+Period()+" | "+message);
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 159);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 159);
   //initialize myPoint
   Comment("Copyright © 2016, Trade60Secondi.com");
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }


//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation  
      
      

      // Indicator Buffer 1
      if(iRSI(NULL, PERIOD_CURRENT, Periodo, PRICE_CLOSE, 1+i) > RSI_Max //Relative Strength Index > fixed value
            )
        {
         if (!inTimeInterval(Time[i], Ora_From_Hour, Min_From_Min, Ora_To_Hour, Min_To_Min))     continue;                
         Buffer1[i] = High[i] + 1 * myPoint; 
        }
        else
        {
         if (!inTimeInterval(Time[i], Ora_From_Hour2, Min_From_Min2, Ora_To_Hour2, Min_To_Min2))  continue;
         Buffer1[i] = High[i] + 1 * myPoint; 
        }
      
        
      //Indicator Buffer 2
      if(iRSI(NULL, PERIOD_CURRENT, Periodo, PRICE_CLOSE, 1+i) < RSI_Min //Relative Strength Index < fixed value
      )
        {
         if (!inTimeInterval(Time[i], Ora_From_Hour, Min_From_Min, Ora_To_Hour, Min_To_Min))     continue;                                
         Buffer2[i] = Low [i] - 1 * myPoint; 
        }
        else
        {
         if (!inTimeInterval(Time[i], Ora_From_Hour2, Min_From_Min2, Ora_To_Hour2, Min_To_Min2)) continue;
         Buffer2[i] = Low [i] - 1 * myPoint;  
        }  
      }
      
  return(0);
  } 
   
  
   
//+------------------------------------------------------------------+
peter.MT4Web
158
peter.MT4Web 2016.08.04 10:13  

You think it's not working properly?

Then test it and find out. Maybe you also find what's going wrong.

whroeder1
13596
whroeder1 2016.08.04 15:33  
  1.    &&  ((StringCompare(TOD, TOD_From)  >= 0 && StringCompare(TOD, "23:59")<= 0)                     //
         ||  (StringCompare(TOD, "00:00")    >= 0 && StringCompare(TOD, TOD_To) <= 0)))); 
    
    Will always be true
    StringCompare("2016.07.15 xx:xx", "00:00")    >= 0
    Will always be false.
    StringCompare("2016.07.15 xx:xx", "23:59")<= 0)
  2. Don't compare strings, convert your times to a datetime and compare directly
    // string TOD      = TimeToString(t, TIME_MINUTES);
    // string TOD_From = StringFormat("%02d", TOD_From_Hour)+":"+StringFormat("%02d", TOD_From_Min);
    // string TOD_To   = StringFormat("%02d", TOD_To_Hour)+":"+StringFormat("%02d", TOD_To_Min);
    // return((StringCompare(TOD, TOD_From)  >= 0 && StringCompare(TOD, TOD_To) <= 0)    
       datetime TOD_From = StringToTime( string(TOD_From_Hour)+":"+string(TOD_From_Min) );
       datetime TOD_To   = StringToTime( string(TOD_To_Hour)+":"+string(TOD_To_Min) );
       return t >= TOD_From && TOD < TOD_to;
  3. int Ora_From_Hour = 08; //time of the day                                                             //
    int Min_From_Min  = 00; //time of the day                                                             //
    int Ora_To_Hour   = 12; //time of the day                                                             //
    int Min_To_Min    = 30; //time of the day 
    You want to allow 08:00 to 12:30. You do not want to trade at 12:30;00.0-12:30:59.9. Thus you do not want the equals.
/
To add comments, please log in or register