Bones for Building a Bot
Building a trading bot
This guide explains the minimal steps to create a trading/betting bot . Follow the sections below to implement, test, and run a safe sandbox bot before any production work.
You are still responsible for your bot, we are not reliable or responsible for any errors or mistakes your bot may produce.
Goals
- Authenticate with the API
- Seed markets/events/odds
- Place / cancel wagers
- Maintain session & websockets
- Run scheduled automated logic (strategy)
- Log, test, and keep safety checks (Sandbox only)
Prerequisites
We are using Python due to its ease of use and wide support of packages and integration. You may find success with other languages as well.
- Python 3.8+
- Recommended packages:
pip install requests pysher schedule pytz- Sandbox credentials and a Sandbox API endpoint (do NOT use production keys).
Project layout
- src/main.py — entry point (startup sequence, scheduling)
- src/mm_calls.py — core MMInteractions class (auth, seeding, place/cancel, websocket)
- src/config.py — endpoints, keys loader
- src/user_info.json — user keys & preferences (create locally)
- src/log.py, src/constants.py — logging and constants used by the bot
Configuration
- Create
src/user_info.json:
{
"access_key": "YOUR_ACCESS_KEY",
"secret_key": "YOUR_SECRET_KEY",
"tournaments": [123],
"load_all_tournaments": false
}- Ensure BASE_URL in config points to the sandbox URL: https://api-ss-sandbox.betprophet.co/
Implementation outline
-
Authentication
- POST partner/auth/login with access_key/secret_key.
- Store access_token and a requests.Session (for headers/cookies).
- Implement refresh token flow (call partner/auth/refresh or re-login on failure).
-
Seeding data
- Pull markets, tournaments, events, and valid odds ladder.
- Build in-memory structures: valid_odds, all_tournaments, sport_events.
- Validate each market has necessary ids (selection, line_id).
-
Balance & wallet
- Implement get_balance() to fetch and store balance.
- Use balance checks before placing wagers.
-
Place / Cancel wagers
- Implement mm_place_wager / mm_cancel_wager endpoints.
- Keep mapping external_id -> wager_id for cancels.
- Implement batching for cancellation if API supports it.
-
Websocket subscription (optional but highly recommended)
- Obtain connection config (key/cluster) and signed channels.
- Use pysher (or websocket-client) to subscribe and update local state on events.
- Bind public/private handlers to process market updates and wager confirmations.
-
Scheduling & automation
- Use schedule to run periodic tasks:
- seed every 30 min
- start_betting every N seconds (sandbox small interval for tests)
- random cancel / batch cancels
- refresh session every few minutes
- Run schedule in a background thread.
- Use schedule to run periodic tasks:
-
Strategy & risk
- Start with a deterministic test strategy (not real money): e.g., place fixed small stakes on random valid odds.
- Implement stake sizing, max exposure, per-tournament limits.
- Add odds filters to avoid thin markets.
- NEVER point to production endpoints or use production keys during development; assert BASE_URL contains "sandbox" before placing bets.
-
Resilience & safety
- Add retry/backoff for transient network errors.
- Rate-limit requests to respect API constraints.
- Validate API responses and handle malformed responses.
- Add logging and error alerts.
Testing
- Use sandbox/test accounts only.
- Add unit tests for:
- auth flow (mock responses)
- seeding/parsing market data
- place/cancel logic (mock API)
- Run
python3 src/main.pyin staging and observe behavior, logs, and API calls.
Deployment & monitoring
- Run in a contained environment (VM or container).
- Use process manager (systemd, supervisord) or Docker to restart on failures.
- Monitor logs, balance changes, and placed wagers.
- Set alerts for failed refreshes, repeated API errors, or unusually large bets.
Extending the bot
- Replace randomized bets with strategy module:
- create src/strategy.py with a class that returns candidate wagers
- integrate strategy into start_betting()
- Add persistence (sqlite/postgres) for wager history and state.
- Add metrics (Prometheus) and dashboards.
Minimal run example
From repo root:
cd sandbox-bot/mm-trading-bot
python3 src/main.pySafety checklist before running
- Use staging credentials
- Confirm BASE_URL points to staging
- Start with extremely small stakes
- Ensure logging is enabled
- Understand scheduled intervals
Useful TODOs
- Implement unit tests for mm_calls methods
- Add Dockerfile for sandboxed runs
- Add integration test that mocks API endpoints
Updated about 2 months ago
