# 基礎功能
# 設定 API 資訊
set_client_info(client_id, client_ver)
介紹
設定呼叫 API 的資訊(非必須)。
參數
- client_id: client 的標識
- client_ver: client 的版本號
- Example
from futu import *
SysConfig.set_client_info("MyFutuAPI", 0)
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
quote_ctx.close()
1
2
3
4
2
3
4
# 設置協議格式
set_proto_fmt(proto_fmt)
介紹
設定通訊協議 Body 格式(非必須) 目前支援 Protobuf 或 Json 兩種格式,預設為 Protobuf。
參數
- proto_fmt: 協議格式,參見ProtoFMT
from futu import *
SysConfig.set_proto_fmt(ProtoFMT.Protobuf)
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
quote_ctx.close()
1
2
3
4
2
3
4
- Example
# 對所有連接設置協議加密
enable_proto_encrypt(is_encrypt)
介紹
數據加密 對所有連線的請求及回傳內容進行加密。如需了解協議的加密流程,請詳見這裡。
參數
參數 類型 説明 is_encrypt bool 是否啟用加密
- Example
from futu import * SysConfig.enable_proto_encrypt(is_encrypt = True) SysConfig.set_init_rsa_file("conn_key.txt") # rsa 私鑰文件路徑 quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111) quote_ctx.close()1
2
3
4
5
# 設置私鑰路徑
set_init_rsa_file(file)
介紹
設置 RSA 私鑰文件路徑。如需瞭解協議加密流程,詳見 這裏。
參數
參數 類型 説明 file str 私鑰文件路徑 Example
from futu import *
SysConfig.enable_proto_encrypt(is_encrypt = True)
SysConfig.set_init_rsa_file("conn_key.txt") # rsa 私鑰文件路徑
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
quote_ctx.close()
1
2
3
4
5
2
3
4
5
# 設置線程模式
set_all_thread_daemon(all_daemon)
介紹
執行緒 設定說明是否將所有內部建立的執行緒設定為背景執行緒:
- 若設定為背景執行緒:主執行緒結束後,整個程式也會隨之結束。 例如:使用實時回調 API 時,需自行確保主執行緒持續運作
- 若設定為非背景執行緒:主執行緒結束後,程式不會終止。 例如:建立行情或交易物件後,若未呼叫 close() 關閉連線...
參數
參數 類型 説明 all_daemon bool 是否設置為 daemon 線程 Example
from futu import *
SysConfig.set_all_thread_daemon(True)
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
# 不調用 quote_ctx.close(),進程也會退出
1
2
3
4
2
3
4
# 設置回調
set_handler(handler)
介紹
設定非同步回調處理物件
參數
- Example
import time
from futu import *
class OrderBookTest(OrderBookHandlerBase):
def on_recv_rsp(self, rsp_str):
ret_code, data = super(OrderBookTest,self).on_recv_rsp(rsp_str)
if ret_code != RET_OK:
print("OrderBookTest: error, msg: %s" % data)
return RET_ERROR, data
print("OrderBookTest ", data) # OrderBookTest 自己的處理邏輯
return RET_OK, data
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
handler = OrderBookTest()
quote_ctx.set_handler(handler) # 設置實時擺盤迴調
quote_ctx.subscribe(['HK.00700'], [SubType.ORDER_BOOK]) # 訂閲買賣擺盤類型,OpenD 開始持續收到服務器的推送
time.sleep(15) # 設置腳本接收 OpenD 的推送持續時間為15秒
quote_ctx.close() # 關閉當條連接,OpenD 會在1分鐘後自動取消相應股票相應類型的訂閲
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 獲取連接 ID
get_sync_conn_id()
介紹
取得連線 ID,連線成功初始化後才會產生數值
返回
- conn_id: 連接 ID
- Example
from futu import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
quote_ctx.get_sync_conn_id()
quote_ctx.close() # 結束後記得關閉當條連接,防止連接條數用盡
1
2
3
4
2
3
4
# 事件通知回調
SysNotifyHandlerBase
介紹
系統通知 通知 OpenD 重要訊息(例如連線中斷等)。
協議 ID
1003
返回
參數 類型 説明 ret RET_CODE 接口調用結果 data tuple 當 ret == RET_OK 時,返回 事件通知數據 str 當 ret != RET_OK,返回錯誤描述 事件通知數據 的格式如下:
參數 類型 説明 notify_type SysNotifyType 通知類型 sub_type ProgramStatusType 子類型。當 notify_type == SysNotifyType.PROGRAM_STATUS 時,sub_type 返回程式狀態類型 GtwEventType 子類型。當 notify_type == SysNotifyType.GTW_EVENT 時,sub_type 返回 OpenD 事件通知類型 0 當 notify_type != SysNotifyType.PROGRAM_STATUS 且 notify_type != SysNotifyType.GTW_EVENT 時,sub_type 返回 0 msg dict 事件資訊。當 notify_type == SysNotifyType.CONN_STATUS 時,msg 返回 連接狀態事件資訊 字典 事件資訊。當 notify_type == SysNotifyType.QOT_RIGHT 時,msg 返回 行情權限事件資訊 字典 - 連接狀態事件資訊 字典結構如下(連接狀態類型為 bool,True 表示連接正常,False 表示連接斷開):
{ 'qot_logined': bool1, 'trd_logined': bool2, }1
2
3
4 - 行情權限事件資訊 字典結構如下(點擊瞭解 行情權限):
{ 'hk_qot_right': value1, 'hk_option_qot_right': value2, 'hk_future_qot_right': value3, 'us_qot_right': value4, 'us_option_qot_right': value5, 'us_future_qot_right': value6, // 已廢棄 'cn_qot_right': value7, 'us_index_qot_right': value8, 'us_otc_qot_right': value9, 'sg_future_qot_right': value10, 'jp_future_qot_right': value11, 'us_future_qot_right_cme': value12, 'us_future_qot_right_cbot': value13, 'us_future_qot_right_nymex': value14, 'us_future_qot_right_comex': value15, 'us_future_qot_right_cboe': value16, }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- 連接狀態事件資訊 字典結構如下(連接狀態類型為 bool,True 表示連接正常,False 表示連接斷開):
- Example
import time
from futu import *
class SysNotifyTest(SysNotifyHandlerBase):
def on_recv_rsp(self, rsp_str):
ret_code, data = super(SysNotifyTest, self).on_recv_rsp(rsp_str)
notify_type, sub_type, msg = data
if ret_code != RET_OK:
logger.debug("SysNotifyTest: error, msg: {}".format(msg))
return RET_ERROR, data
if notify_type == SysNotifyType.GTW_EVENT: # OpenD 事件通知
print("GTW_EVENT, type: {} msg: {}".format(sub_type, msg))
elif notify_type == SysNotifyType.PROGRAM_STATUS: # 程序狀態變化通知
print("PROGRAM_STATUS, type: {} msg: {}".format(sub_type, msg))
elif notify_type == SysNotifyType.CONN_STATUS: ## 連接狀態變化通知
print("CONN_STATUS, qot: {}".format(msg['qot_logined']))
print("CONN_STATUS, trd: {}".format(msg['trd_logined']))
elif notify_type == SysNotifyType.QOT_RIGHT: # 行情權限變化通知
print("QOT_RIGHT, hk: {}".format(msg['hk_qot_right']))
print("QOT_RIGHT, hk_option: {}".format(msg['hk_option_qot_right']))
print("QOT_RIGHT, hk_future: {}".format(msg['hk_future_qot_right']))
print("QOT_RIGHT, us: {}".format(msg['us_qot_right']))
print("QOT_RIGHT, us_option: {}".format(msg['us_option_qot_right']))
print("QOT_RIGHT, cn: {}".format(msg['cn_qot_right']))
print("QOT_RIGHT, us_index: {}".format(msg['us_index_qot_right']))
print("QOT_RIGHT, us_otc: {}".format(msg['us_otc_qot_right']))
print("QOT_RIGHT, sg_future: {}".format(msg['sg_future_qot_right']))
print("QOT_RIGHT, jp_future: {}".format(msg['jp_future_qot_right']))
print("QOT_RIGHT, us_future_cme: {}".format(msg['us_future_qot_right_cme']))
print("QOT_RIGHT, us_future_cbot: {}".format(msg['us_future_qot_right_cbot']))
print("QOT_RIGHT, us_future_nymex: {}".format(msg['us_future_qot_right_nymex']))
print("QOT_RIGHT, us_future_comex: {}".format(msg['us_future_qot_right_comex']))
print("QOT_RIGHT, us_future_cboe: {}".format(msg['us_future_qot_right_cboe']))
return RET_OK, data
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
handler = SysNotifyTest()
quote_ctx.set_handler(handler) # 設置回調
time.sleep(15) # 設置腳本接收 OpenD 的推送持續時間為15秒
quote_ctx.close() # 結束後記得關閉當條連接,防止連接條數用盡`
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42