feat: добавить логирование ордеров, баланса биржи и тайминга циклов

- orders: actual fill price, fee, slippage_pct (exchange vs signal price)
- balance_snapshots: exchange balance vs DB equity divergence tracking
- cycle_log: started_at/finished_at/duration для мониторинга latency
- exp17: расширить тест на 7 активов

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
anor
2026-03-10 03:01:53 +10:00
co-authored by Claude Opus 4.6
parent 536e32261a
commit 7167dedcaa
3 changed files with 178 additions and 8 deletions
+40 -6
View File
@@ -27,11 +27,22 @@ CORR_THRESH = 0.5
TRAIN_RATIO = 0.3
SHIFT = 7
COMMISSION = 0.001
INTEREST_RATE_DAILY = 0.0002
STOP_SCALE = 0.5 # exp16e winner
CORRELATED_GROUPS = [
{"BTC-USD", "ETH-USD"},
{"ADA-USD", "DOT-USD", "XRP-USD"},
]
ASSETS = [
{"symbol": "ETH/USDC", "ticker": "ETH-USD", "tf": "2h", "ws": 64, "leverage": 2, "weight": 0.5},
{"symbol": "DOGE/USDC", "ticker": "DOGE-USD", "tf": "4h", "ws": 48, "leverage": 2, "weight": 0.5},
{"symbol": "BTC/USDC", "ticker": "BTC-USD", "tf": "4h", "ws": 32, "leverage": 2, "weight": 0.241},
{"symbol": "ETH/USDC", "ticker": "ETH-USD", "tf": "2h", "ws": 64, "leverage": 2, "weight": 0.158},
{"symbol": "ADA/USDC", "ticker": "ADA-USD", "tf": "4h", "ws": 64, "leverage": 2, "weight": 0.137},
{"symbol": "DOT/USDC", "ticker": "DOT-USD", "tf": "4h", "ws": 32, "leverage": 2, "weight": 0.137},
{"symbol": "DOGE/USDC", "ticker": "DOGE-USD", "tf": "4h", "ws": 48, "leverage": 2, "weight": 0.127},
{"symbol": "SOL/USDC", "ticker": "SOL-USD", "tf": "2h", "ws": 128, "leverage": 2, "weight": 0.108},
{"symbol": "XRP/USDC", "ticker": "XRP-USD", "tf": "4h", "ws": 64, "leverage": 2, "weight": 0.092},
]
@@ -153,7 +164,9 @@ def run_sim(asset_data, events, variant="baseline"):
if stopped:
pnl_gross = pos.pos_size * (pos.stop_price - pos.entry_price) * pos.direction
commission_cost = pos.pos_value * COMMISSION * 2
pnl_net = pnl_gross - commission_cost
hold_hours = bars_since * bar_h
trade_interest = pos.borrowed * INTEREST_RATE_DAILY * hold_hours / 24
pnl_net = pnl_gross - commission_cost - trade_interest
free_usdc += pos.margin_locked + pnl_net
del open_positions[ticker]
trades.append({"ticker": ticker, "pnl_net": pnl_net,
@@ -194,21 +207,39 @@ def run_sim(asset_data, events, variant="baseline"):
# Use model_prices for prediction input
w = model_prices[price_idx - ws - K:price_idx - K].copy()
confirm = prices[price_idx - K:price_idx].copy() # always Close for confirmation
mu, std = w.mean(), w.std()
if std < 1e-10:
continue
norm_input = (w - mu) / std
pred = model_mod.predict(ad["model"], norm_input)
pred_new = pred[-SHIFT:]
# Shape confirmation: always use Close prices with Close normalization
confirm = prices[price_idx - K:price_idx].copy()
w_close = prices[price_idx - ws - K:price_idx - K]
mu_close, std_close = float(w_close.mean()), float(w_close.std())
if std_close < 1e-10:
continue
corr, confirmed = signals.check_shape_confirmation(
pred_new, confirm, mu, std, K, CORR_THRESH)
pred_new, confirm, mu_close, std_close, K, CORR_THRESH)
if not confirmed:
continue
direction = signals.get_direction(pred_new, K)
if direction is None or direction == -1:
continue
# Correlation filter
skip_corr = False
for g in CORRELATED_GROUPS:
if ticker not in g:
continue
for other in g:
if other != ticker and other in open_positions:
if open_positions[other].direction == direction:
skip_corr = True
if skip_corr:
continue
total_equity = free_usdc + sum(p.margin_locked for p in open_positions.values())
pos_value = total_equity * weight * leverage
if pos_value < 5.0:
@@ -252,7 +283,10 @@ def run_sim(asset_data, events, variant="baseline"):
ad = asset_data[ticker]
last_price = float(ad["prices"][-1])
pnl_gross = pos.pos_size * (last_price - pos.entry_price) * pos.direction
pnl_net = pnl_gross - pos.pos_value * COMMISSION * 2
bars_held = ad["test_data"][-1]["price_idx"] - pos.entry_bar
bar_hours = tf_hours.get(ad["tf"], 4)
trade_interest = pos.borrowed * INTEREST_RATE_DAILY * bars_held * bar_hours / 24
pnl_net = pnl_gross - pos.pos_value * COMMISSION * 2 - trade_interest
free_usdc += pos.margin_locked + pnl_net
per_asset[ticker] = per_asset.get(ticker, 0) + pnl_net
trades.append({"ticker": ticker, "pnl_net": pnl_net,