-
-
Notifications
You must be signed in to change notification settings - Fork 93
Hello, I want to get a quote's price using stockMarketDataStreaming, how do i do that as "message" is a MarketDataMessage and has no getters. @Petersoj
MarketDataListener marketDataListener = (messageType, message) -> {
if (messageType == MarketDataMessageType.QUOTE) {
//message.getQuotePrice
}
};
alpacaAPI.stockMarketDataStreaming().setListener(marketDataListener);
alpacaAPI.stockMarketDataStreaming().subscribeToControl(
MarketDataMessageType.SUCCESS,
MarketDataMessageType.SUBSCRIPTION,
MarketDataMessageType.ERROR,
MarketDataMessageType.QUOTE
);
alpacaAPI.stockMarketDataStreaming().connect();
alpacaAPI.stockMarketDataStreaming().waitForAuthorization(5, TimeUnit.SECONDS);
if (!alpacaAPI.stockMarketDataStreaming().isValid()) {
System.out.println("Websocket is not valid");
}
alpacaAPI.stockMarketDataStreaming().subscribe(
null,
Arrays.asList("AAPL"),
null
);
All reactions
The messageType passed in the MarketDataListener onMessage method tells you which sub-type to cast the message parameter to. message (which is a MarketDataMessage object) has several sub-types that you must cast to in order to get the desired getters/setters of the market data object. You can see all of MarketDataMessage sub-types here. As shown in the Direct Known Subclasses section in that Javadoc, there are several sub-types including SymbolMessage, which is the super type to the BarMessage, QuoteMessage, and TradeMessage classes. These are the classes should be cast to in order to acquire the relevant getters/setters for the messageType. Additionally, here is a list of all the possible ...
Replies: 1 comment 1 reply
The messageType passed in the MarketDataListener onMessage method tells you which sub-type to cast the message parameter to. message (which is a MarketDataMessage object) has several sub-types that you must cast to in order to get the desired getters/setters of the market data object. You can see all of MarketDataMessage sub-types here. As shown in the Direct Known Subclasses section in that Javadoc, there are several sub-types including SymbolMessage, which is the super type to the BarMessage, QuoteMessage, and TradeMessage classes. These are the classes should be cast to in order to acquire the relevant getters/setters for the messageType. Additionally, here is a list of all the possible MarketDataMessageTypes.
Note that the stockMarketDataStreaming().subscribeToControl() method only takes control message types which are SUCCESS, ERROR, and SUBSCRIPTION and ignores all other MarketDataMessageTypes. That method exists so that you can also listen to "meta" information about the stream. See the Javadoc for more information.
So with that said, the following snippet shows how to properly listen to AAPL quotes:
// Add a 'MarketDataListener' that prints quote data and subscription messages
MarketDataListener marketDataListener = (messageType, message) -> {
if (messageType == MarketDataMessageType.SUBSCRIPTION) {
SubscriptionsMessage subscriptionsMessage = ((SubscriptionsMessage) message);
System.out.printf("New Market Data subscriptions: %s\n", subscriptionsMessage);
} else if (messageType == MarketDataMessageType.QUOTE) {
QuoteMessage quoteMessage = (QuoteMessage) message;
System.out.printf("%s\t%.2f\t%.2f\t%s\n", quoteMessage.getSymbol(),
quoteMessage.getBidPrice(), quoteMessage.getAskPrice(), quoteMessage.getTimestamp());
}
};
alpacaAPI.stockMarketDataStreaming().setListener(marketDataListener);
// Listen to 'SubscriptionsMessage', 'SuccessMessage', and 'ErrorMessage' control messages
// that contain information about the stream's current state. Note that these are subscribed
// to before the websocket is connected since these messages usually are sent
// upon websocket connection.
alpacaAPI.stockMarketDataStreaming().subscribeToControl(
MarketDataMessageType.SUCCESS,
MarketDataMessageType.SUBSCRIPTION,
MarketDataMessageType.ERROR);
// Connect the websocket and confirm authentication
alpacaAPI.stockMarketDataStreaming().connect();
alpacaAPI.stockMarketDataStreaming().waitForAuthorization(5, TimeUnit.SECONDS);
if (!alpacaAPI.stockMarketDataStreaming().isValid()) {
System.out.println("Websocket not valid!");
return;
}
// Listen to AAPL quotes
alpacaAPI.stockMarketDataStreaming().subscribe(
null,
Arrays.asList("AAPL"),
null);
All reactions
Thank you very very much for you detailed, comprehensive and quick response as well as code. All works perfectly!
All reactions
-
👍 1