Trading Foundations — Core concepts for bot developers
Understand universal trading concepts you’ll use when building trading bots: risk & bankroll management, odds & expected value, position sizing, order execution and monitoring. This page includes practical examples, an interactive position-size calculator and a printable pre-live checklist.
Download the printable checklist to include in your ops runbook and pin your kill-switch procedure.
Download checklistMetrics quick reference
| Metric | Meaning |
|---|---|
| Drawdown | Peak-to-trough percentage drop from equity high. |
| Sharpe ratio | Return per unit volatility. |
| Win rate | Proportion of profitable trades. |
Quick links
Quick takeaway
Run end-to-end sandbox tests, set maximum risk per trade, implement idempotent order calls, and create a tested kill-switch. Use the downloadable checklist for your operations runbook.
1. Risk & Capital Protection
Risk management is the foundation of sustainable trading. Treat risk limits as business rules that stop automated trading when thresholds are exceeded.
Key principles
- Risk per trade: set as a percentage of bankroll (typical range 0.5–2%).
- Max drawdown limit: e.g., pause trading if equity drops >15–25%.
- Idempotency: ensure replaying a request cannot create duplicate orders (uuid/request token).
- Circuit breakers: time-boxed halts or kill-switch invoked if anomalous losses or rate-limit failures occur.
2. Bankroll & Position Sizing
How you size positions determines survivability — conservative sizing reduces drawdown risk and allows compounding over time.
Common approaches
- Fixed-fraction: risk a percentage of current bankroll per trade.
- Fixed size: same contract/lot each trade.
- Kelly criterion: optimal theoretically — usually scaled down in practice.
Position-size calculator (interactive)
Estimate units/contracts/shares to buy given bankroll, risk percentage and stop-loss distance.
Example: £10,000 bankroll, 1% risk → £100 risk. If stop-loss = 2 price units and price per unit £50, position = 100 / (2 × 50) = 1 unit.
3. Odds, Probability & Expected Value
Profitability depends on your edge. Use clear formulas to estimate whether a strategy has positive expectancy.
| Metric | Formula / Notes |
|---|---|
| Expected Value (EV) | EV = (WinRate × AvgWin) − (LossRate × AvgLoss) |
| Breakeven win rate | = 1 / (1 + PayoffRatio) (e.g., payoff 2 → breakeven ≈ 33%) |
- Test EV sensitivity to slippage and fees.
- Prefer strategies with repeatable, testable edges.
4. Order Types & Execution
Order selection affects fills and realized performance. Implement retries and idempotency for robust execution.
Common order types
- Market, Limit, Stop, Stop-limit, IOC, FOK
Execution best practices
- Include realistic slippage and fill probability in backtests.
- Throttle orders to avoid rate-limit bans and respect exchange rules.
- Use idempotency keys on order endpoints to avoid duplicates.
5. Slippage, Fees & Market Impact
Slippage and fees can erode small margins quickly. Consider both per-trade fees and the market impact of your orders.
- Model fees explicitly in backtests (taker/maker, exchange fees, network fees).
- Estimate slippage per order size using historical tradebook data or simulated fills.
- Break large orders into smaller slices and use TWAP/VWAP where appropriate.
6. Backtesting & Simulation
Backtesting validates a strategy historically but must be done carefully to avoid survivorship and lookahead biases.
- Use tick or fine-grained data where execution matters.
- Include realistic latency, slippage and fees.
- Split data into train/validation/test (walk-forward testing).
7. Monitoring, Logging & Alerts
Robust monitoring prevents runaway losses and helps diagnose issues quickly.
- Emit structured logs (JSON) and persistent trade records.
- Set health checks, heartbeat pings, and alerting for anomalies.
- Reconcile expected vs actual fills regularly.
8. Common Pitfalls
- Overfitting: overly complex rules that don’t generalize.
- Ignoring fees/slippage: small edges can disappear.
- Lack of idempotency: duplicate orders on retries.
- Insufficient monitoring: slow reaction to outages or unexpected market events.
Developer checklist — before going live
- Full sandbox/testnet end-to-end test.
- Idempotent order endpoints and safe retry logic.
- Hard stop-loss and daily/weekly loss caps.
- Backtest with fees and slippage; run walk‑forward validation.
- Monitoring, alerts and reconciliation jobs.
- Rollback/kill-switch tested and documented.
Tip: save the checklist into your ops runbook and pin the kill-switch procedure.
FAQ
How do I size positions?
Use fixed fractional sizing or volatility-based sizing. Test in sandbox and monitor realized drawdowns to calibrate.
What is idempotency?
Idempotent requests include a unique client-order-id so retries do not create duplicates — critical for automation.
How to model slippage?
Estimate average slippage per order size from historical fills or use market impact models; include slippage in backtests and set conservative EV thresholds.
