Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

Metatrader 5 Indicator | KCI Volatility Distance | Source Code Included

New free code from MQL5: indicators, EAs, and scripts for traders.

Image for KCI Volatility Distance

Introduce :

// Example: Adaptive Grid Distance Calculation
input double GridMultiplier = 1.5;

// Obtain the latest directional matrix measurement
double current_matrix = MatrixEngine.Calculate(0, high, low, close);

// Derive the adaptive grid spacing using the matrix value
// as a dynamic representation of current market conditions
double dynamic_grid_step = current_matrix * GridMultiplier;

// Open a new grid position only after the market has moved
// beyond the dynamically calculated spacing threshold
if(MathAbs(CurrentPrice - LastOrderPrice) >= dynamic_grid_step)
  {
   // Execute the next Grid / Averaging order
   ExecuteGridOrder();
  }

3. Feature Normalization for Machine Learning (ML)

Modules Neural network models require normalized input data to accurately recognize patterns, without being distorted by price digit differences between brokers or asset classes (e.g., the stark difference between WTI oil prices and minor forex pairs).

Never miss news-driven moves – use the News OCO Expert Advisor to place pending orders safely around economic releases. Find out more.

  • Logic: Within the KCI Volatility Distance formula, there is an Efficiency Ratio (ER) calculation which naturally produces a scale ratio of 0 to 1.
  • Implementation: You can modify the OOP class slightly to return pure ER values. This array of Matrix values ​​is extracted into an array as a very clean and noise-free training dataset for the Pattern Learner.

--------- Other options ---------

example Modular Header File (KCIVD.mqh)

Save the code below as KCIVD.mqh in the MQL5\Include\ folder. This file will be the main engine called by indicators, expert advisors, and ML modules.

Code snippet

//+------------------------------------------------------------------+
//|                                                        KCIVD.mqh |
//|                                   Copyright 2026, KCI Developer |
//|                         https://www.mql5.com/en/users/ritzfalih |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, KCI Developer"
#property link      "https://www.mql5.com/en/users/ritzfalih"
#property version   "1.0"

class CKCIVolatilityDistance
  {
private:
   int               m_kinetic_period;
   double            m_point;

public:
                     CKCIVolatilityDistance(void);
                    ~CKCIVolatilityDistance(void);
   
   bool              Init(const int period);
   double            Calculate(const int index, 
                               const double &high[], 
                               const double &low[], 
                               const double &close[]);
  };

//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
CKCIVolatilityDistance::CKCIVolatilityDistance(void)
  {
   m_kinetic_period = 14;
   m_point          = _Point;
  }

//+------------------------------------------------------------------+
//| Destructor                                                       |
//+------------------------------------------------------------------+
CKCIVolatilityDistance::~CKCIVolatilityDistance(void)
  {
  }

//+------------------------------------------------------------------+
//| Initialization                                                   |
//+------------------------------------------------------------------+
bool CKCIVolatilityDistance::Init(const int period)
  {
   if(period < 2)
     {
      Print("[KCI VD Error] Period must be at least 2");
      return(false);
     }
   m_kinetic_period = period;
   m_point          = _Point;
   if(m_point <= 0) m_point = 0.00001; // Safety fallback
   return(true);
     }

//+------------------------------------------------------------------+
//| Core Calculation Engine (Optimized for CPU & Speed)              |
//| Note: Arrays must be set as Series (ArraySetAsSeries = true)      |
//+------------------------------------------------------------------+
double CKCIVolatilityDistance::Calculate(const int index, 
                                         const double &high[], 
                                         const double &low[], 
                                         const double &close[])
  {
   double sum_TR    = 0.0;
   double sum_sq_TR = 0.0;
   double path_length = 0.0;

   // --- 1. Loop Window Teroptimasi Tanpa Fungsi Bertingkat ---
   for(int j = 0; j < m_kinetic_period; j++)
     {
      int curr_idx = index + j;
      
      // Optimasi Kecepatan Komparasi True Range (Menghindari MathMax bertingkat)
      double hl = high[curr_idx] - low[curr_idx];
      double hc = MathAbs(high[curr_idx] - close[curr_idx + 1]);
      double lc = MathAbs(low[curr_idx] - close[curr_idx + 1]);
      
      double tr = hl;
      if(hc > tr) tr = hc;
      if(lc > tr) tr = lc;
      
      sum_TR    += tr;
      sum_sq_TR += (tr * tr);

      // Path length pergerakan Close (Efficiency Ratio)
      if(j < m_kinetic_period - 1)
        {
         path_length += MathAbs(close[curr_idx] - close[curr_idx + 1]);
        }
     }

   // --- 2. Perhitungan Efisiensi Rasio ---
   double net_distance = MathAbs(close[index] - close[index + m_kinetic_period - 1]);
   
   if(path_length < net_distance) path_length = net_distance; // Batasan logika
   if(path_length == 0.0)         path_length = m_point;      // Mencegah Zero Division
   
   double efficiency_ratio = net_distance / path_length;

   // --- 3. Perhitungan Standar Deviasi KCI ---
   double mean_TR  = sum_TR / m_kinetic_period;
   double variance = (sum_sq_TR / m_kinetic_period) - (mean_TR * mean_TR);
   if(variance < 0.0) variance = 0.0; // Koreksi presisi floating-point
   
   double std_TR = MathSqrt(variance);

   // --- 4. Output Nilai Akhir KCI VD ---
   return(std_TR * (2.0 - efficiency_ratio));
  }


