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

 
khorosh:

It would be more accurate to put it this way:



That's even truer and now it works:

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

 
And by the way, why do I highlight code as code when writing a comment and it's the same colour? Or should I highlight it as what, so it looks like real code in colour?
 
kolyango:

I tried it on M1 and the message does not appear after the next bar is formed. Does it mean that it does not work?


And it won't work - you haven't written the program correctly. That's because you have no idea how your own code will work. Let's break it down. Expert Advisor body:

int start(){ // Спец. функция start
  if(Fun_New_Bar()){ //проверка наличия нового бара
    Alert("Сформировался новый бар"); // Вывод на экран
  }
  return(0); // Выход из start()
}

A new tick arrives and the Fun_New_Bar() function is called. If it returns true to the main program, an Alert will pop up. Otherwise, the EA terminates its work with a 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.

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; // Поймался новый бар
  }
}

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?

 

How do I make a global variable available to an EA running on a second MT4 terminal that is running?

 
DhP:

How do I make a global variable available to an EA running on a second MT4 terminal that is running?


Through an api, or through a text document.
 
drknn:

Through an api, or through a text document.

How?
 
DhP:

How?

Through the api I don't know how - I've read that it's possible. Through text document - you need to write a dll on a language other than MQL4. For example in Delphi or C++. The dll has to be able to access the file located outside the terminal. It is quite achievable. Further on, everything is clear - the Expert Advisor enters a new value of the global variable into a text document, and the other Expert Advisor reads it from there. The DLL gives access to the text document to both Expert Advisors. Such questions have been asked here before. People have asked something like how to make EAs work together in two different channels...
 
kolyango:

That's even truer and it works now:

No it doesn't - your Alert will beep you on every tick, not once on the first tick of a new candle.
 
kolyango:



This is even more correct and now it works:

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


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.

This is the right way to do it:

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

Through an api I don't know how - I read that it's possible. Through text document - you need to write a dll in a language other than MQL4. For example in Delphi or C++. The dll has to be able to access the file located outside the terminal. It is quite achievable. Further on, everything is clear - the Expert Advisor enters a new value of the global variable into a text document, and the other Expert Advisor reads it from there. The DLL gives access to the text document to both Expert Advisors. Such questions have been asked here before. People were asking something like how to make EAs work together in two different channels...

Thanks, I'll look for it...
Reason: