feat: initial commit — polymarket-bot source + CI/CD pipeline
CI/CD / build-and-push (push) Failing after 30s
CI/CD / build-and-push (push) Failing after 30s
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
"""Database layer using asyncpg for PostgreSQL."""
|
||||
import logging
|
||||
import os
|
||||
from typing import Optional
|
||||
import asyncpg
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Database:
|
||||
def __init__(self) -> None:
|
||||
self._url = os.getenv("DATABASE_URL", "postgresql://bot:bot@localhost:5432/polymarket")
|
||||
self._pool: Optional[asyncpg.Pool] = None
|
||||
|
||||
async def connect(self) -> None:
|
||||
self._pool = await asyncpg.create_pool(self._url)
|
||||
log.info("Database connected")
|
||||
|
||||
async def disconnect(self) -> None:
|
||||
if self._pool:
|
||||
await self._pool.close()
|
||||
|
||||
async def run_migrations(self) -> None:
|
||||
schema_path = os.path.join(os.path.dirname(__file__), "schema.sql")
|
||||
with open(schema_path) as f:
|
||||
schema = f.read()
|
||||
async with self._pool.acquire() as conn:
|
||||
await conn.execute(schema)
|
||||
log.info("Migrations applied")
|
||||
|
||||
async def save_trade(self, trade) -> None:
|
||||
async with self._pool.acquire() as conn:
|
||||
await conn.execute("""
|
||||
INSERT INTO trades (
|
||||
id, market_id, question, direction, size_usdc,
|
||||
entry_price, shares, fee_usdc, net_cost, timestamp, reasoning, paper
|
||||
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12)
|
||||
ON CONFLICT (id) DO NOTHING
|
||||
""",
|
||||
trade.id, trade.market_id, trade.question, trade.direction,
|
||||
trade.size_usdc, trade.entry_price, trade.shares, trade.fee_usdc,
|
||||
trade.net_cost, trade.timestamp, trade.reasoning, trade.paper,
|
||||
)
|
||||
|
||||
async def save_daily_metrics(self, metrics: dict) -> None:
|
||||
async with self._pool.acquire() as conn:
|
||||
await conn.execute("""
|
||||
INSERT INTO metrics_daily (
|
||||
timestamp, total_trades, total_deployed, total_fees,
|
||||
total_pnl, win_rate, avg_edge, sharpe_ratio, calibration_score, paper_mode
|
||||
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)
|
||||
""",
|
||||
metrics["timestamp"], metrics["total_trades"], metrics["total_deployed"],
|
||||
metrics["total_fees"], metrics["total_pnl"], metrics["win_rate"],
|
||||
metrics["avg_edge"], metrics["sharpe_ratio"], metrics["calibration_score"],
|
||||
metrics["paper_mode"],
|
||||
)
|
||||
|
||||
async def get_open_positions(self) -> dict[str, float]:
|
||||
"""Return {market_id: total_net_cost} for all trades in DB.
|
||||
|
||||
Since there is no closed flag, every trade in the DB is treated as an
|
||||
open position. After a TRUNCATE the query returns nothing, so the
|
||||
portfolio correctly resets to a full bankroll.
|
||||
"""
|
||||
async with self._pool.acquire() as conn:
|
||||
rows = await conn.fetch(
|
||||
"SELECT market_id, SUM(net_cost) AS total FROM trades GROUP BY market_id"
|
||||
)
|
||||
return {r["market_id"]: float(r["total"]) for r in rows}
|
||||
|
||||
async def get_recent_trades(self, limit: int = 100) -> list[dict]:
|
||||
async with self._pool.acquire() as conn:
|
||||
rows = await conn.fetch(
|
||||
"SELECT * FROM trades ORDER BY timestamp DESC LIMIT $1", limit
|
||||
)
|
||||
return [dict(r) for r in rows]
|
||||
|
||||
async def get_metrics_history(self, days: int = 42) -> list[dict]:
|
||||
async with self._pool.acquire() as conn:
|
||||
rows = await conn.fetch(
|
||||
"SELECT * FROM metrics_daily ORDER BY timestamp DESC LIMIT $1", days
|
||||
)
|
||||
return [dict(r) for r in rows]
|
||||
@@ -0,0 +1,99 @@
|
||||
"""
|
||||
External data signals for Bayesian probability estimation.
|
||||
Sources: CoinGecko (crypto prices), Alternative.me (Fear&Greed), Polymarket trends
|
||||
"""
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
import httpx
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ExternalSignals:
|
||||
btc_price: float = 0.0
|
||||
btc_change_24h: float = 0.0 # % change
|
||||
eth_price: float = 0.0
|
||||
eth_change_24h: float = 0.0
|
||||
btc_dominance: float = 50.0 # BTC market dominance %
|
||||
fear_greed_index: int = 50 # 0=extreme fear, 100=extreme greed
|
||||
fear_greed_label: str = "neutral"
|
||||
total_market_cap_change: float = 0.0
|
||||
valid: bool = False
|
||||
|
||||
|
||||
class ExternalDataClient:
|
||||
"""Fetches external market signals used to calibrate probability estimates."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._client = httpx.AsyncClient(timeout=15)
|
||||
|
||||
async def get_all_signals(self) -> ExternalSignals:
|
||||
"""Aggregate all external signals. Returns best-effort (partial ok)."""
|
||||
signals = ExternalSignals()
|
||||
|
||||
try:
|
||||
prices = await self._get_crypto_prices()
|
||||
signals.btc_price = prices.get("bitcoin", {}).get("usd", 0)
|
||||
signals.btc_change_24h = prices.get("bitcoin", {}).get("usd_24h_change", 0)
|
||||
signals.eth_price = prices.get("ethereum", {}).get("usd", 0)
|
||||
signals.eth_change_24h = prices.get("ethereum", {}).get("usd_24h_change", 0)
|
||||
except Exception as e:
|
||||
log.warning("CoinGecko fetch failed: %s", e)
|
||||
|
||||
try:
|
||||
fg = await self._get_fear_greed()
|
||||
signals.fear_greed_index = fg["value"]
|
||||
signals.fear_greed_label = fg["label"]
|
||||
except Exception as e:
|
||||
log.warning("Fear&Greed fetch failed: %s", e)
|
||||
|
||||
try:
|
||||
global_data = await self._get_global_market()
|
||||
signals.btc_dominance = global_data.get("btc_dominance", 50)
|
||||
signals.total_market_cap_change = global_data.get("market_cap_change_24h", 0)
|
||||
except Exception as e:
|
||||
log.warning("Global market data fetch failed: %s", e)
|
||||
|
||||
signals.valid = signals.btc_price > 0
|
||||
log.info(
|
||||
"External signals: BTC=$%.0f (%.1f%%) F&G=%d/%s",
|
||||
signals.btc_price,
|
||||
signals.btc_change_24h,
|
||||
signals.fear_greed_index,
|
||||
signals.fear_greed_label,
|
||||
)
|
||||
return signals
|
||||
|
||||
async def _get_crypto_prices(self) -> dict:
|
||||
resp = await self._client.get(
|
||||
"https://api.coingecko.com/api/v3/simple/price",
|
||||
params={
|
||||
"ids": "bitcoin,ethereum",
|
||||
"vs_currencies": "usd",
|
||||
"include_24hr_change": True,
|
||||
},
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
|
||||
async def _get_fear_greed(self) -> dict:
|
||||
resp = await self._client.get("https://api.alternative.me/fng/?limit=1")
|
||||
resp.raise_for_status()
|
||||
data = resp.json()["data"][0]
|
||||
return {
|
||||
"value": int(data["value"]),
|
||||
"label": data["value_classification"],
|
||||
}
|
||||
|
||||
async def _get_global_market(self) -> dict:
|
||||
resp = await self._client.get("https://api.coingecko.com/api/v3/global")
|
||||
resp.raise_for_status()
|
||||
data = resp.json()["data"]
|
||||
return {
|
||||
"btc_dominance": data.get("market_cap_percentage", {}).get("btc", 50),
|
||||
"market_cap_change_24h": data.get("market_cap_change_percentage_24h_usd", 0),
|
||||
}
|
||||
|
||||
async def close(self) -> None:
|
||||
await self._client.aclose()
|
||||
@@ -0,0 +1,213 @@
|
||||
"""
|
||||
Polymarket CLOB API client.
|
||||
Docs: https://docs.polymarket.com
|
||||
"""
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from typing import Optional
|
||||
import httpx
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
POLYMARKET_API = "https://clob.polymarket.com"
|
||||
GAMMA_API = "https://gamma-api.polymarket.com"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Market:
|
||||
id: str
|
||||
condition_id: str
|
||||
question: str
|
||||
yes_token_id: str
|
||||
no_token_id: str
|
||||
yes_price: float # 0-1, current best ask for YES
|
||||
no_price: float
|
||||
volume_24h: float
|
||||
end_date: str
|
||||
active: bool
|
||||
category: str = ""
|
||||
|
||||
|
||||
@dataclass
|
||||
class OrderBook:
|
||||
market_id: str
|
||||
yes_bids: list[tuple[float, float]] = field(default_factory=list) # (price, size)
|
||||
yes_asks: list[tuple[float, float]] = field(default_factory=list)
|
||||
mid_price: float = 0.5
|
||||
|
||||
|
||||
class PolymarketClient:
|
||||
"""
|
||||
Async Polymarket client.
|
||||
In paper mode, API key is not needed — only public data.
|
||||
API key required for placing real orders.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.api_key = os.getenv("POLYMARKET_API_KEY", "")
|
||||
self.secret = os.getenv("POLYMARKET_SECRET", "")
|
||||
self.passphrase = os.getenv("POLYMARKET_PASSPHRASE", "")
|
||||
self._client = httpx.AsyncClient(timeout=30)
|
||||
|
||||
# Keywords that identify crypto / finance markets.
|
||||
# Short tickers are padded with spaces to avoid false substring matches
|
||||
# (e.g. " eth " won't match "Hegseth"; " sol " won't match "solar").
|
||||
_CRYPTO_FINANCE_KEYWORDS: list[str] = [
|
||||
"bitcoin", "btc", " eth ", "ethereum", " sol ", "solana",
|
||||
"xrp", "ripple", "dogecoin", "doge", "litecoin", "ltc",
|
||||
"coinbase", "binance", "kraken", "bybit", "okx",
|
||||
"usdc", "usdt", "stablecoin",
|
||||
"defi", "nft", "blockchain", "crypto",
|
||||
" fdv", "airdrop", "token launch", "token listing",
|
||||
"microstrategy", "mstr", "saylor",
|
||||
"nasdaq", "sp500", "s&p 500", "s&p500",
|
||||
"federal reserve", "fed rate", "interest rate",
|
||||
"inflation", "tariff", "treasury yield",
|
||||
" ipo ", "sec ", "cftc",
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def _is_crypto_finance(cls, question: str) -> bool:
|
||||
q = f" {question.lower()} " # pad so edge keywords match cleanly
|
||||
return any(kw in q for kw in cls._CRYPTO_FINANCE_KEYWORDS)
|
||||
|
||||
async def get_active_markets(
|
||||
self,
|
||||
min_volume: float = 1000,
|
||||
pages: int = 3,
|
||||
page_size: int = 200,
|
||||
max_days_to_resolution: int = 30,
|
||||
) -> list[Market]:
|
||||
"""Fetch active crypto/finance markets from Gamma API (no auth needed).
|
||||
|
||||
Fetches events without tag filtering (tag= param is unreliable),
|
||||
then keeps only markets whose question matches crypto/finance keywords
|
||||
and that resolve within max_days_to_resolution days.
|
||||
"""
|
||||
seen: set[str] = set()
|
||||
markets: list[Market] = []
|
||||
cutoff = datetime.now(timezone.utc) + timedelta(days=max_days_to_resolution)
|
||||
|
||||
for page in range(pages):
|
||||
try:
|
||||
resp = await self._client.get(
|
||||
f"{GAMMA_API}/events",
|
||||
params={
|
||||
"active": True,
|
||||
"closed": False,
|
||||
"limit": page_size,
|
||||
"offset": page * page_size,
|
||||
},
|
||||
)
|
||||
resp.raise_for_status()
|
||||
events = resp.json()
|
||||
|
||||
if not events:
|
||||
break # no more pages
|
||||
|
||||
for event in events:
|
||||
event_title = event.get("title", "")
|
||||
for m in event.get("markets", []):
|
||||
try:
|
||||
if not m.get("active") or m.get("closed"):
|
||||
continue
|
||||
|
||||
question = m.get("question", "")
|
||||
if not self._is_crypto_finance(question) and \
|
||||
not self._is_crypto_finance(event_title):
|
||||
continue
|
||||
|
||||
# Filter: only markets resolving within the cutoff window
|
||||
# Gamma API may return endDate or end_date (snake_case)
|
||||
raw_end = m.get("endDate") or m.get("end_date") or m.get("endDateIso", "")
|
||||
if raw_end:
|
||||
try:
|
||||
end_dt = datetime.fromisoformat(
|
||||
raw_end.replace("Z", "+00:00")
|
||||
)
|
||||
# Make naive datetimes UTC-aware before comparing
|
||||
if end_dt.tzinfo is None:
|
||||
end_dt = end_dt.replace(tzinfo=timezone.utc)
|
||||
if end_dt > cutoff:
|
||||
continue
|
||||
except (ValueError, TypeError):
|
||||
pass # keep market if date unparseable
|
||||
|
||||
market_id = str(m["id"])
|
||||
if market_id in seen:
|
||||
continue
|
||||
|
||||
vol = float(m.get("volume24hr", 0))
|
||||
if vol < min_volume:
|
||||
continue
|
||||
|
||||
raw_prices = m.get("outcomePrices", ["0.5", "0.5"])
|
||||
if isinstance(raw_prices, str):
|
||||
import json as _json
|
||||
raw_prices = _json.loads(raw_prices)
|
||||
yes_price = float(raw_prices[0])
|
||||
|
||||
raw_tokens = m.get("clobTokenIds", ["", ""])
|
||||
if isinstance(raw_tokens, str):
|
||||
import json as _json
|
||||
raw_tokens = _json.loads(raw_tokens)
|
||||
|
||||
seen.add(market_id)
|
||||
markets.append(Market(
|
||||
id=market_id,
|
||||
condition_id=m.get("conditionId", ""),
|
||||
question=question,
|
||||
yes_token_id=raw_tokens[0] if raw_tokens else "",
|
||||
no_token_id=raw_tokens[1] if len(raw_tokens) > 1 else "",
|
||||
yes_price=yes_price,
|
||||
no_price=1 - yes_price,
|
||||
volume_24h=vol,
|
||||
end_date=m.get("endDate", ""),
|
||||
active=True,
|
||||
category="crypto/finance",
|
||||
))
|
||||
except (KeyError, ValueError, IndexError) as e:
|
||||
log.debug("Skipping malformed market: %s", e)
|
||||
|
||||
except httpx.HTTPError as e:
|
||||
log.error("Polymarket API error (page=%d): %s", page, e)
|
||||
break
|
||||
|
||||
log.info(
|
||||
"Loaded %d crypto/finance markets (min_vol=%.0f, resolving within %dd)",
|
||||
len(markets), min_volume, max_days_to_resolution,
|
||||
)
|
||||
return markets
|
||||
|
||||
async def get_order_book(self, token_id: str) -> Optional[OrderBook]:
|
||||
"""Get order book for a specific token."""
|
||||
try:
|
||||
resp = await self._client.get(
|
||||
f"{POLYMARKET_API}/book",
|
||||
params={"token_id": token_id},
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
|
||||
bids = [(float(b["price"]), float(b["size"])) for b in data.get("bids", [])]
|
||||
asks = [(float(a["price"]), float(a["size"])) for a in data.get("asks", [])]
|
||||
|
||||
mid = 0.5
|
||||
if bids and asks:
|
||||
mid = (bids[0][0] + asks[0][0]) / 2
|
||||
|
||||
return OrderBook(
|
||||
market_id=token_id,
|
||||
yes_bids=bids,
|
||||
yes_asks=asks,
|
||||
mid_price=mid,
|
||||
)
|
||||
except Exception as e:
|
||||
log.warning("Order book fetch failed for %s: %s", token_id, e)
|
||||
return None
|
||||
|
||||
async def close(self) -> None:
|
||||
await self._client.aclose()
|
||||
@@ -0,0 +1,57 @@
|
||||
-- Polymarket Bot Database Schema
|
||||
|
||||
CREATE TABLE IF NOT EXISTS trades (
|
||||
id TEXT PRIMARY KEY,
|
||||
market_id TEXT NOT NULL,
|
||||
question TEXT NOT NULL,
|
||||
direction TEXT NOT NULL, -- BUY_YES | BUY_NO
|
||||
size_usdc DOUBLE PRECISION,
|
||||
entry_price DOUBLE PRECISION,
|
||||
shares DOUBLE PRECISION,
|
||||
fee_usdc DOUBLE PRECISION,
|
||||
net_cost DOUBLE PRECISION,
|
||||
timestamp TIMESTAMPTZ NOT NULL,
|
||||
reasoning TEXT,
|
||||
paper BOOLEAN DEFAULT TRUE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS metrics_daily (
|
||||
id SERIAL PRIMARY KEY,
|
||||
timestamp TIMESTAMPTZ NOT NULL,
|
||||
total_trades INTEGER,
|
||||
total_deployed DOUBLE PRECISION,
|
||||
total_fees DOUBLE PRECISION,
|
||||
total_pnl DOUBLE PRECISION,
|
||||
win_rate DOUBLE PRECISION,
|
||||
avg_edge DOUBLE PRECISION,
|
||||
sharpe_ratio DOUBLE PRECISION,
|
||||
calibration_score DOUBLE PRECISION,
|
||||
paper_mode BOOLEAN DEFAULT TRUE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS markets (
|
||||
id TEXT PRIMARY KEY,
|
||||
condition_id TEXT,
|
||||
question TEXT NOT NULL,
|
||||
category TEXT,
|
||||
end_date TEXT,
|
||||
active BOOLEAN DEFAULT TRUE,
|
||||
last_seen TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS signals (
|
||||
id SERIAL PRIMARY KEY,
|
||||
market_id TEXT NOT NULL,
|
||||
timestamp TIMESTAMPTZ NOT NULL,
|
||||
polymarket_price DOUBLE PRECISION,
|
||||
estimated_prob DOUBLE PRECISION,
|
||||
edge DOUBLE PRECISION,
|
||||
confidence DOUBLE PRECISION,
|
||||
direction TEXT,
|
||||
acted_on BOOLEAN DEFAULT FALSE
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_trades_timestamp ON trades(timestamp DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_trades_market ON trades(market_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_metrics_timestamp ON metrics_daily(timestamp DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_signals_timestamp ON signals(timestamp DESC);
|
||||
Reference in New Issue
Block a user