The Last Mile
A perfect signal is worthless if execution fails. Order execution is the critical bridge between deciding to trade and actually holding a position.
The Execution Pipeline
When a signal fires: Signal --> Validation --> Sizing --> Order Build --> Submit --> Monitor --> Confirm
Each step can fail. Each step needs error handling.
Step 1: Signal Validation
Before executing, validate: Is this signal fresh? Have we already acted on it? Do we have an opposing position? Does this trade violate risk limits?
Step 2: Position Sizing
Calculate how much to trade using your formula, fetch current balance and positions, apply constraints (maximum size, minimum size, available margin), and round to exchange precision requirements.
Step 3: Order Building
Construct the order with symbol, side, type, size, price (for limit orders), and optional parameters like time-in-force and reduce-only flags.
Step 4: Submission
Pre-flight checks (API valid, connection healthy, rate limit OK), submit with timeout, handle response. The dangerous state is request sent with no response - you must verify.
Step 5: Monitoring
Track order through states: PENDING --> SUBMITTED --> PARTIAL --> FILLED (or CANCELLED). Log each state change.
Step 6: Confirmation
Verify fill price matches expectations, check if entire order filled, update state, and alert on anomalies.
Execution Strategies
Immediate Execution: Market order, get filled now. Best for time-sensitive signals.
Limit Chase: Start with limit, chase price if not filled. Best when slippage matters.
TWAP: Spread execution over time. Best for large orders that might move the market.
Slippage Management
Slippage is the difference between expected and actual fill price. Sources include market movement, order book depth, and network latency. Set maximum acceptable slippage, use limit orders with buffer, break large orders into chunks, and track expected vs actual for every trade.
Position Mode Handling
One-Way Mode: Single position per symbol, simpler but can't hedge.
Hedge Mode: Separate long and short positions, more flexible but complex.
Your execution logic must know which mode the account uses.
Closing Positions
Close by quantity, close all, partial close, or FLIP mode (close and open opposite in one action).
Error Recovery
Order rejected: reduce size, adjust price, wait for rate limit. Order stuck: cancel and recreate. Partial fill: accept, cancel remainder, or send additional order. Position mismatch: always trust exchange state.
Execution Logging
Log everything: signal details, order parameters, exchange response, fill details, timing. Use structured logs (JSON) for easier analysis. Keep execution logs forever.
Takeaway
Order execution is where trading systems most commonly fail. Build execution logic that assumes everything will fail, verifies every step, and recovers gracefully.