from datetime import datetime, timezone
from pathlib import Path
import pandas as pd
import requests
DATA_DIR = Path("data/polymarket")
def collect_data() -> pd.DataFrame:
"""Call the Polymarket Retail API and normalize the response into a DataFrame."""
url = "https://api.polymarket.us/v1/markets"
response = requests.get(url, params={"limit": 100})
if response.status_code != 200:
raise RuntimeError(f"Failed to retrieve data: {response.status_code}")
markets = response.json()
rows = []
for market in markets:
rows.append(
{
"timestamp": datetime.now(timezone.utc),
"venue": "Polymarket",
"market_title": market.get("question"),
"market_ticker": market.get("slug"), # closest equivalent
"event_ticker": market.get("event_slug"),
"best_bid_price": market.get("bestBid"),
"best_ask_price": market.get("bestAsk"),
"last_traded_price": market.get("lastTradePrice"),
"volume": market.get("volume"),
"open_interest": None, # not available from this endpoint
"liquidity": market.get("liquidity"),
}
)
df = pd.DataFrame(rows)
numeric_cols = [
"best_bid_price",
"best_ask_price",
"last_traded_price",
"volume",
"liquidity",
]
for col in numeric_cols:
df[col] = pd.to_numeric(df[col], errors="coerce")
df["mid_price"] = (df["best_bid_price"] + df["best_ask_price"]) / 2
return df
def main():
df = collect_data()
print("Collected Polymarket data (parquet-like table format):")
print(df.to_string(index=False))
if __name__ == "__main__":
main()
Polymarket is a prediction market. Essentially, people buy and sell bets online (best bid and best ask, respectively) regarding global events (event like "US politics" and market like "Biden wins 2028 election").
However, my code is unable to properly extract these variables from the API/URL and no data is exported to my Pandas DataFrame. I am almost certain the issue lies in incorrect usage of API/URL in which the data isn't being extracted because there's nothing there, but I don't know how to fix it?