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

 
fxsaber #:

In MM calculations, consider this possibility.


Do you think they give loans to everyone?

 
Alexey Viktorov #:

Do you think they give loans to everyone?

What if they do? But it doesn't take into account.
 
Artyom Trishkin #:
And if they do? But it is not accounted for...

Then in the topic about MQL5 language peculiarities you should simply inform that the credit is not included in the balance and when writing code according to the order you should consider the total balance as the sum of balance and credit. And not with such a picture.

 
Alexey Viktorov #:

Then in the topic about MQL5 language peculiarities you should simply inform that the credit is not included in the balance and when writing code by order you should consider the total balance as the sum of balance and credit. And not with such a picture.

Why not a picture?

It leads to exactly this conclusion: "credit is not included in the balance and when writing the code you should consider the total balance as the sum of balance and credit".

 
Artyom Trishkin #:

Why not a picture?

It leads to exactly this conclusion: "credit is not included in the balance and when writing the code, we should consider the total balance as the sum of balance and credit".

Because many people, like me, do not immediately pay attention to the credit. How can the equity be greater than zero if the balance is 0? I looked, closed the topic without understanding anything. And only after a couple of minutes I opened it again in an attempt to understand. And someone may not even try to understand, but just stupidly claim, referring to the authority of fxsaber`a, that it happens. Unfortunately, there are a lot of such people.

 
Alexey Viktorov #:

Because many people, like me, do not immediately pay attention to the credit. How can the equity be greater than zero if the balance is 0? I looked, closed the topic without understanding anything. And only after a couple of minutes I opened it again in an attempt to understand. And someone may not even try to understand, but just stupidly claim, referring to the authority of fxsaber`a, that it happens. Unfortunately, there are a lot of such people.

Logical. But not convincing. After all, there are those who read diagonally ;)
 
Artyom Trishkin #:
Logical. But not convincing. After all, there are those who read diagonally too ;)

A programmer who reads diagonally is a shit coder. That's my opinion. Code and code discussion are not "The Three Musketeers", you should get into such a text and understand what is written.

That's all. Artem, sorry, let's not clog the topic with unnecessary arguments.

 
Alexey Viktorov #:
A programmer who reads diagonally is a shit coder.

And the one who looks past it?

Alexey Viktorov #:
let's not clog the topic with unnecessary arguments.

Good

 

Forum on trading, automated trading systems and testing trading strategies

Errors, bugs, questions

fxsaber, 2024.09.23 19:07

Let's say you open a position by the Q key.


  1. Not in OnChartEvent the calculation was started.
  2. During the calculation, you pressed the Q-key three times, not understanding why the position is not opened.
  3. After the calculations are completed (and it can be a long time later, when it is no longer relevant), the Expert Advisor will open three positions.

To prevent this from happening, solutions have been given.

The problem of ignoring random ChartEvent events. It has been solved.


Simulation of the problem.

Forum on trading, automated trading systems and testing trading strategies.

Bugs, bugs, questions

fxsaber, 2024.09.23 09:13 AM

double Calc()
{
  double Res = 0;

  for (int i = 0; i < 1 e8; i++)
    Res += MathSin(MathRand());
    
  return(Res);
}

void OnChartEvent( const int id, const long&, const double&, const string& ) 
{
  if (id == CHARTEVENT_KEYDOWN)
  {
    Print("Calculating...");    
    Print(Calc());
  }
}

This Expert Advisor does calculations at the press of a key. How can I modify the Expert Advisor so that repeated keystrokes during calculations will not cause repeated calculations?


2024.09.23 10:09:54.802 Calculating...
2024.09.23 10:09:57.138 2380.6605050566823
2024.09.23 10:09:57.138 Calculating...
2024.09.23 10:09:59.475 3362.419766547137

This is the log when I pressed the key twice (without interruption). You can see that the second calculation was started after the calculation.



Solution1.

Forum on trading, automated trading systems and testing trading strategies.

Errors, bugs, questions

