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

 
fxsaber # :

This is a bad explanation, because

This is a proof that the standard mathematical properties are not guaranteed for doubles.

The example you provided uses the "distributive" property for multiplication.
 

Another way to explain the meaning of DBL_EPSILON:

//+------------------------------------------------------------------+
//| Returns the size of a unit in the last place (machine epsilon)   |
//| for a specified double value. This is the positive distance      |
//| between this double value and the representable value next       |
//| larger in magnitude.                                             |
//+------------------------------------------------------------------+
double machine_eps(const double value)
  {
   union _d {double value; long bits;} dbl;
   dbl.value = value;
   dbl.bits++;

   return dbl.value - value;
  }

void  OnStart()
  {
   double epsilon = machine_eps(1.0);

   Print(epsilon);   // 2.220446049250313e-16
  }

DBL_EPSILON is just the gap size between consecutive doubles in [1,2).

https://www.exploringbinary.com/the-spacing-of-binary-floating-point-numbers/

https://en.wikipedia.org/wiki/Machine_epsilon

 

To better understand DBL_EPSILON, consider incrementing a floating-point counter:

//+------------------------------------------------------------------+
//| Advances a fp double value by a specified number of epsilons.    |
//+------------------------------------------------------------------+
double DoubleAdvance(const double value, const long distance)
  {
   union _d {double value; long bits;} dbl;
   dbl.value = value;
   dbl.bits += distance;

   return dbl.value;
  }

void OnStart()
  {
   double myvalue = 1.0;
   //doubel myvalue = 1024.0;

   // Print the first 10 representable numbers from the counter
   for (int i = 0; i < 10; i++)
     {
      myvalue = DoubleAdvance(myvalue, 1);  // Increment the integer representation by 1 bit
      Print(myvalue);
     }
  }

If the initial value of the counter is 1.0, the step size (epsilon) of the counter will be 2.220446049250250313e-16 (DBL_EPSILON) ≈ 2 at the 16th decimal place.

/*
1.0000000000000002
1.0000000000000004
1.0000000000000007
1.0000000000000009
1.000000000000001
1.0000000000000013
1.0000000000000016
1.0000000000000018
1.000000000000002
1.0000000000000022
*/

If the initial value of the counter is 1024, the step size (epsilon) of the counter will be 2.2737363675443232321e-13 (1024 * DBL_EPSILON) ≈ 2 at the 13th decimal place.

/*
1024.0000000000002
1024.0000000000005
1024.0000000000007
1024.000000000001
1024.0000000000011
1024.0000000000014
1024.0000000000016
1024.0000000000018
1024.000000000002
1024.0000000000023
*/

If the initial value of the counter is 4503599627370496 (2^52), the step size of the counter will be 1.0 (2^52 * DBL_EPSILON)= 1. At this range, a double behaves like an integer counter, no fractions could be representable.

/*
 4503599627370497.0
 4503599627370498.0
 4503599627370499.0
 4503599627370500.0
 4503599627370501.0
 4503599627370502.0
 4503599627370503.0
 4503599627370504.0
 4503599627370505.0
 4503599627370506.0
*/

You can see that the smaller numbers have smaller gaps between them, and the larger numbers have larger gaps between them.

Therefore, DBL_EPSILON is the relative error rate. relative means relative to the magnitude of the number.

at 1.0, the absolute error = 1 * 2.2204460492503131e-016 (DBL_EPSILON).

at d, the absolute error ≈ d * DBL_EPSILON.

