Package markets.alpaca.client.ws
Class AlpacaCryptoStream
java.lang.Object
markets.alpaca.client.ws.internal.AbstractAlpacaStream
markets.alpaca.client.ws.internal.AbstractMarketDataStream
markets.alpaca.client.ws.AlpacaCryptoStream
- All Implemented Interfaces:
AutoCloseable
WebSocket client for the Alpaca real-time crypto pricing stream (
/v1beta3/crypto/us).
Usage
var stream = AlpacaClientFactory.cryptoStream(
creds,
AlpacaStreamEnvironment.PRODUCTION,
new CryptoStreamListener() {
@Override public void onTrade(CryptoTrade t) { System.out.println(t); }
});
stream.connect(CryptoSubscription.builder().trades("BTC/USD").bars("*").build());
Only available on the production endpoint. The crypto stream is sourced from the Alpaca exchange. The free plan is limited to 10 symbols for orderbooks.
-
Field Summary
Fields inherited from class markets.alpaca.client.ws.internal.AbstractMarketDataStream
GSON -
Constructor Summary
ConstructorsConstructorDescriptionAlpacaCryptoStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, String url, CryptoStreamListener listener) Creates a new crypto stream client for a custom WebSocket URL.AlpacaCryptoStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamEnvironment environment, CryptoStreamListener listener) Creates a new crypto stream client.AlpacaCryptoStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamEnvironment environment, CryptoStreamListener listener, AlpacaStreamReconnectPolicy reconnectPolicy) Creates a new crypto stream client with a custom reconnect policy.AlpacaCryptoStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamEnvironment environment, CryptoStreamListener listener, AlpacaStreamReconnectPolicy reconnectPolicy, Executor callbackExecutor) Creates a new crypto 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).voidOpens 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 crypto channels and pairs.voidUnsubscribes from the given crypto channels and pairs.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
-
AlpacaCryptoStream
public AlpacaCryptoStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamEnvironment environment, CryptoStreamListener listener) Creates a new crypto stream client. UseAlpacaClientFactory.cryptoStream(markets.alpaca.client.AlpacaCredentials, markets.alpaca.client.ws.AlpacaStreamEnvironment, markets.alpaca.client.ws.CryptoStreamListener)rather than calling this constructor directly. -
AlpacaCryptoStream
public AlpacaCryptoStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamEnvironment environment, CryptoStreamListener listener, AlpacaStreamReconnectPolicy reconnectPolicy) Creates a new crypto stream client with a custom reconnect policy. UseAlpacaClientFactory.cryptoStream(markets.alpaca.client.AlpacaCredentials, markets.alpaca.client.ws.AlpacaStreamEnvironment, markets.alpaca.client.ws.CryptoStreamListener)rather than calling this constructor directly. -
AlpacaCryptoStream
public AlpacaCryptoStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamEnvironment environment, CryptoStreamListener listener, AlpacaStreamReconnectPolicy reconnectPolicy, Executor callbackExecutor) Creates a new crypto stream client with a custom reconnect policy and listener executor. UseAlpacaClientFactory.cryptoStream(markets.alpaca.client.AlpacaCredentials, markets.alpaca.client.ws.AlpacaStreamEnvironment, markets.alpaca.client.ws.CryptoStreamListener)rather than calling this constructor directly. -
AlpacaCryptoStream
public AlpacaCryptoStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, String url, CryptoStreamListener listener) Creates a new crypto 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 crypto channels and pairs. Additive — existing subscriptions for channels not mentioned here are preserved. -
connect
Opens the stream and subscribes after authentication completes.This is equivalent to calling
subscribe(CryptoSubscription)beforeAbstractAlpacaStream.connect(); the subscription is buffered until the server accepts the credentials. -
unsubscribe
Unsubscribes from the given crypto channels and pairs.
-