Visual Indicator File (KCI_Volatility_Distance.mq5) --- Other options ---

Save this code in the MQL5\Indicators\ folder. This indicator is now very clean because it calls the OOP structure from the .mqh file above.

Code snippet

//+------------------------------------------------------------------+
//|                                      KCI Volatility Distance.mq5 |
//|                                   Copyright 2026, KCI Developer |
//|                         https://www.mql5.com/en/users/ritzfalih |
//+------------------------------------------------------------------+
#property copyright   "Copyright 2026, KCI Developer"
#property link        "https://www.mql5.com/en/users/ritzfalih"
#property description "KCI Volatility Distance - Indicator Module"
#property indicator_separate_window

#property indicator_buffers 1
#property indicator_plots   1
#property indicator_label1  "KCI-VD"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrGold
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

// Include Mesin Utama KCIVD
#include <KCIVD.mqh>

// --- Input ---
input group "=== KCI Volatility Settings ==="
input int KineticPeriod = 14;   // Calculation period (≥2)

// --- Buffer ---
double KVR_Buffer[];

// Global Object Instance
CKCIVolatilityDistance kci_engine;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(!kci_engine.Init(KineticPeriod))
      return(INIT_PARAMETERS_INCORRECT);

   SetIndexBuffer(0, KVR_Buffer, INDICATOR_DATA);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0.0);
   ArraySetAsSeries(KVR_Buffer, true);

   IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
   IndicatorSetString(INDICATOR_SHORTNAME, "KCI-VD(" + IntegerToString(KineticPeriod) + ")");

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   if(rates_total < KineticPeriod + 2) return(0);

   // Atur array bawaan sebagai series
   ArraySetAsSeries(high, true);
   ArraySetAsSeries(low, true);
   ArraySetAsSeries(close, true);

   if(prev_calculated == 0)
     {
      ArrayInitialize(KVR_Buffer, 0.0);
     }

   int limit = (prev_calculated == 0) ? (rates_total - KineticPeriod - 2) : (rates_total - prev_calculated + 1);

   // Jalankan mesin kalkulasi lewat objek OOP
   for(int i = limit; i >= 0 && !IsStopped(); i--)
     {
      KVR_Buffer[i] = kci_engine.Calculate(i, high, low, close);
     }

   return(rates_total);
  }

Cross-Functional Integration Guide (How to Use in EA/ML)

With the CKCIVolatilityDistance class structure, you can use it directly in any Expert Advisor for maximum performance. Here's an example of its logical implementation:

Integration in Machine Learning Module (Feature Extraction / Signal Validation)

If you have a neural network or pattern learner module that requires volatility normalization as an input feature, you can extract the KCI VD value purely:

Code snippet

#include <KCIVD.mqh>

CKCIVolatilityDistance KCI_ML;

int OnInit()
  {
   // Initialize the KCI Volatility Distance engine
   // with the desired calculation period.
   KCI_ML.Init(14);

   return(INIT_SUCCEEDED);
  }

void GetMLFeatures()
  {
   double high[], low[], close[];

   // Copy a sufficient amount of historical price data
   // (e.g., the most recent 50 bars) into local arrays.
   CopyHigh(_Symbol, _Period, 0, 50, high);
   CopyLow(_Symbol, _Period, 0, 50, low);
   CopyClose(_Symbol, _Period, 0, 50, close);

   ArraySetAsSeries(high, true);
   ArraySetAsSeries(low, true);
   ArraySetAsSeries(close, true);

   // Calculate the KCI Volatility Distance value for
   // the current bar (index 0) or the last closed bar (index 1).
   double kci_current_volatility = KCI_ML.Calculate(0, high, low, close);

   // Feed 'kci_current_volatility' into the Neural Network
   // feature vector for signal validation or prediction.
  }

4. Multi-Symbol Scanner (Dashboard Indicator)

Because KCIDirectionalMatrix does not rely on iCustom and frees local memory quickly, it is perfect as a signal search engine on a multi-symbol dashboard.

  • Logic: Install one dashboard indicator on one chart, but iterate the matrix calculation loop to 20-30 different symbols simultaneously.
  • Implementation: The indicator will scan the entire Market Watch (Major, Cross, Metals, Indices) to identify assets whose Matrix values ​​are contracting (preparing for a breakout) or experiencing strong trending momentum. This will prevent Windows Server VPS from lagging or overloading the CPU.

Code snippet

// Example: Multi-Asset Market Scanning Engine
string symbols[] = {"EURUSD", "GBPUSD", "XAUUSD", "BTCUSD", "US30"};

for(int s = 0; s < ArraySize(symbols); s++)
  {
   // Load the required historical market data
   // for the current trading instrument.
   // ...

   // Evaluate the directional strength
   // of the most recently completed candle.
   double strength = MatrixEngine.Calculate(1, h, l, c);

   // Generate a trading signal only when the
   // directional strength exceeds the configured threshold.
   if(strength > Threshold)
     {
      // Update the dashboard with the detected
      // BUY/SELL opportunity for the current instrument.
      DrawDashboardSignal(symbols[s], strength);
     }
  }

This is the KCI Volatility Distance code; at least, this code can be used for various purposes. You can further develop it with standard MT5 indicators and custom MT5 indicators to achieve even greater accuracy and precise analysis.

Thanks for reading. Discover premium MetaTrader solutions at RobotFX.

74838

Best MetaTrader Indicators + Profitable Expert Advisors