-
-
Notifications
You must be signed in to change notification settings - Fork 81
Extensions Package
The Alpaca.Markets.Extensions NuGet package contains some additional helper methods and classes that extend basic SDK features and help to implement some advanced usage scenarios. All code examples that use this extension package assume that you've added the next line into your C# file using
section:
using Alpaca.Markets.Extensions;
The latest version of ASP.NET Core supports the special version of the System.Net.Http.HttpClient class that can be obtained with a pluggable System.Net.Http.IHttpClientFactory interface. This interface is the recommended way for handling HTTP communication in long-running ASP.NET and ordinal .NET Core applications. Moreover, it's the only supported way for implementing proper HTTP communication in the client-side Blazor applications (running in the browser as WebAssembly components). To properly support this scenario all REST API clients in this SDK provide a way for passing a concrete instance of the System.Net.Http.HttpClient interface obtained via DI container into existing client implementations.
The extensions package makes one additional step for supporting ASP.NET Core scenarios - it contains helper methods for the Microsoft.Extensions.DependencyInjection.IServiceCollection interface that helps to register existing REST API clients in the DI container with minimal effort just by calling methods AddAlpacaDataClient or AddAlpacaTradingClient in a fluent manner.
You have to add such calls in your services configuration method (depends on your host):
// Registers IAlpacaTradingClient interface in the dependency injection container
builder.Services.AddAlpacaTradingClient(Environments.Paper, SECRET_KEY);
// Registers IAlpacaDataClient interface in the dependency injection container
builder.Services.AddAlpacaDataClient(Environments.Paper, SECRET_KEY);
Right now you can only connect one WebSocket connection with your user credentials for obtaining the Alpaca or Polygon.io streaming data. If you want to run more than one algorithm, you can't do it without special tricks. The Alpaca Proxy Agent project provides a way for solving this problem using a local (or server, or even could) helper WebSocket server that handles different connections and mixes all of them into the single one connection to the Alpaca servers.
The Alpaca.Markets.IEnvironment interface provides you a way for customizing any URL used by your application but re-implementing this interface only for overriding the single value is not very useful. The extensions package contains extension method named WithProxyForAlpacaDataStreamingClient that provides simple way for overriding default URL values for the Alpaca.Markets.IAlpacaDataStreamingClient initialization. If you omit the second argument of this method it will be read from the DATA_PROXY_WS
environment variable, or a default value equal to ws://127.0.0.1:8765
will be used as a last resort.
// Override Alpaca Proxy Agent URL used by this environment
var alpacaDataStreamingClient = Environments.Paper
.WithProxyForAlpacaDataStreamingClient("ws://localhost:8888")
.GetAlpacaDataStreamingClient(SECRET_KEY)
.WithReconnect();
The default implementation of WebSocket clients in this SDK doesn't handle disconnection events from the underlying connection - they just forward it via the SocketClosed event up to your code and never try to reconnect again. It makes code for these classes more simple and easy to maintain but for most real-world scenarios this behavior is not good enough. Most users expect automatic reconnection capabilities from such streaming adaptors.
It's not easy to add such behavior into the existing classes without breaking backward compatibility and (potentially) introducing the new bugs. The solution is simple - wrap existing implementations into the helper classes that handle reconnection (and most importantly resubscription) properly. These wrappers now can be found in the extensions library and can be easily obtained from the original Alpaca.Markets.IAlpacaDataStreamingClient interface using the WithReconnect extension method in a fluent manner.
// Default reconnection configuration (5 attempts with 1 to 5 seconds random delay)
var alpacaDataStreamingClient = Environments.Paper
.GetAlpacaDataStreamingClient(SECRET_KEY)
.WithReconnect();
// Override max reconnection attempts providing custom value for 2nd method argument
var alpacaDataStreamingClient = Environments.Paper
.GetAlpacaDataStreamingClient(SECRET_KEY)
.WithReconnect(new ReconnectionParameters
{
MaxReconnectionAttempts = 10
});