Build a Trading Bot
This guide builds a small market-making bot: every minute it reads every open market's best bid and offer, takes the midpoint as fair value, and bids a couple of cents under it on both YES and NO. It uses three endpoints and fits on one screen. Would you rather describe the bot and have an agent write it? See Build with AI agents.
1. Get an API key
Sign in and create a key on your Developer page, then export VPX_API_KEY=vpx_live_…. The bot trades from your account, so it can only spend your available cash.
2. Run the bot
Each round takes down the quotes it placed last round with Cancel orders (naming their ids, so your own orders and your VPX bots' stay put), reads every open market with its prices with List markets, and quotes with Place an order. Buying YES at mid − 2 and NO at 100 − mid − 2 quotes both sides without holding any shares, and the two bids never match each other. When you stop it, it takes its quotes down.
import os
import time
import requests
BASE_URL = "https://www.vancouverpx.ca/api/v1"
HEADERS = {"Authorization": f"Bearer {os.environ['VPX_API_KEY']}"}
EDGE = 2 # cents under fair value
QUANTITY = 10 # shares per quote
def call(method, path, **kwargs):
response = requests.request(method, BASE_URL + path, headers=HEADERS, timeout=10, **kwargs)
if not response.ok:
raise RuntimeError(response.json()["code"])
return response.json()
mine = [] # ids of the orders this bot has resting
try:
while True:
call("DELETE", "/orders", json={"ids": mine}) # take down last round's quotes, and only those
mine = []
markets = call("GET", "/markets", params={"status": "OPEN"})["markets"]
for market in markets:
if market["bestBid"] is None or market["bestAsk"] is None:
continue
mid = (market["bestBid"] + market["bestAsk"]) / 2
for outcome, price in (("YES", mid - EDGE), ("NO", 100 - mid - EDGE)):
price = round(price)
if not 1 <= price <= 99:
continue
try:
order = call("POST", "/orders", json={
"marketId": market["id"], "side": "BUY", "outcome": outcome,
"price": price, "quantity": QUANTITY,
})
mine.append(order["id"])
except RuntimeError as code:
print(market["question"], outcome, code)
time.sleep(60)
finally:
call("DELETE", "/orders", json={"ids": mine}) # and when it stops, Ctrl-C included3. Make it yours
- Replace the midpoint with your own model of fair value; that is where the edge is. A market's
venuesname the same question on Polymarket and Kalshi. - Check your positions and skew quotes to avoid piling into one side, and read your fills to track what traded.
- Read Market Integrity before quoting tight: resting orders get picked off when news breaks.
Generating a client? The whole API is an OpenAPI spec.