Skip to content

Navigation Menu

Sign in
Sign up

How do I get a Quote Price #119

Answered by Petersoj
Sneepex2 asked this question in Q&A
Discussion options

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
 );
You must be logged in to vote

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

Comment options

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);
You must be logged in to vote
1 reply
Comment options

Thank you very very much for you detailed, comprehensive and quick response as well as code. All works perfectly!

Answer selected by Petersoj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /