OPEN-SOURCE SCRIPT

AetherEdge - Q-Learning Trendline Drawer

511
🖊️ Overview

A self-evolving auto-drawing engine that learns how to draw trendlines via tabular Q-Learning. It quantizes the market into a discrete state (trend posture, volatility regime, proximity to swing structure) and maps it through a state×action Q-table (a NeuraLib Q-head) to an optimal drawing action — anchor a support line, anchor a resistance line, steepen, flatten, or hold. When price breaks a line, the realized outcome feeds back as reward via Q(s,a) ← Q(s,a) + α[r + γ·maxₐ′Q(s′,a′) − Q(s,a)], so the agent itself learns which drawing behavior pays off in each market state. Every rendered line carries an expiry, with its learned strength (Q-value) reflected in color, width, and opacity.

🔶 Key Features

Tabular Q-Learning — a state×action Q-table held as a NeuraLib Q-head, updated by the Bellman rule
State quantization — trend, volatility, and proximity compressed into discrete bins for an interpretable finite state space
Self-evolving drawing policy — no prior knowledge; the optimal way to draw is learned from the live chart
Reward on break — realized post-break return becomes reward, with an extra reward when a line is respected (bounces)
AI-optimal trendlines — multiple optimal lines auto-generated and adjusted from learned strength
Expiry + strength encoding — each line has a lifespan; Q-derived strength drives color, width, and opacity; old lines fade
Exploration vs. exploitation — ε-greedy over drawing actions
Premium panel — the current state's Q-row (all action values), optimal action, policy decisiveness, and active/broken counts at a glance

🧠 Technical Architecture

The agent first quantizes the market into a discrete state. Three axes — trend posture (ATR-normalized fast/slow EMA gap), volatility regime (ATR ratio), and proximity to the nearest swing structure — are each split into a configurable number of bins and composed into a single state index. The state space is thus a finite trendBins × volBins × distBins, and the Q-table holds the value of every action for every state.
Each bar, the agent picks a drawing action via ε-greedy: with probability ε it explores (a random action); otherwise it exploits (the max-Q action for the current state). Once chosen, a trendline is constructed anchored on two swing pivots, with STEEPEN/FLATTEN actions biasing the slope. New lines too close to an existing one are suppressed to avoid clutter.
Learning is driven by break / respect events. When a line breaks, the lookahead return in the break direction accrues as reward; when price tests a line and respects it (bounces), a separate reward is added. On the next confirmed bar, the update Q(s,a) ← Q(s,a) + α[r + γ·maxₐ′Q(s′,a′) − Q(s,a)] is applied to the prior state-action. Each line's strength is the spawning Q-value squashed logistically to [0,1], driving color saturation, line width (strong lines are thicker), and opacity. As lifecycle management, expired and long-broken lines retire, and when over the cap the weakest (lowest-strength) line is pruned.

⚙️ Recommended Settings & Tuning Guide

BTC (1H–4H): Training Horizon 800–1500, α 0.2–0.3, γ 0.9, ε 0.08, Swing Sensitivity 8. State bins of 3 each give a standard granularity
ETH (1H–4H): As BTC, with Reward Lookahead 5–8 to value post-break follow-through
SOL (15m–1H): High volatility favors Volatility Bins 4 for finer regime classification, Break Buffer 0.1–0.15 to absorb fakeouts, Min Separation ≈ 0.8
XRP (1H–4H): Spike-prone; α ≈ 0.3 to learn fast, Respect Reward 0.4 to favor strong lines that bounce
Learning Rate α: higher (0.3–0.5) learns faster but values are noisier; lower (0.1–0.2) is stable but slower
State bins: more bins raise granularity but enlarge the state space and slow learning — balance against Training Horizon
ε (exploration): ~0.1 when studying drawing behavior; ~0.05 in live use to stabilize the policy

💡 How to Use in Practice

Reacting to AI-optimal lines: thick, saturated lines = high Q (high strength) = levels the agent learned matter. A reliable basis for S/R judgments
Reading strength (color/width): the thicker and more opaque a line, the higher its learned strength; treat faint, thin lines as tentative
Using breaks: on a break the line recolors (cyan up / gold down) and thickens; breaks of higher-strength lines matter more
Using respects (bounces): when price approaches a strong line and bounces, it marks a pullback/throwback entry in that line's direction
Multi-timeframe usage: map the major high-strength lines on the higher timeframe (4H), then time breaks/bounces on the lower one (15m–1H)
Combinations: pair with volume or horizontal S/R, filtering for high-strength lines to lift precision

⚠️ Important Notes

Initial learning period: right after launch the Q-table is near-zero and the drawing policy is immature; treat line strength as low-confidence until enough state visits accumulate (several hundred bars)
Learning resets: changing parameters, switching symbol/timeframe, or recompiling reinitializes the Q-table and the line set, restarting learning from zero
State-space constraint: more bins mean more unvisited states, whose lines rely on near-zero initial Q and thus show lower strength
On forward-looking reward: rewards use confirmed post-break lookahead returns (a standard RL training construct); current-bar drawing is on confirmed values, but as with any adaptive system, historical and live behavior can differ — always forward-test
Constraints: this is a lightweight tabular implementation operating within Pine's compute budget

🚨 Disclaimer

This indicator is an analytical and educational visualization tool. The Q-Learning, Q-table, reward shaping, trendline generation, and strength 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.

Thông báo miễn trừ trách nhiệm

Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.