iT邦幫忙

2021 iThome 鐵人賽

DAY 10
0

以下內容,都是 shioaji 的官網文件的內容,只是加了一些我自己的理解,感謝永豐提供這麼完整的 sample code

繪圖會用到 numpy, pandas, bqplot...等套件,如果之前沒有安裝,可以先行安裝

pip install numpy pandas bqplot
  • 引用套件
import json
import datetime as dt
import numpy as np
import pandas as pd
import bqplot as bq
import shioaji as sj
  • 登入 shioaji
api = sj.Shioaji()
api.login(person_id="帳號", passwd="密碼")
  • 建立表格的資料集
bid_color = '#ff0000'
ask_color = '#03ba00'
x_bar_data = np.zeros(10)
y_bar_data = np.zeros(10, dtype=np.int64)
color_bar_data = np.array(['#000000' for i in range(10)])
y_bar_data_diff = np.zeros(10, dtype=np.int64)
  • 建立繪圖的方法
def bidask_bar_chart():
    global x_bar_data, y_bar_data, color_bar_data, y_data_diff
    global bar_bidask, bar_bidask_diff
    x_ord = bq.OrdinalScale()
    y_sc = bq.LinearScale()
    stroke_color = '#000000'
    
    bar_bidask = bq.Bars(
        x = x_bar_data,
        y = y_bar_data,
        scales = {'x': x_ord, 'y': y_sc},
        stroke = stroke_color,
        color_mode = 'group',
        padding = 0,
        colors = color_bar_data.tolist(),
        type = 'stacked',
        orientation = 'horizontal',
        opacities = [0.7 for _ in x_bar_data]
    )

    bar_bidask_diff = bq.Bars(
        x = x_bar_data,
        y = y_bar_data_diff,
        scale = {'x': x_ord, 'y': y_sc},
        stroke = stroke_color, 
        color_mode = 'group', 
        padding = 0, 
        colors = color_bar_data.tolist()[::-1],
        type = 'stacked', 
        orientation = 'horizontal',
        opacities=[0.7 for _ in x_bar_data],
    )
    
    ax_x = bq.Axis(scale=x_ord, orientation='vertical')
    ax_y = bq.Axis(scale=y_sc, tick_format='0.0f')

    fig = bq.Figure(marks=[bar_bidask, bar_bidask_diff])

    return fig

  • 建立資料處理方法
def proc_ask_bid_bardata(topic, quote_msg):
    global x_bar_data, y_bar_data, color_bar_data, y_data_diff
    ask_price = quote_msg.get('AskPrice', [0])[0]
    bid_price = quote_msg.get('BidPrice', [0])[0]

    x_bar_data[:5] = quote_msg['BidPrice'][::-1]
    x_bar_data[5:] = quote_msg['AskPrice']

    y_bar_data[:5] = quote_msg['BidVolume'][::-1]
    y_bar_data[5:] = quote_msg['AskVolume']
    y_bar_data[5:] *= -1
    
    y_bar_data_diff[5:] = quote_msg["DiffAskVol"]
    y_bar_data_diff[:5] = quote_msg["DiffBidVol"][::-1]
    color_bar_data[:5] = bid_color
    color_bar_data[5:] = ask_color

  • 建立圖表更新方法
def update_barchart(x_bar_data, y_bar_data, color_bar_data, y_bar_data_diff):
    global bar_bidask, bar_bidask_diff
    with bar_bidask.hold_sync() and bar_bidask_diff.hold_sync():
        bar_bidask.x = x_bar_data.copy()
        bar_bidask.y = y_bar_data.copy()
        bar_bidask.colors = color_bar_data.tolist()
        bar_bidask_diff.x = x_bar_data.copy()
        bar_bidask_diff.y = y_bar_data_diff.copy()
        bar_bidask_diff.colors = color_bar_data.tolist()[::-1]
  • 建立取得價格後的回呼方法
@sj.on_quote
def quote_callback(topic, quote_msg):
    global x_bar_data, y_bar_data, color_bar_data, y_data_diff
    if topic.startswith('Q'):
        proc_ask_bid_bardata(topic, quote_msg)
        update_barchart(x_bar_data, y_bar_data, color_bar_data, y_bar_data_diff)
  • 建立 solace 事件回呼方法
@sj.on_event
def event_callback(resp_code, event_code, event):
    print("Response Code: {} | Event Code: {} | Event: {}".format(resp_code, event_code, event))
  • 設定回呼方法
api.quote.set_callback(quote_callback)
api.quote.set_event_callback(event_callback)
  • 繪圖
bidask_bar_chart()
  • 訂閱報價
stock2330 = api.Contracts.Stocks["2330"]
api.quote.subscribe(stock2330, quote_type="bidask")

因為我是在盤後才跑這個程式,所以沒有辦法取得即資料,所以暫時不確定以上的程式能不能正確運行,之後如果有機會在交易時間跑的話,會再驗證,如果有問題的話,會再修改。


上一篇
繪圖 - 即時行情
下一篇
繪圖 - 即時 tick 資料
系列文
永豐金融APIs - 從零開始到放棄!?30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言