(DBL_EPSILON is a reference rate at 1.0, something like the bank's annual interest rate, and it is equal to 2^-52).


A side-note: if you try to print 1024.0000000000003, it will print 1024.0000000000002 instead (rounding to the nearest representable number), because 1024.0000000000003 is not a representable number (non-existing fp number).

This is called a representation error:

void OnStart()
  {
   Print(1024.0000000000003);  // 1024.0000000000002
  }

The previous/next representable numbers are reached at through bit manipulation (enum as before: DoubleAdvance(value, -1)) or by using DBL_EPSILON:

void OnStart()
  {
   double myvalue = 1024.0000000000014;
   Print( myvalue * (1 - DBL_EPSILON) );  // 1024.0000000000011
   Print( myvalue * (1 + DBL_EPSILON) );  // 1024.0000000000016
  }

DBL_EPSILON is mainly used to estimate (or correct) fp round-off errors after mathematical operations on doubles (numerical analysis):

|true result - fp result| / true result <= DBL_EPSILON

 

Does the OnTester() call get disabled in the MT5 tester when there is a genetic optimisation algorithm and NOT a custom optimisation criterion?

I think not. But I would like to... Preferably without config parsing.

PS. CARAUL!!! Everything is gone! At each pass of optimisation (should/shouldn't) - OnTester() is called, which can greatly increase the total time of optimisation!

Help who can... )))))

 
Can you tell me how to define IP_ADAPTER_INFO ? This is the definition of the value is not correct
struct IP_ADAPTER_INFO {
// struct _IP_ADAPTER_INFO *Next; // Next NIC information.
uint ComboIndex; // Combo index & nbsp;
uchar AdapterName[256]; // NIC name
uchar Description[256]; // Description of the NIC.
uchar Address[6]; // MAC address &nbsp uchar Address[6]; // MAC address.
uint AddressLength; // MAC Address Length
uint Index; // NIC index
uint Type; // NIC Type NIC Type; // NIC Type
bool DhcpEnabled; // Whether DHCP is enabled or not. nbsp;; DhcpEnabled
uchar CurrentIpAddress[16];// current IP address
uchar IpAddressList[16]; // List of IP addresses.
uchar GatewayList[16]; // Default Gateway
uchar DhcpServer[16]; // DHCP Server
datetime LeaseObtained; // Get lease time
datetime LeaseExpires; // Lease expiration time
}; datetime LeaseObtained; // LeaseObtained.

#import "iphlpapi.dll"
int GetAdaptersInfo(IP_ADAPTER_INFO &AdapterInfo[], int &Size);
#import
 
Moving slightly ahead of the local time immediately leads to disconnection from the trade server.
2023.05.24 11:11:15.645 '4999464569': connection to MetaQuotes-Demo lost
 

In my search I came across an interesting undocumented function

you can hide input comment for a dialogue box

input int  param=0;  /* показываемый комментарий */   // комментарий для программиста

original post here

https://www.mql5.com/ru/forum/1271#comment_9019

Предложение по комментариям к внешним переменным
Предложение по комментариям к внешним переменным
  • 2010.06.30
  • www.mql5.com
Общее обсуждение: Предложение по комментариям к внешним переменным
 
lynxntech #:

In my search I came across an interesting undocumented function

you can hide input comment for a dialogue box

original post here

https://www.mql5.com/ru/forum/1271#comment_9019

Thanks, missed it... Useful.
 

In Tester, the History table is overwritten only in two cases.

  1. HistorySelect call.
  2. Call of OnTrade (even if it is not specified).
This is done to save resources. That's why you can run into such ambiguity.

#include <MT4Orders.mqh> // https://www.mql5.com/ru/code/16006

#define  Ask SymbolInfoDouble(_Symbol, SYMBOL_ASK)

void OnTick()
{
  const ulong Ticket1 = OrderSend(_Symbol, OP_BUYLIMIT, 1, Ask - 1000 * _Point, 0, 0, 0);
  const ulong Ticket2 = OrderSend(_Symbol, OP_BUYLIMIT, 1, Ask - 1000 * _Point, 0, 0, 0);

  OrderDelete(Ticket2);
  HistorySelect(0, INT_MAX); // Если убрать эту строку, то конечный Print-результат будет иным.
  
  OrderDelete(Ticket1);    
  HistorySelect(0, INT_MAX);
  
  for (int i = 0; i < HistoryOrdersTotal(); i++)
    Print(HistoryOrderGetTicket(i)); // 2 3 или 3 2.
      
  TesterStop();
}
Search string: Uluchshenie 064.
 
Few things.

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

Errors, bugs, questions

fxsaber, 2023.07.19 19:10

ulong HistoryDealGetTicket( int ) { return(0); } // Перегрузка в каком-нибудь далеком mqh.

void OnStart()
{      
  if (HistorySelect(0, INT_MAX))
  {
    for (uint i = HistoryDealsTotal(); (bool)i--; ) // Хорошо - потенциальная перегрузка не сломает результат.
      Print(HistoryDealGetTicket(i));
      
    for (int i = HistoryDealsTotal() - 1; i >= 0; i--) // Классика - плохо из-за потенциальной перегрузки.
      Print(HistoryDealGetTicket(i));
  }
}

Forum on trading, automated trading systems and testing trading strategies

Bugs, bugs, questions

fxsaber, 2023.07.19 18:55

HistoryOrderGetTicket, PositionGetTicket, OrderGetTicket - similar.