diff --git a/ChanMacro/cli.py b/ChanMacro/cli.py index 15ce4f4..bcfed84 100644 --- a/ChanMacro/cli.py +++ b/ChanMacro/cli.py @@ -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 diff --git a/ChanMacro/web/app.py b/ChanMacro/web/app.py index be01938..feeb5e3 100644 --- a/ChanMacro/web/app.py +++ b/ChanMacro/web/app.py @@ -6,7 +6,8 @@ import sys import os sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from datetime import date as Date, timedelta +import json +from datetime import date as Date from flask import Flask, render_template, jsonify, request from database import get_connection @@ -23,7 +24,7 @@ app = Flask(__name__) def _build_state(target: Date): - """Shared: build MarketStateVector for a date.""" + """Build MarketStateVector and persist regime to DB.""" ps = PriceStructureScorer().compute(target) br = BreadthScorer().compute(target) oi = OIMatrixScorer().compute(target) @@ -44,6 +45,23 @@ def _build_state(target: Date): oi_matrix_score=oi, volatility_regime_score=vol, ) state.market_state_hash = state.compute_hash() + + # Persist regime to DB so load_state() works across requests + 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