feat(resolution): add automatic market resolution detector with conservative payout validation
CI/CD / build-and-push (push) Successful in 8s
CI/CD / build-and-push (push) Successful in 8s
- PolymarketClient.get_market_resolution(): query Gamma API by market id; resolved only when closed AND uma status final AND outcome prices binary (never settle on disputed/ambiguous outcomes) - bot/main.py: check_resolutions() runs every 10 cycles (~10 min) in paper mode, settles open positions via PaperExecutor.close_position() - close_reason now persisted as 'resolved' (resolution has its own column) - tests/test_resolution_detector.py: 10 tests covering API parsing shapes and the BUY_NO settlement flow; 27/27 suite green Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
340c8523cf
commit
e137116e7f
+57
@@ -27,6 +27,50 @@ log = logging.getLogger("bot.main")
|
||||
PAPER_MODE = os.getenv("PAPER_MODE", "true").lower() == "true"
|
||||
PAPER_BANKROLL = float(os.getenv("PAPER_BANKROLL", "10000"))
|
||||
|
||||
# Check open positions for market resolution every N trading cycles (~N minutes
|
||||
# at the 60s cycle cadence). Keeps Gamma API load at ~1 request per open
|
||||
# position per 10 minutes.
|
||||
RESOLUTION_CHECK_INTERVAL = 10
|
||||
|
||||
|
||||
async def check_resolutions(
|
||||
poly: PolymarketClient,
|
||||
executor: PaperExecutor,
|
||||
db: Database,
|
||||
) -> None:
|
||||
"""Detect resolved markets and settle their open paper positions.
|
||||
|
||||
For each open position, asks the Gamma API whether the market resolved.
|
||||
On a definitive resolution, PaperExecutor.close_position() settles the
|
||||
payout, persists close_reason='resolved' + resolution + close_pnl, and
|
||||
sends the Telegram notification.
|
||||
"""
|
||||
positions = await db.get_open_position_details()
|
||||
checked = 0
|
||||
resolved = 0
|
||||
for pos in positions:
|
||||
market_id = str(pos["market_id"])
|
||||
try:
|
||||
res = await poly.get_market_resolution(market_id)
|
||||
except Exception as exc:
|
||||
log.warning("Resolution check failed for market %s: %s", market_id, exc)
|
||||
continue
|
||||
checked += 1
|
||||
if res is None or not res.resolved or res.resolution is None:
|
||||
continue
|
||||
pnl = await executor.close_position(
|
||||
market_id, res.resolution, question=pos.get("question") or "",
|
||||
)
|
||||
resolved += 1
|
||||
log.info(
|
||||
"MARKET_RESOLVED market_id=%s resolution=%.1f pnl=%s | %s",
|
||||
market_id,
|
||||
res.resolution,
|
||||
f"{pnl:+.2f}" if pnl is not None else "n/a",
|
||||
(pos.get("question") or "")[:60],
|
||||
)
|
||||
log.info("Resolution check: %d positions checked, %d resolved", checked, resolved)
|
||||
|
||||
|
||||
async def run_trading_loop(
|
||||
poly: PolymarketClient,
|
||||
@@ -40,9 +84,22 @@ async def run_trading_loop(
|
||||
"""Main trading loop — runs every 60 seconds."""
|
||||
log.info("Trading loop started. PAPER_MODE=%s", PAPER_MODE)
|
||||
checkpoint_monitor = CheckpointMonitor()
|
||||
cycle_count = 0
|
||||
|
||||
while True:
|
||||
try:
|
||||
cycle_count += 1
|
||||
|
||||
# 0. Resolution detector — every RESOLUTION_CHECK_INTERVAL cycles,
|
||||
# settle paper positions whose market resolved on Polymarket.
|
||||
# Runs before evaluation so freed cash/families are usable this cycle.
|
||||
if (
|
||||
PAPER_MODE
|
||||
and isinstance(executor, PaperExecutor)
|
||||
and cycle_count % RESOLUTION_CHECK_INTERVAL == 0
|
||||
):
|
||||
await check_resolutions(poly, executor, db)
|
||||
|
||||
# 1. Fetch active markets (90-day window)
|
||||
markets = await poly.get_active_markets()
|
||||
log.info("Found %d active markets", len(markets))
|
||||
|
||||
Reference in New Issue
Block a user