Skip to content

Stocks

Subscribe

To subscribe quotes is very easy, just call subscribe function with contract which we've discussed in previous topic.

Subscribe

api.quote.subscribe?

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

Quote Parameters:

quote_type: tick price or bid/ask price to subscribe.
    {'tick', 'bidask'}
intraday_odd: 盤中零股
    {True, False}
version: version of quote format.
    {'v1', 'v0'}

Tick

In

api.quote.subscribe(
    api.Contracts.Stocks["2330"], 
    quote_type = sj.constant.QuoteType.Tick, # or 'tick'
    version = sj.constant.QuoteVersion.v1 # or 'v1'
)

Out:

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

Exchange.TSE 
Tick(
    code = '2330', 
    datetime = datetime.datetime(2021, 7, 2, 13, 16, 35, 92970), 
    open = Decimal('590'), 
    avg_price = Decimal('589.05'), 
    close = Decimal('590'), 
    high = Decimal('593'), 
    low = Decimal('587'), 
    amount = Decimal('590000'), 
    total_amount = Decimal('8540101000'), 
    volume = 1, 
    total_volume = 14498, 
    tick_type = 1, 
    chg_type = 4, 
    price_chg = Decimal('-3'), 
    pct_chg = Decimal('-0.505902'), 
    bid_side_total_vol= 6638, 
    ask_side_total_vol = 7860, 
    bid_side_total_cnt = 2694, 
    ask_side_total_cnt = 2705, 
    closing_oddlot_shares = 0, 
    fixed_trade_vol = 0, 
    suspend = 0, 
    simtrade = 0, 
    intraday_odd = 0
)
Response Code: 200 | Event Code: 16 | Info: MKT/*/TSE/2330 | Event: Subscribe or Unsubscribe ok

MKT/idcdmzpcr01/TSE/2330 
{
    'AmountSum': [1688787000.0], 
    'Close': [593.0], 
    'Date': '2021/07/01', 
    'TickType': [2], 
    'Time': '09:10:20.628620', 
    'VolSum': [2837], 
    'Volume': [1]
}

Tick intraday odd

In

api.quote.subscribe(
    api.Contracts.Stocks["2330"], 
    quote_type = sj.constant.QuoteType.Tick, # or 'tick'
    version = sj.constant.QuoteVersion.v1, # or 'v1'
    intraday_odd = True
)

Out

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

Exchange.TSE 
Tick(
    code = '2330', 
    datetime = datetime.datetime(2021, 7, 2, 13, 16, 55, 544646), 
    open = Decimal('591'), 
    avg_price = Decimal('590.24415'), 
    close = Decimal('590'), 
    high = Decimal('591'), 
    low = Decimal('589'), 
    amount = Decimal('276120'), 
    total_amount = Decimal('204995925'), 
    volume = 468, 
    total_volume = 347307, 
    tick_type = 1, 
    chg_type = 4, 
    price_chg = Decimal('-3'), 
    pct_chg = Decimal('-0.505902'), 
    bid_side_total_vol= 68209, 
    ask_side_total_vol = 279566, 
    bid_side_total_cnt = 28, 
    ask_side_total_cnt = 56, 
    closing_oddlot_shares = 0, 
    fixed_trade_vol = 0, 
    suspend = 0, 
    simtrade = 1, 
    intraday_odd = 1
)
Response Code: 200 | Event Code: 16 | Info: TIC/v2/*/TSE/2330/ODDLOT | Event: Subscribe or Unsubscribe ok

TIC/v2/replay/TSE/2330/ODDLOT 
{
    'Date': '2021/07/01', 
    'Time': '09:23:36.880878', 
    'Close': '593', 
    'TickType': 1, 
    'Shares': 1860, 
    'SharesSum': 33152, 
    'Simtrade': 1
}

BidAsk

In

api.quote.subscribe(
    api.Contracts.Stocks["2330"], 
    quote_type = sj.constant.QuoteType.BidAsk, # or 'bidask'
    version = sj.constant.QuoteVersion.v1 # or 'v1'
)

Out

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

Exchange.TSE 
BidAsk(
    code = '2330', 
    datetime = datetime.datetime(2021, 7, 1, 9, 9, 54, 36828), 
    bid_price = [Decimal('593'), Decimal('592'), Decimal('591'), Decimal('590'), Decimal('589')], 
    bid_volume = [248, 180, 258, 267, 163], 
    diff_bid_vol = [3, 0, 0, 0, 0], 
    ask_price = [Decimal('594'), Decimal('595'), Decimal('596'), Decimal('597'), Decimal('598')], 
    ask_volume = [1457, 531, 506, 90, 259], 
    diff_ask_vol = [0, 0, 0, 0, 0], 
    suspend = 0, 
    simtrade = 0,
    intraday_odd = 0
)
Response Code: 200 | Event Code: 16 | Info: QUT/*/TSE/2330 | Event: Subscribe or Unsubscribe ok

QUT/idcdmzpcr01/TSE/2330 
{
    'AskPrice': [594.0, 595.0, 596.0, 597.0, 598.0], 
    'AskVolume': [1465, 532, 507, 92, 258], 
    'BidPrice': [593.0, 592.0, 591.0, 590.0, 589.0], 
    'BidVolume': [254, 178, 255, 268, 163], 
    'Date': '2021/07/01', 
    'Time': '09:09:48.447219'
}

BidAsk intraday odd

In

api.quote.subscribe(
    api.Contracts.Stocks["2330"], 
    quote_type = sj.constant.QuoteType.BidAsk, # or 'bidask'
    version = sj.constant.QuoteVersion.v1 # or 'v1'
    intraday_odd=True
)

Out

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

Exchange.TSE 
BidAsk(
    code = '2330',
    datetime = datetime.datetime(2021, 7, 2, 13, 17, 45, 743299),
    bid_price = [Decimal('589'), Decimal('588'), Decimal('587'), Decimal('586'), Decimal('585')], 
    bid_volume = [59391, 224490, 74082, 68570, 125246], 
    diff_bid_vol = [49874, 101808, 23863, 38712, 77704], 
    ask_price = [Decimal('590'), Decimal('591'), Decimal('592'), Decimal('593'), Decimal('594')], 
    ask_volume = [26355, 9680, 18087, 11773, 3568], 
    diff_ask_vol = [13251, -14347, 39249, -20397, -10591], 
    suspend = 0, 
    simtrade = 1, 
    intraday_odd = 1
)
Response Code: 200 | Event Code: 16 | Info: QUO/v2/*/TSE/2330/ODDLOT | Event: Subscribe or Unsubscribe ok

QUO/v2/replay/TSE/2330/ODDLOT 
{
    'Date': '2021/07/01', 
    'Time': '09:43:47.143789', 
    'BidPrice': ['592', '591', '590', '589', '588'], 
    'AskPrice': ['593', '594', '595', '596', '597'], 
    'BidShares': [16979, 12009, 45045, 5501, 12956], 
    'AskShares': [17276, 14823, 26518, 23388, 10527], 
    'Simtrade': 1
}

Quote Callback

In default, we set quote callback as print function. You can modify callback function as you wish. Just remember, always avoid making calulations inside the callback function.

Tick quote callback

In: pythonic way by using decorator

from shioaji import TickSTKv1, Exchange

@api.on_tick_stk_v1()
def quote_callback(exchange: Exchange, tick:TickSTKv1):
    print(f"Exchange: {exchange}, Tick: {tick}")
@api.quote.on_quote
def quote_callback(topic: str, quote: dict):
    print(f"Topic: {topic}, Quote: {quote}")

In: traditional way

from shioaji import TickSTKv1, Exchange

def quote_callback(exchange: Exchange, tick:TickSTKv1):
    print(f"Exchange: {exchange}, Tick: {tick}")

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

api.quote.set_quote_callback(quote_callback)

Out

Exchange: Exchange.TSE, Tick: Tick(code='2330', datetime=datetime.datetime(2021, 7, 2, 13, 16, 35, 92970), open=Decimal('590'), avg_price=Decimal('589.05'), close=Decimal('590'), high=Decimal('593'), low=Decimal('587'), amount=Decimal('590000'), total_amount=Decimal('8540101000'), volume=1, total_volume=14498, tick_type=1, chg_type=4, price_chg=Decimal('-3'), pct_chg=Decimal('-0.505902'), trade_bid_volume=6638, ask_side_total_vol=7860, bid_side_total_cnt=2694, ask_side_total_cnt=2705, closing_oddlot_shares=0, fixed_trade_vol=0, suspend=0, simtrade=0, intraday_odd=0)
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]}
  • Intraday odd share the callback function with common stock.
  • Advanced quote callback settings please refer to Quote-Binding Mode.

BidAsk quote callback

In: pythonic way by using decorator

from shioaji import BidAskSTKv1, Exchange

