Set Limit Hours For An Indicator

 

Hello,

I Want To Set an Time Limitation For My Indicator. (MT5)

How Can I Do That?

For Example, I Want to Start Calculating From 9:00 To 00:00.

Thanks.

 
p4rnak: How Can I Do That?

Code it.

 
William Roeder #:

Code it.

OK, but how?

I Use This One But It's Not Work On MT5 :


bool time;

int startTime = 6;
int endTime = 17;

void OnStart()
  {

   datetime tm=TimeCurrent(); //gets current time in datetime data type
   string str=TimeToString(tm,TIME_MINUTES); //changing the data type to a string
   string current = StringSubstr(str, 0, 2); //selecting the first two characters of the datetime e.g. if it's 5:00:00 then it'll save it as 5, if it's 18 it will save 18.
   int currentTimeInt = StringToInteger(current); //changes the string to an integer



   if(currentTimeInt>startTime && endTime>currentTimeInt) {
      time = true;

   }

   else {
      time = false;

   }
   }

And Then Use This On My Main Indicator :

for(i=limit;i<rates_total-3 && !IsStopped();i++)
     {
      //---- Upper Fractal
      if(High[i]>=High[i+1] && High[i]>High[i+2] && High[i]>=High[i-1] && High[i]>High[i-2] && time == true)
         ExtUpperBuffer[i]=High[i];
      else ExtUpperBuffer[i]=EMPTY_VALUE;

      //---- Lower Fractal
      if(Low[i]<=Low[i+1] && Low[i]<Low[i+2] && Low[i]<=Low[i-1] && Low[i]<Low[i-2] && time == true)
         ExtLowerBuffer[i]=Low[i];
      else ExtLowerBuffer[i]=EMPTY_VALUE;
     }
 
p4rnak #:

OK, but how?

I Use This One But It's Not Work On MT5 :

 

And Then Use This On My Main Indicator :

You are on the right track - there is a handy function TimeToStruct(). Have a look at the documentation - you can easily extract the hour using the  MqlDateTime.hour structure, which you can compare the current time hour.

Another simpler way is to calculate how many seconds have elapsed since midnight ( TimeCurrent()%(24*3600) ) - if it's greater than 9am (9 * 3600) then execute.

 
R4tna C #:

You are on the right track - there is a handy function TimeToStruct(). Have a look at the documentation - you can easily extract the hour using the  MqlDateTime.hour structure, which you can compare the current time hour.

The other way is to calculate how many seconds have elapsed since midnight if it's greater than 9am (9 * 3600) then execute.

Could you give me an example?

 
p4rnak #:

Could you give me an example?

   ulong secsSinceMidnight = TimeCurrent()%(24*3600);
   ulong secsStartTime         = 9 * 3600;
   
   PrintFormat("Secs = %d Time = %s", secsSinceMidnight, TimeToString(datetime(secsSinceMidnight), TIME_SECONDS ));
   
   if(secsSinceMidnight > secsStartTime)
     {
      Print("Execute");
     }
   else
     {
      Print("Pause");
     }  
 
R4tna C #:

so, What is the problem in my coding; becuase it didnt change time value to true or false.

 
p4rnak #:

so, What is the problem in my coding; becuase it didnt change time value to true or false.

I checked it - with endtime = 17 it was false (because the MetaTrader time is 17:48) but with endtime = 23 it was true

Although with your approach representing midnight needs some additional logic - I prefer calculating secs since midnight (as per the example I gave above)

 
R4tna C #:

I checked it - with endtime = 17 it was false (because the MetaTrader time is 17:48) but with endtime = 23 it was true

Although with your approach representing midnight needs some additional logic - I prefer calculating secs since midnight (as per the example I gave above)

Thanks Mate,
Could You Give Me An Example; That Untill 9am to 11pm (broker time) make "bool time;" true; and for another time it's set as false?


thanks

 
p4rnak #:

Thanks Mate,
Could You Give Me An Example; That Untill 9am to 11pm (broker time) make "bool time;" true; and for another time it's set as false?


thanks

Here, try this:

   ulong secsSinceMidnight = TimeCurrent() % (24 * 3600);
   ulong secsStartTime         = 9  * 3600;
   ulong secsEndTime           = 23 * 3600;

   bool time = false;

   PrintFormat("SecsSinceMidnight = %d TimeSinceMidnight = %s [start = %s end = %s]", 
               secsSinceMidnight, 
               TimeToString(datetime(secsSinceMidnight), TIME_SECONDS),
               TimeToString(datetime(secsStartTime), TIME_SECONDS),
               TimeToString(datetime(secsEndTime), TIME_SECONDS)
               );

   if((secsSinceMidnight > secsStartTime)
      && (secsSinceMidnight < secsEndTime))
     {
      time = true;
     }
   else
     {
      time = false;
     }
   
   PrintFormat("time = %s", string(time));
   
 
R4tna C #:

Here, try this:

wow, Perfect.

I Have To Use This Under OnCalculate; Right?

Reason: