Package markets.alpaca.client.ws
Class AlpacaNewsStream
java.lang.Object
markets.alpaca.client.ws.internal.AbstractAlpacaStream
markets.alpaca.client.ws.internal.AbstractMarketDataStream
markets.alpaca.client.ws.AlpacaNewsStream
- All Implemented Interfaces:
AutoCloseable
WebSocket client for the Alpaca real-time news stream (
/v1beta1/news).
Usage
var stream = AlpacaClientFactory.newsStream(
creds,
AlpacaStreamEnvironment.PRODUCTION,
new NewsStreamListener() {
@Override public void onArticle(NewsArticle a) { System.out.println(a.headline()); }
});
stream.connect(NewsSubscription.builder().symbols("AAPL", "TSLA").build());
Only available on the production endpoint. Use "*" as a symbol to receive news for all
available symbols.
-
Field Summary
Fields inherited from class markets.alpaca.client.ws.internal.AbstractMarketDataStream
GSON -
Constructor Summary
ConstructorsConstructorDescriptionAlpacaNewsStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, String url, NewsStreamListener listener) Creates a new news stream client for a custom WebSocket URL.AlpacaNewsStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamEnvironment environment, NewsStreamListener listener) Creates a new news stream client.AlpacaNewsStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamEnvironment environment, NewsStreamListener listener, AlpacaStreamReconnectPolicy reconnectPolicy) Creates a new news stream client with a custom reconnect policy.AlpacaNewsStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamEnvironment environment, NewsStreamListener listener, AlpacaStreamReconnectPolicy reconnectPolicy, Executor callbackExecutor) Creates a new news 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(NewsSubscription 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 news for the given symbols.voidUnsubscribes from news for the given 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
-
AlpacaNewsStream
public AlpacaNewsStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamEnvironment environment, NewsStreamListener listener) Creates a new news stream client. UseAlpacaClientFactory.newsStream(markets.alpaca.client.AlpacaCredentials, markets.alpaca.client.ws.AlpacaStreamEnvironment, markets.alpaca.client.ws.NewsStreamListener)rather than calling this constructor directly. -
AlpacaNewsStream
public AlpacaNewsStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamEnvironment environment, NewsStreamListener listener, AlpacaStreamReconnectPolicy reconnectPolicy) Creates a new news stream client with a custom reconnect policy. UseAlpacaClientFactory.newsStream(markets.alpaca.client.AlpacaCredentials, markets.alpaca.client.ws.AlpacaStreamEnvironment, markets.alpaca.client.ws.NewsStreamListener)rather than calling this constructor directly. -
AlpacaNewsStream
public AlpacaNewsStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamEnvironment environment, NewsStreamListener listener, AlpacaStreamReconnectPolicy reconnectPolicy, Executor callbackExecutor) Creates a new news stream client with a custom reconnect policy and listener executor. UseAlpacaClientFactory.newsStream(markets.alpaca.client.AlpacaCredentials, markets.alpaca.client.ws.AlpacaStreamEnvironment, markets.alpaca.client.ws.NewsStreamListener)rather than calling this constructor directly. -
AlpacaNewsStream
public AlpacaNewsStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, String url, NewsStreamListener listener) Creates a new news 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 news for the given symbols. Additive. UseNewsSubscription.builder().symbols("*").build()for all symbols. -
connect
Opens the stream and subscribes after authentication completes.This is equivalent to calling
subscribe(NewsSubscription)beforeAbstractAlpacaStream.connect(); the subscription is buffered until the server accepts the credentials. -
unsubscribe
Unsubscribes from news for the given symbols.
-