Package markets.alpaca.client.ws
Class AlpacaStockStream
java.lang.Object
markets.alpaca.client.ws.internal.AbstractAlpacaStream
markets.alpaca.client.ws.internal.AbstractMarketDataStream
markets.alpaca.client.ws.AlpacaStockStream
- All Implemented Interfaces:
AutoCloseable
WebSocket client for the Alpaca real-time stock pricing stream (
/v2/{source}).
Usage
var creds = new AlpacaCredentials(keyId, secretKey);
var stream = AlpacaClientFactory.stockStream(
creds,
StockSource.IEX,
AlpacaStreamEnvironment.PRODUCTION,
new StockStreamListener() {
@Override public void onTrade(StockTrade t) { System.out.println(t); }
});
stream.connect(StockSubscription.builder().trades("AAPL").quotes("AAPL").build());
// Later:
stream.close();
The client automatically reconnects with jittered exponential backoff on unexpected disconnects and re-subscribes to the previously confirmed symbol set after re-authentication.
Callbacks are dispatched on OkHttp's reader thread by default. Use the overloads that accept
an Executor to offload listener work.
-
Field Summary
Fields inherited from class markets.alpaca.client.ws.internal.AbstractMarketDataStream
GSON -
Constructor Summary
ConstructorsConstructorDescriptionAlpacaStockStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, String url, StockStreamListener listener) Creates a new stock stream client for a custom WebSocket URL.AlpacaStockStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, StockSource source, AlpacaStreamEnvironment environment, StockStreamListener listener) Creates a new stock stream client.AlpacaStockStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, StockSource source, AlpacaStreamEnvironment environment, StockStreamListener listener, AlpacaStreamReconnectPolicy reconnectPolicy) Creates a new stock stream client with a custom reconnect policy.AlpacaStockStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, StockSource source, AlpacaStreamEnvironment environment, StockStreamListener listener, AlpacaStreamReconnectPolicy reconnectPolicy, Executor callbackExecutor) Creates a new stock 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(StockSubscription sub) Opens the stream and subscribes after authentication completes.protected voiddispatchDataMessage(com.google.gson.JsonObject msg, String type) Called for each data message element with a non-controlTvalue.protected voidhandleMarketDataClose(int code, String reason, boolean willReconnect) protected voidprotected voidprotected voidnotifyError(int code, String message) protected voidCalled after a reconnect attempt succeeds and the stream is fully re-authenticated (and re-subscribed, if applicable).protected voidnotifyReconnecting(int attempt) Called just before a reconnect attempt is initiated.protected voidnotifySubscriptionConfirmed(Map<String, List<String>> subscriptions) voidSubscribes to the given stock channels and symbols.voidUnsubscribes from the given stock channels and symbols.Methods inherited from class markets.alpaca.client.ws.internal.AbstractMarketDataStream
getConfirmedSubscriptions, handleClose, handleOpen, handleText, resubscribeAfterAuth, sendSubscriptionMessageMethods inherited from class markets.alpaca.client.ws.internal.AbstractAlpacaStream
authenticationFuture, authenticationResultFuture, close, closeTerminal, closeTerminal, completeAuthentication, completeAuthentication, connect, handleBytes, invokeCallback, isClosed, isReconnecting, requireOpenForCommand, resetReconnectAttempts, send, waitForAuthentication, waitForAuthenticationResult
-
Constructor Details
-
AlpacaStockStream
public AlpacaStockStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, StockSource source, AlpacaStreamEnvironment environment, StockStreamListener listener) Creates a new stock stream client. UseAlpacaClientFactory.stockStream(markets.alpaca.client.AlpacaCredentials, markets.alpaca.client.ws.StockSource, markets.alpaca.client.ws.AlpacaStreamEnvironment, markets.alpaca.client.ws.StockStreamListener)rather than calling this constructor directly. -
AlpacaStockStream
public AlpacaStockStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, StockSource source, AlpacaStreamEnvironment environment, StockStreamListener listener, AlpacaStreamReconnectPolicy reconnectPolicy) Creates a new stock stream client with a custom reconnect policy. UseAlpacaClientFactory.stockStream(markets.alpaca.client.AlpacaCredentials, markets.alpaca.client.ws.StockSource, markets.alpaca.client.ws.AlpacaStreamEnvironment, markets.alpaca.client.ws.StockStreamListener)rather than calling this constructor directly. -
AlpacaStockStream
public AlpacaStockStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, StockSource source, AlpacaStreamEnvironment environment, StockStreamListener listener, AlpacaStreamReconnectPolicy reconnectPolicy, Executor callbackExecutor) Creates a new stock stream client with a custom reconnect policy and listener executor. UseAlpacaClientFactory.stockStream(markets.alpaca.client.AlpacaCredentials, markets.alpaca.client.ws.StockSource, markets.alpaca.client.ws.AlpacaStreamEnvironment, markets.alpaca.client.ws.StockStreamListener)rather than calling this constructor directly. -
AlpacaStockStream
public AlpacaStockStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, String url, StockStreamListener listener) Creates a new stock 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
-
handleMarketDataClose
- Specified by:
handleMarketDataClosein classAbstractMarketDataStream
-
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
-
notifyConnected
protected void notifyConnected()- Specified by:
notifyConnectedin classAbstractMarketDataStream
-
notifyAuthenticated
protected void notifyAuthenticated()- Specified by:
notifyAuthenticatedin classAbstractMarketDataStream
-
notifySubscriptionConfirmed
- Specified by:
notifySubscriptionConfirmedin classAbstractMarketDataStream
-
notifyError
- Specified by:
notifyErrorin classAbstractMarketDataStream
-
notifyReconnected
protected void notifyReconnected()Description copied from class:AbstractAlpacaStreamCalled after a reconnect attempt succeeds and the stream is fully re-authenticated (and re-subscribed, if applicable). Default implementation is a no-op.- Overrides:
notifyReconnectedin classAbstractAlpacaStream
-
dispatchDataMessage
Description copied from class:AbstractMarketDataStreamCalled for each data message element with a non-controlTvalue.- Specified by:
dispatchDataMessagein classAbstractMarketDataStream- Parameters:
msg- the JSON object for this messagetype- theTfield value (e.g."t","q","b")
-
subscribe
Subscribes to the given stock channels and symbols. Additive — existing subscriptions for channels not mentioned here are preserved. The server will reply with a subscription confirmation; the listener'sStockStreamListener.onSubscriptionConfirmed(java.util.Map<java.lang.String, java.util.List<java.lang.String>>)will fire.Can be called at any time after
AbstractAlpacaStream.connect()— if not yet authenticated, the request will be buffered and sent after authentication completes. -
connect
Opens the stream and subscribes after authentication completes.This is equivalent to calling
subscribe(StockSubscription)beforeAbstractAlpacaStream.connect(); the subscription is buffered until the server accepts the credentials. -
unsubscribe
Unsubscribes from the given stock channels and symbols. Subtractive — only the specified symbols are removed from the respective channels.
-