Automated Trading
This guide covers automated trading strategies and implementation on Seesaw.
Overview
Automated trading allows you to:
- Execute trades based on programmatic rules
- React faster than manual trading
- Run consistent strategies across many markets
Architecture
Components
Data Sources
On-Chain Data
| Data | Source | Update Frequency |
|---|---|---|
| Market State | Solana RPC | Poll or WebSocket |
| Order Book | Solana RPC | Poll or WebSocket |
| Position | Solana RPC | After each trade |
| Oracle Price | Pyth | ~400ms |
Derived Data
| Metric | Calculation |
|---|---|
| Mid Price | (best_bid + best_ask) / 2 |
| Spread | best_ask - best_bid |
| Implied Probability | price / 10000 |
| Time Remaining | t_end - now |
Strategy Types
1. Momentum Strategy
Trade in the direction of recent price movement:
2. Mean Reversion Strategy
Bet on prices returning to average:
3. Arbitrage Strategy
Exploit price discrepancies:
| Opportunity | Action |
|---|---|
| YES + NO < 10000 bps | Buy both (guaranteed profit) |
| YES + NO > 10000 bps | Sell both (if you own them) |
4. Market Making Strategy
Provide liquidity for spread capture:
See Market Making for details.
Risk Controls
Pre-Trade Checks
| Check | Action if Failed |
|---|---|
| Position limit | Reject order |
| Collateral check | Reject order |
| Market state | Reject order |
| Time remaining | Reject order |
Real-Time Limits
Circuit Breakers
| Trigger | Response |
|---|---|
| 5 failed orders | Pause 30 seconds |
| RPC error | Switch provider |
| Position limit | Cancel all pending |
| Time limit | Close all positions |
Order Execution
Building Transactions
1. Derive PDAs (market, orderbook, position, vault)
2. Build place_order instruction
3. Add compute budget if needed
4. Sign and send
5. Confirm and update state
Retry Logic
Priority Fees
During congestion, add priority fees:
| Congestion | Priority Fee |
|---|---|
| Low | 0 |
| Medium | 10,000 microlamports |
| High | 100,000 microlamports |
State Management
Position Tracking
Track after each transaction:
Position {
yes_shares: current YES holdings
no_shares: current NO holdings
locked_yes: shares in sell orders
locked_no: shares in sell orders
pending_orders: active order IDs
pnl: realized profit/loss
}
Order Tracking
Order {
order_id: unique identifier
side: BuyYes | SellYes | BuyNo | SellNo
price: limit price in bps
quantity: remaining shares
status: pending | filled | cancelled
}
Monitoring
Key Metrics
| Metric | Alert Threshold |
|---|---|
| Fill Rate | < 10% of orders filling |
| Latency | > 2s to confirm |
| Position | > 80% of limit |
| P&L | > -5% of capital |
| Error Rate | > 5% of requests |
Logging
Log all:
- Order submissions
- Fills and partial fills
- Cancellations
- Errors and retries
- Position changes
Implementation Patterns
Event Loop
Polling vs WebSocket
| Method | Pros | Cons |
|---|---|---|
| Polling | Simple, reliable | Latency, RPC load |
| WebSocket | Real-time, efficient | Complex, reconnection |
Multi-Market Trading
For trading multiple markets simultaneously:
- Maintain separate state per market
- Share capital allocation
- Prioritize by opportunity
- Coordinate risk limits
Performance Optimization
Reduce Latency
| Optimization | Benefit |
|---|---|
| Local RPC | Lower network latency |
| Pre-signed transactions | Faster submission |
| Batch updates | Fewer requests |
| Connection pooling | Reuse connections |
Compute Optimization
| Technique | Description |
|---|---|
| Prefetch PDAs | Derive addresses once |
| Cache account data | Reduce RPC calls |
| Parallel requests | Concurrent data fetching |
Testing
Backtesting
Test strategies against historical data:
- Collect historical order book snapshots
- Simulate order execution
- Calculate hypothetical P&L
- Adjust parameters
Paper Trading
Test on devnet before mainnet:
- Deploy to devnet
- Run strategy with fake funds
- Verify behavior matches expectations
- Monitor for edge cases
Production Testing
Gradual rollout:
| Phase | Capital | Duration |
|---|---|---|
| Alpha | 1% | 1 week |
| Beta | 10% | 2 weeks |
| Production | 100% | Ongoing |
Operational Considerations
Uptime Requirements
| Requirement | Solution |
|---|---|
| 24/7 operation | Cloud hosting, redundancy |
| Failover | Multiple instances |
| Monitoring | Alerting system |
Key Security
| Risk | Mitigation |
|---|---|
| Key exposure | Environment variables, vault |
| Transaction limits | On-chain position limits |
| Unauthorized access | IP whitelisting, firewalls |
Disaster Recovery
| Scenario | Response |
|---|---|
| System crash | Auto-restart, state recovery |
| RPC failure | Switch to backup provider |
| Strategy malfunction | Circuit breaker, manual override |
Example: Simple Trading Bot
Strategy
- Every 30 seconds, check Pyth oracle
- If price up 0.5% in last 5 min, buy YES
- If price down 0.5% in last 5 min, buy NO
- Max position: 100 shares
- Exit at 60 seconds before market end
Flow
Next Steps
- Review Market Making for liquidity provision
- Study Risks for risk management
- See SDK Guide for implementation details