Where did you test this...if you tested on an open of a bar it is pretty clear that you get 0 because TimeSeconds gives you the Seconds passed since the last minute (bar opening). At the open of a bar not a second has elapsed...
Where did you test this...if you tested on an open of a bar it is pretty clear that you get 0 because TimeSeconds gives you the Seconds passed since the last minute (bar opening). At the open of a bar not a second has elapsed...
I am also using Print to write the values to console and it is obvious that all numbers are non-zero before the division and the result is a zero after the operation.
You can cut and paste the code and run it yourself.
Thanks for your response.
ForexWatchman
I am trying to divide number of seconds by a constant integer and the division always results in zero. What am I doing wrong?
Here is the code.
string str1 = "2010.3.15 16:00";
string str2 = "2010.3.17 07:00";
int specifiedPeriod = 5;
datetime TN = TimeCurrent();
datetime dt1 = StrToTime(str1);
datetime dt2 = StrToTime(str2);
// everything is good upto this point
int barTN = TimeSeconds(TN)/(60*specifiedPeriod);
int barDT1 = TimeSeconds(dt1)/(60*specifiedPeriod);
int barDT2 = TimeSeconds(dt2)/(60*specifiedPeriod);
// I am getting barTN, dt1 and dt2 all zero
You are trying to divide an integer that is >= 0 and <60 by an integer that is > 60... Of course the result will be zero. TimeSeconds() returns the amount of seconds elapsed from the beginning of the minute for the specified time. See here -> https://docs.mql4.com/dateandtime/TimeSeconds.
I know you are trying to help but you are not looking at my code. TimeSeconds returns number of seconds from 1970 for the datetime parameter that is passed. Therefore the number returned is not between 0 and 60. It is much bigger depending upon the datetime parameter passed.
I have the code working using a different alogorithm. I am just curious to know how does the integer arithmatic work in MQL. If you have an example of integer division that works, please share that with me.
Thanks
ForexWatchman
Not true.
I know you are trying to help but you are not looking at my code. TimeSeconds returns number of seconds from 1970 for the datetime parameter that is passed. Therefore the number returned is not between 0 and 60. It is much bigger depending upon the datetime parameter passed.
No. From https://docs.mql4.com/dateandtime/TimeSeconds:
int TimeSeconds( | datetime time) |
But if u don't believe the documentation (or me). Here's some proof for u - a simple script:
int start() { while(true) { Print( TimeSeconds(TimeLocal()) ); Sleep(1000); } }And a sample output:
10:30:52 test EURCHF,Daily: 53
10:30:53 test EURCHF,Daily: 54
10:30:54 test EURCHF,Daily: 55
10:30:55 test EURCHF,Daily: 56
10:30:56 test EURCHF,Daily: 57
10:30:57 test EURCHF,Daily: 58
10:30:58 test EURCHF,Daily: 59
10:31:00 test EURCHF,Daily: 0
10:31:00 test EURCHF,Daily: 1
10:31:01 test EURCHF,Daily: 2
10:31:02 test EURCHF,Daily: 3
10:31:03 test EURCHF,Daily: 4
10:31:04 test EURCHF,Daily: 5
10:31:05 test EURCHF,Daily: 6
I am just curious
to know how does the integer arithmatic work in MQL. If you have an
example of integer division that works, please share that with me.
It works normally (like in most other programming languages). You are just miss-using TimeSeconds().
int barTN = /*TimeSeconds*/(TN)/(60*specifiedPeriod);Alternately you could use
barTN = iBarShift(Symbol(), 0, TN);
Thanks WHRoeder. I was missing iBarShift.
I actually wrote a loop to get over that problem. Here is the script I used:
int specifiedPeriod = 15; // can be any period for which you want to find the bar
datetime TN = TimeCurrent();
datetime dt1 = StrToTime(str1);
int newNum = 0;
int counter = 0;
while (newNum < TN)
{
counter++;
newNum = (60*specifiedPeriod)*counter;
}
newNum = 0;
counter = 0;
while (newNum < dt1)
{
counter++;
newNum = (60*specifiedPeriod)*counter;
}
int barTN = counter-1;
int barDT1 = counter-1;
iBarShift is the perfect and elegant solution. Really appreciate your help.
Thanks to you too Gordon. I know you tried to help as well.
-ForexWatchman
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I am trying to divide number of seconds by a constant integer and the division always results in zero. What am I doing wrong?
Here is the code.
string str1 = "2010.3.15 16:00";
string str2 = "2010.3.17 07:00";
int specifiedPeriod = 5;
datetime TN = TimeCurrent();
datetime dt1 = StrToTime(str1);
datetime dt2 = StrToTime(str2);
// everything is good upto this point
int barTN = TimeSeconds(TN)/(60*specifiedPeriod);
int barDT1 = TimeSeconds(dt1)/(60*specifiedPeriod);
int barDT2 = TimeSeconds(dt2)/(60*specifiedPeriod);
// I am getting barTN, dt1 and dt2 all zero
Please help. Thanks in advance
ForexWatchman