refactor(indicators): Integrated R-Squared and Slope calculation

This commit is contained in:
Toh4iem9
2026-02-13 15:57:46 +01:00
parent 0ee80b3495
commit 643ddb8a34
@@ -1,9 +1,9 @@
//+------------------------------------------------------------------+
//| LinearRegression_Calculator.mqh |
//| VERSION 3.10: Supports both Moving and Static Channels. |
//| Copyright 2025, xxxxxxxx |
//| VERSION 4.00: Integrated R-Squared and Slope calculation. |
//| Copyright 2026, xxxxxxxx |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx"
#property copyright "Copyright 2026, xxxxxxxx"
#include <MyIncludes\HeikinAshi_Tools.mqh>
@@ -23,6 +23,10 @@ protected:
ENUM_CHANNEL_MODE m_channel_mode;
double m_deviations;
// Precalc for optimization
double m_sum_x, m_sum_x2;
double m_denom_x;
//--- Persistent Buffer
double m_price[];
@@ -32,7 +36,10 @@ public:
CLinearRegressionCalculator(void) {};
virtual ~CLinearRegressionCalculator(void) {};
// Init 1: Full (For Channels)
bool Init(int period, ENUM_CHANNEL_MODE mode, double deviations);
// Init 2: Simple (For R2/Slope only)
bool Init(int period);
//--- Method 1: Moving Regression (The "Wavy" line)
void CalculateMoving(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
@@ -41,19 +48,42 @@ public:
//--- Method 2: Static Channel (The "Straight" segment for current bars)
void CalculateStaticChannel(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
double &middle_buffer[], double &upper_buffer[], double &lower_buffer[]);
//--- Method 3: Rolling Statistics (R-Squared & Slope) - NEW
void CalculateState(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
double &out_slope[], double &out_r2[], double &out_forecast[]);
};
//+------------------------------------------------------------------+
//| Init |
//| Init (Full) |
//+------------------------------------------------------------------+
bool CLinearRegressionCalculator::Init(int period, ENUM_CHANNEL_MODE mode, double deviations)
{
m_period = (period < 2) ? 2 : period;
m_channel_mode = mode;
m_deviations = (deviations <= 0) ? 2.0 : deviations;
// Pre-calc X sums (0..N-1)
m_sum_x = 0;
m_sum_x2 = 0;
for(int k = 0; k < m_period; k++)
{
m_sum_x += k;
m_sum_x2 += k * k;
}
m_denom_x = m_period * m_sum_x2 - m_sum_x * m_sum_x;
return true;
}
//+------------------------------------------------------------------+
//| Init (Simple) |
//+------------------------------------------------------------------+
bool CLinearRegressionCalculator::Init(int period)
{
return Init(period, DEVIATION_STANDARD, 2.0); // Delegate with defaults
}
//+------------------------------------------------------------------+
//| Method 1: Moving Regression (Wavy) |
//+------------------------------------------------------------------+
@@ -206,6 +236,66 @@ void CLinearRegressionCalculator::CalculateStaticChannel(int rates_total, const
}
}
//+------------------------------------------------------------------+
//| Method 3: Rolling Statistics (NEW) |
//+------------------------------------------------------------------+
void CLinearRegressionCalculator::CalculateState(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
double &out_slope[], double &out_r2[], double &out_forecast[])
{
if(rates_total < m_period)
return;
int start_index = (prev_calculated == 0) ? 0 : prev_calculated - 1;
if(ArraySize(m_price) != rates_total)
ArrayResize(m_price, rates_total);
if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close))
return;
int loop_start = MathMax(m_period - 1, start_index);
for(int i = loop_start; i < rates_total; i++)
{
double sum_y = 0, sum_xy = 0, sum_y2 = 0;
for(int k = 0; k < m_period; k++)
{
double price = m_price[i - m_period + 1 + k];
double x = k; // Map x to 0..N-1
sum_y += price;
sum_xy += x * price;
sum_y2 += price * price;
}
double b = 0;
if(m_denom_x != 0)
b = (m_period * sum_xy - m_sum_x * sum_y) / m_denom_x; // Slope
double a = (sum_y - b * m_sum_x) / m_period; // Intercept
double forecast = a + b * (m_period - 1); // Current Value
// R-Squared Calc
// SST = Total Sum of Squares = Sum(y^2) - (Sum(y)^2)/N
// SSR = Regression Sum of Squares = b * (Sum(xy) - Sum(x)Sum(y)/N)
// R2 = SSR / SST
// Alternative standard formula: R2 = (N*SumXY - SumX*SumY)^2 / (DenomX * DenomY)
double denom_y = (m_period * sum_y2) - (sum_y * sum_y);
double r2 = 0;
if(m_denom_x > 0 && denom_y > 0)
{
double num = (m_period * sum_xy - m_sum_x * sum_y);
r2 = (num * num) / (m_denom_x * denom_y);
}
out_slope[i] = b;
out_r2[i] = r2;
out_forecast[i] = forecast; // Same as 'middle_buffer' in Moving mode
}
}
//+------------------------------------------------------------------+
//| Prepare Price (Standard) |
//+------------------------------------------------------------------+