iT邦幫忙

2021 iThome 鐵人賽

DAY 5
0
永豐金融APIs

永豐金融APIs - 從零開始到放棄!?系列 第 5

基本操作 - 取得報價

訂閱報價

  1. 取得報價後的處理 (callback)
    執行訂閱後,資料會一直自動更新,只要 server 端有發布新的報價,我們的程式就會自動去取回來,所以取回來之後,要怎麼處理,也就是自動化的精髓,這裡我只是示範一下,所以使用 print,只有把回傳的資料列出來而已,後續如果有機會的話,我再來看怎麼寫這個自動化

    • 定義 callback 程式
    @sj.on_quote
    def quote_callback(topic, quote_msg):
        print(topic, quote_msg)
    
    # Shioaji 使用的訂閱 library 為 solace, 這裡的 event 指的是 solace 在訂閱的過程中發生的事件,像是斷線, 重新連線...等,一般來說不用特別設定,所以這裡也只是 print 出來而已
    @sj.on_event
    def event_callback(resp_code, event_code, event):
        print("Response Code: {} | Event Code: {} | Event: {}".format(resp_code, event_code, event))
    
    • 設定 callback
    api.quote.set_callback(quote_back)
    api.quote.set_event_callback(event_callback)
    
  2. 設定要訂閱的股票或期貨代碼

# 台積電 2330
tse2330 = api.Contracts.Stocks['2330']
# 台指期
txfj1 = api.Contracts.Futures['TXFJ1']
  1. 訂閱
    ps: quote 指的是報價,subscribe 就是訂閱報價
# 成交價
api.quote.subscribe(tse2330)

# 五檔
api.quote.subscribe(tse2330, quote_type='bidask')

# 成交價
api.quote.subscribe(txfj1)

# 五檔
api.quote.subscribe(txfj1, quote_type='bidask')

報價回傳資料格式 (quote_message)

  • 股票

code (str): 股票代碼
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 (盤中零股)

  • 股票五檔

code (str): 股票代碼
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 (試撮)

  • 期貨

code (str): code
datetime (datetime.datetime): datetime
open (Decimal): open
underlying_price (Decimal): underlying price (標的物價格)
bid_side_total_vol(int): total buy deal volume (lot, 買盤成交總量)
ask_side_total_vol(int): total sell deal volume (lot, 賣盤成交總量)
avg_price (Decimal): average price
close (Decimal): deal price
high (Decimal): high since market open
low (Decimal): low since market open
amount (Decimal): amount (= deal price)
total_amount (Decimal): total amount (= sum of deal price)
volume (int): volume (lot)
total_volume (int): total volume (lot)
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 (%)
simtrade (int): simulated trade (試撮)

  • 期貨五檔

code (str): code
datetime (datetime.datetime): datetime
bid_total_vol (int): total buy deal volume (lot, 買盤成交總量)
ask_total_vol (int): total sell deal volume (lot, 賣盤成交總量)
bid_price (list of Decimal): bid price
bid_volume (list of int): bid volume (lot)
diff_bid_vol (list of int): (lot, 買價增減量)
ask_price (list of Decimal): ask price
ask_volume (list of int): ask volume (lot)
diff_ask_vol (list of int): (lot, 賣價增減量)
first_derived_bid_price (Decimal): first derived bid price (衍生一檔買價)
first_derived_ask_price (Decimal): first derived ask price (衍生一檔賣價)
first_derived_bid_vol (int): first derived bid volume (衍生一檔買量)
first_derived_ask_vol (int): first derived bid volume (衍生一檔賣量)
underlying_price (Decimal): underlying price (標的物價格)
simtrade (int): simulated trade (試撮)


上一篇
基本操作 - 登入,取得市場清單
下一篇
基本操作 - 下單
系列文
永豐金融APIs - 從零開始到放棄!?30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言