Alpaca Integration¶
Execute trades through Alpaca Markets.
Overview¶
Alpaca provides:
- Commission-free trading
- Paper trading for testing
- REST and WebSocket APIs
- Extended hours trading
Setup¶
Create Account¶
- Sign up at alpaca.markets
- Generate API keys in dashboard
- Note your API key and secret
Configure sigc¶
YAML
# sigc.yaml
output:
type: alpaca
alpaca:
api_key: ${ALPACA_API_KEY}
api_secret: ${ALPACA_API_SECRET}
base_url: https://api.alpaca.markets # Live
# base_url: https://paper-api.alpaca.markets # Paper
paper: true # Start with paper trading
Environment Variables¶
Paper Trading¶
Always test with paper trading first:
Paper trading:
- Uses separate paper account
- Same API, different endpoint
- Real market data
- Simulated execution
Order Types¶
Market Orders¶
Limit Orders¶
YAML
output:
alpaca:
orders:
type: limit
limit_offset_pct: 0.1 # 0.1% better than market
time_in_force: day
Order Parameters¶
| Parameter | Values | Description |
|---|---|---|
type |
market, limit, stop, stop_limit |
Order type |
time_in_force |
day, gtc, ioc, fok |
Time in force |
extended_hours |
true, false |
Trade in extended hours |
Execution Configuration¶
Basic Execution¶
TWAP Execution¶
VWAP Execution¶
YAML
output:
alpaca:
execution:
algorithm: vwap
duration_minutes: 60
participation_rate: 0.05 # 5% of volume
Smart Routing¶
Position Synchronization¶
Sync Mode¶
Full Sync¶
Closes all positions not in target weights:
Incremental Sync¶
Only adjusts positions in target:
Account Information¶
Check Account¶
Text Only
Alpaca Account:
Account ID: abc123
Status: ACTIVE
Cash: $100,000.00
Portfolio Value: $250,000.00
Buying Power: $500,000.00
Equity: $250,000.00
Margin Multiplier: 2x
Restrictions:
Trading Blocked: No
Transfers Blocked: No
Check Positions¶
Text Only
Current Positions:
Ticker | Quantity | Market Value | Unrealized P&L
-------+----------+--------------+---------------
AAPL | 500 | $92,820 | +$1,520
MSFT | 250 | $93,628 | +$2,128
GOOGL | 100 | $14,021 | -$179
...
Order Management¶
View Orders¶
Text Only
Open Orders:
Order ID | Ticker | Side | Qty | Type | Status
---------+--------+------+-----+-------+--------
ord_123 | NVDA | buy | 50 | limit | pending
ord_124 | AMD | sell | 100 | limit | filled
Cancel Orders¶
Safety Features¶
Position Limits¶
Order Validation¶
YAML
output:
alpaca:
safety:
require_limit_price: true
max_slippage_pct: 0.5
check_buying_power: true
Rate Limiting¶
Monitoring¶
Order Status¶
Text Only
Order ord_123:
Symbol: AAPL
Side: buy
Quantity: 100
Type: limit
Limit Price: $185.00
Status: filled
Filled Qty: 100
Filled Avg Price: $184.95
Submitted: 2024-01-15 09:30:05
Filled: 2024-01-15 09:30:08
Activity Feed¶
WebSocket Streaming¶
Error Handling¶
Common Errors¶
| Error | Cause | Solution |
|---|---|---|
insufficient_funds |
Not enough buying power | Reduce position sizes |
invalid_qty |
Fractional shares issue | Use whole shares |
market_closed |
Outside trading hours | Wait or use extended hours |
asset_not_tradeable |
Stock not available | Check asset status |
Retry Logic¶
YAML
output:
alpaca:
retry:
max_attempts: 3
delay_seconds: 5
backoff: exponential
retryable_errors:
- connection_error
- rate_limit
Fallback Behavior¶
YAML
output:
alpaca:
on_error:
reject: log_and_continue # log_and_continue | halt | alert
insufficient_funds: reduce_size
market_closed: queue_for_open
Extended Hours¶
YAML
output:
alpaca:
extended_hours:
enabled: true
pre_market: "04:00" # 4 AM - 9:30 AM
after_hours: "20:00" # 4 PM - 8 PM
Multi-Account¶
Configure Multiple Accounts¶
YAML
output:
type: alpaca
accounts:
- name: main
api_key: ${ALPACA_MAIN_KEY}
api_secret: ${ALPACA_MAIN_SECRET}
allocation: 0.7
- name: secondary
api_key: ${ALPACA_SEC_KEY}
api_secret: ${ALPACA_SEC_SECRET}
allocation: 0.3
Example: Full Configuration¶
YAML
# sigc.yaml
output:
type: alpaca
alpaca:
api_key: ${ALPACA_API_KEY}
api_secret: ${ALPACA_API_SECRET}
paper: false # Live trading
orders:
type: limit
limit_offset_pct: 0.05
time_in_force: day
extended_hours: false
execution:
algorithm: twap
duration_minutes: 30
sync:
mode: full
reconcile: true
close_unlisted: true
safety:
max_position_pct: 0.05
max_order_value: 100000
check_buying_power: true
retry:
max_attempts: 3
delay_seconds: 5
streaming:
enabled: true
events: [trade_updates]
Best Practices¶
1. Start with Paper Trading¶
Run paper trading for at least 30 days.
2. Use Limit Orders¶
Avoid unexpected fills.
3. Set Conservative Limits¶
4. Monitor in Real-Time¶
5. Handle Errors Gracefully¶
Next Steps¶
- Safety Systems - Pre-trade checks
- Monitoring - Production monitoring
- Yahoo Finance - Free data source