The datetime declaration needs to be STATIC or make sure it's on the on the global scope. You don't need D'1970.01.01 00:00:00'. zero is fine and the same thing as that date. infact, you don't even need =0 as this is the default value for any declared variable. Also, assuming time_period=0 (which it needs to be), Time[0] is the same as iTime(NULL,time_period,0) and faster to type!
The datetime declaration needs to be STATIC or make sure it's on the on the global scope. You don't need D'1970.01.01 00:00:00'. zero is fine and the same thing as that date. infact, you don't even need =0 as this is the default value for any declared variable. Also, assuming time_period=0 (which it needs to be), Time[0] is the same as iTime(NULL,time_period,0) and faster to type!
Hope that's of use
V
Oh... and
Hi
Why STATIC - it is working as I have it there - What problems can jump up that I am not aware of? I use iTime because I force differnet time frames and not just the current time frame of the current chart.
Why STATIC - it is working as I have it there - What problems can jump up that I am not aware of? I use iTime because I force differnet time frames and not just the current time frame of the current chart.
Thanks for the reply it always helps!!
If a variable is declared in start(), it will be reinitialised every tick so if
My question might sound quite ridiculous, but I don't really know -even after analyzing mql4 documentation (https://docs.mql4.com/predefined/variables/volume) -what is that volume variable for. I know only, that if local history is empty, function returns 0. After writing script which writes down 50 last values of volume i got inputs which varies between 30 and 50. Does it denote any change of price in pips during single tick or it's any measure of volume of transactions made on forex market during every single tick - which btw would be sort of imponderabilia ? If it's so, then how is it evaluated- in brokers dealing rooms?
My question might sound quite ridiculous, but I don't really know -even after analyzing mql4 documentation (https://docs.mql4.com/predefined/variables/volume) -what is that volume variable for.
It's the number of ticks that occurred during the bar in question . . . a tick can be a change in Bid, a change in Ask or, I believe, a change in other MarketInfo data.
Volume is unreliable because you can miss ticks. Therefor this code will return false if you miss a tick at the start of a new bar and you ignore entire bars.
This will also not work correctly if you change pair/time frame, etc. Deinit/init cycles do NOT reload static or common(global) variables.
Instead of static variables
I use this pattern
type Function(){
static type v = const;
:
}
int init(){
OnInitFunction(); OnInitFunction2()...
}
:
type functionV; OnInitFunction(){ functionV = const; }
type Function(){
:
}
I don't recommend using a function to detect a new bar because it can be called only once per start(). It fails if you call it a second time. (This is my only exception to factoring code to functions.)
New bar or first bar
datetime Time0;
int init(){ Time0=0;}
int start(){
// Every bar codebool newBar = Time0 != Time[0]; Time0 = Time[0];
if (!newBar) return;
// New bar code
New bar only
datetime Time0;
int init(){ Time0=Time[0];}
int start(){
// Every bar codeif (Time0 == Time[0]) return; Time0 = Time[0];
// New bar code
New bar or near start of the first bar
datetime Time0;
int init(){ Time0=Time[0];
if (TimeCurrent() - Time0 < Period()) // 1.7%
Time0 = 0;
}
int start(){
// Every bar codeif (Time0 == Time[0]) return; Time0 = Time[0];
// New bar code
It is more concise and dispenses with the unnecessary variable New_Bar
In my opinion it's bad practice to use a function for this . . . you can only use it once per tick within start() if you call it a second time on the same tick, the same tick that is the start of a new bar, it will return false . . .
Just some observations....
The datetime declaration needs to be STATIC or make sure it's on the on the global scope. You don't need D'1970.01.01 00:00:00'. zero is fine and the same thing as that date. infact, you don't even need =0 as this is the default value for any declared variable. Also, assuming time_period=0 (which it needs to be), Time[0] is the same as iTime(NULL,time_period,0) and faster to type!
Hope that's of use
V
Oh... and
Thanks for SRC I was not aware will try next time
Just some observations....
The datetime declaration needs to be STATIC or make sure it's on the on the global scope. You don't need D'1970.01.01 00:00:00'. zero is fine and the same thing as that date. infact, you don't even need =0 as this is the default value for any declared variable. Also, assuming time_period=0 (which it needs to be), Time[0] is the same as iTime(NULL,time_period,0) and faster to type!
Hope that's of use
V
Oh... and
Hi
Why STATIC - it is working as I have it there - What problems can jump up that I am not aware of? I use iTime because I force differnet time frames and not just the current time frame of the current chart.
Thanks for the reply it always helps!!
Hi
Why STATIC - it is working as I have it there - What problems can jump up that I am not aware of? I use iTime because I force differnet time frames and not just the current time frame of the current chart.
Thanks for the reply it always helps!!
is inside start(), that means every tick current_bar_opentime will be set to ...well 0 and will therfore always be != iTime(NULL,time_period,0).
it's explained fully here ..https://book.mql4.com/variables/types
V
This happens because when EA is started New_Time is 0 and Function_New_Bar() returns true. You can avoid this with the following code:
My question might sound quite ridiculous, but I don't really know -even after analyzing mql4 documentation (https://docs.mql4.com/predefined/variables/volume) -what is that volume variable for. I know only, that if local history is empty, function returns 0. After writing script which writes down 50 last values of volume i got inputs which varies between 30 and 50. Does it denote any change of price in pips during single tick or it's any measure of volume of transactions made on forex market during every single tick - which btw would be sort of imponderabilia ? If it's so, then how is it evaluated- in brokers dealing rooms?
My question might sound quite ridiculous, but I don't really know -even after analyzing mql4 documentation (https://docs.mql4.com/predefined/variables/volume) -what is that volume variable for.
This happens because when EA is started New_Time is 0 and Function_New_Bar() returns true. You can avoid this with the following code:
type Function(){ static type v = const; : }Hi,
First off all thank you all for your posts and help in coding.
As in this topic in my opinion best works this:
And if you need different time frame use something like this:
instead Time[0].
This one is my first and shortest option.
Hi,
First off all thank you all for your posts and help in coding.
As in this topic in my opinion best works this:
And if you need different time frame use something like this:
instead Time[0].
This one is my first and shortest option.
Doesn't this do the same thing?
It is more concise and dispenses with the unnecessary variable New_Bar
Doesn't this do the same thing?
It is more concise and dispenses with the unnecessary variable New_Bar