KCI Volatility Distance
Info
The KCI Volatility Distance is a Indicator for MetaTrader 5 that introduce:the kci volatility distance is an advanced, adaptive algorithm meticulously engineered to map market momentum and trend direction with pure precision. Utilizing a proprietary matrix-based calculation, this tool dynamically filters out market noise and provides a strictly quantitative perspective on directional strength.
Usage
This tool is typically used for enhancing chart analysis and decision making.
Platform
This Indicator works exclusively on MetaTrader 5 (both build 600+ and newer versions).
Setup
Place the downloaded file in MQL5/Indicators folder via File ? Open Data Folder in MetaTrader 5.
How to Install and Use KCI Volatility Distance
1. Installation: Place your file in the MQL/Indicators folder via "Open Data Folder" and restart your terminal.
2. Loading: Find the indicator in the Navigator, drag it onto your chart, and configure the input parameters in the popup window.
3. Customization: Press Ctrl+I to open the indicator list, select your tool, and click "Properties" to change colors, levels, or visual styles.
4. Updating: Replace the old file in the Indicators folder with the new version and restart the platform to apply changes.
Frequently Asked Questions
Q: Why is my indicator not showing? A: Verify the file is in the MQL/Indicators folder, or try right-clicking the "Indicators" tree in the Navigator and clicking "Refresh."
Q: Do custom indicators slow down the platform? A: Too many complex indicators can impact performance; remove unused ones via the "Indicator List" (Ctrl+I).
Q: Can I use MT4 indicators on MT5? A: No, MQL4 and MQL5 are distinct languages; ensure the indicator is compiled specifically for your platform version.
What this tool does
Introduce:The KCI Volatility Distance is an advanced, adaptive algorithm meticulously engineered to map market momentum and trend direction with pure precision.
Typical Use Case
This Indicator excels in automated trading and technical analysis on MetaTrader 5.
Compatible Platform & Setup
This Indicator works on MetaTrader 5. Place the file in the MQL5/Indicators folder and restart the terminal.
Description & Settings
Related: Quantora Market Volatility Monitor MT5 - Professional ATR and Volatility Dashboard - another powerful indicator for MetaTrader 5 traders.
Introduce:Also recommended: Volatility Regime Indicator - similar indicator with strong performance on MetaTrader 5.
The KCI Volatility Distance is an advanced, adaptive algorithm meticulously engineered to map market momentum and trend direction with pure precision. Utilizing a proprietary matrix-based calculation, this tool dynamically filters out market noise and provides a strictly quantitative perspective on directional strength. Built with a highly optimized Object-Oriented Programming (OOP) core, it is designed for both visual trading clarity and seamless integration into Expert Advisors or Machine Learning modules, ensuring ultra-light CPU performance across multiple assets.
Functions & Explanations
Matrix Momentum Engine: Computes internal price dynamics within a multidimensional array framework. Function: Identifies the true, underlying strength of the current market direction before a major breakout occurs, providing an edge in early trend detection.
Dynamic Noise Filter: An adaptive mechanism that recalibrates itself based on live market conditions. Function: Automatically suppresses false signals and erratic price spikes during consolidation or low-volume periods, safeguarding automated algorithms from executing premature trades.
Directional Strength Output: Translates complex mathematical matrix values into a clean, standalone numeric flow. Function: Serves as a definitive gauge for precise entry triggers, continuation patterns validation, or dynamic lot sizing in automated systems.
Requirements
Technical Indicator / Algorithmic Trading Core / Quantitative Analysis Tool.
Highly optimized execution, extremely lightweight for low CPU consumption on VPS environments (< 50 KB).
Open-source script ( .mq5 file) featuring an embedded OOP class architecture.
Picture. 1
picture. 3
Simple parameter settings
Picture. 2
KCI Volatility Distance Integration Guide into Expert Advisor (EA)
There are two main methods for integrating this algorithm into an EA. The first method (Embedded OOP) is highly recommended for maximum CPU efficiency and Machine Learning dataset collection. The second method (iCustom Invocation) is suitable for rapid prototype testing.
Method 1: Direct Use via Embedded OOP Class (Ultra-Lightweight)
This method embeds the engine directly into the EA without calling external indicators. The code is placed at the global level, initialized in OnInit, and executed in OnTick.
Code snippet
class CKCIDirectionalMatrix {
// (Insert the complete CKCIDirectionalMatrix class implementation here)
// ...
};
CKCIDirectionalMatrix MatrixEngine;
input int MatrixPeriod = 14;
input double EntryThreshold = 0.0050; // Momentum strength threshold
int OnInit() {
if (!MatrixEngine.Init(MatrixPeriod)) return (INIT_PARAMETERS_INCORRECT);
return (INIT_SUCCEEDED);
}
void OnTick() {
// Prepare arrays to store the latest market price data
double high[], low[], close[];
if (CopyHigh(_Symbol, _Period, 0, MatrixPeriod + 2, high) < 0) return;
if (CopyLow(_Symbol, _Period, 0, MatrixPeriod + 2, low) < 0) return;
if (CopyClose(_Symbol, _Period, 0, MatrixPeriod + 2, close) < 0) return;
// Arrays must be configured as time series (Index 0 = most recent bar)
ArraySetAsSeries(high, true);
ArraySetAsSeries(low, true);
ArraySetAsSeries(close, true);
// Calculate the matrix output for the most recently closed bar (Index 1)
double current_matrix_strength = MatrixEngine.Calculate(1, high, low, close);
// --- A. Machine Learning / Feature Extraction Module ---
// Feed 'current_matrix_strength' into your neural network
// or AI feature vector as an input variable.
// --- B. Trading Decision Module (Signal Filtering) ---
if (current_matrix_strength > EntryThreshold) {
// Momentum validation passed.
// Market trend is considered sufficiently strong.
// -> Execute BUY or SELL orders here according
// to your EA's directional strategy.
// -> Alternatively, use this value to dynamically
// adjust Smart Grid spacing or other adaptive parameters.
}
}
Method 2: Calling via iCustom()
If you release the KCI Volatility Distance indicator .mq5 file separately and want the EA to read it through the standard indicator buffer.
Code snippet
int MatrixHandle;
input int MatrixPeriod = 14;
int OnInit() {
MatrixHandle = iCustom(_Symbol, _Period, "KCI Volatility Distance", MatrixPeriod);
if (MatrixHandle == INVALID_HANDLE) {
Print("Failed to load the KCI Volatility Distance indicator.");
return (INIT_FAILED);
}
return (INIT_SUCCEEDED);
}
void OnTick() {
double MatrixBuffer[];
ArraySetAsSeries(MatrixBuffer, true);
// Retrieve the two most recent values from Buffer 0 (KCI-VD Line)
if (CopyBuffer(MatrixHandle, 0, 0, 2, MatrixBuffer) <= 0) {
Print("Failed to copy matrix buffer data.");
return;
}
// Matrix value from the most recently closed candle
double current_matrix_strength = MatrixBuffer[1];
// Matrix value from the currently forming candle (real-time)
double previous_matrix_strength = MatrixBuffer[0];
// --- Trading Decision Logic ---
if (current_matrix_strength > previous_matrix_strength) {
// Momentum is expanding.
// -> Use this as an additional confirmation to
// increase the Take Profit distance.
//
// -> Alternatively, use it to trigger an adaptive
// Trailing Stop adjustment.
}
}
Given the highly adaptive and CPU-light architecture of the KCI Volatility Distance, its potential extends beyond traditional visual indicators. The flexibility of its Object-Oriented Programming (OOP)-based code allows it to serve as the engine for a variety of advanced algorithmic scenarios.
See how KCI Volatility Distance can be used in this indicator's Arrow Placement: KCI Arrow and on expert advisor EA KCI N-Matrix Engine
Here are some strategic implementations of the KCI Volatility Distance for various EA and complex indicator development purposes:
- Anti Stop-Hunting Defense System (Dynamic SL & TP)
- Adaptive Smart Grid & Averaging Algorithm
- Feature Normalization for Machine Learning (ML) Modules
- Multi-Symbol Scanner (Dashboard Indicator)
Example: Volatility Anomaly Detection (Anti Stop-Hunt Filter)
double matrix_value = MatrixEngine.Calculate(1, high, low, close);
// ATR serves as the baseline volatility benchmark
double atr_baseline = iATR(_Symbol, _Period, 14);
// Detect abnormal market expansion relative to the expected volatility range
if (matrix_value > (atr_baseline * 2.5)) {
Print("Warning: Volatility anomaly detected. Quantum SL Defense activated.");
// Protective actions:
// - Prevent opening new positions.
// - Dynamically widen the Stop Loss of existing positions
// to reduce the probability of stop-hunt events.
ModifySLToSafeZone(matrix_value);
}
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();
}
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
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[]);
};
Visual Indicator File (KCI_Volatility_Distance.mq5)
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
input int KineticPeriod = 14; // Calculation period (≥2)
double KVR_Buffer[];
CKCIVolatilityDistance kci_engine;
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);
}
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
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
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.
You may also like: Session Volatility Heatmap - excellent alternative for indicator users on MetaTrader 5.
Source Code
#property copyright "RobotFX"
#property link "https://robotfx.org"
#property description "This code is educational. Feel free to use, modify and upgrade as you wish. Visit ROBOTFX.ORG for professional trading tools"
#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
.......
⚠ Limitations & Risk Warning
- This tool is provided for educational and testing purposes only.
- Past performance does not guarantee future results.
- Trading involves substantial risk of loss. Use on a demo account first.
- Results may vary depending on market conditions, broker, and settings.
- We recommend thorough backtesting and forward testing before using with real funds.