fxsaber, 2024.09.23 09:35 PM

Unfortunately, this is the only solution that came to mind.

void OnChartEvent( const int id, const long&, const double&, const string& ) 
{
  if (id == CHARTEVENT_KEYDOWN)
  {
    TEMP Temp;
    
    if (Temp.Is())
    {
      Print("Calculating...");    
      Print(Calc());
    }   
  }
}

class TEMP
{
private:
  static ulong PrevTime;
  
  const ulong Interval;
  bool IsValue;
  
public:
  TEMP( const ulong uInterval = 1000 ) : Interval(uInterval), IsValue(false) {}
  
  bool Is() { return(this.IsValue = (::GetTickCount64() - TEMP::PrevTime) > this.Interval); }
  
  ~TEMP( void ) { if (this.IsValue) TEMP::PrevTime = ::GetTickCount64(); }
};

static ulong TEMP::PrevTime = 0;

Solution2.

Forum on trading, automated trading systems and testing trading strategies

Errors, bugs, questions

Aleksandr Slavskii, 2024.09.23 11:00 AM

Yes, according to your hint, it seems to work))))

//+------------------------------------------------------------------+
double Calc()
  {
   double Res = 0;

   for(int i = 0; i < 1 e8; i++)
      Res += MathSin(MathRand());

   EventChartCustom(0, 0, 0, 0, "");

   return(Res);
  }
//+------------------------------------------------------------------+
void OnChartEvent(const int id, const long&, const double&, const string&)
  {
   static bool aaa = true;
   if(id == CHARTEVENT_KEYDOWN && aaa)
     {
      Print("Calculating...");
      Print(Calc());
      aaa = false;
     }
   if(id == CHARTEVENT_CUSTOM)
      aaa = true;
  }
//+------------------------------------------------------------------+
 
fxsaber #:


Task simulation.



Solution1.


Solution2.

The second time the button will be pressed at the time when the instructions after the call will be executed

EventChartCustom(0, 0, 0, 0, "");

but before the exit from the current OnChartEvent call (the same Print will not even start executing yet), and, by the law of meanness, it is then that all the conditions for the most unfavourable scenario)))) will be created. Bingo!!! It will be an epic and instructive fiasco))))))

Question: who will be to blame?

Answer: the one who uses asynchronousness in the combat code, poorly realising the depth of possible depths and the pain that lives there.

This is so that everyone can understand how it will be (click the copka after Calculated appears):

double Calc()
  {
   double Res = 0;

   for(int i = 0; i < 1 e8; i++)
      Res += MathSin(MathRand());
   EventChartCustom(0, 0, 0, 0, "");
   return(Res);
  }
//+------------------------------------------------------------------+

void OnChartEvent(const int id, const long&, const double&, const string&)
  {
   static bool aaa = true;
   if(id == CHARTEVENT_KEYDOWN && aaa)
     {
      Print("Calculating...");
      double res = Calc();
      Print("Calculated");
      Sleep(5000);
      Print(res);
      aaa = false;
     }
   if(id == CHARTEVENT_CUSTOM)
      aaa = true;
  }

Somehow this is how it should be:

class EventLocker{
public:
   EventLocker(){
      m_lock = true;
   }
   ~EventLocker(){
      EventChartCustom(0, 0, 0, 0, "");
   }
   static void Unlock() {m_lock = false;}
   static bool Locked() {return m_lock;}
private:
   static bool m_lock;
};

bool EventLocker::m_lock = false;

double Calc()
  {
   double Res = 0;

   for(int i = 0; i < 1 e8; i++)
      Res += MathSin(MathRand());
   return(Res);
  }
//+------------------------------------------------------------------+

void OnChartEvent(const int id, const long&, const double&, const string&)
  {
   if(id == CHARTEVENT_KEYDOWN && !EventLocker::Locked())
     {
      EventLocker lk;
      Print("Calculating...");
      Print(Calc());
     }
   if(id == CHARTEVENT_CUSTOM)
      EventLocker::Unlock();
  }