mql5-execution-microstructu.../Include/RequestLatencyLab/Legacy/BenchmarkStatistics.mqh

171 lines
No EOL
6.8 KiB
MQL5

//+------------------------------------------------------------------+
//| BenchmarkStatistics.mqh |
//| Copyright 2026, MetaQuotes Ltd. |
//| www.mql5.com |
//+------------------------------------------------------------------+
#ifndef REQUEST_LATENCY_LAB_LEGACY_BENCHMARK_STATISTICS_MQH
#define REQUEST_LATENCY_LAB_LEGACY_BENCHMARK_STATISTICS_MQH
//+-------------------------------------------------------------------+
//| Исходные реализации (без изменений алгоритма) — для проверки |
//| согласованности адаптированных функций с исходным кодом (DEP-02). |
//| Исходное поведение на пустом входе: возврат 0. |
//+-------------------------------------------------------------------+
double LegacyMinValue(const double &arr[], const int count)
{
if(count <= 0)
return(0);
double m = arr[0];
for(int i = 1; i < count; i++)
if(arr[i] < m)
m = arr[i];
return(m);
}
//+------------------------------------------------------------------+
//| Максимум первых count элементов (исходная реализация) |
//+------------------------------------------------------------------+
double LegacyMaxValue(const double &arr[], const int count)
{
if(count <= 0)
return(0);
double m = arr[0];
for(int i = 1; i < count; i++)
if(arr[i] > m)
m = arr[i];
return(m);
}
//+------------------------------------------------------------------+
//| Среднее первых count элементов (исходная реализация) |
//+------------------------------------------------------------------+
double LegacyAvgValue(const double &arr[], const int count)
{
if(count <= 0)
return(0);
double s = 0;
for(int i = 0; i < count; i++)
s += arr[i];
return(s / count);
}
//+------------------------------------------------------------------+
//| Перцентиль p (0..100) по первым count элементам массива. |
//| Работает на КОПИИ данных (ArraySort не портит исходный массив). |
//| Линейная интерполяция между соседними отсортированными. |
//+------------------------------------------------------------------+
double LegacyPercentile(const double &arr[], const int count, const double p)
{
if(count <= 0)
return(0);
if(count == 1)
return(arr[0]);
double tmp[];
if(ArrayResize(tmp, count) != count)
return(0);
for(int i = 0; i < count; i++)
tmp[i] = arr[i];
ArraySort(tmp);
const double pos = p / 100.0 * (count - 1);
const int lo = (int)MathFloor(pos);
const int hi = (int)MathCeil(pos);
if(lo == hi)
return(tmp[lo]);
const double frac = pos - lo;
return(tmp[lo] + (tmp[hi] - tmp[lo]) * frac);
}
//+------------------------------------------------------------------+
//| Адаптированные проверяемые версии для лаборатории. |
//| Возвращают false при пустой выборке, нулевом count, некорректном |
//| p или невозможности выделить память; значение не выдумывается. |
//+------------------------------------------------------------------+
bool LabMinValue(const double &arr[], const int count, double &result)
{
if(count <= 0)
return(false);
result = arr[0];
for(int i = 1; i < count; i++)
if(arr[i] < result)
result = arr[i];
return(true);
}
//+------------------------------------------------------------------+
//| Максимум выборки (адаптированная версия) |
//+------------------------------------------------------------------+
bool LabMaxValue(const double &arr[], const int count, double &result)
{
if(count <= 0)
return(false);
result = arr[0];
for(int i = 1; i < count; i++)
if(arr[i] > result)
result = arr[i];
return(true);
}
//+------------------------------------------------------------------+
//| Среднее выборки (адаптированная версия) |
//+------------------------------------------------------------------+
bool LabAvgValue(const double &arr[], const int count, double &result)
{
if(count <= 0)
return(false);
double s = 0;
for(int i = 0; i < count; i++)
s += arr[i];
result = s / count;
return(true);
}
//+------------------------------------------------------------------+
//| Проверяемый перцентиль. p задан явно в шкале 0..100 (допустим |
//| дробный p, например 99.9). Алгоритм совпадает с LegacyPercentile |
//| на допустимых входах. |
//+------------------------------------------------------------------+
bool LabPercentile(const double &arr[], const int count,
const double p0_100, double &result)
{
if(count <= 0)
return(false);
if(p0_100 < 0.0 || p0_100 > 100.0)
return(false);
if(count == 1)
{
result = arr[0];
return(true);
}
double tmp[];
if(ArrayResize(tmp, count) != count)
return(false);
for(int i = 0; i < count; i++)
tmp[i] = arr[i];
ArraySort(tmp);
const double pos = p0_100 / 100.0 * (count - 1); // h=(n-1)*p
const int lo = (int)MathFloor(pos);
const int hi = (int)MathCeil(pos);
if(lo == hi)
{
result = tmp[lo];
return(true);
}
const double frac = pos - lo;
result = tmp[lo] + (tmp[hi] - tmp[lo]) * frac; // линейная интерполяция
return(true);
}
//+------------------------------------------------------------------+
//| Выборочное стандартное отклонение (делитель n-1). |
//| n=0 и n=1 -> false (значение пустое). |
//+------------------------------------------------------------------+
bool LabStdDev(const double &arr[], const int count, double &result)
{
if(count <= 1)
return(false);
double mean = 0;
if(!LabAvgValue(arr, count, mean))
return(false);
double acc = 0;
for(int i = 0; i < count; i++)
{
const double d = arr[i] - mean;
acc += d * d;
}
result = MathSqrt(acc / (count - 1));
return(true);
}
#endif // REQUEST_LATENCY_LAB_LEGACY_BENCHMARK_STATISTICS_MQH
//+------------------------------------------------------------------+