Skip to content

Extensions Package

Oleg Rakhmatulin edited this page Oct 12, 2020 · 9 revisions

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.

IHttpClient Support

The latest version of ASP.NET Core supports the abstract version of the HttpClient class and replace it with a pluggable IHttpClient 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 IHttpClient 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 IServiceCollection interface that helps to register existing REST API clients in the DI container with minimal effort just by calling methods AddAlpacaDataClient, AddAlpacaTradingClient or AddPolygonDataClient in a fluent manner.

Examples

TBD

Proxy Agent Support

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 or Polygon.io servers.

The IEnvironment interface provides you 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 two extension methods named WithProxyForAlpacaDataStreamingClient and WithProxyForPolygonStreamingClient that provide simple way for overriding default URL values for the IAlpacaDataStreamingClient and IPolygonStreamingClient initialization. If you omit the second argument of these methods it will be read from the DATA_PROXY_WS, or default value equal to ws://127.0.0.1:8765 will be used as a last resort.

Examples

TBD

Auto-reconnection Support

The default implementation of WebSocket clients in this SDK doesn't handle disconnection events from underlying connection - they just forward it via the OnSocketClosed 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 IAlpacaDataStreamingClient and IPolygonStreamingClient interfaces using the WithReconnect extension methods in a fluent manner.

Examples

TBD

Clone this wiki locally