以下內容,都是 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
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)
@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")
因為我是在盤後才跑這個程式,所以沒有辦法取得即資料,所以暫時不確定以上的程式能不能正確運行,之後如果有機會在交易時間跑的話,會再驗證,如果有問題的話,會再修改。