MQL5 IsNewBar function ignore 1970.01.01
MQL5 IsNewBar function ignore 1970.01.01
Loading saved threads...
Dragon · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Dragon
Original post: https://stackoverflow.com/questions/77052734
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
i am working with mql5 and want to have a little function to detect a new bar.
Seems not to be a hard point, smile ...
But i am really confused about my function.
My goal is:
I want to have a bool function that give back true if a new bar is opened.
During initialisation / starting an EA first Tick will be nothing, so 1970.01.01 with value 0 should happen and be ignored. I really want to wait until the first complete new bar is opened.
//+------------------------------------------------------------------+
//| IsNewBar
//+------------------------------------------------------------------+
bool IsNewBar(ENUM_TIMEFRAMES m_timeframe){
static datetime lastbar;
datetime currentbar = (datetime)SeriesInfoInteger(_Symbol,m_timeframe,SERIES_LASTBAR_DATE);
if(lastbar != currentbar){
lastbar = currentbar;
// if lastbar is 1970.01.01 (=value 0) ... This will happen during start an EA in the middle of a bar.
if(lastbar != 0){
Print("true");
return true;
}else{
Print("false");
return false;
}
}
return false;
}
This is my testing code to activate the function:
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick(){
if(IsNewBar(PERIOD_M1)==true){
Print("x");
}
}
It seems like completely ignored the first Tick 1970.01.01 thing.
I did not figure out where my problem is.
Please help! Thank you
Quote
Report
Mark SdS · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Mark SdS
Original post: https://stackoverflow.com/a/77070087
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I do:
//Global:
datetime newBarTime;
//onInit:
newBarTime = iTime(0,0,0) + Periodseconds();
//on the run
if(TimeCurrent() >= newBarTime){
newBarTime = iTime(0,0,0) + Periodseconds();
return true;
}
Quote
Report
Dragon · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Dragon
Original post: https://stackoverflow.com/a/77072116
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I found the problem of my original code.
If anyone is interesting in, here is the working function:
bool IsNewBar(ENUM_TIMEFRAMES m_timeframe){
static datetime lastbar;
datetime currentbar = (datetime)SeriesInfoInteger(_Symbol,m_timeframe,SERIES_LASTBAR_DATE);
if(lastbar != currentbar){
if(lastbar == 0){
lastbar = currentbar;
return false;
}else{
lastbar = currentbar;
return true;
}
}
return false;
}
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: Dragon Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/a/77072116 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I found the problem of my original code. If anyone is interesting in, here is the working function: bool IsNewBar(ENUM_TIMEFRAMES m_timeframe){ static datetime lastbar; datetime currentbar = (datetime)SeriesInfoInteger(_Symbol,m_timeframe,SERIES_LASTBAR_DATE); if(lastbar != currentbar){ if(lastbar == 0){ lastbar = currentbar; return false; }else{ lastbar = currentbar; return true; } } return false; }
Checking account access…