ML SUPERTREND ULTIMATE -ML- QLearning +Per + LSTM + CNN
๐ค ML SuperTrend Ultimate: Deep Q-Learning + (Novier-Stokes xortex + mp4 expmls)
First fully-working LSTM + Deep Q-Network trading system implemented in Pine Script!
A self-learning trading agent that uses cutting-edge machine learning techniques to adapt to market conditions in real-time โ no external libraries, no Python, just pure Pine Script.
๐ฅ What Makes This Unique?
This is NOT just another indicator. This is a complete reinforcement learning system that:
- โ Learns from experience using Deep Q-Learning
- โ Remembers patterns with LSTM neural networks
- โ Adapts in real-time without retraining
- โ Prioritizes important data with PER (Prioritized Experience Replay)
- โ Works in your browser โ no GPU, no Python, no servers
Why It's Special
| Traditional Indicators | ML SuperTrend Ultimate | |----------------------|------------------------| | Static parameters | Learns optimal parameters | | Same for all markets | Adapts to each market | | Looks at 1-2 bars | Analyzes 8-20 bars history | | Simple rules | Deep neural networks | | No learning | Continuous learning |
โ ๏ธ DISCLAIMER
This is an experimental research project for educational purposes.
- NOT financial advice
- NO profit guarantees
- Use at your own risk
- Author bears NO responsibility for any losses
๐ What's Inside?
๐ง Deep Q-Network (DQN)
The "brain" that makes trading decisions.- 8 possible actions (ATR multipliers: 0.3 โ 1.5)
- 4-layer MLP (Multi-Layer Perceptron): 24 โ 16 โ 8 โ 4 neurons
- Q-values predict expected reward for each action
- Epsilon-greedy exploration (10% โ 2% decay)
๐ฎ LSTM Neural Network
Understands temporal patterns and market context.- 24 hidden units (configurable)
- Dynamic timesteps (8-20 bars, adapts to volatility)
- 4 gates: Forget, Input, Cell, Output
- Backpropagation Through Time (BPTT)
๐พ Prioritized Experience Replay (PER)
Smart memory that focuses on important lessons.- 70,000 state buffer (replay memory)
- Prioritized sampling based on TD-error
- Importance sampling for bias correction
- Beta annealing (0.4 โ 1.0)
๐ฏ Adam Optimizer
State-of-the-art optimization for neural networks.- Adaptive learning rate (starts at 0.01)
- Momentum + RMSprop combined
- Gradient clipping for stability
- Per-parameter learning rates
๐ Rich Feature Set
20+ features extracted from market data:- Technical: RSI, MACD, ATR, Stochastic
- Volume: OBV, Volume Rate of Change
- Advanced: Ichimoku, VWAP, Hurst proxy
- Volatility: Heidelberg index, ATR ratios
- Custom: NN confidence, entropy
๐ Architecture Overview
Market Data
โ
[Feature Extraction] โ 20 features
โ
[LSTM Layer] โ Temporal patterns (8-20 timesteps)
โ
[MLP Network] โ 24โ16โ8โ4 neurons
โ
[Q-Values] โ 8 actions (ATR multipliers)
โ
[Action Selection] โ Epsilon-greedy
โ
[SuperTrend] โ Adaptive coefficient
โ
Trading Signals
โ
[Reward] โ (close - entry) / episode_length
โ
[Experience Replay] โ Store in buffer (70k states)
โ
[PER Sampling] โ Prioritize high TD-error
โ
[Backpropagation] โ Update Q-network
โ
[LSTM BPTT] โ Update LSTM weights
โก Key Features
1. Real-Time Learning
- No pre-training needed
- Learns continuously as market evolves
- TD-Error-driven updates
2. Adaptive Parameters
- ATR multiplier: 0.3 - 1.5 (agent selects)
- LSTM timesteps: 8-20 (volatility-based)
- Learning rate: adaptive (0.001 - 0.05)
3. Advanced Techniques
- Priority Experience Replay (PER)
- Backpropagation Through Time (BPTT)
- Gradient clipping
- Adaptive Hinge Loss with L2 penalty
- Dual-kernel CNN filter
4. Robust Design
- Dropout (0.3) prevents overfitting
- L2 regularization (0.0008 MLP, 0.0003 LSTM)
- Leaky ReLU activation (no vanishing gradients)
- Epsilon decay (0.10 โ 0.02)
๐ฌ Technical Specifications
Reinforcement Learning Parameters
State Space: 20-dimensional vector (5 features ร 4 timesteps)
Action Space: 8 discrete actions [0.3, 0.4, 0.5, 0.7, 0.9, 1.0, 1.2, 1.5]
Reward Function: (close - entryprice) / episodelength
Discount Factor (ฮณ): 0.99
Epsilon: 0.10 โ 0.02 (decay: 0.999)
Training Frequency: Every 10 bars
Network Architecture
LSTM:
Hidden Size: 8 (default, configurable)
Timesteps: 8-20 (dynamic)
Gates: Forget, Input, Cell, Output
Activation: tanh (gates), sigmoid (cell)
MLP (DQN): Input: 20 features Layer 1: 24 neurons (Leaky ReLU) Layer 2: 16 neurons (Leaky ReLU) Layer 3: 8 neurons (Leaky ReLU) Layer 4: 4 neurons (Leaky ReLU) Output: 8 Q-values (linear)
Dropout: 0.3 L2 Lambda: 0.0008 (MLP), 0.0003 (LSTM)
Experience Replay
Buffer Size: 70,000 transitions
Batch Size: 6 samples
Priority Alpha (ฮฑ): 0.6
Priority Beta (ฮฒ): 0.4 โ 1.0 (annealing)
Priority Epsilon: 1e-5
Optimizer
Type: Adam
Learning Rate: 0.01 (adaptive: 0.001 - 0.05)
Beta1: 0.9 (momentum)
Beta2: 0.999 (RMSprop)
Epsilon: 1e-8
Gradient Clip: 1.0
๐ Quick Start
Installation (TradingView)
- Open TradingView
- Navigate to Pine Editor (bottom panel)
- Create new indicator
- Copy-paste code from
mlsupertrendultimate.pine - Click "Add to Chart"
First Run
- Initial training: Wait for 200-500 updates
- Monitor EMA Error: Should decrease over time
- Watch TD-Error: Convergence indicator
- Enable debug panel: See learning metrics
Recommended Settings
Timeframe: H1 (1 hour) or H4 (4 hours)
Asset: BTC, ETH, major forex pairs
History: At least 1000 bars for initial training
Auto Optimize: Enabled
Show Debug Panel: Enabled (while learning)
๐ Performance Metrics
The system tracks several metrics to show learning progress:
Training Metrics
- TD-Error: Should decrease from ~0.5 to <0.1
- EMA Error: Smoothed error, should converge
- Update Count: Number of gradient updates
- Epsilon: Exploration rate (10% โ 2%)
Q-Value Metrics
- Avg Max Q: Average of maximum Q-values
- Avg Old Q: Average of current Q-predictions
- Avg Target Q: Average of target Q-values
- Zero TD Count: How many samples have TD-error โ 0
Example Learning Curve
Updates 0-500:
TD-Error: 0.5 โ 0.3 (high, exploring)
EMA Error: 0.7 โ 0.5 (decreasing)
Epsilon: 0.10 โ 0.08 (still exploring)
Updates 500-2000: TD-Error: 0.3 โ 0.15 (converging) EMA Error: 0.5 โ 0.2 (good convergence) Epsilon: 0.08 โ 0.04 (exploitation phase)
Updates 2000+: TD-Error: 0.15 โ 0.05 (converged!) EMA Error: 0.2 โ 0.1 (stable) Epsilon: 0.04 โ 0.02 (minimal exploration)
๐ Educational Value
Perfect for learning:
- How LSTM networks work
- Deep Q-Learning implementation from scratch
- Reinforcement Learning for trading
- Neural network training (Adam, BPTT)
- Experience Replay and prioritization
- Advanced ML techniques in constrained environment
Code Structure
๐ Project Root
โโโ ๐ mlsupertrendultimate.pine (Main indicator)
โโโ ๐ README.md (This file)
โโโ ๐ LICENSE (MIT)
โโโ ๐ CHANGELOG.md (Version history)
โโโ ๐ docs/
โ โโโ ๐ ARCHITECTURE.md (Detailed architecture)
โ โโโ ๐ TRAINING.md (Training guide)
โ โโโ ๐ FAQ.md (Common questions)
โ โโโ ๐ RESEARCH.md (Research notes)
โโโ ๐ images/
โโโ ๐ผ๏ธ screenshot_1.png (Trading signals)
โโโ ๐ผ๏ธ screenshot_2.png (Debug panel)
โโโ ๐ผ๏ธ architecture.png (System diagram)
๐ค Contributing
Contributions are welcome! Please read CONTRIBUTING.md for guidelines.
Ways to Contribute
- ๐ Bug reports - Found an issue? Open an issue!
- ๐ก Feature requests - Have an idea? Share it!
- ๐ Documentation - Improve README, add examples
- ๐ง Code - Submit pull requests
- โญ Star the repo - Show your support!
Development
git clone https://github.com/YOUR_USERNAME/ml-supertrend-ultimate.git
cd ml-supertrend-ultimate
Edit mlsupertrendultimate.pine
Test on TradingView
Submit pull request
๐ References
This project implements techniques from cutting-edge research:
- Deep Q-Learning
- Prioritized Experience Replay
- LSTM Networks
- Adam Optimizer
๐ Contact & Support
- GitHub Issues: Report bugs or request features
- GitHub Discussions: Ask questions, share ideas
- Email: sail-com@mail.ru
โญ Show Your Support
If you find this project useful:
- โญ Star the repository
- ๐ Share with others
- ๐ Write about it
- ๐ค Contribute
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
MIT License
Copyright (c) 2025 [Diogenov Pavel]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
[Full MIT License text in LICENSE file]
๐ Acknowledgments
- Created with: Claude Sonnet 4.5 by Anthropic ๐ค
- Inspired by: DeepMind's DQN research
- Built in: Altai Krai, Barnaul, Russia ๐ท๐บ
- For: The trading & ML community ๐
๐ Roadmap
v1.0 (Current)
- โ LSTM + DQN implementation
- โ Prioritized Experience Replay
- โ Adam optimizer
- โ Real-time training
v1.1 (Planned)
- [ ] Multi-asset support
- [ ] Improved reward shaping
- [ ] Advanced visualization
- [ ] Performance analytics
v2.0 (Future)
- [ ] Dueling DQN architecture
- [ ] Double Q-Learning
- [ ] Rainbow DQN
- [ ] Attention mechanisms