feat(observability): fine-grained metrics for summary, trades, and cycle log
CI/CD / build-and-push (push) Successful in 1m51s
CI/CD / build-and-push (push) Successful in 1m51s
api/summary — new fields:
open_trades_count, closed_trades_count, cash_available (bankroll−deployed),
legacy_incomplete_count, reentry_guard_blocks_24h
parallel fetch via asyncio.gather for sub-ms overhead
api/trades?status=open — trade enrichment:
days_open (float, rounded to 1 decimal)
signal_components {fg, mom, news, mfld} parsed from reasoning via regex
Old trades without feat_str in reasoning return signal_components: null
bayesian.py — reasoning now embeds feat_str:
"fg=+0.0600 mom=+0.0000 news=+0.0000 mfld=-0.7483 |"
Manifold counters: _manifold_fetched / _manifold_on_trade per cycle
get_cycle_stats() exposes manifold_matches_accepted / manifold_matches_rejected
bot/main.py — CYCLE SUMMARY 4 new fields:
reentry_guard_blocked, legacy_incomplete_seen,
family_conflicts_prevented, manifold_matches_accepted/rejected
legacy_incomplete_count queried from DB once per cycle
db.py — get_legacy_incomplete_count(): open trades with NULL edge_net
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -120,6 +120,14 @@ class Database:
|
||||
market_id, new_key,
|
||||
)
|
||||
|
||||
async def get_legacy_incomplete_count(self) -> int:
|
||||
"""Return count of open trades with NULL edge_net (legacy data without signal values)."""
|
||||
async with self._pool.acquire() as conn:
|
||||
row = await conn.fetchrow(
|
||||
"SELECT COUNT(*) FROM trades WHERE closed_at IS NULL AND edge_net IS NULL"
|
||||
)
|
||||
return int(row[0])
|
||||
|
||||
async def get_recently_closed_inverted(self, hours: int = 24) -> set[str]:
|
||||
"""Return market_ids closed for inversion bug within the last N hours.
|
||||
|
||||
|
||||
+14
-1
@@ -100,6 +100,7 @@ async def run_trading_loop(
|
||||
len(inverted_guard), sorted(inverted_guard),
|
||||
)
|
||||
|
||||
reentry_guard_count = 0
|
||||
cycle_trades = 0
|
||||
for market in markets:
|
||||
if market.id in inverted_guard:
|
||||
@@ -107,6 +108,7 @@ async def run_trading_loop(
|
||||
"reentry_guard_triggered market=%s | skipping — closed for inversion within 24h | %s",
|
||||
market.id, market.question[:60],
|
||||
)
|
||||
reentry_guard_count += 1
|
||||
continue
|
||||
|
||||
# evaluate() returns None for all skips — reasons are logged internally
|
||||
@@ -142,6 +144,7 @@ async def run_trading_loop(
|
||||
|
||||
# 8. [CYCLE SUMMARY] — one block per cycle, stable format for grep/compare
|
||||
stats = strategy.get_cycle_stats()
|
||||
legacy_incomplete_count = await db.get_legacy_incomplete_count()
|
||||
n_total = len(markets)
|
||||
n_uncertainty = sum(1 for m in markets if 0.35 <= m.yes_price <= 0.65)
|
||||
n_eval = stats["evaluated_count"]
|
||||
@@ -164,7 +167,12 @@ async def run_trading_loop(
|
||||
" blocked_by_edge_net_nonpositive:%d\n"
|
||||
" blocked_by_edge_net_below_regime:%d\n"
|
||||
" trades_executed: %d\n"
|
||||
" gnews_queries_used: %d/%d",
|
||||
" gnews_queries_used: %d/%d\n"
|
||||
" reentry_guard_blocked: %d\n"
|
||||
" legacy_incomplete_seen: %d\n"
|
||||
" family_conflicts_prevented: %d\n"
|
||||
" manifold_matches_accepted: %d\n"
|
||||
" manifold_matches_rejected: %d",
|
||||
n_total,
|
||||
n_uncertainty,
|
||||
stats["max_edge_gross"],
|
||||
@@ -177,6 +185,11 @@ async def run_trading_loop(
|
||||
stats["skip_edge_net_below_regime"],
|
||||
cycle_trades,
|
||||
stats["gnews_queries_used"], MAX_NEWS_QUERIES_PER_CYCLE,
|
||||
reentry_guard_count,
|
||||
legacy_incomplete_count,
|
||||
stats["skip_family"],
|
||||
stats["manifold_matches_accepted"],
|
||||
stats["manifold_matches_rejected"],
|
||||
)
|
||||
|
||||
# 9. Update daily metrics
|
||||
|
||||
@@ -201,6 +201,8 @@ class BayesianStrategy:
|
||||
self._skip_prior_extreme: int = 0
|
||||
self._skip_edge_net_nonpositive: int = 0 # edge_net <= 0
|
||||
self._skip_edge_net_below_regime: int = 0 # 0 < edge_net < regime_min
|
||||
self._manifold_fetched: int = 0 # markets where Manifold prob was retrieved
|
||||
self._manifold_on_trade: int = 0 # subset of above that ended in a trade signal
|
||||
# (edge_gross, edge_net, regime_min) for every market that reached the
|
||||
# edge computation stage (passed prior-extreme, family, unsupported filters)
|
||||
self._evaluated_edges: list[tuple[float, float, float]] = []
|
||||
@@ -212,6 +214,8 @@ class BayesianStrategy:
|
||||
self._skip_prior_extreme = 0
|
||||
self._skip_edge_net_nonpositive = 0
|
||||
self._skip_edge_net_below_regime = 0
|
||||
self._manifold_fetched = 0
|
||||
self._manifold_on_trade = 0
|
||||
self._evaluated_edges = []
|
||||
|
||||
def get_cycle_stats(self) -> dict:
|
||||
@@ -230,6 +234,8 @@ class BayesianStrategy:
|
||||
"evaluated_count": len(edges),
|
||||
"gross_gt_002": sum(1 for g in all_gross if g > 0.02),
|
||||
"gross_gt_004": sum(1 for g in all_gross if g > 0.04),
|
||||
"manifold_matches_accepted": self._manifold_on_trade,
|
||||
"manifold_matches_rejected": self._manifold_fetched - self._manifold_on_trade,
|
||||
}
|
||||
|
||||
async def evaluate(
|
||||
@@ -401,9 +407,12 @@ class BayesianStrategy:
|
||||
# Applies a log-odds adjustment proportional to divergence from prior.
|
||||
# No query budget — 30 min cache means network cost is paid once per cycle.
|
||||
manifold_log_adj = 0.0
|
||||
manifold_used = False
|
||||
if (is_politics or is_tech) and self._manifold is not None:
|
||||
manifold_prob = await self._manifold.get_probability(market.question)
|
||||
if manifold_prob is not None:
|
||||
manifold_used = True
|
||||
self._manifold_fetched += 1
|
||||
m_clamped = max(0.05, min(0.95, manifold_prob))
|
||||
m_log = math.log(m_clamped / (1 - m_clamped))
|
||||
p_log = math.log(prior / (1 - prior))
|
||||
@@ -487,6 +496,8 @@ class BayesianStrategy:
|
||||
f"regime_min={regime_min:.2f} | days={days} | "
|
||||
f"family={family} | "
|
||||
f"Direction={direction} | "
|
||||
f"fg={_fg_contribution:+.4f} mom={_momentum_contribution:+.4f} "
|
||||
f"news={news_log_adj:+.4f} mfld={manifold_log_adj:+.4f} | "
|
||||
f"Signals: {', '.join(sources[1:])}"
|
||||
)
|
||||
|
||||
@@ -501,6 +512,8 @@ class BayesianStrategy:
|
||||
)
|
||||
|
||||
self._signal_count += 1
|
||||
if manifold_used:
|
||||
self._manifold_on_trade += 1
|
||||
return TradingSignal(
|
||||
market_id=market.id,
|
||||
question=market.question,
|
||||
|
||||
Reference in New Issue
Block a user