import requests import time import numpy as np import re from datetime import datetime # ===== PROXY ===== PROXIES = { "http": "socks5h://127.0.0.1:10808", "https": "socks5h://127.0.0.1:10808" } HEADERS = { "User-Agent": "Mozilla/5.0" } # ===== CONFIG ===== VOL_WINDOW = 120 MODEL_PROB = 0.97 price_history = [] signals = [] history = [] events_cache = [] events_last_update = 0 # ===== BTC PRICE ===== def get_btc_price(): urls = [ "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd", "https://api.kraken.com/0/public/Ticker?pair=XBTUSD" ] for url in urls: try: r = requests.get( url, headers=HEADERS, proxies=PROXIES, timeout=5 ) if r.status_code != 200: continue data = r.json() if "bitcoin" in data: return float(data["bitcoin"]["usd"]) if "result" in data: return float(data["result"]["XXBTZUSD"]["c"][0]) except: pass return None # ===== EVENTS ===== def get_btc_markets(): try: r = requests.get( "https://gamma-api.polymarket.com/events", headers=HEADERS, proxies=PROXIES, timeout=10 ) events = r.json() btc_markets = [] for e in events: title = e.get("title","").lower() if "Bitcoin" in title and "min" in title: markets = e.get("markets", []) for m in markets: btc_markets.append(m) print("btc markets:", len(btc_markets)) return btc_markets except Exception as e: print("event error:", e) return [] def get_events_cached(): global events_cache global events_last_update if time.time() - events_last_update > 30: events_cache = get_btc_markets() events_last_update = time.time() return events_cache # ===== TARGET ===== def extract_target(question): match = re.search(r'(\d{4,6})', question) if match: return float(match.group(1)) return None # ===== VOL ===== def compute_volatility(prices): if len(prices) < 10: return 0 returns = np.diff(np.log(prices)) return np.std(returns) # ===== STRATEGY ===== def tail_strategy(price, target, vol, time_left): distance = price - target vol_price = price * vol * 3 if time_left < 10: if distance > vol_price: return "YES" if distance < -vol_price: return "NO" return None # ===== MAKER PRICE ===== def compute_maker_price(): margin = 0.03 price = MODEL_PROB - margin return round(price,3) # ===== SIGNAL RECORD ===== def record_signal(question, target, price, signal, end_time): signals.append({ "market": question, "target": target, "price": price, "signal": signal, "end_time": end_time }) # ===== EVALUATE ===== def evaluate_signals(btc_price): global signals global history now = datetime.utcnow() remaining = [] for s in signals: if now < s["end_time"]: remaining.append(s) continue if s["signal"] == "YES": win = btc_price > s["target"] else: win = btc_price < s["target"] s["win"] = win history.append(s) signals = remaining # ===== STATS ===== def print_stats(): if len(history) == 0: return wins = sum(1 for h in history if h["win"]) total = len(history) print("\n===== STRATEGY STATS =====") print("trades:", total) print("wins:", wins) print("win_rate:", round(wins/total,3)) print("==========================\n") # ===== MAIN ===== print("Polymarket BTC5min bot started\n") while True: btc_price = get_btc_price() if btc_price is None: print("price unavailable") time.sleep(2) continue price_history.append(btc_price) if len(price_history) > VOL_WINDOW: price_history.pop(0) vol = compute_volatility(price_history) markets = get_events_cached() now = datetime.utcnow() for m in markets: question = m.get("question","") target = extract_target(question) if not target: continue try: end_time = datetime.fromisoformat( m["endDate"].replace("Z","") ) except: continue time_left = (end_time - now).total_seconds() if time_left < 0 or time_left > 600: continue signal = tail_strategy( btc_price, target, vol, time_left ) if not signal: continue maker_price = compute_maker_price() print("\n========== SIGNAL ==========") print("Market:", question) print("BTC:", btc_price) print("Target:", target) print("Time left:", round(time_left,2)) print("Signal:", signal) print("Suggested maker price:", maker_price) print("============================\n") record_signal( question, target, btc_price, signal, end_time ) evaluate_signals(btc_price) print_stats() time.sleep(2
Note.ms
/qiulingyan