Class AlpacaTradingStream

java.lang.Object
markets.alpaca.client.ws.internal.AbstractAlpacaStream
markets.alpaca.client.ws.AlpacaTradingStream
All Implemented Interfaces:
AutoCloseable

public final class AlpacaTradingStream extends AbstractAlpacaStream
WebSocket client for the Alpaca real-time trading stream (/stream).

Provides real-time order lifecycle events (trade_updates) for the authenticated brokerage account. The trading stream uses a different wire protocol than the market data streams: authentication uses action: authenticate with credentials nested under data, and subscriptions use action: listen.

Usage


 var stream = AlpacaClientFactory.tradingStream(
     creds,
     TradingEnvironment.PAPER,
     new TradingStreamListener() {
         @Override public void onAuthenticated() {
             // subscribe to trade updates right after auth
         }
         @Override public void onTradeUpdate(TradeUpdate u) { System.out.println(u); }
     });

 stream.connect(TradingSubscription.TRADE_UPDATES);
 

The client reconnects with jittered exponential backoff and re-listens to the previously active streams after re-authentication.

Callbacks are dispatched on OkHttp's reader thread by default. Use the overloads that accept an Executor to offload listener work. The paper endpoint sends binary frames, which are transparently decoded as UTF-8 text.