$ show alpacahq/alpaca-trade-api-csharp
This repository is a comprehensive .NET SDK for the Alpaca Markets API, providing both REST and WebSocket clients for algorithmic trading, market data retrieval (stocks, crypto, options), and account management. The architecture is a layered client library with core (`Alpaca.Markets`) and extensions (`Alpaca.Markets.Extensions`) NuGet packages, targeting multiple .NET frameworks (netstandard2.0/2.1, net462, net8.0, net10.0). Entry points include configuration classes that instantiate typed clients (e.g., `IAlpacaTradingClient`, `IAlpacaDataClient`, `IAlpacaCryptoDataClient`, `IAlpacaOptionsDataClient`) via environment-specific base URLs (live, paper). The library supports advanced order types (bracket, OCO, trailing stop), real-time streaming with automatic reconnection, and efficient pagination using `IAsyncEnumerable`. The codebase exhibits high craftsmanship with clean separation of interfaces, messages, and parameters; fluent builder patterns for orders; and robust testing infrastructure using mocks. The extensions package adds DI integration (`AddAlpaca*Client`), Polly-based retry, and reusable subscription management. While the SDK is actively maintained and well-tested, it relies on Newtonsoft.Json (rather than System.Text.Json) and maintains compatibility with older frameworks, adding complexity. A minor risk is dependency on internal Alpaca API endpoints that could change, but the library's design isolates these concerns.
The LLM identified 4 files worth highlighting for reuse:
Provides generic extension methods that convert paginated REST API calls into `IAsyncEnumerable` streams. This pattern allows consumers to efficiently iterate over large datasets (historical bars, trades, quotes) without manual page token management. The implementation handles both single-symbol and multi-symbol requests, using channels internally, and is reusable for any paginated data source with minimal adaptation.
A base class that wraps WebSocket-based streaming clients with automatic reconnection logic. It monitors connection status, triggers reconnection on failure with configurable delays and retry limits, and restores all previously active subscriptions. This pattern is reusable for any WebSocket client that requires resilience, and the implementation avoids common pitfalls like thread safety and resource leaks.
Base class for all Alpaca streaming clients, implementing WebSocket connection management, authentication handshake, and event dispatching. Uses `System.IO.Pipelines` for efficient non-blocking I/O and provides a modular subscription system. The pattern is a well-structured foundation that can be adapted for any WebSocket API requiring authentication and multiple message types.
A utility class for constructing URL query strings with proper encoding, supporting boolean serialization and collection formatting. It encapsulates the repetitive task of building parameterized URIs and is used extensively across all REST client implementations. The implementation is clean, testable, and directly reusable in any .NET project that needs to construct HTTP query strings.