Skip to content

Alpaca Streaming API

Oleg Rakhmatulin edited this page May 18, 2020 · 18 revisions

Alpaca Streaming API .NET Clients

All WebSocket-based streaming messages described here and here is available for you via events of Alpaca.Markets.AlpacaStreamingClient and Alpaca.Markets.AlpacaDataStreamingClient classes respectively. All these events provide you read-only interfaces for related JSON objects.

Client Instance Creation

For creating an instance of each client class we recommend using special extension methods of the IEnvironment interface designed for this task.

For Alpaca.Markets.AlpacaStreamingClient class:

var client = Environments.Paper.GetAlpacaStreamingClient(new SecretKey(KEY_ID, SECRET_KEY));

For Alpaca.Markets.AlpacaDataStreamingClient class:

var client = Environments.Paper.AlpacaDataStreamingClient(new SecretKey(KEY_ID, SECRET_KEY));

You can read more about these extension methods on this Wiki page.

General Usage Pattern

You have to call the ConnectAsync method for connecting the stream and the DisconnectAsync method for disconnecting. It also would be very helpful to free resources used by the instance of Alpaca.Markets.AlpacaStreamingClient or Alpaca.Markets.AlpacaDataStreamingClient objects using standard .NET IDisposable pattern. Another helpful method is ConnectAndAuthenticateAsync - you can use it if you do not want to track connected and authenticated states separately.

Subscribing Trading Events

The Alpaca.Markets.AlpacaStreamingClient class provides two events:

  • The OnAccountUpdate event occurred when a new account update received from stream.
  • The OnTradeUpdate event occurred when a new trade (order) update received from stream.

As soon as client will connect and successfully authenticate you will receive these events. You do not need to call any special methods for enabling/disabling these events - just subscribe to them as usual .NET events.

Subscribing Data Updates

The Alpaca.Markets.AlpacaDataStreamingClient class uses another approach for active subscriptions management. Each active subscription represented as instance of IAlpacaDataSubscription<T> generic interface, instantiated with specific update type.

  • You have to obtain such a subscription object using the GetQuoteSubscription, GetMinuteAggSubscription, or GetMinuteAggSubscription method.
  • Each subscription object exposes the Received event with specific update type (IStreamTrade for trade subscription, IStreamQuote for quote subscription, IStreamAgg for minute aggregates subscription) - you should subscribe this .NET event for receiving notifications from the streaming client.
  • All subscription objects for newly created Alpaca.Markets.AlpacaDataStreamingClient class is in the unsubscribed state regardless of .NET event invocations list size.
  • You can change the state of a single subscription object or a list of different subscriptions using the Subscribe and Unsubscribe methods of the Alpaca.Markets.AlpacaDataStreamingClient class respectively.

The next code snippet demonstrates this process in detail - create client, create subscriptions, subscribe events, wait for events, close connection:

using var client = Environments.Live
    .GetAlpacaDataStreamingClient(new SecretKey(KEY_ID, SECRET_KEY));

await client.ConnectAndAuthenticateAsync();

var waitObjects = new []
{
    new AutoResetEvent(false),
    new AutoResetEvent(false)
};

var tradeSubscription = client.GetTradeSubscription("AAPL");
tradeSubscription.Received += (trade) =>
{
    Console.WriteLine($"Trade received for the {trade.Symbol} contract");
    waitObjects[0].Set();
};

var quoteSubscription = client.GetQuoteSubscription("AAPL");
quoteSubscription.Received += (quote) =>
{
    Console.WriteLine($"Quote received for the {quote.Symbol} contract");
    waitObjects[1].Set();
};

client.Subscribe(tradeSubscription, quoteSubscription);

WaitHandle.WaitAll(waitObjects, TimeSpan.FromSeconds(10));

client.Unsubscribe(tradeSubscription, quoteSubscription);

await client.DisconnectAsync();
Clone this wiki locally