heisbuba commited on
Commit
99818f1
·
verified ·
1 Parent(s): 56a5c60

Create deep_diver.js

Browse files
Files changed (1) hide show
  1. src/static/js/deep_diver.js +286 -0
src/static/js/deep_diver.js ADDED
@@ -0,0 +1,286 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const input = document.getElementById('token-input');
2
+ const results = document.getElementById('search-results');
3
+ const matrix = document.getElementById('matrix-content');
4
+ const skeleton = document.getElementById('skeleton-container');
5
+ let searchTimeout = null;
6
+ let currentCoinData = null;
7
+
8
+ // Search Logic
9
+ input.addEventListener('input', (e) => {
10
+ clearTimeout(searchTimeout);
11
+ const q = e.target.value.trim();
12
+
13
+ if(q.length < 3) {
14
+ results.style.display = 'none';
15
+ results.innerHTML = '';
16
+ return;
17
+ }
18
+
19
+ searchTimeout = setTimeout(async () => {
20
+ try {
21
+ const res = await fetch(`/api/search-tickers?q=${encodeURIComponent(q)}`);
22
+ const data = await res.json();
23
+ if(data && data.length > 0) {
24
+ results.innerHTML = data.map(t => `
25
+ <div class="res-item" data-id="${escapeHtml(t.id)}" data-symbol="${escapeHtml(t.symbol)}">
26
+ <img src="${escapeHtml(t.thumb)}" alt="${escapeHtml(t.symbol)}" loading="lazy">
27
+ <div style="flex-grow:1; display:flex; align-items:center;">
28
+ <span style="font-weight:bold; color:var(--text-main);">${escapeHtml(t.symbol.toUpperCase())}</span>
29
+ <span class="res-name">${escapeHtml(t.name)}</span>
30
+ </div>
31
+ </div>
32
+ `).join('');
33
+ results.querySelectorAll('.res-item').forEach(el => {
34
+ el.addEventListener('click', () => dive(el.dataset.id, el.dataset.symbol));
35
+ });
36
+ results.style.display = 'block';
37
+ } else {
38
+ results.innerHTML = '<div class="res-item" style="cursor:default; color:var(--text-dim);">No results found</div>';
39
+ results.style.display = 'block';
40
+ }
41
+ } catch (e) {
42
+ console.error(e);
43
+ results.style.display = 'none';
44
+ }
45
+ }, 600);
46
+ });
47
+
48
+ // Hide dropdown on outside click
49
+ document.addEventListener('click', (e) => {
50
+ if (!e.target.closest('.search-container')) {
51
+ results.style.display = 'none';
52
+ }
53
+ });
54
+
55
+ // Main Data Fetch (DIVE)
56
+ const ESC_MAP = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
57
+ function escapeHtml(str) {
58
+ return String(str).replace(/[&<>"']/g, c => ESC_MAP[c]);
59
+ }
60
+
61
+ let diveRunId = 0;
62
+
63
+ async function dive(id, symbol) {
64
+ const thisRun = ++diveRunId;
65
+ results.style.display = 'none';
66
+ input.value = symbol.toUpperCase();
67
+ matrix.classList.remove('visible');
68
+ skeleton.style.display = 'block';
69
+ resetMeanReversionUI();
70
+
71
+ try {
72
+ const res = await fetch(`/api/dive/${encodeURIComponent(id)}`);
73
+ const d = await res.json();
74
+ if (thisRun !== diveRunId) return;
75
+
76
+ if(d.status === "success") {
77
+ currentCoinData = { id: id, ...d };
78
+ updateWatchlistUI(d.is_watched);
79
+
80
+ // TOKEN DETAILS
81
+ document.getElementById('v-price').innerText = d.vitals.price;
82
+ document.getElementById('v-mcap').innerText = d.vitals.mcap;
83
+ document.getElementById('v-vol').innerText = d.vitals.vol24h;
84
+ document.getElementById('v-supply').innerText = d.supply.total;
85
+
86
+ // QUANT RATIOS
87
+ document.getElementById('v-vtmr').innerText = d.ratios.vtmr;
88
+ document.getElementById('v-vtpc').innerText = d.ratios.vtpc;
89
+ document.getElementById('v-liquidity').innerText = d.ratios.liquidity;
90
+ const depth = d.depth || {};
91
+ document.getElementById('v-depth-up').innerText = depth.up || '--';
92
+ document.getElementById('v-depth-down').innerText = depth.down || '--';
93
+
94
+ const hist = d.historical || {};
95
+ document.getElementById('v-avg-7d-vol').innerText = hist.avg_7d_daily_vol || '--';
96
+ document.getElementById('v-avg-30d-vol').innerText = hist.avg_30d_vol || '--';
97
+ document.getElementById('v-vwap-30d').innerText = hist.vwap_30d || '--';
98
+
99
+ // PRICE CHANGE
100
+ updateVel('v-1h', d.velocity.h1);
101
+ updateVel('v-24h', d.velocity.h24);
102
+ updateVel('v-7d', d.velocity.d7);
103
+ updateVel('v-30d', d.velocity.m1);
104
+ updateVel('v-yearly', d.velocity.y1 || d.velocity.yearly);
105
+
106
+ // LINKS
107
+ if(d.links) {
108
+ document.getElementById('link-cg').href = d.links.cg;
109
+ document.getElementById('link-tv').href = d.links.tv;
110
+ }
111
+
112
+ skeleton.style.display = 'none';
113
+ matrix.classList.add('visible');
114
+ } else {
115
+ alert("Error: " + (d.message || "Failed to load data"));
116
+ skeleton.style.display = 'none';
117
+ }
118
+ } catch (err) {
119
+ console.error(err);
120
+ alert("Network error. Please try again.");
121
+ skeleton.style.display = 'none';
122
+ }
123
+ }
124
+
125
+ function updateVel(id, val) {
126
+ const el = document.getElementById(id);
127
+ if(!el) return;
128
+ el.innerText = val || "--";
129
+
130
+ if (val && val.toString().startsWith('-')) {
131
+ el.className = 'mono stats-val neg';
132
+ } else if (val && val !== "--") {
133
+ el.className = 'mono stats-val pos';
134
+ } else {
135
+ el.className = 'mono stats-val';
136
+ }
137
+ }
138
+
139
+ // URL param handler
140
+ document.addEventListener('DOMContentLoaded', () => {
141
+ const params = new URLSearchParams(window.location.search);
142
+ const ticker = params.get('ticker');
143
+
144
+ if (ticker) {
145
+ input.value = ticker.toUpperCase();
146
+ input.dispatchEvent(new Event('input', { bubbles: true }));
147
+ input.focus();
148
+ }
149
+ });
150
+
151
+ function updateWatchlistUI(isWatched) {
152
+ const btn = document.getElementById('btn-watchlist');
153
+ const text = document.getElementById('watchlist-text');
154
+ btn.setAttribute('data-is-watched', isWatched);
155
+
156
+ if (isWatched) {
157
+ btn.classList.add('active-watch');
158
+ text.innerText = "Watched";
159
+ } else {
160
+ btn.classList.remove('active-watch');
161
+ text.innerText = "Watchlist";
162
+ }
163
+ }
164
+
165
+ async function toggleWatchlist() {
166
+ if (!currentCoinData) return;
167
+
168
+ const btn = document.getElementById('btn-watchlist');
169
+ const isCurrentlyWatched = btn.getAttribute('data-is-watched') === 'true';
170
+ const action = isCurrentlyWatched ? 'remove' : 'add';
171
+
172
+ updateWatchlistUI(!isCurrentlyWatched);
173
+
174
+ try {
175
+ const res = await fetch('/api/watchlist/toggle', {
176
+ method: 'POST',
177
+ headers: {
178
+ 'Content-Type': 'application/json',
179
+ 'X-CSRFToken': window.QV.csrf
180
+ },
181
+ body: JSON.stringify({
182
+ coin_id: currentCoinData.id,
183
+ symbol: currentCoinData.vitals.symbol,
184
+ name: currentCoinData.vitals.name,
185
+ price: currentCoinData.vitals.price,
186
+ vtmr: currentCoinData.ratios.vtmr,
187
+ mcap: currentCoinData.vitals.mcap,
188
+ chg_1y: currentCoinData.velocity.y1 || currentCoinData.velocity.yearly,
189
+ chg_24h: currentCoinData.velocity.h24,
190
+ chg_7d: currentCoinData.velocity.d7,
191
+ chg_30d: currentCoinData.velocity.m1,
192
+ action: action
193
+ })
194
+ });
195
+
196
+ const result = await res.json();
197
+
198
+ if (result.status === "success") {
199
+ updateWatchlistUI(result.is_watched);
200
+ } else {
201
+ updateWatchlistUI(isCurrentlyWatched);
202
+ console.error("Watchlist sync error:", result.message);
203
+ }
204
+ } catch (err) {
205
+ updateWatchlistUI(isCurrentlyWatched);
206
+ console.error("Watchlist toggle failed:", err);
207
+ }
208
+ }
209
+
210
+ // --- Mean Reversion ---
211
+
212
+ function resetMeanReversionUI() {
213
+ const btn = document.getElementById('btn-calc-mr');
214
+ const container = document.getElementById('mr-results-container');
215
+
216
+ if (btn) {
217
+ btn.disabled = false;
218
+ btn.innerText = "CALCULATE";
219
+ }
220
+ if (container) {
221
+ container.style.display = 'none';
222
+ }
223
+
224
+ const el1d = document.getElementById('v-mr-1d');
225
+ const el4h = document.getElementById('v-mr-4h');
226
+ const el1h = document.getElementById('v-mr-1h');
227
+
228
+ if (el1d) el1d.innerText = '--';
229
+ if (el4h) el4h.innerText = '--';
230
+ if (el1h) el1h.innerText = '--';
231
+ }
232
+
233
+ async function calculateMeanReversion() {
234
+ if (!currentCoinData || !currentCoinData.id) return;
235
+
236
+ const btn = document.getElementById('btn-calc-mr');
237
+ const container = document.getElementById('mr-results-container');
238
+
239
+ btn.disabled = true;
240
+ btn.innerText = "CALCULATING...";
241
+
242
+ try {
243
+ const res = await fetch('/api/mean-reversion', {
244
+ method: 'POST',
245
+ headers: {
246
+ 'Content-Type': 'application/json',
247
+ 'X-CSRFToken': window.QV.csrf
248
+ },
249
+ body: JSON.stringify({ coin_id: currentCoinData.id })
250
+ });
251
+
252
+ if (!res.ok) {
253
+ let errMsg = `Server error (HTTP ${res.status}) calculating mean reversion.`;
254
+ try {
255
+ const errData = await res.json();
256
+ errMsg = errData.message || errMsg;
257
+ } catch (_) {
258
+
259
+ }
260
+ alert("Error: " + errMsg);
261
+ btn.disabled = false;
262
+ btn.innerText = "CALCULATE";
263
+ return;
264
+ }
265
+
266
+ const data = await res.json();
267
+
268
+ if (data.status === 'success') {
269
+ document.getElementById('v-mr-1d').innerText = data.line1d || '--';
270
+ document.getElementById('v-mr-4h').innerText = data.line4h || '--';
271
+ document.getElementById('v-mr-1h').innerText = data.line1h || '--';
272
+
273
+ container.style.display = 'block';
274
+ btn.innerText = "CALCULATED";
275
+ } else {
276
+ alert("Error: " + (data.message || "Failed to calculate mean reversion metrics"));
277
+ btn.disabled = false;
278
+ btn.innerText = "CALCULATE";
279
+ }
280
+ } catch (err) {
281
+ console.error(err);
282
+ alert("Network error processing mean reversion calculation.");
283
+ btn.disabled = false;
284
+ btn.innerText = "CALCULATE";
285
+ }
286
+ }