alpaca-websocket
WebSocket client for the Alpaca trading platform real-time data and account updates.
Overview
alpaca-websocket provides a robust, asynchronous interface for Alpaca's streaming APIs. It supports real-time market data (trades, quotes, bars) and account-related event streams (order updates, position changes).
Features
- Real-Time Market Data: Subscribe to stock and crypto streams for sub-second updates.
- Account Updates: Stream trading events to react instantly to order fills and cancellations.
- Automatic Reconnection: Intelligent connection management with configurable retry logic.
- Multiple Stream Support: Connect to Market Data v2 and Trading streams simultaneously.
- Type-Safe Messages: Strongly typed representations for all WebSocket message types.
- Configurable: Flexible configuration for timeouts, reconnection, and buffer sizes.
Installation
Add to your Cargo.toml:
[]
= "0.2.0"
Usage
use AlpacaWebSocketClient;
use AlpacaCredentials;
use StreamType;
async
API Reference
Main Types
AlpacaWebSocketClient- The primary client for managing WebSocket connections.WebSocketConfig- Configuration options for timeouts and reconnection behavior.StreamType- Enum specifying which Alpaca stream to connect to.
Stream Types
StreamType::MarketData- Real-time stock market data (IEX or SIP)StreamType::CryptoData- Real-time cryptocurrency dataStreamType::Trading- Account and order updatesStreamType::News- Real-time news feed
Main Modules
client- WebSocket client implementation.config- Configuration types and builders.messages- Message types for all streaming data.
Message Types
Market Data
TradeMessage- Real-time trade executionsQuoteMessage- Real-time bid/ask quotesBarMessage- Real-time OHLCV bars
Trading Updates
TradeUpdateEvent- Order status changes (fill, cancel, etc.)ConnectionStatus- Connection state notifications
Subscriptions
Subscription- Builder for managing stream subscriptions
Configuration
use WebSocketConfig;
let config = builder
.reconnect_enabled
.reconnect_max_attempts
.reconnect_delay_ms
.ping_interval_secs
.build;
Examples
See the examples/ directory for detailed streaming usage:
stream_market_data.rs- Subscribing to trade and quote updates.stream_trade_updates.rs- Listening for order execution events.
Changelog
v0.2.0 (latest)
- Integrated into the workspace structure.
- Improved subscription management with builder pattern.
- Added support for enhanced Crypto Market Data.
- Added News stream support.
- Improved connection status handling.
- Added configurable reconnection logic.
License
MIT