2026-02-14 15:36:18 +01:00
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| FDI_Calculator.mqh |
|
|
|
|
|
//| Engine for Fractal Dimension Index (Carlos Sevcik Method). |
|
2026-10-25 10:23:57 +01:00
|
|
|
//| VERSION 3.10: Full Heikin Ashi & Multi-Price Integration |
|
2026-02-14 15:36:18 +01:00
|
|
|
//| Copyright 2026, xxxxxxxx |
|
|
|
|
|
//+------------------------------------------------------------------+
|
2026-10-25 10:23:57 +01:00
|
|
|
#property copyright "Copyright 2026, xxxxxxxx"
|
|
|
|
|
#property version "3.10" // Enterprise: Added Full Heikin Ashi & Universal Price Pipeline
|
|
|
|
|
#property description "Calculation engine for Fractal Dimension Index delegating to central CFractalEngine."
|
2026-02-14 15:36:18 +01:00
|
|
|
|
2026-10-16 16:29:07 +02:00
|
|
|
#ifndef FDI_CALCULATOR_MQH
|
|
|
|
|
#define FDI_CALCULATOR_MQH
|
|
|
|
|
|
|
|
|
|
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
2026-10-25 10:23:57 +01:00
|
|
|
#include <MyIncludes\Fractal_Engine.mqh>
|
2026-10-16 16:29:07 +02:00
|
|
|
|
|
|
|
|
//+==================================================================+
|
|
|
|
|
//| CLASS: CFDICalculator |
|
|
|
|
|
//+==================================================================+
|
2026-02-14 15:36:18 +01:00
|
|
|
class CFDICalculator
|
|
|
|
|
{
|
|
|
|
|
protected:
|
2026-10-25 10:23:57 +01:00
|
|
|
int m_period;
|
|
|
|
|
ENUM_FDI_METHOD m_method;
|
|
|
|
|
ENUM_APPLIED_PRICE_HA_ALL m_source_type;
|
2026-02-14 15:36:18 +01:00
|
|
|
|
2026-10-25 10:23:57 +01:00
|
|
|
// Persistent Price Buffers
|
|
|
|
|
double m_price[];
|
|
|
|
|
double m_ha_open[], m_ha_high[], m_ha_low[], m_ha_close[];
|
2026-02-14 15:36:18 +01:00
|
|
|
|
2026-10-25 10:23:57 +01:00
|
|
|
// Embedded Engines
|
|
|
|
|
CFractalEngine m_fractal_engine;
|
|
|
|
|
CHeikinAshi_Calculator m_ha_engine;
|
|
|
|
|
|
|
|
|
|
virtual bool PreparePriceSeries(const int rates_total, const int start_index,
|
|
|
|
|
const double &open[], const double &high[],
|
|
|
|
|
const double &low[], const double &close[]);
|
2026-02-14 15:36:18 +01:00
|
|
|
|
|
|
|
|
public:
|
2026-10-16 16:29:07 +02:00
|
|
|
CFDICalculator(void);
|
2026-10-25 10:23:57 +01:00
|
|
|
virtual ~CFDICalculator(void);
|
2026-02-14 15:36:18 +01:00
|
|
|
|
2026-10-25 10:23:57 +01:00
|
|
|
bool Init(const int period, const ENUM_FDI_METHOD method = FDI_NORMALIZED_BROWNIAN,
|
|
|
|
|
const ENUM_APPLIED_PRICE_HA_ALL source = PRICE_CLOSE_STD);
|
|
|
|
|
int GetPeriod(void) const { return m_period; }
|
|
|
|
|
double GetTheoreticalRandomWalkD(void) const { return m_fractal_engine.GetTheoreticalRandomWalkD(); }
|
2026-02-14 15:36:18 +01:00
|
|
|
|
2026-10-25 10:23:57 +01:00
|
|
|
// Main Calculation Method
|
|
|
|
|
void Calculate(const int rates_total, const int prev_calculated,
|
|
|
|
|
const double &open[], const double &high[],
|
|
|
|
|
const double &low[], const double &close[],
|
|
|
|
|
double &out_fdi[], const int history_limit = 0);
|
|
|
|
|
|
|
|
|
|
// Legacy 8-parameter overload (for backward compatibility with standard applied prices)
|
|
|
|
|
void Calculate(const int rates_total, const int prev_calculated,
|
|
|
|
|
const ENUM_APPLIED_PRICE price_type,
|
|
|
|
|
const double &open[], const double &high[],
|
|
|
|
|
const double &low[], const double &close[],
|
|
|
|
|
double &out_fdi[], const int history_limit = 0);
|
2026-02-14 15:36:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
2026-10-16 16:29:07 +02:00
|
|
|
//| Constructor |
|
2026-02-14 15:36:18 +01:00
|
|
|
//+------------------------------------------------------------------+
|
2026-10-16 16:29:07 +02:00
|
|
|
CFDICalculator::CFDICalculator(void) :
|
|
|
|
|
m_period(30),
|
|
|
|
|
m_method(FDI_NORMALIZED_BROWNIAN),
|
2026-10-25 10:23:57 +01:00
|
|
|
m_source_type(PRICE_CLOSE_STD)
|
2026-10-16 16:29:07 +02:00
|
|
|
{
|
2026-10-25 10:23:57 +01:00
|
|
|
ArraySetAsSeries(m_price, false);
|
|
|
|
|
ArraySetAsSeries(m_ha_open, false);
|
|
|
|
|
ArraySetAsSeries(m_ha_high, false);
|
|
|
|
|
ArraySetAsSeries(m_ha_low, false);
|
|
|
|
|
ArraySetAsSeries(m_ha_close, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| Destructor |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
CFDICalculator::~CFDICalculator(void)
|
|
|
|
|
{
|
|
|
|
|
ArrayFree(m_price);
|
|
|
|
|
ArrayFree(m_ha_open);
|
|
|
|
|
ArrayFree(m_ha_high);
|
|
|
|
|
ArrayFree(m_ha_low);
|
|
|
|
|
ArrayFree(m_ha_close);
|
2026-10-16 16:29:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| Initialization |
|
|
|
|
|
//+------------------------------------------------------------------+
|
2026-10-25 10:23:57 +01:00
|
|
|
bool CFDICalculator::Init(const int period, const ENUM_FDI_METHOD method, const ENUM_APPLIED_PRICE_HA_ALL source)
|
2026-02-14 15:36:18 +01:00
|
|
|
{
|
2026-10-25 10:23:57 +01:00
|
|
|
m_period = (period < 5) ? 5 : period;
|
|
|
|
|
m_method = method;
|
|
|
|
|
m_source_type = source;
|
2026-02-14 15:36:18 +01:00
|
|
|
|
2026-10-25 10:23:57 +01:00
|
|
|
// Initialize Embedded Central Fractal Engine
|
|
|
|
|
if(!m_fractal_engine.Init(m_period, m_method))
|
|
|
|
|
return false;
|
2026-02-14 15:36:18 +01:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
2026-10-25 10:23:57 +01:00
|
|
|
//| Prepare Price Series (Unified Standard / Heikin Ashi) |
|
2026-02-14 15:36:18 +01:00
|
|
|
//+------------------------------------------------------------------+
|
2026-10-16 16:29:07 +02:00
|
|
|
bool CFDICalculator::PreparePriceSeries(const int rates_total, const int start_index,
|
|
|
|
|
const double &open[], const double &high[],
|
|
|
|
|
const double &low[], const double &close[])
|
2026-02-14 15:36:18 +01:00
|
|
|
{
|
2026-10-16 16:29:07 +02:00
|
|
|
if(ArraySize(m_price) != rates_total)
|
|
|
|
|
{
|
2026-10-25 10:23:57 +01:00
|
|
|
ArrayResize(m_price, rates_total, 2048);
|
2026-10-16 16:29:07 +02:00
|
|
|
ArraySetAsSeries(m_price, false);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-25 10:23:57 +01:00
|
|
|
bool is_ha = (m_source_type <= PRICE_HA_CLOSE);
|
|
|
|
|
|
|
|
|
|
if(is_ha)
|
2026-02-14 15:36:18 +01:00
|
|
|
{
|
2026-10-25 10:23:57 +01:00
|
|
|
if(ArraySize(m_ha_open) != rates_total)
|
2026-02-14 15:36:18 +01:00
|
|
|
{
|
2026-10-25 10:23:57 +01:00
|
|
|
ArrayResize(m_ha_open, rates_total, 2048);
|
|
|
|
|
ArrayResize(m_ha_high, rates_total, 2048);
|
|
|
|
|
ArrayResize(m_ha_low, rates_total, 2048);
|
|
|
|
|
ArrayResize(m_ha_close, rates_total, 2048);
|
|
|
|
|
ArraySetAsSeries(m_ha_open, false);
|
|
|
|
|
ArraySetAsSeries(m_ha_high, false);
|
|
|
|
|
ArraySetAsSeries(m_ha_low, false);
|
|
|
|
|
ArraySetAsSeries(m_ha_close, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_ha_engine.Calculate(rates_total, start_index, open, high, low, close,
|
|
|
|
|
m_ha_open, m_ha_high, m_ha_low, m_ha_close);
|
|
|
|
|
|
|
|
|
|
for(int i = start_index; i < rates_total; i++)
|
|
|
|
|
{
|
|
|
|
|
switch(m_source_type)
|
|
|
|
|
{
|
|
|
|
|
case PRICE_HA_OPEN:
|
|
|
|
|
m_price[i] = m_ha_open[i];
|
|
|
|
|
break;
|
|
|
|
|
case PRICE_HA_HIGH:
|
|
|
|
|
m_price[i] = m_ha_high[i];
|
|
|
|
|
break;
|
|
|
|
|
case PRICE_HA_LOW:
|
|
|
|
|
m_price[i] = m_ha_low[i];
|
|
|
|
|
break;
|
|
|
|
|
case PRICE_HA_MEDIAN:
|
|
|
|
|
m_price[i] = (m_ha_high[i] + m_ha_low[i]) * 0.5;
|
|
|
|
|
break;
|
|
|
|
|
case PRICE_HA_TYPICAL:
|
|
|
|
|
m_price[i] = (m_ha_high[i] + m_ha_low[i] + m_ha_close[i]) * 0.333333333333;
|
|
|
|
|
break;
|
|
|
|
|
case PRICE_HA_WEIGHTED:
|
|
|
|
|
m_price[i] = (m_ha_high[i] + m_ha_low[i] + 2.0 * m_ha_close[i]) * 0.25;
|
|
|
|
|
break;
|
|
|
|
|
case PRICE_HA_CLOSE:
|
|
|
|
|
default:
|
|
|
|
|
m_price[i] = m_ha_close[i];
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-02-14 15:36:18 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-10-25 10:23:57 +01:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for(int i = start_index; i < rates_total; i++)
|
|
|
|
|
{
|
|
|
|
|
switch(m_source_type)
|
|
|
|
|
{
|
|
|
|
|
case PRICE_OPEN_STD:
|
|
|
|
|
m_price[i] = open[i];
|
|
|
|
|
break;
|
|
|
|
|
case PRICE_HIGH_STD:
|
|
|
|
|
m_price[i] = high[i];
|
|
|
|
|
break;
|
|
|
|
|
case PRICE_LOW_STD:
|
|
|
|
|
m_price[i] = low[i];
|
|
|
|
|
break;
|
|
|
|
|
case PRICE_MEDIAN_STD:
|
|
|
|
|
m_price[i] = (high[i] + low[i]) * 0.5;
|
|
|
|
|
break;
|
|
|
|
|
case PRICE_TYPICAL_STD:
|
|
|
|
|
m_price[i] = (high[i] + low[i] + close[i]) * 0.333333333333;
|
|
|
|
|
break;
|
|
|
|
|
case PRICE_WEIGHTED_STD:
|
|
|
|
|
m_price[i] = (high[i] + low[i] + 2.0 * close[i]) * 0.25;
|
|
|
|
|
break;
|
|
|
|
|
case PRICE_CLOSE_STD:
|
|
|
|
|
default:
|
|
|
|
|
m_price[i] = close[i];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 15:36:18 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
2026-10-16 16:29:07 +02:00
|
|
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
2026-10-25 10:23:57 +01:00
|
|
|
//| Main Calculation (Primary Interface) |
|
2026-10-16 16:29:07 +02:00
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
void CFDICalculator::Calculate(const int rates_total, const int prev_calculated,
|
|
|
|
|
const double &open[], const double &high[],
|
|
|
|
|
const double &low[], const double &close[],
|
|
|
|
|
double &out_fdi[], const int history_limit)
|
|
|
|
|
{
|
|
|
|
|
if(rates_total < m_period)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if(ArraySize(out_fdi) != rates_total)
|
|
|
|
|
{
|
2026-10-25 10:23:57 +01:00
|
|
|
ArrayResize(out_fdi, rates_total, 2048);
|
2026-10-16 16:29:07 +02:00
|
|
|
ArraySetAsSeries(out_fdi, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int start_prep = (prev_calculated > 0) ? (prev_calculated - 1) : 0;
|
2026-10-25 10:23:57 +01:00
|
|
|
if(!PreparePriceSeries(rates_total, start_prep, open, high, low, close))
|
2026-10-16 16:29:07 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
int start_index = (prev_calculated > m_period) ? (prev_calculated - 1) : m_period;
|
|
|
|
|
if(history_limit > 0 && prev_calculated == 0)
|
|
|
|
|
{
|
|
|
|
|
int limit_start = rates_total - history_limit;
|
|
|
|
|
if(limit_start > start_index)
|
|
|
|
|
start_index = limit_start;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(prev_calculated == 0)
|
|
|
|
|
{
|
|
|
|
|
for(int i = 0; i < start_index; i++)
|
|
|
|
|
out_fdi[i] = EMPTY_VALUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(int i = start_index; i < rates_total; i++)
|
|
|
|
|
{
|
2026-10-25 10:23:57 +01:00
|
|
|
out_fdi[i] = m_fractal_engine.CalculateFDIPoint(i, m_period, m_price);
|
2026-10-16 16:29:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-25 10:23:57 +01:00
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| Legacy Calculation Overload (100% Backward Compatible) |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
void CFDICalculator::Calculate(const int rates_total, const int prev_calculated,
|
|
|
|
|
const ENUM_APPLIED_PRICE price_type,
|
|
|
|
|
const double &open[], const double &high[],
|
|
|
|
|
const double &low[], const double &close[],
|
|
|
|
|
double &out_fdi[], const int history_limit)
|
|
|
|
|
{
|
|
|
|
|
m_source_type = (ENUM_APPLIED_PRICE_HA_ALL)price_type;
|
|
|
|
|
Calculate(rates_total, prev_calculated, open, high, low, close, out_fdi, history_limit);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-16 16:29:07 +02:00
|
|
|
#endif // FDI_CALCULATOR_MQH
|
2026-02-14 15:36:18 +01:00
|
|
|
//+------------------------------------------------------------------+
|