My timer probably isn't very nice but I use a boolean.
So if the current time is between the two times I want it to run (e.g. 6am and 5pm), then set the boolean to true.
bool time;
int startTime = 6; int endTime = 17;
void OnTick() {
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;
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
As already said by forex2start, you CAN NOT return a value in OnTick() event handler which is defined with void. Use this :
Anyway, I doubt this condition with strings will give you the expected result.
My timer probably isn't very nice but I use a boolean.
So if the current time is between the two times I want it to run (e.g. 6am and 5pm), then set the boolean to true.
bool time;
int startTime = 6;
int endTime = 17;
void OnTick()
{
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;
}
if(time == true) {
//do calculations here
}
}
My timer probably isn't very nice but I use a boolean.
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code properly in a new post.