Features of the mql5 language, subtleties and tricks - page 101

 
fxsaber:

Result

Yes, I didn't take into account the internal work of Sleep, which takes about 4 ms on my computer.

A good alternative to Sleep (who cares about accuracy :)) is

void sleep(int m)
  {
   if(m>0) Sleep(int(0.995*m+0.5)-1);
  }

I can not guarantee the accuracy of the coefficient 0.995. I picked it up for my computer. It may be good for all.

Files:
TestSleep.mq5  1 kb
 
fxsaber:

To be honest, I don't even know what it means and where in MQL5 one can encounter it.

This means that spikes are possible in any situation, even in synchronous functions. The only thing to do is to be aware of them and not to pay attention to them, because it cannot be solved.

https://en.wikipedia.org/wiki/Interrupt
Interrupt - Wikipedia
Interrupt - Wikipedia
  • en.wikipedia.org
This article is about computer interrupts. For the study of the effect of disruptions on job performance, see Interruption science. For other uses, see Interruption. In system programming, an interrupt is a signal to the processor emitted by hardware or software indicating an event that needs immediate attention. An interrupt alerts the...
 

In addition to files and global variables, there is another way to transfer information between programs

Forum on trading, automated trading systems and trading strategies testing

Libraries: TradeTransactions

fxsaber, 2018.09.20 16:23

// Пример хранения/обмена данными через Ресурсы внутри Терминала
#include <fxsaber\TradeTransactions\ResourceData.mqh> // https://www.mql5.com/ru/code/22166

void OnStart()
{  
  const RESOURCEDATA<int> ResourceINT("::int"); // Ресурс для обмена int-ами. const - как доказательство, что ничего не пишется в объект класса
  
  int ArrayINT[] = {1, 2, 3};
  int Num = 5;
  
  ResourceINT = ArrayINT;  // Ресурс хранит массив.
  ResourceINT += Num;      // Добавили в ресурс еще значение.
  ResourceINT += ArrayINT; // Добавили массив.
  
  int ArrayINT2[];  
  ResourceINT.Get(ArrayINT2); // Считали данные из ресурса.
  ArrayPrint(ArrayINT2);      // Вывели: 1 2 3 5 1 2 3

  ResourceINT.Free();                // Удалили данные из ресурса
  Print(ResourceINT.Get(ArrayINT2)); // Убедились, что данных нет: 0

  const RESOURCEDATA<MqlTick> ResourceTicks("::Ticks"); // Ресурс для обмена тиками. const - как доказательство, что ничего не пишется в объект класса
  MqlTick Tick;
  
  if (SymbolInfoTick(_Symbol, Tick))
    for (int i = 0; i < 3; i++)
      ResourceTicks += Tick; // Добавили в ресурс тики

  MqlTick Ticks[];
  ResourceTicks.Get(Ticks); // Считали данные из ресурса.
  ArrayPrint(Ticks);        // Вывели.
  
  // Это полное имя ресурса для обращения из другой программы
  const string NameOut = StringSubstr(MQLInfoString(MQL_PROGRAM_PATH), StringLen(TerminalInfoString(TERMINAL_PATH)) + 5) + "::Ticks";  
  Print(NameOut); // Вывели полное имя ресурса.
  
  const RESOURCEDATA<MqlTick> Resource(NameOut); // Ресурс для доступа к данным (read-only) из другой программы
  
  MqlTick TicksOut[];
  Resource.Get(TicksOut); // Считали данные из ресурса.
  ArrayPrint(TicksOut);   // Вывели.
  
  Resource.Free();   // Не получится повлиять на данные read-only-ресурса.
  Print(_LastError); // ERR_INVALID_PARAMETER - Ошибочный параметр при вызове системной функции.
}
 
fxsaber:

In addition to files and global variables, there is another way to transfer information between programs

We're talking about programs within a single running terminal, right?

 
Nikolai Semko:

We are talking about programs within a single running terminal, right?

Yes. That's why global variables are mentioned.

And between Terminals I've started to use this way.
 
fxsaber:

Yes. That's why global variables are mentioned.

And between Terminals started using this way.

Yes, it's really cool!
Very cool find using RAM disks instead of SSDs.

 
fxsaber:

Yes. That's why global variables are mentioned.

And between Terminals I used this way.

I have been using such an easy semi-hacking method using user32.dll for a long time. But it is impossible to pass the tick arrays in that way.

I invented it a long time ago, when I was still mastering MQL4. Of course, it is not the shortest solution in terms of reasonable organization of the exchange, but it works fast and well (maybe faster than all existing solutions), that's why I haven't racked my brains anymore.

Besides, this method does not require any additional action.

The idea is that for the whole Windows there is a common for all variable of string type - the name of the main Windows window. All can change it and all can see it.

 
Nikolai Semko:

I've been using such an easy semi-hacker way for a long time, using user32.dll. But you can't transfer tick arrays that way.

If you use dll, there is a universal solution for all types of data.

 
Nikolai Semko:

but it works fast (maybe faster than all existing solutions)

First of all, it goes through message queue. Secondly, you have to do some additional conversions (back and forth). Plus there's some validation going on.

By the way, you shouldn't write the size of the structure explicitly. That's what sizeof is for.

 
fxsaber:

If you use dll, then there is a universal solution for all types of data.

I'm not arguing. Your solution is really more universal.
But personally I need the bridge between terminals only to transfer the current tick.

My version is just easier to understand because of its primitiveness and slightly faster. I measured it in comparison with yours - it is 1.5 - 2 times faster. Reading one tick is 90 microseconds vs. 160 microseconds.

Files:
Reason: