Skip to content

Streaming Market Data

subscribe

In

api.quote.subscribe?

Out

Signature:
    api.quote.subscribe(
        contract:shioaji.contracts.Contract,
        quote_type:shioaji.constant.QuoteType=<QuoteType.Tick: 'tick'>,
    )
Docstring: <no docstring>
Type:      method

In above you can see the subscribe function signature with contract and quote type parameter, contract we have discussed in previous topic. quote_type is about which quote you want to subscribe(tick or bidask).

Tick

In

api.quote.subscribe(api.Contracts.Stocks["2330"], quote_type=sj.constant.QuoteType.Tick)

Out

Response Code: 200 | Event Code: 16 | Info: MKT/TSE/2330 | Event: Subscribe or Unsubscribe ok

You can start receiving tick data by above action.

In

api.quote.subscribe(api.Contracts.Stocks["2890"], quote_type='tick')

Out

Response Code: 200 | Event Code: 16 | Info: MKT/*/TSE/2890 | Event: Subscribe or Unsubscribe ok

MKT/redisrd/TSE/2330 {'Close': [239.5], 'Time': '11:11:42.624718', 'VolSum': [12206], 'Volume': [5]}
MKT/redisrd/TSE/2330 {'Close': [239.5], 'Time': '11:11:47.628718', 'VolSum': [12206], 'Volume': [2]}

quote_type can also be just string 'tick' is same as sj.constant.QuoteType.Tick, we create enum for your with editor can just use autocomplete tools to help you typing less letter.

In

api.quote.subscribe(api.Contracts.Stocks["2890"], quote_type='tick')

Out

Response Code: 200 | Event Code: 16 | Info: MKT/*/TSE/2890 | Event: Subscribe or Unsubscribe ok

quote_type can also be just string 'tick' is same as sj.constant.QuoteType.Tick, we create enum for your with editor can just use autocomplete tools to help you typing less letter.

BidAsk

In

api.quote.subscribe(api.Contracts.Stocks["2330"], quote_type=sj.constant.QuoteType.BidAsk)

Out

