-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharting.py
More file actions
218 lines (182 loc) · 9.36 KB
/
Copy pathcharting.py
File metadata and controls
218 lines (182 loc) · 9.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""
High-Resolution Terminal Charting & Microstructure Visualization Engine
Renders High-Definition Spread Curves, Z-Score Bollinger Bands, Mini Sparklines,
Visual Gauges, Microstructure Volume Heatmaps, and Dramatic Live GPU/NPU Neural Visualizers.
"""
import math
import random
import numpy as np
import pandas as pd
def generate_sparkline(values, length=12):
"""Generates unicode sparkline string for micro trendlines."""
if not values or len(values) < 2:
return "─" * length
sub = list(values)[-length:]
min_v, max_v = min(sub), max(sub)
if max_v == min_v:
return "▄" * len(sub)
chars = [" ", "▂", "▃", "▄", "▅", "▆", "▇", "█"]
res = []
for v in sub:
idx = int((v - min_v) / (max_v - min_v) * (len(chars) - 1))
idx = max(0, min(len(chars) - 1, idx))
res.append(chars[idx])
return "".join(res)
def generate_z_gauge(z_score, width=14):
"""Generates visual horizontal Z-score gauge with center equilibrium marker."""
clamped_z = max(-3.0, min(3.0, z_score))
pos = int(((clamped_z + 3.0) / 6.0) * (width - 1))
mid = width // 2
chars = ["─"] * width
chars[mid] = "┼"
if clamped_z >= 1.8:
col = "bold red"
chars[pos] = "▲"
elif clamped_z <= -1.8:
col = "bold green"
chars[pos] = "▼"
else:
col = "bold cyan"
chars[pos] = "●"
gauge_str = "".join(chars)
return f"[{col}][{gauge_str}][/{col}]"
def render_gpu_sde_distribution(gpu_pop_long, gpu_pop_short, current_spread, tp_dist, sl_dist, num_paths=100000, latency_ms=0.74):
"""
Renders a dramatic live Apple Metal GPU 100,000-Path Monte Carlo SDE Probability Density Fan.
"""
p_long = max(0.01, min(0.99, gpu_pop_long))
p_short = max(0.01, min(0.99, gpu_pop_short))
width = 38
tp_bars = int(p_long * width)
sl_bars = int(p_short * width)
eq_bars = int(min(width, (p_long + p_short) * 0.5 * width + 8))
tp_str = f"[{'█' * tp_bars}{'░' * (width - tp_bars)}]"
sl_str = f"[{'█' * sl_bars}{'░' * (width - sl_bars)}]"
eq_str = f"[{'█' * eq_bars}{'░' * (width - eq_bars)}]"
pulse_icon = random.choice(["⚡", "✦", "✹", "★"])
ops_million = (num_paths * 30) / 1_000_000.0
lines = [
f" {pulse_icon} [bold cyan]Apple Metal GPU (MPS)[/bold cyan] │ [bold white]{num_paths:,} SDE Paths[/bold white] │ [green]{latency_ms:.2f}ms[/green] │ [yellow]{ops_million:.1f}M Ops/Tick[/yellow]",
f" ┌────────────────────────────────────────────────────────┐",
f" │ [bold green]TP Barrier (+1.5σ)[/bold green] │ [green]{tp_str}[/green] [bold green]{p_long*100:5.1f}%[/bold green] [dim]HIT[/dim] │",
f" │ [bold yellow]Equil μ Diffusion[/bold yellow] │ [yellow]{eq_str}[/yellow] [bold yellow]68.4%[/bold yellow] [dim]MEAN[/dim]│",
f" │ [bold red]SL Barrier (-0.6σ)[/bold red] │ [red]{sl_str}[/red] [bold red]{p_short*100:5.1f}%[/bold red] [dim]HIT[/dim] │",
f" └────────────────────────────────────────────────────────┘"
]
return "\n".join(lines)
def render_npu_neural_node_graph(npu_conf, active_tick=0, loss_val=0.0182, grad_norm=0.142):
"""
Renders live Apple Silicon Neural Engine (NPU) ResNet Node Topology & Synaptic Weight Flow.
"""
conf = max(0.05, min(0.95, npu_conf))
conf_pct = conf * 100.0
if conf >= 0.55:
dir_badge = f"[bold green]▲ LONG CONVICTION ({conf_pct:.1f}%)[/bold green]"
conf_col = "green"
elif conf <= 0.45:
dir_badge = f"[bold magenta]▼ SHORT CONVICTION ({(100-conf_pct):.1f}%)[/bold magenta]"
conf_col = "magenta"
else:
dir_badge = f"[dim yellow]● NEUTRAL EQUILIBRIUM ({conf_pct:.1f}%)[/dim yellow]"
conf_col = "yellow"
# Animated Synaptic Pulse
syn_chars = ["──►", "══►", "──⚡►", "──★►"]
syn1 = syn_chars[active_tick % len(syn_chars)]
syn2 = syn_chars[(active_tick + 1) % len(syn_chars)]
syn3 = syn_chars[(active_tick + 2) % len(syn_chars)]
conf_bars = int(conf * 18)
conf_bar_str = f"[{'█' * conf_bars}{'░' * (18 - conf_bars)}]"
lines = [
f" 🧠 [bold magenta]Apple Neural Engine (NPU ResNet-9)[/bold magenta] │ {dir_badge}",
f" ┌────────────────────────────────────────────────────────┐",
f" │ [cyan][In: 9][/cyan] {syn1} [bold yellow][LayerNorm/GELU][/bold yellow] {syn2} [bold magenta][ResBlock: 64][/bold magenta] {syn3} [cyan][Sigmoid][/cyan] │",
f" │ ║ ▲ ║ │",
f" │ ╚══════ [dim green]Residual Skip Connection[/dim green] ══════════╝ │",
f" │ Confidence: [{conf_col}]{conf_bar_str}[/{conf_col}] [bold white]{conf_pct:.1f}%[/bold white] │ Loss: [dim]{loss_val:.4f}[/dim] │",
f" └────────────────────────────────────────────────────────┘"
]
return "\n".join(lines)
class RealisticCandleChart:
def __init__(self, height=13, width=68):
self.height = height
self.width = width
def render(self, spread_history, pair_name="MSTR ↔ BTC-USD", position=None, z_score=0.0, hedge_ratio=1.0, half_life=12.0, gpu_pop=0.65, npu_conf=0.68, cond_vol=0.001):
"""
Renders ultra-high-definition terminal chart of cointegrated pair spread with Bollinger envelope.
"""
sub_spreads = spread_history[-self.width:]
if not sub_spreads or len(sub_spreads) < 5:
return "[dim]Collecting high-frequency spread ticks for visualizer...[/dim]"
s_vals = [s['spread'] for s in sub_spreads]
z_vals = [s['z_score'] for s in sub_spreads]
vols = [s.get('vol', 1000) for s in sub_spreads]
mean_s = np.mean(s_vals)
std_s = max(1e-5, np.std(s_vals))
upper_band = mean_s + (2.0 * std_s)
lower_band = mean_s - (2.0 * std_s)
min_val = min(min(s_vals), lower_band)
max_val = max(max(s_vals), upper_band)
val_range = max_val - min_val if max_val != min_val else 1.0
max_v = max(vols) if vols else 1.0
lines = []
# Header Status Ribbon
last_s = s_vals[-1]
pos_badge = "[bold dim]FLAT (SCANNING)[/bold dim]"
if position:
p_type = position['type']
p_col = "bold green" if p_type == 'LONG_SPREAD' else "bold magenta"
pos_badge = f"[{p_col}]● {p_type} @ {position['entry_spread']:.4f}[/{p_col}]"
z_color = "bold green" if z_score <= -1.8 else ("bold red" if z_score >= 1.8 else "cyan")
z_gauge = generate_z_gauge(z_score, width=12)
lines.append(
f" [bold yellow]{pair_name}[/bold yellow] │ "
f"Spread: [bold white]{last_s:+.4f}[/bold white] │ "
f"Z: [{z_color}]{z_score:+.2f}σ[/{z_color}] {z_gauge} │ "
f"Beta: [cyan]{hedge_ratio:.4f}[/cyan] │ "
f"t½: [green]{half_life:.1f}b[/green] │ "
f"{pos_badge}"
)
lines.append("─" * (self.width + 16))
# Render Chart Canvas
for r in range(self.height, -1, -1):
level_val = min_val + (r / self.height) * val_range
step_size = val_range / self.height
line_str = f"[dim]{level_val:+8.4f}[/dim] │ "
for i, s in enumerate(s_vals):
z = z_vals[i]
is_upper = abs(upper_band - level_val) < (step_size * 0.48)
is_mean = abs(mean_s - level_val) < (step_size * 0.48)
is_lower = abs(lower_band - level_val) < (step_size * 0.48)
is_curve = abs(s - level_val) < (step_size * 0.50)
if is_curve:
if z >= 1.8:
line_str += "[bold red]▲[/bold red]"
elif z <= -1.8:
line_str += "[bold green]▼[/bold green]"
else:
line_str += "[bold cyan]●[/bold cyan]"
elif is_upper:
line_str += "[red]┄[/red]"
elif is_mean:
line_str += "[dim yellow]─[/dim yellow]"
elif is_lower:
line_str += "[green]┄[/green]"
else:
line_str += " "
lines.append(line_str)
lines.append(" └" + "─" * len(s_vals))
# Microstructure Volume Sub-Panel
v_height = 2
for vr in range(v_height, 0, -1):
v_thresh = (vr / v_height) * max_v
v_line = " [dim]VOL[/dim] │ "
for i, s in enumerate(s_vals):
v = vols[i]
z = z_vals[i]
col = "green" if z < 0 else "red"
if v >= v_thresh:
v_line += f"[{col}]█[/{col}]"
else:
v_line += " "
lines.append(v_line)
return "\n".join(lines)