OPEN-SOURCE SCRIPT
AetherEdge - KNN-Augmented RL Hybrid

🖊️ Overview
A self-evolving hybrid that fuses episodic memory with a learned value network. Each bar's state and its realized outcome are stored in an experience memory bank; to value the present, the agent runs a k-Nearest-Neighbors search over that bank — retrieving the most similar historical states and distance-weighting their outcomes — then blends that memory estimate with a NeuraLib NN/Q estimate. The fusion combines the generalization of gradient learning with the instant recall of episodic memory. A reward-driven update adapts the per-feature similarity weights, so the agent learns which dimensions of "similarity" actually matter for prediction. Visualization: auto breakout-zone boxes where high-similarity memory predicts a decisive move, plus directional arrows on the fused signal.
🔶 Key Features
kNN×RL hybrid — episodic-memory kNN retrieval fused with a NeuraLib value network
Episodic memory augmentation — instant distance-weighted recall of similar past outcomes
Memory ↔ network blend — continuously adjustable from pure memory (kNN) to pure network (NN/Q)
Reward-driven similarity-weight learning — learns which feature dimensions define "similar"
High-similarity breakout zones — zones spawn only when memory retrieval is confident; opacity tracks similarity
Fused signal arrows — the integrated kNN+NN verdict shown as directional arrows (with glow)
Exploration vs. exploitation — ε-greedy exploration
Premium panel — NN-vs-kNN breakdown, fused value, memory similarity, conviction, similarity weights, memory/zone counts
🧠 Technical Architecture
The agent perceives the market as a four-dimensional state vector (momentum spread, RSI deviation, a volatility-regime ratio, position-within-range — all z-normalized). The architecture fuses two value-estimation paths.
Episodic memory (kNN) path: each bar stores state, action taken, and realized value (reward) in a memory bank. To value the present, a weighted Euclidean distance — using adaptive per-feature weights — is computed against all memories, and the k nearest are retrieved. Each neighbor's distance is turned into similarity via a Gaussian-style kernel sim = exp(−sharpness × distance), and a per-action similarity-weighted average yields the kNN memory value.
Value network (NeuraLib) path: a single hidden-layer (tanh) network maps state to per-action Q-values, trained by a DQN-style update.
Fusion: per direction, fused = (1−blend)·Q_network + blend·Q_knn, and the action with the largest fused value is selected. The signature element is similarity-weight adaptation: on each reward, weights of feature dimensions consistent with the profitable outcome are raised and irrelevant ones lowered (w ← clip(w + lr·(|feature|·|reward| − baseline))). The very definition of "similar" thus evolves from reward. Zone spawning fires only when both memory similarity and conviction clear their gates, with box opacity tracking similarity.
⚙️ Recommended Settings & Tuning Guide
BTC (1H–4H): Training Horizon 800–1500, Neighbors k 6, Memory Depth 300, Blend 0.5, γ 0.93. A 50/50 memory-network fusion as standard
ETH (1H–4H): As BTC, with Reward Lookahead 5–8
SOL (15m–1H): High volatility favors Similarity Sharpness 2.5–3.0 to weight only close neighbors, Min Similarity 0.7 to be selective on zones
XRP (1H–4H): Spike-prone; Blend ≈ 0.6 to lean on memory (recalling past sharp moves), k ≈ 8
Memory ↔ Network Blend: toward 0 favors generalization (robust to novel states); toward 1 favors memory (replays known patterns) — tune to market repetitiveness
Neighbors (k): small (3–5) is sharp but noisy; large (8–15) is smooth but less responsive
Similarity Weight LR: higher adapts feature weights faster but less stably; 0.01–0.03 is the stable zone
Memory Bank Depth: deeper retains more diverse memory but mixes in older regimes — balance against Training Horizon
💡 How to Use in Practice
Reacting to fused arrows: an arrow means both kNN memory and NN value back the direction and conviction cleared the gate — the integrated verdict, a core basis for trend-following entries
Using high-similarity zones: zones mark areas where memory shows a decisive move occurred in similar past conditions; watch breaks/bounces at zone edges
Reading memory similarity: higher panel similarity means the present closely matches a clear past pattern — higher prediction confidence; be cautious when low (novel conditions)
NN vs. kNN agreement: when both (e.g. the "NN / kNN" in the LONG row) are high together, confidence is highest; stand aside when they disagree
Watching similarity weights: the panel's Sim Weights (M·R·V·P = momentum, RSI, volatility, position) reveal which features the agent currently weights for similarity
Multi-timeframe usage: map high-similarity zones on the higher timeframe (4H), then refine timing on fused arrows on the lower one (15m–1H)
Combinations: filter for high-similarity, high-conviction arrows to focus on memory-backed, high-quality entries
⚠️ Important Notes
Initial learning period: right after launch the memory bank is nearly empty and kNN retrieval is non-functional; treat signals and zones as low-confidence until memory accumulates (several hundred bars)
Learning resets: changing parameters, switching symbol/timeframe, or recompiling reinitializes the memory bank, network weights, similarity weights, and zones, restarting learning from zero
Memory bias: the bank is built from recent states, so when the regime shifts substantially, past memory fits the present less well — low Memory Similarity is the tell
On forward-looking reward: rewards and memory values use closed-bar forward returns (a standard RL training construct); current-bar evaluation is on confirmed values, but as with any adaptive system, historical and live behavior can differ — always forward-test
Constraints: this is a lightweight implementation operating within Pine's compute budget; kNN search scales with memory depth × neighbors, so extreme settings affect performance
🚨 Disclaimer
This indicator is an analytical and educational visualization tool. The kNN search, value network, reward shaping, similarity-weight adaptation, breakout zones, and fused signal are quantitative heuristics computed on-chart from price data — they are not financial advice, buy/sell signals, or any guarantee of future performance. Reinforcement-learning agents can and do make wrong calls. Always combine any tool with your own analysis and disciplined risk management.
A self-evolving hybrid that fuses episodic memory with a learned value network. Each bar's state and its realized outcome are stored in an experience memory bank; to value the present, the agent runs a k-Nearest-Neighbors search over that bank — retrieving the most similar historical states and distance-weighting their outcomes — then blends that memory estimate with a NeuraLib NN/Q estimate. The fusion combines the generalization of gradient learning with the instant recall of episodic memory. A reward-driven update adapts the per-feature similarity weights, so the agent learns which dimensions of "similarity" actually matter for prediction. Visualization: auto breakout-zone boxes where high-similarity memory predicts a decisive move, plus directional arrows on the fused signal.
🔶 Key Features
kNN×RL hybrid — episodic-memory kNN retrieval fused with a NeuraLib value network
Episodic memory augmentation — instant distance-weighted recall of similar past outcomes
Memory ↔ network blend — continuously adjustable from pure memory (kNN) to pure network (NN/Q)
Reward-driven similarity-weight learning — learns which feature dimensions define "similar"
High-similarity breakout zones — zones spawn only when memory retrieval is confident; opacity tracks similarity
Fused signal arrows — the integrated kNN+NN verdict shown as directional arrows (with glow)
Exploration vs. exploitation — ε-greedy exploration
Premium panel — NN-vs-kNN breakdown, fused value, memory similarity, conviction, similarity weights, memory/zone counts
🧠 Technical Architecture
The agent perceives the market as a four-dimensional state vector (momentum spread, RSI deviation, a volatility-regime ratio, position-within-range — all z-normalized). The architecture fuses two value-estimation paths.
Episodic memory (kNN) path: each bar stores state, action taken, and realized value (reward) in a memory bank. To value the present, a weighted Euclidean distance — using adaptive per-feature weights — is computed against all memories, and the k nearest are retrieved. Each neighbor's distance is turned into similarity via a Gaussian-style kernel sim = exp(−sharpness × distance), and a per-action similarity-weighted average yields the kNN memory value.
Value network (NeuraLib) path: a single hidden-layer (tanh) network maps state to per-action Q-values, trained by a DQN-style update.
Fusion: per direction, fused = (1−blend)·Q_network + blend·Q_knn, and the action with the largest fused value is selected. The signature element is similarity-weight adaptation: on each reward, weights of feature dimensions consistent with the profitable outcome are raised and irrelevant ones lowered (w ← clip(w + lr·(|feature|·|reward| − baseline))). The very definition of "similar" thus evolves from reward. Zone spawning fires only when both memory similarity and conviction clear their gates, with box opacity tracking similarity.
⚙️ Recommended Settings & Tuning Guide
BTC (1H–4H): Training Horizon 800–1500, Neighbors k 6, Memory Depth 300, Blend 0.5, γ 0.93. A 50/50 memory-network fusion as standard
ETH (1H–4H): As BTC, with Reward Lookahead 5–8
SOL (15m–1H): High volatility favors Similarity Sharpness 2.5–3.0 to weight only close neighbors, Min Similarity 0.7 to be selective on zones
XRP (1H–4H): Spike-prone; Blend ≈ 0.6 to lean on memory (recalling past sharp moves), k ≈ 8
Memory ↔ Network Blend: toward 0 favors generalization (robust to novel states); toward 1 favors memory (replays known patterns) — tune to market repetitiveness
Neighbors (k): small (3–5) is sharp but noisy; large (8–15) is smooth but less responsive
Similarity Weight LR: higher adapts feature weights faster but less stably; 0.01–0.03 is the stable zone
Memory Bank Depth: deeper retains more diverse memory but mixes in older regimes — balance against Training Horizon
💡 How to Use in Practice
Reacting to fused arrows: an arrow means both kNN memory and NN value back the direction and conviction cleared the gate — the integrated verdict, a core basis for trend-following entries
Using high-similarity zones: zones mark areas where memory shows a decisive move occurred in similar past conditions; watch breaks/bounces at zone edges
Reading memory similarity: higher panel similarity means the present closely matches a clear past pattern — higher prediction confidence; be cautious when low (novel conditions)
NN vs. kNN agreement: when both (e.g. the "NN / kNN" in the LONG row) are high together, confidence is highest; stand aside when they disagree
Watching similarity weights: the panel's Sim Weights (M·R·V·P = momentum, RSI, volatility, position) reveal which features the agent currently weights for similarity
Multi-timeframe usage: map high-similarity zones on the higher timeframe (4H), then refine timing on fused arrows on the lower one (15m–1H)
Combinations: filter for high-similarity, high-conviction arrows to focus on memory-backed, high-quality entries
⚠️ Important Notes
Initial learning period: right after launch the memory bank is nearly empty and kNN retrieval is non-functional; treat signals and zones as low-confidence until memory accumulates (several hundred bars)
Learning resets: changing parameters, switching symbol/timeframe, or recompiling reinitializes the memory bank, network weights, similarity weights, and zones, restarting learning from zero
Memory bias: the bank is built from recent states, so when the regime shifts substantially, past memory fits the present less well — low Memory Similarity is the tell
On forward-looking reward: rewards and memory values use closed-bar forward returns (a standard RL training construct); current-bar evaluation is on confirmed values, but as with any adaptive system, historical and live behavior can differ — always forward-test
Constraints: this is a lightweight implementation operating within Pine's compute budget; kNN search scales with memory depth × neighbors, so extreme settings affect performance
🚨 Disclaimer
This indicator is an analytical and educational visualization tool. The kNN search, value network, reward shaping, similarity-weight adaptation, breakout zones, and fused signal are quantitative heuristics computed on-chart from price data — they are not financial advice, buy/sell signals, or any guarantee of future performance. Reinforcement-learning agents can and do make wrong calls. Always combine any tool with your own analysis and disciplined risk management.
סקריפט קוד פתוח
ברוח האמיתית של TradingView, יוצר הסקריפט הזה הפך אותו לקוד פתוח, כך שסוחרים יוכלו לעיין בו ולאמת את פעולתו. כל הכבוד למחבר! אמנם ניתן להשתמש בו בחינם, אך זכור כי פרסום חוזר של הקוד כפוף ל־כללי הבית שלנו.
כתב ויתור
המידע והפרסומים אינם מיועדים להיות, ואינם מהווים, ייעוץ או המלצה פיננסית, השקעתית, מסחרית או מכל סוג אחר המסופקת או מאושרת על ידי TradingView. קרא עוד ב־תנאי השימוש.
סקריפט קוד פתוח
ברוח האמיתית של TradingView, יוצר הסקריפט הזה הפך אותו לקוד פתוח, כך שסוחרים יוכלו לעיין בו ולאמת את פעולתו. כל הכבוד למחבר! אמנם ניתן להשתמש בו בחינם, אך זכור כי פרסום חוזר של הקוד כפוף ל־כללי הבית שלנו.
כתב ויתור
המידע והפרסומים אינם מיועדים להיות, ואינם מהווים, ייעוץ או המלצה פיננסית, השקעתית, מסחרית או מכל סוג אחר המסופקת או מאושרת על ידי TradingView. קרא עוד ב־תנאי השימוש.