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'>,
        intraday_odd:bool=False,
    )
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).intraday_odd is about which market you want to subscribe(intraday_odd or not).

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["2330"], quote_type='tick')

Out

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

MKT/idcdmzpcr01/TSE/2330 
{
    'AmountSum': [4739351000.0], 
    'Close': [596.0], 
    'Date': '2021/03/30', 
    'TickType': [2], 
    'Time': '10:01:33.349431', 
    'VolSum': [7932], 
    'Volume': [1]
}

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.

Tick intraday odd

In

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

Out

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

TIC/v2/*/TSE/2330/ODDLOT 
{
    'Date': '2020/10/20', 
    'Time': '10:56:17.431370', 
    'Close': '450', 
    'TickType': 2, 
    'Shares': 24, 
    'SharesSum': 2995
}

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/*/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.

BidAsk intraday odd

In

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

Out

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

QUO/v2/*/TSE/2330/ODDLOT 
{
    'Date': '2020/10/20', 
    'Time': '10:56:17.431370', 
    'BidPrice': ['450', '449.5', '449', '448.5', '448'], 
    'AskPrice': ['451', '451.5', '452', '452.5', '453'], 
    'BidShares': [137, 2994, 2994, 3005, 785], 
    'AskShares': [403, 500, 100, 45, 123]
}

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/*/TSE/2330, Quote: {'AmountSum': [4739351000.0], 'Close': [596.0], 'Date': '2021/03/30', 'TickType': [2], 'Time': '10:01:33.349431', 'VolSum': [7932], 'Volume': [1]}

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

Stock

AmountSum (list of float): amount of sum.
Close (list of float): deal price.
Date (str): date.
TickType (list of int): 
    {1: deal of buy, 2: deal of sell, 0: can't judge}
Time (str): time.
VolSum (list of int): sum of volume.
Volume (list of int): deal volume.

Futures

Amount (list of float): amount of sum.
AmountSum (list of float): amount of sum.
AvgPrice (list of float): average price.
Close (list of float): close.
Code (str): contract code.
Date (str): date.
DiffPrice (list of float): diff price.
DiffRate (list of float): diff rate.
DiffType (list of str): diff type.
High (list of float): high.
Low (list of float): low.
Open (float): open.
TargetKindPrice (float): target kind price.
TickType (list of int): tick type.
    {1: deal of buy, 2: deal of sell, 0: can't judge}
Time (str): time.
TradeAskVolSum (int): trade ask volume sum.
TradeBidVolSum (int): trade bid volume sum.
VolSum (list of int): sum of volume.
Volume (list of int): deal volume.

BidAsk

Stock

AskPrice (list of float): ask price.
AskVolume (list of float): ask volume.
BidPrice (list of float): bid price.
BidVolume (list of float): bid volume.
Date (str): date.
Time (str): time.

Futures

AskPrice (list of float): AskPrice.
AskVolSum (list of int): AskVolSum.
AskVolume (list of int): AskVolume.
BidPrice (list of float): BidPrice.
BidVolSum (list of int): BidVolSum.
BidVolume (list of int): BidVolume.
Code (str): Code.
Date (str): Date.
DiffAskVol (list of int): DiffAskVol.
DiffAskVolSum (int): DiffAskVolSum.
DiffBidVol (list of int): DiffBidVol.
DiffBidVolSum (int): DiffBidVolSum.
FirstDerivedAskPrice (float): FirstDerivedAskPrice.
FirstDerivedAskVolume (int): FirstDerivedAskVolume.
FirstDerivedBidPrice (float): FirstDerivedBidPrice.
FirstDerivedBidVolume (int): FirstDerivedBidVolume.
TargetKindPrice (float): TargetKindPrice.
Time (str): Time.

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.