@api.on_bidask_stk_v1()
def quote_callback(exchange: Exchange, bidask:BidAskSTKv1):
    print(f"Exchange: {exchange}, BidAsk: {bidask}")
@api.quote.on_quote
def quote_callback(topic: str, quote: dict):
    print(f"Topic: {topic}, Quote: {quote}")

In: traditional way

from shioaji import BidAskSTKv1, Exchange

def quote_callback(exchange: Exchange, bidask:BidAskSTKv1):
    print(f"Exchange: {exchange}, BidAsk: {bidask}")

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

api.quote.set_quote_callback(quote_callback)

Out

Exchange: Exchange.TSE, BidAsk: BidAsk(code='2330', datetime=datetime.datetime(2021, 7, 2, 13, 17, 29, 726428), bid_price=[Decimal('589'), Decimal('588'), Decimal('587'), Decimal('586'), Decimal('585')], bid_volume=[223, 761, 1003, 809, 1274], diff_bid_vol=[0, 0, 0, 0, 0], ask_price=[Decimal('590'), Decimal('591'), Decimal('592'), Decimal('593'), Decimal('594')], ask_volume=[304, 232, 183, 242, 131], diff_ask_vol=[1, 0, 0, 0, 0], suspend=0, simtrade=0, intraday_odd=0)
Topic: QUT/idcdmzpcr01/TSE/2330, Quote: {'AskPrice': [590.0, 591.0, 592.0, 593.0, 594.0], 'AskVolume': [303, 232, 183, 242, 131], 'BidPrice': [589.0, 588.0, 587.0, 586.0, 585.0], 'BidVolume': [224, 762, 1003, 809, 1274], 'Date': '2021/07/02', 'Time': '13:17:26.391840'}
  • Intraday odd share the callback function with common stock.
  • Advanced quote callback settings please refer to Quote-Binding Mode.

Quote Format

Tick

Stock

code (str): code
datetime (datetime.datetime): datetime
open (Decimal): open
avg_price (Decimal): average price
close (Decimal): deal price
high (Decimal): high since market open
low (Decimal): low since market open
amount (Decimal): amount (NTD)
total_amount (Decimal): total amount (NTD)
volume (int): volume (K shares)
    if intraday_odd: (share)
total_volume (int): total volume (K shares)
    if intraday_odd: (share)
tick_type (int): tick type (內外盤別)
    {1: buy deal, 2: sell deal, 0: can't judge}
chg_type (int): (漲跌註記)
    {1: limit up, 2: up, 3: unchanged, 4: down, 5: limit down}
price_chg (Decimal): price change
pct_chg (Decimal): percentage change (%)
bid_side_total_vol(int): total buy deal volume (K shares, 買盤成交總量)
    if intraday_odd: (share)
ask_side_total_vol (int): total sell deal volume (K shares, 賣盤成交總量)
    if intraday_odd: (share)
bid_side_total_cnt (int): total number of buy deal (買盤成交筆數)
ask_side_total_cnt (int): total number of sell deal (賣盤成交筆數)
closing_oddlot_shares (int): (share, 盤後零股成交股數)
fixed_trade_vol (int): fixed trade volume (K shares, 定盤成交量)
    if intraday_odd: 0
suspend (bool): suspend (暫停交易)
simtrade (bool): simulated trade (試撮)
intraday_odd (bool): intraday odd (盤中零股)
AmountSum (list of float): amount of sum
Close (list of float): deal price
Date (str): date (yyyy/MM/dd)
TickType (list of int): 
    {1: buy deal, 2: sell deal, 0: can't judge}
Time (str): time (HH:mm:ss.ffffff)
VolSum (list of int): total volume (K shares)
Volume (list of int): volume (K shares)

BidAsk

Stock

code (str): code
datetime (datetime.datetime): datetime
bid_price (list of Decimal): bid price
bid_volume (list of int): bid volume (K shares)
diff_bid_vol (list of int): (K shares, 買價增減量)
ask_price (list of Decimal): ask price
ask_volume (list of int): ask volume
diff_ask_vol (list of int): (K shares, 賣價增減量)
suspend (bool): suspend (暫停交易)
simtrade (bool): simulated trade (試撮)
AskPrice (list of float): ask price
AskVolume (list of int): ask volume
BidPrice (list of float): bid price
BidVolume (list of int): bid volume
Date (str): date (yyyy/MM/dd)
Time (str): time (HH:mm:ss.ffffff)

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.