PROTECTED SOURCE SCRIPT
Eccodax Robust k-NN Machine Learning Lorentzian

Here is the complete, final, corrected, and clean code, already including:
✅ Fixed shadowing of the variable d
✅ No compilation warnings
✅ No temporal leaks
✅ Target = real future return
✅ Robust Lorentzian distance
✅ Correct Matrix structure
✅ Consistent feature engineering
✅ Min-Max normalization
✅ Weighted k-NN inference
✅ Correct price reconstruction
1. What this code is
It is a predictive indicator based on classic Machine Learning (k-Nearest Neighbors), fully implemented in PineScript v6, designed to:
Learn historical market patterns
Compare the current state with similar past states
Estimate the expected future price movement
Reconstruct a projected price consistent with the current level
It is not an oscillator, it is not a traditional technical indicator, and it does not react only to the immediate past.
2. What the Model Learns (Supervised Learning)
2.1 Features (Input Variables)
The model uses three dimensions of information, all normalized by Z-score:
Return
Measures the percentage change in price
Captures the immediate momentum of the market
Momentum (ROC)
Measures acceleration or deceleration of the movement
Differentiates trends from consolidations
Volatility
Measures the degree of market uncertainty
Adjusts the weight of strong movements vs. noise
These three variables form a market state vector.
2.2 Normalization (Z-Score)
Each feature is converted to:
Mean ≈ 0
Standard deviation ≈ 1
This ensures that:
No variable dominates the distance
The statistical comparison is valid
The model is stable in different price regimes
2.3 Target (Predicted Variable)
The model does not predict absolute price. It learns:
Observed future return after forecastBars
That is:
Learns movement, not level
Eliminates historical bias
Avoids predictions inconsistent with the current price
3. How the model makes the prediction
3.1 Search for similar patterns (k-NN)
For each current candle, the model:
Analyzes the last lookback candles
Calculates the Euclidean distance between the current state and each past state
Selects the k most similar states
Observes what happened after them
3.2 Inference
The predicted return is calculated as:
Weighted average of the future returns of the neighbors
Weights inversely proportional to the distance
More similar states → greater influence.
4. Price Reconstruction (Key Information)
From the predicted return, the model reconstructs:
Predicted Price = Current Close × (1 + Predicted Return)
Predicted Price = Current Close × (1 + Predicted Return)
This ensures that:
The forecast respects the current market level
The output is visually interpretable
There is no regression to past regimes
5. Relevant Information the Indicator Delivers
5.1 Predicted Price (Green Line)
What it is: Estimated price after forecastBars.
How to use:
Above the current price → bullish bias
Below → bearish bias
Large distance → expectation of strong movement
5.2 Predicted Return (Implicit)
Even though not plotted directly, it is the most important information in the model.
Positive → expectation of appreciation
Negative → expectation of decline
Negative → expectation of decline
Near zero → sideways market
5.3 Directional Classification (optional)
The model also acts as a binary classifier:
High if expected return > 0
Low if expected return < 0
This is used as:
Noise filter
Trend confirmation
False signal reduction
5.4 Implicit statistical context
The indicator carries information that is not visual, but is fundamental:
Market regime (trending vs. sideways)
Statistical similarity with the past
Relative confidence (via distance from neighbors)
6. What this indicator does NOT do
It is important to align expectations:
❌ Does not predict exogenous events
❌ Does not anticipate gaps
❌ Does not work well on illiquid assets
❌ Does not extrapolate long trends
k-NN replicates patterns, does not create scenarios Unprecedented.
7. Where this model works best
Markets with repetitive structure
Medium timeframes (5m – 1D)
Liquid assets
Environments with alternating regimes
8. How to use it in practice (professional recommendation)
Ideal use:
k-NN direction → bias
Technical indicator → timing
Risk management → execution
Never use it in isolation for entry.
9. Executive summary
This code delivers:
A functional supervised ML model in Pine
Prediction consistent with the current price
Statistical market direction
Reduction of historical bias
Solid foundation for quantitative strategies
✅ Fixed shadowing of the variable d
✅ No compilation warnings
✅ No temporal leaks
✅ Target = real future return
✅ Robust Lorentzian distance
✅ Correct Matrix structure
✅ Consistent feature engineering
✅ Min-Max normalization
✅ Weighted k-NN inference
✅ Correct price reconstruction
1. What this code is
It is a predictive indicator based on classic Machine Learning (k-Nearest Neighbors), fully implemented in PineScript v6, designed to:
Learn historical market patterns
Compare the current state with similar past states
Estimate the expected future price movement
Reconstruct a projected price consistent with the current level
It is not an oscillator, it is not a traditional technical indicator, and it does not react only to the immediate past.
2. What the Model Learns (Supervised Learning)
2.1 Features (Input Variables)
The model uses three dimensions of information, all normalized by Z-score:
Return
Measures the percentage change in price
Captures the immediate momentum of the market
Momentum (ROC)
Measures acceleration or deceleration of the movement
Differentiates trends from consolidations
Volatility
Measures the degree of market uncertainty
Adjusts the weight of strong movements vs. noise
These three variables form a market state vector.
2.2 Normalization (Z-Score)
Each feature is converted to:
Mean ≈ 0
Standard deviation ≈ 1
This ensures that:
No variable dominates the distance
The statistical comparison is valid
The model is stable in different price regimes
2.3 Target (Predicted Variable)
The model does not predict absolute price. It learns:
Observed future return after forecastBars
That is:
Learns movement, not level
Eliminates historical bias
Avoids predictions inconsistent with the current price
3. How the model makes the prediction
3.1 Search for similar patterns (k-NN)
For each current candle, the model:
Analyzes the last lookback candles
Calculates the Euclidean distance between the current state and each past state
Selects the k most similar states
Observes what happened after them
3.2 Inference
The predicted return is calculated as:
Weighted average of the future returns of the neighbors
Weights inversely proportional to the distance
More similar states → greater influence.
4. Price Reconstruction (Key Information)
From the predicted return, the model reconstructs:
Predicted Price = Current Close × (1 + Predicted Return)
Predicted Price = Current Close × (1 + Predicted Return)
This ensures that:
The forecast respects the current market level
The output is visually interpretable
There is no regression to past regimes
5. Relevant Information the Indicator Delivers
5.1 Predicted Price (Green Line)
What it is: Estimated price after forecastBars.
How to use:
Above the current price → bullish bias
Below → bearish bias
Large distance → expectation of strong movement
5.2 Predicted Return (Implicit)
Even though not plotted directly, it is the most important information in the model.
Positive → expectation of appreciation
Negative → expectation of decline
Negative → expectation of decline
Near zero → sideways market
5.3 Directional Classification (optional)
The model also acts as a binary classifier:
High if expected return > 0
Low if expected return < 0
This is used as:
Noise filter
Trend confirmation
False signal reduction
5.4 Implicit statistical context
The indicator carries information that is not visual, but is fundamental:
Market regime (trending vs. sideways)
Statistical similarity with the past
Relative confidence (via distance from neighbors)
6. What this indicator does NOT do
It is important to align expectations:
❌ Does not predict exogenous events
❌ Does not anticipate gaps
❌ Does not work well on illiquid assets
❌ Does not extrapolate long trends
k-NN replicates patterns, does not create scenarios Unprecedented.
7. Where this model works best
Markets with repetitive structure
Medium timeframes (5m – 1D)
Liquid assets
Environments with alternating regimes
8. How to use it in practice (professional recommendation)
Ideal use:
k-NN direction → bias
Technical indicator → timing
Risk management → execution
Never use it in isolation for entry.
9. Executive summary
This code delivers:
A functional supervised ML model in Pine
Prediction consistent with the current price
Statistical market direction
Reduction of historical bias
Solid foundation for quantitative strategies
Protected script
This script is published as closed-source. However, you can use it freely and without any limitations – learn more here.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Protected script
This script is published as closed-source. However, you can use it freely and without any limitations – learn more here.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.