Polymarket Bot Tutorial · Chapter 22 of 32
Polymarket पर NegRisk multi-outcome bots: sum-to-1 mechanics, YES legs 1 तक sum नहीं करते तो leg arbitrage, legs के across hedging, और multi-outcome markets से जुड़े execution pitfalls.
इस chapter में क्या covered है
Multi-outcome NegRisk markets mutually exclusive होते हैं - exactly एक ही outcome YES resolve करता है। यह chapter chapter 11 की execution mechanics के ऊपर strategy layer है: legs के across कैसे hedge करें, sum-to-1 arb कब real है, और first deploy पर ज़्यादातर NegRisk bots किन bugs से टकराते हैं।
- NegRisk vs binary recap
- Sum-to-1 invariant और arbitrage
- Leg-by-leg hedge construction
- Execution: orders में neg_risk flag
- NegRisk bots में common bugs
- Code: सभी legs snapshot करें और under-1.00 sum detect करें
NegRisk vs binary recap
Binary: एक yes/no market, दो tokens, sum 1.0 तक। NegRisk: N mutually exclusive outcomes, N tokens, event के across सभी YES legs लगभग 1.0 तक sum करते हैं।
Execution के हिसाब से, NegRisk को हर order पर negRisk: true चाहिए (chapter 11) और यह एक अलग exchange contract के through route होता है। Strategy के हिसाब से, NegRisk binaries से दो unique opportunities देता है: sum 1.0 से drift करे तो cross-leg arb, और multiple YES legs खरीदकर hedge construction।
NegRisk के unique costs: ज्यादा legs = ज्यादा spread tax (आप जिस हर leg को trade करते हैं, उसकी cost लगभग 0.5-1c spread होती है), illiquid events में sum-to-1 deviations ज़्यादा wide होते हैं (arb अक्सर available होता है लेकिन छोटा होता है)।
Sum-to-1 invariant और arbitrage
Arb premise: अगर सभी N YES legs खरीदने की cost $1.00 से कम है, तो resolution पर आपने guaranteed profit lock कर लिया (एक leg को $1.00 pay करना ही होगा; बाकी $0 पर चले जाएंगे)।
Practical तौर पर, arb gap आम तौर पर 0-3c होता है, जो हर leg पर spread + fees खा जाते हैं, और opening के कुछ मिनटों में गायब हो जाता है। Capacity सबसे पतली leg की liquidity से limited होती है।
Arb कुछ specific resolution failure modes के भी subject होती है: एक "none of the above" outcome जो तब स्पष्ट रूप से YES resolve करता है जब कोई named candidate qualify नहीं करता। अगर event में ऐसा leg है और आपने उसे नहीं खरीदा, तो आपका "complete hedge" actual payout miss कर देता है।
Leg-by-leg hedge construction
NegRisk leg पर position hold करते हुए, आप competing legs के YES खरीदकर proportion में hedge कर सकते हैं। अगर आपके पास Trump-YES 0.50 पर है और आप Trump loss के against hedge करना चाहते हैं, तो आप बाकी named legs का portfolio खरीदते हैं।
Per-leg hedge weight ≈ Trump losing होने पर उस leg की current implied probability। Approximation: weight_i = price_i / (1 - trump_price).
Hedge imperfect होता है क्योंकि इस्तेमाल की गई prices point-in-time होती हैं और news आने पर conditional probabilities shift करती हैं। Hedge को weekly या major news पर rebalance करें। इसे over-engineer न करें; hedge का purpose variance कम करना है, eliminate करना नहीं।
Execution: orders में neg_risk flag
NegRisk-specific सबसे common bug: order placement payload में negRisk: true भूल जाना। Order API से accept हो जाता है लेकिन settle गलत तरीके से होता है क्योंकि वह NegRisk exchange की बजाय standard CTF exchange पर route हो जाता है।
// CORRECT for NegRisk markets:
await client.createAndPostOrder(
{ tokenID, price, size, side: Side.BUY },
{ tickSize: '0.01', negRisk: true }, // <-- REQUIRED
OrderType.FOK
);
Source of truth: Gamma API से market.negRisk। इसे पढ़ें; आगे pass करें। कभी भी अंदाज़े के आधार पर flag hardcode न करें।
NegRisk bots में common bugs
Multiple bots के production debug logs से।
- Missing negRisk flag: orders accepted, settlement fails. Cure: हर wrapper में flag enforce करें।
- "Other" leg के बिना hedging: "None of the above" outcome वाले events में, उसे exclude करने वाला hedge portfolio incomplete होता है। Cure: hedge बनाते समय हमेशा Other leg check करें।
- Sum-to-1 arb under-sizing: arber 1c edge realize करता है लेकिन per leg 5 shares trade करता है; total profit spread से पहले 5 cents, net negative। Cure: arb को meaningful absolute dollars निकालने के लिए size करें, headline percentages chase न करें।
- Stale leg pricing: bot 3 leg prices fetch करता है, total 200ms लगता है, fetch के दौरान last leg की price बदल जाती है। Cure: सभी legs parallel में fetch करें + snapshot को एक observation की तरह treat करें।
Code: सभी legs snapshot करें और under-1.00 sum detect करें
Reference: NegRisk event की सभी YES legs को parallel में snapshot करें, arb detect करें।
import asyncio, aiohttp
async def fetch_leg_ask(session, token_id):
async with session.get(f"https://clob.polymarket.com/book?token_id={token_id}") as r:
d = await r.json()
asks = d.get("asks", [])
return float(asks[0]["price"]) if asks else None
async def check_arb(event_slug):
event = await fetch_event(event_slug)
if not event["markets"][0]["negRisk"]: return None
legs = []
for m in event["markets"]:
toks = json.loads(m["clobTokenIds"])
yes_token = toks[0]
legs.append(yes_token)
async with aiohttp.ClientSession() as s:
asks = await asyncio.gather(*[fetch_leg_ask(s, t) for t in legs])
if any(a is None for a in asks): return None
total = sum(asks)
if total < 0.97:
return {"edge": 1 - total, "legs": list(zip(legs, asks))}
return None
All legs का atomic execution कठिन problem है और इसके लिए per-leg FOK + partial fill पर rollback चाहिए (chapter 16 के stat-arb code जैसा similar pattern)।





