# 基本機能

# API情報の設定

set_client_info(client_id, client_ver)

  • 概要

    API呼び出し情報の設定。任意呼び出しAPI

  • パラメータ

    • client_id: クライアントの識別子
    • client_ver: クライアントのバージョン番号
  • 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

# プロトコル形式の設定

set_proto_fmt(proto_fmt)

  • 概要

    通信プロトコル body の形式を設定。現在 Protobuf|Json の2形式をサポート。デフォルトは ProtoBuf。任意呼び出しAPI

  • パラメータ

    • 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
  • 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

# スレッドモードの設定

set_all_thread_daemon(all_daemon)

  • 概要

    内部で作成されるすべてのスレッドを daemon スレッドに設定するかどうか。

    • daemon スレッドに設定した場合:メインスレッド終了後、プロセスも終了します。
      例:リアルタイムコールバックAPIを使用する場合、メインスレッドの存続を自分で保証する必要があります。メインスレッド終了後はプロセスも終了し、プッシュデータを受信できなくなります。
    • 非 daemon スレッドに設定した場合:メインスレッド終了後も、プロセスは終了しません。
      例:相場または取引オブジェクト作成後、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

# コールバックの設定

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

# 接続 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

# イベント通知コールバック

SysNotifyHandlerBase

  • 概要

    接続切断などの重要メッセージを OpenD に通知

  • プロトコル ID

    1003

  • 戻り値

    パラメータ 説明
    ret RET_CODE API呼び出し結果
    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
  • 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