Update app.py
Browse files
app.py
CHANGED
|
@@ -12,13 +12,9 @@ PORT = 7860
|
|
| 12 |
HISTORY_LENGTH = 300
|
| 13 |
BROADCAST_RATE = 0.1
|
| 14 |
|
| 15 |
-
# --- Tuning Parameters ---
|
| 16 |
-
# Short: Scalping/Spread focus
|
| 17 |
DECAY_LAMBDA_SHORT = 100.0
|
| 18 |
-
# Long: Swing/Structure focus
|
| 19 |
DECAY_LAMBDA_LONG = 500.0
|
| 20 |
-
|
| 21 |
-
DECAY_LAMBDA_MACRO = 2000.0
|
| 22 |
|
| 23 |
IMPACT_SENSITIVITY = 0.5
|
| 24 |
|
|
@@ -54,16 +50,10 @@ def analyze_structure(diff_x, diff_y, current_mid):
|
|
| 54 |
marginal_vol = cum_vol - prev_vol
|
| 55 |
prev_vol = cum_vol
|
| 56 |
|
| 57 |
-
# 1. Short Term Decay
|
| 58 |
w_short += marginal_vol * math.exp(-dist / DECAY_LAMBDA_SHORT)
|
| 59 |
-
|
| 60 |
-
# 2. Long Term Decay
|
| 61 |
w_long += marginal_vol * math.exp(-dist / DECAY_LAMBDA_LONG)
|
| 62 |
-
|
| 63 |
-
# 3. Macro Term Decay
|
| 64 |
w_macro += marginal_vol * math.exp(-dist / DECAY_LAMBDA_MACRO)
|
| 65 |
|
| 66 |
-
# Helper for impact calc
|
| 67 |
def get_impact(w_imb):
|
| 68 |
if w_imb == 0: return 0.0
|
| 69 |
imp = math.sqrt(abs(w_imb)) * IMPACT_SENSITIVITY
|
|
@@ -73,7 +63,7 @@ def analyze_structure(diff_x, diff_y, current_mid):
|
|
| 73 |
"proj_short": current_mid + get_impact(w_short),
|
| 74 |
"proj_long": current_mid + get_impact(w_long),
|
| 75 |
"proj_macro": current_mid + get_impact(w_macro),
|
| 76 |
-
"net_score": w_short
|
| 77 |
}
|
| 78 |
|
| 79 |
def process_market_data():
|
|
@@ -82,9 +72,8 @@ def process_market_data():
|
|
| 82 |
|
| 83 |
mid = market_state['current_mid']
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
raw_asks = sorted(market_state['asks'].items(), key=lambda x: x[0])[:600]
|
| 88 |
|
| 89 |
d_b_x, d_b_y, cum = [], [], 0
|
| 90 |
for p, q in raw_bids:
|
|
@@ -125,17 +114,14 @@ def process_market_data():
|
|
| 125 |
now = time.time()
|
| 126 |
if analysis:
|
| 127 |
if not market_state['pred_history_short'] or (now - market_state['pred_history_short'][-1]['t'] > 0.5):
|
| 128 |
-
# Short
|
| 129 |
market_state['pred_history_short'].append({'t': now, 'p': analysis['proj_short']})
|
| 130 |
if len(market_state['pred_history_short']) > HISTORY_LENGTH:
|
| 131 |
market_state['pred_history_short'].pop(0)
|
| 132 |
|
| 133 |
-
# Long
|
| 134 |
market_state['pred_history_long'].append({'t': now, 'p': analysis['proj_long']})
|
| 135 |
if len(market_state['pred_history_long']) > HISTORY_LENGTH:
|
| 136 |
market_state['pred_history_long'].pop(0)
|
| 137 |
|
| 138 |
-
# Macro
|
| 139 |
market_state['pred_history_macro'].append({'t': now, 'p': analysis['proj_macro']})
|
| 140 |
if len(market_state['pred_history_macro']) > HISTORY_LENGTH:
|
| 141 |
market_state['pred_history_macro'].pop(0)
|
|
@@ -278,15 +264,13 @@ HTML_PAGE = f"""
|
|
| 278 |
|
| 279 |
const priceSeries = priceChart.addLineSeries({{ color: '#2962FF', lineWidth: 2, title: 'Price' }});
|
| 280 |
|
| 281 |
-
// Short Term: Show Past + Future Dotted
|
| 282 |
const pastShortSeries = priceChart.addLineSeries({{ color: '#555555', lineWidth: 1, title: 'Short (Past)' }});
|
| 283 |
const futureShortSeries = priceChart.addLineSeries({{ color: '#ff9800', lineWidth: 2, lineStyle: 2, title: 'Short (Fut)' }});
|
| 284 |
|
| 285 |
-
// Long Term: Past Only
|
| 286 |
const pastLongSeries = priceChart.addLineSeries({{ color: '#7b1fa2', lineWidth: 1, title: 'Long (Past)' }});
|
| 287 |
|
| 288 |
-
// Macro Term: Past Only
|
| 289 |
const pastMacroSeries = priceChart.addLineSeries({{ color: '#4dd0e1', lineWidth: 1, title: 'Macro (Past)' }});
|
|
|
|
| 290 |
|
| 291 |
const rawChart = LightweightCharts.createChart(document.getElementById('tv-raw'), {{
|
| 292 |
...chartCommon,
|
|
@@ -377,9 +361,10 @@ HTML_PAGE = f"""
|
|
| 377 |
if (data.analysis) {{
|
| 378 |
const {{ proj_short, proj_long, proj_macro, net_score }} = data.analysis;
|
| 379 |
|
| 380 |
-
// Only plot short term future line
|
| 381 |
futureShortSeries.setData([last, {{ time: last.time + 60, value: proj_short }}]);
|
| 382 |
|
|
|
|
|
|
|
| 383 |
dom.projShort.innerText = proj_short.toLocaleString(undefined, {{minimumFractionDigits: 0, maximumFractionDigits: 0}});
|
| 384 |
dom.projLong.innerText = proj_long.toLocaleString(undefined, {{minimumFractionDigits: 0, maximumFractionDigits: 0}});
|
| 385 |
dom.projMacro.innerText = proj_macro.toLocaleString(undefined, {{minimumFractionDigits: 0, maximumFractionDigits: 0}});
|
|
|
|
| 12 |
HISTORY_LENGTH = 300
|
| 13 |
BROADCAST_RATE = 0.1
|
| 14 |
|
|
|
|
|
|
|
| 15 |
DECAY_LAMBDA_SHORT = 100.0
|
|
|
|
| 16 |
DECAY_LAMBDA_LONG = 500.0
|
| 17 |
+
DECAY_LAMBDA_MACRO = 15000.0
|
|
|
|
| 18 |
|
| 19 |
IMPACT_SENSITIVITY = 0.5
|
| 20 |
|
|
|
|
| 50 |
marginal_vol = cum_vol - prev_vol
|
| 51 |
prev_vol = cum_vol
|
| 52 |
|
|
|
|
| 53 |
w_short += marginal_vol * math.exp(-dist / DECAY_LAMBDA_SHORT)
|
|
|
|
|
|
|
| 54 |
w_long += marginal_vol * math.exp(-dist / DECAY_LAMBDA_LONG)
|
|
|
|
|
|
|
| 55 |
w_macro += marginal_vol * math.exp(-dist / DECAY_LAMBDA_MACRO)
|
| 56 |
|
|
|
|
| 57 |
def get_impact(w_imb):
|
| 58 |
if w_imb == 0: return 0.0
|
| 59 |
imp = math.sqrt(abs(w_imb)) * IMPACT_SENSITIVITY
|
|
|
|
| 63 |
"proj_short": current_mid + get_impact(w_short),
|
| 64 |
"proj_long": current_mid + get_impact(w_long),
|
| 65 |
"proj_macro": current_mid + get_impact(w_macro),
|
| 66 |
+
"net_score": w_short
|
| 67 |
}
|
| 68 |
|
| 69 |
def process_market_data():
|
|
|
|
| 72 |
|
| 73 |
mid = market_state['current_mid']
|
| 74 |
|
| 75 |
+
raw_bids = sorted(market_state['bids'].items(), key=lambda x: -x[0])[:1000]
|
| 76 |
+
raw_asks = sorted(market_state['asks'].items(), key=lambda x: x[0])[:1000]
|
|
|
|
| 77 |
|
| 78 |
d_b_x, d_b_y, cum = [], [], 0
|
| 79 |
for p, q in raw_bids:
|
|
|
|
| 114 |
now = time.time()
|
| 115 |
if analysis:
|
| 116 |
if not market_state['pred_history_short'] or (now - market_state['pred_history_short'][-1]['t'] > 0.5):
|
|
|
|
| 117 |
market_state['pred_history_short'].append({'t': now, 'p': analysis['proj_short']})
|
| 118 |
if len(market_state['pred_history_short']) > HISTORY_LENGTH:
|
| 119 |
market_state['pred_history_short'].pop(0)
|
| 120 |
|
|
|
|
| 121 |
market_state['pred_history_long'].append({'t': now, 'p': analysis['proj_long']})
|
| 122 |
if len(market_state['pred_history_long']) > HISTORY_LENGTH:
|
| 123 |
market_state['pred_history_long'].pop(0)
|
| 124 |
|
|
|
|
| 125 |
market_state['pred_history_macro'].append({'t': now, 'p': analysis['proj_macro']})
|
| 126 |
if len(market_state['pred_history_macro']) > HISTORY_LENGTH:
|
| 127 |
market_state['pred_history_macro'].pop(0)
|
|
|
|
| 264 |
|
| 265 |
const priceSeries = priceChart.addLineSeries({{ color: '#2962FF', lineWidth: 2, title: 'Price' }});
|
| 266 |
|
|
|
|
| 267 |
const pastShortSeries = priceChart.addLineSeries({{ color: '#555555', lineWidth: 1, title: 'Short (Past)' }});
|
| 268 |
const futureShortSeries = priceChart.addLineSeries({{ color: '#ff9800', lineWidth: 2, lineStyle: 2, title: 'Short (Fut)' }});
|
| 269 |
|
|
|
|
| 270 |
const pastLongSeries = priceChart.addLineSeries({{ color: '#7b1fa2', lineWidth: 1, title: 'Long (Past)' }});
|
| 271 |
|
|
|
|
| 272 |
const pastMacroSeries = priceChart.addLineSeries({{ color: '#4dd0e1', lineWidth: 1, title: 'Macro (Past)' }});
|
| 273 |
+
const futureMacroSeries = priceChart.addLineSeries({{ color: '#00bcd4', lineWidth: 2, lineStyle: 2, title: 'Macro (Fut)' }});
|
| 274 |
|
| 275 |
const rawChart = LightweightCharts.createChart(document.getElementById('tv-raw'), {{
|
| 276 |
...chartCommon,
|
|
|
|
| 361 |
if (data.analysis) {{
|
| 362 |
const {{ proj_short, proj_long, proj_macro, net_score }} = data.analysis;
|
| 363 |
|
|
|
|
| 364 |
futureShortSeries.setData([last, {{ time: last.time + 60, value: proj_short }}]);
|
| 365 |
|
| 366 |
+
futureMacroSeries.setData([last, {{ time: last.time + 3600, value: proj_macro }}]);
|
| 367 |
+
|
| 368 |
dom.projShort.innerText = proj_short.toLocaleString(undefined, {{minimumFractionDigits: 0, maximumFractionDigits: 0}});
|
| 369 |
dom.projLong.innerText = proj_long.toLocaleString(undefined, {{minimumFractionDigits: 0, maximumFractionDigits: 0}});
|
| 370 |
dom.projMacro.innerText = proj_macro.toLocaleString(undefined, {{minimumFractionDigits: 0, maximumFractionDigits: 0}});
|