[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 1086

 
drknn:


What the fuck is this then?

bool Fun_New_Bar() // Fun_New_Bar detection fi
{ // ... new bar


static datetime New_Time=0; // Time of current bar

This is called static variable initialisation. Zero will only be assigned when this variable is initialised.
 
khorosh:
This is called initializing a static variable. Zero will only be assigned when that variable is initialized.


Oh, man! Cool. A subroutine was called, a static variable was initialized. The subroutine ended, leaving its variables in the RAM address space. And most importantly, without freeing the space itself from busyness, right?

Your static initialization in your subroutine will happen on each new tick of the NEW!!!! This applies to the initialization of all variables for all subroutines. The scope and lifetime of your static variable is what, Huh?

 
drknn:

Oh, man! Cool. A subroutine was called, a static variable was initialized. The subroutine ended, leaving its variables in the RAM address space. And most importantly, without freeing the space itself from busyness, right?
It's easy to check if it's working properly. Why this theorizing? Practice is the criterion of truth. You should be happy if you suddenly identify a gap in your knowledge. I am always pleased in such cases and say thank you to the one who enlightened me.
 
drknn:


Oh, man! Cool. A subroutine was called, a static variable was initialized. The subroutine ended, leaving its variables in the RAM address space. And most importantly, without freeing the space itself from busyness, right?

Your static initialization in your subroutine will happen on every new tick of the NEW!!!! This applies to the initialization of all variables for all subroutines. The scope and lifetime of your static variable is what, Huh?


Yes, check on your M1 graph! After all, everything works as it should, it seems! And it's not beeping every tick, but only when a new tick is formed!

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);
}

 
khorosh:
It's easy to check if it works. Why all this theorizing? Practice is the criterion of truth. You should be pleased if you find out about your knowledge gap. I am always satisfied in such cases and say thank you to the one who enlightened me.


I agree. I got a little excited. My apologies. Checking the statics. Write a script.

//+------------------------------------------------------------------+
//|                 Старт работы скрипта                             |
//+------------------------------------------------------------------+
int start(){
  int SchVizovov=0;
	for(int i=1;i<5;i++){
		SchVizovov=Proverka(i);
	}
  Alert("-------------------");
	return(0);
}
//+------------------------------------------------------------------+
//|                  Пользовательские подпрограммы                   |
//+------------------------------------------------------------------+
int Proverka(int Sch){
	
	static int My_value=0; //
	if(My_value==0){
		My_value=Sch;
		Alert("Статическая переменная инициализирована. Proverka() вызвана ",Sch,"-й раз. Статическая переменная = ",My_value);
		return(Sch);
	}
	else{
	 My_value=Sch;
	 Alert("Статическая не реинициализирована. Proverka() вызвана ",Sch,"-й раз. Статическая переменная = ",My_value);
	}
	return(Sch);
}

Compile it, put it on the chart. The result is http://s2.ipicture.ru/uploads/20101229/k2u2OUqX.jpg.

Well. Alas, I have to admit that the statik is not reinitialized. Then the code will work. This is not good. :( Language help should explain such nuances. The help tells you only that a static variable does not lose its value on function exit. But it doesn't say that this variable won't reinitialize when being re-initialized. Geez, I've hit upon the fact that help in the meta-editor is incomplete again. Geez, I thought I'll never get caught again :)))))))

 
drknn:


I agree. I got a little excited. My apologies. Checking the statics. Write a script.

Compile it, and then throw it on the chart. The result is http://s2.ipicture.ru/uploads/20101229/k2u2OUqX.jpg.

Well. Alas, I have to admit that the statik is not reinitialized. Then the code will work. This is not good. :( Language help should explain such nuances. Help tells you only that a static variable does not lose its value on function exit. But it doesn't say that this variable won't reinitialize when being re-initialized. Oh crap, I'm caught again. Geez, I thought I'd never get caught again :)))))))

Don't get upset though, we're all learning here. It so happens that some nuance escapes your attention, and you find out about it when you thought you knew everything.

The reference says that initialization of a static variable is one-time, which means that there is no reinitialization as such.

 
khorosh:
The main thing is not to get upset, we're all learning here. It so happens that some nuance escapes your attention, and you find out about it when you thought you already knew everything.


I'm not upset that I found out a gap in my knowledge - I get upset when I find faults in my code, when I spent more than an hour to find them, when it turns out that it was not my fault, but a bug in the help system or in the terminal. If add up the time I spent during the last 6 years of programming to find errors related to such issues, I can say that quite a large chunk of my life is simply trashed. That's what's frustrating - time of life is wasted on bullshit. It flies away and never comes back. Well, today's situation was not upsetting - on the contrary, it was pleasing - I did not have to spend nights searching for an error - it was quickly identified.

Nikolai, once again my apologies - I've thought for 6 years that a variable declared initialized with a value inside function, re-initializes when this function is called again. (I'm silent about variables of global shortcut of the terminal - that's another song). Turns out there are exceptions to the rule. I've got a simply royal lesson today!

 
khorosh:

The help says that the initialisation of a static variable is one-time, which means that there is no reinitialisation as such.

To quote the help:

Static variables are initialised once before calling the specialised init() function,

And where did we initialize a static variable? In a subprogram. We broke the initialization rule - we broke it.

Before calling the init() function, the static variable was neither declared nor called from the subprogram. This is what causes confusion. Since there was no initialization of the static variable before the init() block, the singularity rule will be invalid because it is bound to a particular fragment of the program structure. This is what the help says about it. But it does not fulfill it - reinitialization still does not occur despite the fact that the one-time rule is invalid!

The point is that the one-time rule imposes a rigidity. This is where the relation of equivalence between the subject and the predicate of the judgement comes into play. So it is equivalence - one does not exist without the other.

But then again, the language also allows for another violation of the singularity rule. If we declare this variable as it should be and if it can be initialized with a value only before the initialization block, we cannot initialize this variable anywhere else in the code, because it would violate the singularity rule. But the script I made shows that it's not so - the variable can be initialized with another value on the fly.

I have never used these variables in my code and won't do so since such rules are violated here. I don't know what other pitfalls there are with them.

 

I HAVE A PROBLEM ....

When I set up mail in the terminal settings, In SMTP - I use smtp.gmail.com:25

I get the error - Mail: 530 5.7.0 Must issue a STARTTLS command first. l3sm2329679fan.0
 
drknn:

To quote from the help:

Where did we initialise the static variable? In a subroutine. You broke the initialization rule - you broke it.

Before calling the init() function, the static variable was neither declared nor called from the subprogram. This is what causes confusion. Since a static variable was not initialized before the initialization block, the singularity rule is invalid because it is bound to a particular fragment of program structure. This is exactly what the help says about it. But the devil doesn't fulfill it - reinitialization still doesn't occur despite the fact that the singularity rule is invalid!

Also, there is an example of using a static variable in the tutorial from which it is clear that it is initialized only once, otherwise the tick counter would not work.

//--------------------------------------------------------------------
// staticvar.mq4
// Предназначен для использования в качестве примера в учебнике MQL4.
//--------------------------------------------------------------------
int start()                            // Специальная функция start()
  {
   static int Tick;                    // Статическая локальная перем
   Tick++;                             // Счётчик тиков
   Comment("Поступил тик № ",Tick);    // Сообщение, содержащее номер
   return;                             // Оператор выхода из start()
  }
//--------------------------------------------------------------------
Reason: