# Basic Functions

# Set Interface Information(deprecated)

set_client_info(client_id, client_ver)

  • Introduction

    Set calling interface information (unnecessary).

  • Parameters

    • client_id: the identification of the client
    • client_ver: the version number of the client
from moomoo import *
SysConfig.set_client_info("MymoomooAPI", 0)
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
quote_ctx.close()
1
2
3
4

# Set Protocol Format

set_proto_fmt(proto_fmt)

  • Introduction

    Set the communication protocol body format, Protobuf and Json formats are currently supported , default ProtoBuf, unnecessary interface

  • Parameters

    • proto_fmt: protocol format, refer to ProtoFMT
  • Example
from moomoo import *
SysConfig.set_proto_fmt(ProtoFMT.Protobuf)
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
quote_ctx.close()
1
2
3
4

# Set Protocol Encryption Globally

Enable_proto_encrypt(is_encrypt)

  • Introduction Setting protocol encryption can help users protect their requests and returned contents globally. For more information about Protocol Encryption Process, please check here.

  • Parameters

    Parameter Type Description
    is_encrypt bool Enable encryption or not.
  • Example
from moomoo import *
SysConfig.enable_proto_encrypt(True)
SysConfig.set_init_rsa_file("conn_key.txt")   # rsa private key file path
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
quote_ctx.close()
1
2
3
4
5

# Set the Path of Private Key

set_init_rsa_file(file)

  • Introduction

    Set the Path of Private Key in moomoo API. For more information about Protocol Encryption Process, please check here.

  • Parameters

    Parameter Type Description
    file str Private key file path.
  • Example

from moomoo import *
SysConfig.enable_proto_encrypt(True)
SysConfig.set_init_rsa_file("conn_key.txt")   # rsa private key file path
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
quote_ctx.close()
1
2
3
4
5

# Set Thread Mode

set_all_thread_daemon(all_daemon)

  • Introduction Whether to set all internally threads to be daemon threads.

    • If it is set to be daemon threads, then after the main thread exits, the process also exits.
      For example, when using the real-time callback API, you need to make sure the main thread survives by yourself. Otherwise, when the main thread exits, the process also exits and you will no longer receive the push data.
    • If it is set to a non-daemon thread, the process will not exit after the main thread exits. For example, if you do not call close() to close the connection after creating a quote or trade object, the process will not exit even if the main thread exits.
  • Parameters

    Parameter Type Description
    all_daemon bool Whether to set threads to be daemon threads.
  • Example
from moomoo import *
SysConfig.set_all_thread_daemon(True)
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
# the process will exit without calling quote_ctx.close(), 
1
2
3
4

# Set Callback

set_handler(handler)

  • Introduction

    Set asynchronous callback processing object

import time
from moomoo import *
class OrderBookTest(OrderBookHandlerBase):
    def on_recv_rsp(self, rsp_pb):
        ret_code, data = super(OrderBookTest,self).on_recv_rsp(rsp_pb)
        if ret_code != RET_OK:
            print("OrderBookTest: error, msg: %s" % data)
            return RET_ERROR, data
        print("OrderBookTest ", data) # OrderBookTest's own processing logic
        return RET_OK, data
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
handler = OrderBookTest()
quote_ctx.set_handler(handler) # Setting real-time order book callback
quote_ctx.subscribe(['HK.00700'], [SubType.ORDER_BOOK]) # Subscribe to the order book type, OpenD starts to receive pushed data from the server continuously
time.sleep(15) # Set the script to receive OpenD push duration to 15 seconds
quote_ctx.close() # Close the current connection, OpenD will automatically cancel the subscription of the corresponding stock in 1 minute
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# Get Connection ID

get_sync_conn_id()

  • Introduction

    Get the connection ID, the value will be available after the connection is successfully initialized

  • Return

    • conn_id: connection ID
  • Example
from moomoo import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
quote_ctx.get_sync_conn_id()
quote_ctx.close() # After using the connection, remember to close it to prevent the number of connections from running out
1
2
3
4

# Event Notification Callback

SysNotifyHandlerBase

  • Introduction

    Notify OpenD of some important news, such as disconnection, etc.

  • Protocol ID

    1003

  • Return

    Field Type Description
    ret int Returned value. On success, ret == RET_OK. On error, ret != RET_OK.
    data tuple If ret == RET_OK, event notification data is returned.
    str If ret != RET_OK, error description is returned.
    • The format of event notification data is as follows:


      Field Type Description
      notify_type SysNotifyType Notification data type
      sub_type ProgramStatusType Subtype. If notify_type == SysNotifyType.PROGRAM_STATUS, program status type is returned.
      GtwEventType Subtype. If notify_type == SysNotifyType.GTW_EVENT, OpenD event type is returned.
      0 If notify_type !=SysNotifyType.PROGRAM_STATUS and notify_type !=SysNotifyType.GTW_EVENT, no useful information is returned.
      msg dict Event information. If notify_type == SysNotifyType.CONN_STATUS, connection status event information is returned.
      Event information. If notify_type == SysNotifyType.QOT_RIGHT, quote right event information is returned.

      • The format of connection status event information is as follows(The value of connection status is a bool type, with True for normal, and False for disconnected):
        {
            'qot_logined': bool1, 
            'trd_logined': bool2,
        }
        
        1
        2
        3
        4
      • The format of quote right event information is as follows(the type of quote right refers to Quote Right):
        {
            '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,  // deprecated
            '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 moomoo 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 event notification
            print("GTW_EVENT, type: {} msg: {}".format(sub_type, msg))
        elif (notify_type == SysNotifyType.PROGRAM_STATUS):  # Notification of change in program status
            print("PROGRAM_STATUS, type: {} msg: {}".format(sub_type, msg))
        elif (notify_type == SysNotifyType.CONN_STATUS):  ## Notification of change in connection status
            print("CONN_STATUS, qot: {}".format(msg['qot_logined']))
            print("CONN_STATUS, trd: {}".format(msg['trd_logined']))
        elif (notify_type == SysNotifyType.QOT_RIGHT):  # Notification of change in quote 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, us_future: {}".format(msg['us_future_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)   # Set real-time swing callback
time.sleep(15)  # Set the script to receive OpenD push duration to 15 seconds
quote_ctx.close()  # After using the connection, remember to close it to prevent the number of connections from running out
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