[WARNING CLOSED!] Any newbie question, so as not to clutter up the forum. Professionals, don't go by. Can't go anywhere without you. - page 1085
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
The function is not mine - I picked it up online but haven't used it. Looked at it carefully now - there's an operator missing.
The correct way is this:
Wrong again! The EA will be beeping again at every tick, because Fun_New_Bar() will always return true. The question is why do we need Fun_New_Bar() if it always returns true?
And it won't work - you must have written the program incorrectly. And that's because you have no idea how your own code will work. Let's break it down. The body of the Expert Advisor:
A new tick arrives and the Fun_New_Bar() function is called. If it returns true to the main program, Alert will pop up. Otherwise, the EA terminates its work by return and waits for a new tick. Everything repeats on a new tick.
You say that the code does not work? Now look at what you have in your subprogram.
A boolean function is declared. Since it is a function and not a procedure, it should return something to the main program from which it is called. Since the function is boolean, it should return a boolean value. Where do you have a string that returns anything to the main program? It does not! This means that at the start of the program, since nothing is returned from the function, the parentheses of the if(Fun_New_Bar()) expression will always be false and the alert will never pop up.
Let's go further. Why did you declare datetime New_Time=0; as static? What is your reasoning? You declared a variable and immediately initialized it with zero. On the next tick, the same thing will happen - the variable will be declared and initialized by zero again. If(New_Time!=Time[0]){ This line checks if the variable's value is not equal to the current time. Well, yes, the variable has zero, but the current time is not zero. The condition is fulfilled, the current time value is written into the variable, parameter New_Bar becomes true. On the next tick , New_Time will not equal Time[0] again , it will successfully check for inequality again and the next two operations will be executed. In other words, when checking the condition if(New_Time!=Time[0]), the expression in brackets will be true at every tick. ALWAYS. Which begs the question, if it is always true, why the hell should this check be here? Maybe we should just remove it? Why should we assign New_Time=Time[0] and New_Bar=true, if these two variables are not used anywhere? Why the hell do we need these two assignments? Do you want to play around? Or are you too lazy to think?
Try this code. It seems to work, but see if it's right or wrong. If something is wrong, correct it:
//--------------------------------------------------------------------
int start() // Спец. функция start
{
if(Fun_New_Bar())//проверка наличия нового бара
return; // Выход из start()
}
//--------------------------------------------------------------------
bool Fun_New_Bar() // Ф-ия обнаружения ..
{ // .. нового бара
static datetime New_Time=0; // Время текущего бара
bool New_Bar=false; // Нового бара нет
if(New_Time!=Time[0]) // Сравниваем время
{
New_Time=Time[0]; // Теперь время такое
New_Bar=true; // Поймался новый бар
Alert("Сформировался новый бар"); // Вывод на экран
}
}
//--------------------------------------------------------------------
Try this code. It seems to work, just see if it's right or wrong:
kolyango: I'll show you the right code. Look.
No, that's not right - I've got the code all spelled out for you. Don't you understand anything?
But look, it seems to be working correctly! I checked it on M1 timeframe:
int start() // Спец. функция start
{
if(Fun_New_Bar())//проверка наличия нового бара
{
Alert("Сформировался новый бар"); // Вывод на экран
}
return(0); // Выход из start()
}
//--------------------------------------------------------------------
bool Fun_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);
}
But look, it seems to be working correctly! Checked it on the M1 timeframe:
Wrong again! The EA will be beeping again at every tick, since Fun_New_Bar() will always return true. The question is why the hell do we need Fun_New_Bar() if it always returns true?
New_Time=Time[0] функция Fun_New_Bar() не будет возвращать истину
kolyango: I'll show you the right code. Look.
drknn:
... Программирование - это не игра в угадайки - тут думать надо...
... Well, at least sometimes ... :-)))
And the variant using Bars is less preferable, because the value of Bars can change not only due to appearance of a new bar.
So, enlighten me - can the number of candles on the chart change in connection with anything else but the appearance of a new bar? :)))))
Oh, yeah, that's right - history loading. :) Anyway, its function will be beeping at every tick - it doesn't remember time correctly.