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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
π Polymarket Bot β Backtester / Scanner de marchΓ©s.
|
| 4 |
+
Test rapide des stratΓ©gies sur les marchΓ©s actuels sans placer d'ordres.
|
| 5 |
+
"""
|
| 6 |
+
import asyncio
|
| 7 |
+
import json
|
| 8 |
+
import logging
|
| 9 |
+
import time
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
|
| 12 |
+
from polymarket_bot import (
|
| 13 |
+
BotConfig, GammaClient, CLOBDataClient,
|
| 14 |
+
ArbitrageStrategy, ValueBetStrategy, LeaderFollowerStrategy,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
logging.basicConfig(
|
| 18 |
+
level=logging.INFO,
|
| 19 |
+
format="%(asctime)s | %(name)-20s | %(levelname)-5s | %(message)s",
|
| 20 |
+
datefmt="%H:%M:%S",
|
| 21 |
+
)
|
| 22 |
+
logger = logging.getLogger("scanner")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
async def scan_markets():
|
| 26 |
+
"""Scanne les marchΓ©s actuels pour les opportunitΓ©s."""
|
| 27 |
+
print("""
|
| 28 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 29 |
+
β π POLYMARKET MARKET SCANNER β SINGLE PASS π β
|
| 30 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 31 |
+
""")
|
| 32 |
+
|
| 33 |
+
config = BotConfig(dry_run=True)
|
| 34 |
+
gamma = GammaClient()
|
| 35 |
+
clob = CLOBDataClient()
|
| 36 |
+
|
| 37 |
+
# 1. Charger les marchΓ©s
|
| 38 |
+
logger.info("Loading markets from Gamma API...")
|
| 39 |
+
markets = await gamma.get_all_active_markets()
|
| 40 |
+
logger.info(f"Loaded {len(markets)} active markets")
|
| 41 |
+
|
| 42 |
+
# Filtrer
|
| 43 |
+
filtered = [
|
| 44 |
+
m for m in markets
|
| 45 |
+
if m.volume >= config.min_market_volume
|
| 46 |
+
and not m.closed
|
| 47 |
+
and m.active
|
| 48 |
+
and m.yes_token
|
| 49 |
+
and m.no_token
|
| 50 |
+
and not any(tag in config.excluded_tags for tag in m.tags)
|
| 51 |
+
]
|
| 52 |
+
logger.info(f"After filtering: {len(filtered)} markets")
|
| 53 |
+
|
| 54 |
+
# 2. Afficher les top marchΓ©s
|
| 55 |
+
print("\nπ TOP 10 MARCHΓS PAR VOLUME:")
|
| 56 |
+
print("-" * 80)
|
| 57 |
+
sorted_markets = sorted(filtered, key=lambda m: m.volume, reverse=True)[:10]
|
| 58 |
+
for i, m in enumerate(sorted_markets, 1):
|
| 59 |
+
yes = m.yes_token
|
| 60 |
+
no = m.no_token
|
| 61 |
+
spread = 1.0 - (yes.price + no.price) if yes and no else 0
|
| 62 |
+
print(
|
| 63 |
+
f" {i:2d}. {m.question[:60]:<60s}\n"
|
| 64 |
+
f" Volume: ${m.volume:>12,.0f} | YES: {yes.price:.2f} | NO: {no.price:.2f} | "
|
| 65 |
+
f"Spread: {spread:+.4f}"
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# 3. Scanner les stratΓ©gies
|
| 69 |
+
print("\n\nπ SCANNING STRATEGIES...")
|
| 70 |
+
print("=" * 80)
|
| 71 |
+
|
| 72 |
+
# Arbitrage
|
| 73 |
+
print("\nπ STRATΓGIE 1: ARBITRAGE")
|
| 74 |
+
print("-" * 40)
|
| 75 |
+
arb = ArbitrageStrategy(config, clob)
|
| 76 |
+
arb_signals = await arb.scan(filtered[:50])
|
| 77 |
+
|
| 78 |
+
if arb_signals:
|
| 79 |
+
for s in arb_signals[:10]:
|
| 80 |
+
meta = s.metadata
|
| 81 |
+
print(
|
| 82 |
+
f" π° {s.action} | Spread: {meta.get('spread', 0):.4f} | "
|
| 83 |
+
f"Size: ${s.size_usd:.2f} | Expected: ${s.expected_profit:.2f}\n"
|
| 84 |
+
f" {meta.get('question', '')[:70]}"
|
| 85 |
+
)
|
| 86 |
+
else:
|
| 87 |
+
print(" Aucune opportunitΓ© d'arbitrage dΓ©tectΓ©e (normal β marchΓ©s efficaces)")
|
| 88 |
+
|
| 89 |
+
# Value Bet
|
| 90 |
+
print("\n\nπ‘ STRATΓGIE 2: VALUE BET")
|
| 91 |
+
print("-" * 40)
|
| 92 |
+
vb = ValueBetStrategy(config, clob)
|
| 93 |
+
vb_signals = await vb.scan(filtered[:30])
|
| 94 |
+
|
| 95 |
+
if vb_signals:
|
| 96 |
+
for s in vb_signals[:10]:
|
| 97 |
+
meta = s.metadata
|
| 98 |
+
print(
|
| 99 |
+
f" π {s.action} | Edge: {meta.get('edge', 0):.4f} | "
|
| 100 |
+
f"Kelly: {meta.get('kelly_frac', 0):.4f} | "
|
| 101 |
+
f"Size: ${s.size_usd:.2f}\n"
|
| 102 |
+
f" {meta.get('question', '')[:70]}"
|
| 103 |
+
)
|
| 104 |
+
else:
|
| 105 |
+
print(" Pas assez d'historique pour dΓ©tecter des signaux (nΓ©cessite plusieurs cycles)")
|
| 106 |
+
|
| 107 |
+
# Leader-Follower
|
| 108 |
+
print("\n\nπ STRATΓGIE 3: LEADER-FOLLOWER")
|
| 109 |
+
print("-" * 40)
|
| 110 |
+
lf = LeaderFollowerStrategy(config, clob)
|
| 111 |
+
lf_pairs = lf._find_correlated_pairs(filtered[:30])
|
| 112 |
+
if lf_pairs:
|
| 113 |
+
print(f" {len(lf_pairs)} paires corrΓ©lΓ©es dΓ©tectΓ©es:")
|
| 114 |
+
for leader, follower, sim in lf_pairs[:5]:
|
| 115 |
+
print(
|
| 116 |
+
f" π SimilaritΓ©: {sim:.4f}\n"
|
| 117 |
+
f" Leader: {leader.question[:60]}\n"
|
| 118 |
+
f" Follower: {follower.question[:60]}"
|
| 119 |
+
)
|
| 120 |
+
else:
|
| 121 |
+
print(" Aucune paire fortement corrΓ©lΓ©e trouvΓ©e")
|
| 122 |
+
|
| 123 |
+
# RΓ©sumΓ©
|
| 124 |
+
total_signals = len(arb_signals) + len(vb_signals)
|
| 125 |
+
total_expected = sum(s.expected_profit for s in arb_signals + vb_signals)
|
| 126 |
+
|
| 127 |
+
print(f"""
|
| 128 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 129 |
+
β π SCAN SUMMARY β
|
| 130 |
+
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£
|
| 131 |
+
β Markets scanned: {len(filtered):>6d}
|
| 132 |
+
β Arb signals: {len(arb_signals):>6d}
|
| 133 |
+
β Value bet signals: {len(vb_signals):>6d}
|
| 134 |
+
β Correlated pairs: {len(lf_pairs):>6d}
|
| 135 |
+
β Total expected: ${total_expected:>10,.2f}
|
| 136 |
+
β Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
| 137 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ""")
|
| 138 |
+
|
| 139 |
+
# Export
|
| 140 |
+
scan_results = {
|
| 141 |
+
"timestamp": datetime.now().isoformat(),
|
| 142 |
+
"markets_scanned": len(filtered),
|
| 143 |
+
"arb_signals": len(arb_signals),
|
| 144 |
+
"vb_signals": len(vb_signals),
|
| 145 |
+
"lf_pairs": len(lf_pairs),
|
| 146 |
+
"arb_details": [
|
| 147 |
+
{
|
| 148 |
+
"market": s.metadata.get("question", ""),
|
| 149 |
+
"spread": s.metadata.get("spread", 0),
|
| 150 |
+
"expected_profit": s.expected_profit,
|
| 151 |
+
"size_usd": s.size_usd,
|
| 152 |
+
}
|
| 153 |
+
for s in arb_signals
|
| 154 |
+
],
|
| 155 |
+
}
|
| 156 |
+
with open("scan_results.json", "w") as f:
|
| 157 |
+
json.dump(scan_results, f, indent=2)
|
| 158 |
+
logger.info("Results saved to scan_results.json")
|
| 159 |
+
|
| 160 |
+
await gamma.close()
|
| 161 |
+
await clob.close()
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
if __name__ == "__main__":
|
| 165 |
+
asyncio.run(scan_markets())
|