Response Code: 200 | Event Code: 16 | Info: QUT/*/TSE/2330 | Event: Subscribe or Unsubscribe ok

QUT/redisrd/TSE/2330 {'AskPrice': [240.0, 240.5, 241.0, 241.5, 242.0], 'AskVolume': [1808, 1789, 1645, 582, 1170], 'BidPrice': [239.5, 239.0, 238.5, 238.0, 237.5], 'BidVolume': [204, 765, 389, 475, 359], 'Date': '2019/03/18', 'Time': '11:11:42.624718'}

quote_type set as 'bidask' will receive the order book data.

Quote Callback

In default, we set quote callback as print function. Let's talk about set quote callback

In

@api.quote.on_quote
def quote_callback(topic: str, quote: dict):
    print(f"Topic: {topic}, Quote: {quote}")

Out

Topic: MKT/redisrd/TSE/2330, Quote: {'Close': [239.5], 'Time': '11:11:42.624718', 'VolSum': [12206], 'Volume': [5]}

In above with pythonic way using decorator to set your quote callback. When callback setting done, The new arrived quote will use this function to processing.

In

def quote_callback(topic: str, quote: dict):
    print(f"Topic: {topic}, Quote: {quote}")

api.quote.set_quote_callback(quote_callback)

The traditional way to set quote callback.

Event Callback

event

In this api, we use solace as mesh borker. This event mean the status for your client with solace connection situation. If you have no experience with networking, please skip this part, In defalut, we help you reconnect solace broker 50 times without any setting. Best way is keep your network connection alive.

In

@api.quote.on_event
def event_callback(resp_code: int, event_code: int, info: str, event: str):
    print(f'Event code: {event_code} | Event: {event}')

Out

Event code: 16 | Event: Subscribe or Unsubscribe ok

Like the quote callback, your can also set event cllback with two way just make quote -> event.

In

api.quote.set_event_callback?

Out

Signature: api.quote.set_event_callback(func:Callable[[int, int, str, str], NoneType]) -> None
Docstring: <no docstring>
Type:      method

If you want to set event callback please pay attention one the event callback signature.

Event Code

Event Code Event Code Enumerator Description
0 SOLCLIENT_SESSION_EVENT_UP_NOTICE The Session is established.
1 SOLCLIENT_SESSION_EVENT_DOWN_ERROR The Session was established and then went down.
2 SOLCLIENT_SESSION_EVENT_CONNECT_FAILED_ERROR The Session attempted to connect but was unsuccessful.
3 SOLCLIENT_SESSION_EVENT_REJECTED_MSG_ERROR The appliance rejected a published message.
4 SOLCLIENT_SESSION_EVENT_SUBSCRIPTION_ERROR The appliance rejected a subscription (add or remove).
5 SOLCLIENT_SESSION_EVENT_RX_MSG_TOO_BIG_ERROR The API discarded a received message that exceeded the Session buffer size.
6 SOLCLIENT_SESSION_EVENT_ACKNOWLEDGEMENT The oldest transmitted Persistent/Non-Persistent message that has been acknowledged.
7 SOLCLIENT_SESSION_EVENT_ASSURED_PUBLISHING_UP Deprecated -- see notes in solClient_session_startAssuredPublishing.The AD Handshake (that is, Guaranteed Delivery handshake) has completed for the publisher and Guaranteed messages can be sent.
8 SOLCLIENT_SESSION_EVENT_ASSURED_CONNECT_FAILED Deprecated -- see notes in solClient_session_startAssuredPublishing.The appliance rejected the AD Handshake to start Guaranteed publishing. Use SOLCLIENT_SESSION_EVENT_ASSURED_DELIVERY_DOWN instead.
8 SOLCLIENT_SESSION_EVENT_ASSURED_DELIVERY_DOWN Guaranteed Delivery publishing is not available.The guaranteed delivery capability on the session has been disabled by some action on the appliance.
9 SOLCLIENT_SESSION_EVENT_TE_UNSUBSCRIBE_ERROR The Topic Endpoint unsubscribe command failed.
9 SOLCLIENT_SESSION_EVENT_DTE_UNSUBSCRIBE_ERROR Deprecated name; SOLCLIENT_SESSION_EVENT_TE_UNSUBSCRIBE_ERROR is preferred.
10 SOLCLIENT_SESSION_EVENT_TE_UNSUBSCRIBE_OK The Topic Endpoint unsubscribe completed.
10 SOLCLIENT_SESSION_EVENT_DTE_UNSUBSCRIBE_OK Deprecated name; SOLCLIENT_SESSION_EVENT_TE_UNSUBSCRIBE_OK is preferred.
11 SOLCLIENT_SESSION_EVENT_CAN_SEND The send is no longer blocked.
12 SOLCLIENT_SESSION_EVENT_RECONNECTING_NOTICE The Session has gone down, and an automatic reconnect attempt is in progress.
13 SOLCLIENT_SESSION_EVENT_RECONNECTED_NOTICE The automatic reconnect of the Session was successful, and the Session was established again.
14 SOLCLIENT_SESSION_EVENT_PROVISION_ERROR The endpoint create/delete command failed.
15 SOLCLIENT_SESSION_EVENT_PROVISION_OK The endpoint create/delete command completed.
16 SOLCLIENT_SESSION_EVENT_SUBSCRIPTION_OK The subscribe or unsubscribe operation has succeeded.
17 SOLCLIENT_SESSION_EVENT_VIRTUAL_ROUTER_NAME_CHANGED The appliance's Virtual Router Name changed during a reconnect operation.This could render existing queues or temporary topics invalid.
18 SOLCLIENT_SESSION_EVENT_MODIFYPROP_OK The session property modification completed.
19 SOLCLIENT_SESSION_EVENT_MODIFYPROP_FAIL The session property modification failed.
20 SOLCLIENT_SESSION_EVENT_REPUBLISH_UNACKED_MESSAGES After successfully reconnecting a disconnected session, the SDK received an unknown publisher flow name response when reconnecting the GD publisher flow.

Quote Format

Tick

BidAsk

Conclusion

Contract object will be used by a lot of place like place order and subscribe quote etc... So Keep in mind how to get the contract you want to use.