Your Function_New_Bar() is a bit off...
You need to return(Check_New_Bar) at the end of the function or nothing is ever passed into your IF condition. Also, your function is expecting to return an int and your IF condition is looking for a bool.
Try this...
bool Function_New_Bar() { static datetime New_Time = 0; bool New_Bar = false; if (New_Time!= Time[0]) { New_Time = Time[0]; New_Bar = true; } return(New_Bar); }V
Your Function_New_Bar() is a bit off...
You need to return(Check_New_Bar) at the end of the function or nothing is ever passed into your IF condition. Also, your function is expecting to return an int and your IF condition is looking for a bool.
Try this...
VViffer,
Its still the same, when i attach my EA in 15 minutes time frame candle bar, it will alert "New bar just form". For your info, when i attach the EA to the 15 minutes chart, the current candle bar still running about 3 minutes, and it should alert..... "waiting for new bar". and it must wait for next 15 minutes on 1st tick, then alert "New bar just form."
Yaiks..
Viffer,
Its still the same, when i attach my EA in 15 minutes time frame candle bar, it will alert "New bar just form". For your info, when i attach the EA to the 15 minutes chart, the current candle bar still running about 3 minutes, and it should alert..... "waiting for new bar". and it must wait for next 15 minutes on 1st tick, then alert "New bar just form."
Yaiks..
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:
bool Function_New_Bar() { if(Volume[0]>1) return(false); static datetime New_Time = 0; bool New_Bar = false; if (New_Time!= Time[0]) { New_Time = Time[0]; New_Bar = true; } return(New_Bar); }
Attempt 2...
bool Function_New_Bar() { static datetime New_Time = Time[0]; bool New_Bar = false; if (New_Time!= Time[0]) { New_Time = Time[0]; New_Bar = true; } return(New_Bar); }
Haven't tested this so let me know if it works or not
V
Attempt 2...
Haven't tested this so let me know if it works or not
V
There is bug on the the code above.....hmmm..
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:
Thanks robofx.org. it works.
//NEW BAR CHECK -----------------------------------------------------+ Fun_New_Bar(); if (New_Bar == false) return; //+--------------------------------------------------------------------+ //+--------------------------------------------------------------------+ void Fun_New_Bar() { static datetime New_Time = 0; New_Bar = false; if (New_Time!= Time[0]) { New_Time = Time[0]; New_Bar = true; } } //--------------------------------------------------------------------+
There is bug on the the code above.....hmmm..
Well, looks like you can't declare a STATIC with an expression. Never knew that. Robofx, we have the same idea that the problem is because New_Time starts as 0 but I'm not so sure about the reliability of Volume[0] for the same reason that we don't use volume in the first place... concievably, we miss the first tick of a subsequent new bar, we action on the next tick but because volume[0] >1 we never get to execute the Time[0] section. Perhaps better a special test for the first run only...
bool Function_New_Bar() { static datetime New_Time; bool New_Bar = false; if (New_Time==0) { New_Time = Time[0]; } if (New_Time!= Time[0]) { New_Time = Time[0]; New_Bar = true; } return(New_Bar); }V
Hi
I use the following simple code to find if I am in a new bar or not
datetime current_bar_opentime=D'1970.01.01 00:00:00';
bool newbar=false;
//+------------------------------------------------------------------+
//| Is this a new bar? |
//+------------------------------------------------------------------+
if(iTime(NULL,time_period,0)!=current_bar_opentime)
{
current_bar_opentime=iTime(NULL,time_period,0);
newbar=true;
}//end if
Hope This helps - i am doing this now for only 6 months and this is working for me.
johan
Hi
I use the following simple code to find if I am in a new bar or not
datetime current_bar_opentime=D'1970.01.01 00:00:00';
bool newbar=false;
//+------------------------------------------------------------------+
//| Is this a new bar? |
//+------------------------------------------------------------------+
if(iTime(NULL,time_period,0)!=current_bar_opentime)
{
current_bar_opentime=iTime(NULL,time_period,0);
newbar=true;
}//end if
Hope This helps - i am doing this now for only 6 months and this is working for me.
johan
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
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I have create a code which detect new bar been form. but seems there is a bug.
What i wanted is, when we attach the EA to any chart, if at the time when attach to the chart.... the current bar still not yet finish perform.... it will wait for new bar to begin then alert new bar been form. If the current bar not yet perform, it should alert.... waiting new bar to perform. please view my code and give me a hand. Seems like forever it stated waiting for bar to perform. Yaik.... whats going wrong? Thanks.
//+------------------------------------------------------------------+
//| Puzai_candle_scalping.mq4 |
//| Joel Loh |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Joel Loh"
#property link "http://www.metaquotes.net"
extern int Quant_Bars=1; // Amount of bars
bool Check_New_Bar=false; // Flag of a new bar
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
Alert("EA been attached");
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
Check_New_Bar = Function_New_Bar(); // Function call
if (Check_New_Bar==false){
Alert("Waiting for new bar.");
}else{
Alert("New bar just form.");
}
//----
return(0);
}
//+------------------------------------------------------------------+
int Function_New_Bar() // Funct. detecting ..
{ // .. a new bar
static datetime New_Time=0; // Time of the current bar
Check_New_Bar=false; // No new bar
if(New_Time!=Time[0]) // Compare time
{
New_Time=Time[0]; // Now time is so
Check_New_Bar=true; // A new bar detected
}
}