alphax/tests/test_market_regime.py
2026-05-28 00:02:11 +08:00

81 lines
2.6 KiB
Python

from app.core.market_regime import classify_market_regime
from app.core.global_risk import evaluate_global_risk
def _overview(**overrides):
data = {
"sample_count": 200,
"benchmarks": {
"BTC/USDT": {"change_24h": 0.5},
"ETH/USDT": {"change_24h": 0.4},
},
"advance_decline_ratio": 1.0,
"avg_change_24h": 0.1,
"hot_count_5pct": 8,
"crash_count_5pct": 4,
"funding": {"avg_funding_rate": 0.0001, "extreme_positive_count": 2},
}
data.update(overrides)
return data
def test_market_regime_reduces_size_for_clear_risk_off():
result = classify_market_regime(
_overview(
benchmarks={"BTC/USDT": {"change_24h": -3.4}, "ETH/USDT": {"change_24h": -4.2}},
advance_decline_ratio=0.4,
crash_count_5pct=35,
)
)
assert result["regime"] == "risk_off"
assert result["risk_level"] == "critical"
assert result["position_multiplier"] == 0.25
def test_market_regime_detects_altcoin_rotation():
result = classify_market_regime(
_overview(
benchmarks={"BTC/USDT": {"change_24h": 0.6}, "ETH/USDT": {"change_24h": 1.1}},
advance_decline_ratio=1.4,
avg_change_24h=1.2,
hot_count_5pct=22,
crash_count_5pct=3,
)
)
assert result["regime"] == "altcoin_rotation"
assert result["risk_level"] == "medium"
def test_global_risk_blocks_same_sector_concentration(pg_conn, monkeypatch):
monkeypatch.setattr(
"app.core.global_risk.get_crypto_market_overview",
lambda allow_live_fallback=False: _overview(),
)
pg_conn.execute(
"""
INSERT INTO paper_trades (
recommendation_id, symbol, side, status, opened_at, entry_price, qty,
notional_usdt, current_price, pnl_pct, created_at, updated_at
) VALUES
(1, 'DOGE/USDT', 'long', 'open', '2026-05-23T10:00:00', 1, 100, 1000, 1, 0, '2026-05-23T10:00:00', '2026-05-23T10:00:00'),
(2, 'SHIB/USDT', 'long', 'open', '2026-05-23T10:00:00', 1, 100, 1000, 1, 0, '2026-05-23T10:00:00', '2026-05-23T10:00:00')
"""
)
pg_conn.commit()
result = evaluate_global_risk(
conn=pg_conn,
config={
"account_equity_usdt": 20000,
"global_risk_max_same_sector_positions": 2,
"global_risk_max_same_direction_positions": 10,
},
rec={"symbol": "PEPE/USDT", "sector": "MEME"},
additional_notional=1000,
)
assert result["allow_new_entries"] is False
assert result["decision"] == "block_same_sector_concentration"