fix: persist regime to DB in shared _build_state, deduplicate save logic
- _build_market_state (CLI) now saves regime_history automatically - _build_state (web) now saves regime_history automatically - Remove duplicate regime save from cmd_score - Remove unused imports (timedelta, get_connection) - Fix: web dashboard never updated regime_history table
This commit is contained in:
+20
-22
@@ -49,6 +49,24 @@ def _build_market_state(target: Date) -> tuple:
|
||||
oi_matrix_score=oi, volatility_regime_score=vol,
|
||||
)
|
||||
state.market_state_hash = state.compute_hash()
|
||||
|
||||
# Persist regime to DB so subsequent calls have correct state
|
||||
from database import get_connection
|
||||
conn = get_connection()
|
||||
conn.execute("""
|
||||
INSERT OR REPLACE INTO regime_history
|
||||
(date, regime, confidence, regime_version, maturity_score, all_scores_json,
|
||||
prior_regime, confirmation_days)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""", (
|
||||
str(target), r.regime.value, r.confidence, r.regime_version,
|
||||
r.maturity_score, json.dumps(r.all_scores),
|
||||
r.prior_regime.value if r.prior_regime else None,
|
||||
r.confirmation_days,
|
||||
))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
return state, r
|
||||
|
||||
|
||||
@@ -93,13 +111,13 @@ def cmd_fetch(args):
|
||||
|
||||
def cmd_score(args):
|
||||
"""Compute all factor scores and regime for a date."""
|
||||
from database import init_db, get_connection
|
||||
from database import init_db
|
||||
|
||||
target = parse_date(args.date) if args.date else Date.today()
|
||||
init_db()
|
||||
logger.info(f"Computing scores for {target}...")
|
||||
|
||||
state, regime_result = _build_market_state(target)
|
||||
state, _ = _build_market_state(target)
|
||||
|
||||
# Output
|
||||
ps = state.price_structure_score
|
||||
@@ -128,26 +146,6 @@ def cmd_score(args):
|
||||
print(f" Market State Hash: {state.market_state_hash}")
|
||||
print()
|
||||
|
||||
# Store regime to DB
|
||||
conn = get_connection()
|
||||
conn.execute("""
|
||||
INSERT OR REPLACE INTO regime_history
|
||||
(date, regime, confidence, regime_version, maturity_score, all_scores_json,
|
||||
prior_regime, confirmation_days)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""", (
|
||||
str(target),
|
||||
state.regime.value,
|
||||
state.regime_confidence,
|
||||
state.regime_version,
|
||||
state.regime_maturity_score,
|
||||
json.dumps(regime_result.all_scores),
|
||||
regime_result.prior_regime.value if regime_result.prior_regime else None,
|
||||
regime_result.confirmation_days,
|
||||
))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
return state
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user