[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

 
khorosh:

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?
 
drknn:


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("Сформировался новый бар"); // Вывод на экран
}
}
//--------------------------------------------------------------------

 
kolyango:


Try this code. It seems to work, just see if it's right or wrong:

No, it's not - I've got it all spelled out for you. Don't you understand anything?
 

kolyango: I'll show you the right code. Look.

//+------------------------------------------------------------------+
//|                                                            0.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

int MyChandle;// количество свеч на графике
//+------------------------------------------------------------------+
//|                  Блок инициализации                                |
//+------------------------------------------------------------------+
int init(){
  MyChandle=0;// ну или можно вот так : MyChandle=Bars;

  return(0);
}
//+------------------------------------------------------------------+
//|                  Блок деинициализации                              |
//+------------------------------------------------------------------+
int deinit(){
  if (!IsTesting()){
    Comment("");
    Print("Советник деинициализирован (выключен совсем)");
  }
  return(0);
}
//+------------------------------------------------------------------+
//|                  Старт работы советника                            |
//+------------------------------------------------------------------+
int start(){
  if(MyChandle<Bars){
   Alert("Было свеч = ", MyChandle," Стало свеч = ",Bars);

   MyChandle=Bars;
  }
  return(0);
}
//+------------------------------------------------------------------+
//|                  Пользовательские подпрограммы                       |
//+------------------------------------------------------------------+
 
drknn:
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);
}

 
kolyango:


But look, it seems to be working correctly! Checked it on the M1 timeframe:



Look, can you read, or not - told you above - not the right code - it will signal on every tick, not once on a new candle. See above - I gave you the right code. What else don't you need to be happy? To know why your code is wrong? I've already explained, it's wrong because all the time the subroutine returns true and only true. The alert will pop up on every tick! Programming is not a guessing game - you have to think.
 
drknn:

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?
It works correctly - I checked it on the chart. When
New_Time=Time[0]

функция Fun_New_Bar() не будет возвращать истину
 
drknn:

kolyango: I'll show you the right code. Look.

And the variant using Bars is less preferable, because the value of Bars can change not only due to the appearance of a new bar.
 

drknn:
... Программирование - это не игра в угадайки - тут думать надо...

... Well, at least sometimes ... :-)))

 
khorosh:
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.