Class AlpacaTradingStream
- All Implemented Interfaces:
AutoCloseable
/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.
-
Constructor Summary
ConstructorsConstructorDescriptionAlpacaTradingStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, String url, TradingStreamListener listener) Creates a new trading stream client for a custom WebSocket URL.AlpacaTradingStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, TradingEnvironment environment, TradingStreamListener listener) Creates a new trading stream client.AlpacaTradingStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, TradingEnvironment environment, TradingStreamListener listener, AlpacaStreamReconnectPolicy reconnectPolicy) Creates a new trading stream client with a custom reconnect policy.AlpacaTradingStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, TradingEnvironment environment, TradingStreamListener listener, AlpacaStreamReconnectPolicy reconnectPolicy, Executor callbackExecutor) Creates a new trading stream client with a custom reconnect policy and listener executor. -
Method Summary
Modifier and TypeMethodDescriptionprotected okhttp3.RequestBuilds the WebSocket upgrade request (URL + any auth headers).voidconnect(TradingSubscription subscription) Opens the stream and starts listening after authentication completes.protected voidhandleClose(int code, String reason, boolean willReconnect) Called when the connection closes, either cleanly or due to an error.protected voidhandleOpen(okhttp3.WebSocket ws) Called when the WebSocket connection is open.protected voidhandleText(okhttp3.WebSocket ws, String text) Called for each text frame received from the server.voidlisten(TradingSubscription subscription) Subscribes to (or replaces) the active set of trading streams.protected voidnotifyReconnecting(int attempt) Called just before a reconnect attempt is initiated.Methods inherited from class markets.alpaca.client.ws.internal.AbstractAlpacaStream
authenticationFuture, authenticationResultFuture, close, closeTerminal, closeTerminal, completeAuthentication, completeAuthentication, connect, handleBytes, invokeCallback, isClosed, isReconnecting, notifyReconnected, requireOpenForCommand, resetReconnectAttempts, send, waitForAuthentication, waitForAuthenticationResult
-
Constructor Details
-
AlpacaTradingStream
public AlpacaTradingStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, TradingEnvironment environment, TradingStreamListener listener) Creates a new trading stream client. UseAlpacaClientFactory.tradingStream(markets.alpaca.client.AlpacaCredentials, markets.alpaca.client.ws.TradingEnvironment, markets.alpaca.client.ws.TradingStreamListener)rather than calling this constructor directly. -
AlpacaTradingStream
public AlpacaTradingStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, TradingEnvironment environment, TradingStreamListener listener, AlpacaStreamReconnectPolicy reconnectPolicy) Creates a new trading stream client with a custom reconnect policy. UseAlpacaClientFactory.tradingStream(markets.alpaca.client.AlpacaCredentials, markets.alpaca.client.ws.TradingEnvironment, markets.alpaca.client.ws.TradingStreamListener)rather than calling this constructor directly. -
AlpacaTradingStream
public AlpacaTradingStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, TradingEnvironment environment, TradingStreamListener listener, AlpacaStreamReconnectPolicy reconnectPolicy, Executor callbackExecutor) Creates a new trading stream client with a custom reconnect policy and listener executor. UseAlpacaClientFactory.tradingStream(markets.alpaca.client.AlpacaCredentials, markets.alpaca.client.ws.TradingEnvironment, markets.alpaca.client.ws.TradingStreamListener)rather than calling this constructor directly. -
AlpacaTradingStream
public AlpacaTradingStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, String url, TradingStreamListener listener) Creates a new trading stream client for a custom WebSocket URL.
-
-
Method Details
-
buildRequest
protected okhttp3.Request buildRequest()Description copied from class:AbstractAlpacaStreamBuilds the WebSocket upgrade request (URL + any auth headers).- Specified by:
buildRequestin classAbstractAlpacaStream
-
handleOpen
protected void handleOpen(okhttp3.WebSocket ws) Description copied from class:AbstractAlpacaStreamCalled when the WebSocket connection is open.- Specified by:
handleOpenin classAbstractAlpacaStream
-
handleText
Description copied from class:AbstractAlpacaStreamCalled for each text frame received from the server.- Specified by:
handleTextin classAbstractAlpacaStream
-
handleClose
Description copied from class:AbstractAlpacaStreamCalled when the connection closes, either cleanly or due to an error.- Specified by:
handleClosein classAbstractAlpacaStream- Parameters:
code- WebSocket close code, or-1for transport failuresreason- human-readable close reason
-
notifyReconnecting
protected void notifyReconnecting(int attempt) Description copied from class:AbstractAlpacaStreamCalled just before a reconnect attempt is initiated.- Overrides:
notifyReconnectingin classAbstractAlpacaStream- Parameters:
attempt- 1-based attempt counter
-
listen
Subscribes to (or replaces) the active set of trading streams.The
streamslist is authoritative: to stop a stream, send the list without it. Can be called before authentication — the request is queued and sent after auth completes. -
connect
Opens the stream and starts listening after authentication completes.This is equivalent to calling
listen(TradingSubscription)beforeAbstractAlpacaStream.connect(); the requested streams are replayed when the server authorizes the connection.
-