# Transaction Objects
- Python
- Proto
- C#
- Java
- C++
- JavaScript
# Create the connection
OpenSecTradeContext(filter_trdmarket=TrdMarket.HK, host='127.0.0.1', port=11111, is_encrypt=None, security_firm=SecurityFirm.FUTUSECURITIES)
OpenFutureTradeContext(host='127.0.0.1', port=11111, is_encrypt=None, security_firm=SecurityFirm.FUTUSECURITIES)
Description
Create transaction object and initialize transaction connection.
Select the corresponding transaction objects according to the transaction varieties.Examples Transaction Varieties OpenSecTradeContext Securities Market This transaction object supports following transaction varieties: Securities, Options.OpenFutureTradeContext Futures Market This transaction object supports the transaction variety: Futures.OpenHKTradeContext HK Market (Obsolete) We recommend using OpenSecTradeContext to create securities transaction connection.OpenUSTradeContext US Market (Obsolete) We recommend using OpenSecTradeContext to create securities transaction connection.OpenHKCCTradeContext HKCC Market (Obsolete) We recommend using OpenSecTradeContext to create securities transaction connection.OpenCNTradeContext A-share Market (Obsolete) We recommend using OpenSecTradeContext to create securities transaction connection.Parameters
Parameter Type Description filter_trdmarket TrdMarket Filter accounts according to the transaction market authority. - This parameter is only available for OpenSecTradeContext.
- This parameter is only used to filter accounts and will not affect transaction connections.
host str The IP listened by OpenD. port int The port listened by OpenD. is_encrypt bool Whether to enable encryption. Default None means: use the setting of enable_proto_encrypt.security_firm SecurityFirm Specified security firm Example
from futu import *
trd_ctx = OpenSecTradeContext(filter_trdmarket=TrdMarket.HK, host='127.0.0.1', port=11111, is_encrypt=None, security_firm=SecurityFirm.FUTUSECURITIES)
trd_ctx.close() # After using the connection, remember to close it to prevent the number of connections from running out
2
3
# Close the connection
close()
Description
Close the trading object. By default, the threads created inside the Futu API will prevent the process from exiting, and the process can exit normally only after all Contexts are closed. But through set_all_thread_daemon, all internal threads can be set as daemon threads. At this time, even if close of Context is not called, the process can exit normally.
Example
from futu import *
trd_ctx = OpenSecTradeContext(filter_trdmarket=TrdMarket.HK, host='127.0.0.1', port=11111)
trd_ctx.close() # After using the connection, remember to close it to prevent the number of connections from running out
2
3
# InitConnect.proto
Description
Create transaction object and initialize transaction connection
Parameters
message C2S
{
required int32 clientVer = 1; //Client version number. clientVer = number before "." * 100 + number after ".". For example: clientVer = 1 * 100 + 1 = 101 for version 1.1 , and clientVer = 2 * 100 + 21 = 221 for version 2.21.
required string clientID = 2; //The unique identifier for client, no specific generation rules, the client can guarantee the uniqueness
optional bool recvNotify = 3; //Whether this connection receives notifications of market status or events that transaction needs to be re-unlocked. If True, OpenD will push these notifications to this connection, otherwise false means not receiving or pushing
//If the communication is to be encrypted, you must first configure the RSA key in both OpenD and the client. If it is not configured, it will not be encrypted.
//It will be encrypted if the RSA key is configured and the specified encryption algorithm is not PacketEncAlgo_None. (Even if it is not set here and the RSA key is configured, the default encryption method will be used), and the FTAES_ECB algorithm is used by default
optional int32 packetEncAlgo = 4; //Specify the packet encryption algorithm, see the enumeration definition of Common.PacketEncAlgo
optional int32 pushProtoFmt = 5; //Specify the push protocol format on this connection, if not specified, use the push_proto_type configuration item
optional string programmingLanguage = 6; //Interface programming language, used for statistical language preference
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- Return
message S2C
{
required int32 serverVer = 1; //OpenD version number
required uint64 loginUserID = 2; //OpenD login user ID
required uint64 connID = 3; //The connection ID of this connection, the unique identifier of the connection
required string connAESKey = 4; //The key for subsequent AES encrypted communication of this connection is fixed as a 16-byte long string
required int32 keepAliveInterval = 5; //Heartbeat keepalive interval
optional string aesCBCiv = 6; //The iv of AES encrypted communication CBC encryption mode is fixed as a 16-byte long string
}
message Response
{
required int32 retType = 1 [default = -400]; //Returned result, see the enumeration definition of Common.RetType
optional string retMsg = 2; //Description of returned result
optional int32 errCode = 3; //Error code. The client usually uses retType and retMsg to judge the results and details. ErrCode only logs and accounts only when individual protocols fail.
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- For interface result, refer to RetType
Protocol ID
1001
# Create and initialize the connection
bool InitConnect(String ip, short port, bool isEnableEncrypt)
Description
Create transaction object and initialize transaction connection
Parameters
Parameter Type Description ip str OpenD listening address. port int OpenD listening port. bEnableEncrypt bool Whether to enable encryption. Return
- ret: Whether the execution is started, it does not represent the connection result, and the result is called back through OnInitConnect
Example
public class Program : FTSPI_Trd, FTSPI_Conn {
FTAPI_Trd trd = new FTAPI_Trd();
public Program() {
trd.SetClientInfo("csharp", 1); //Set client information
trd.SetConnCallback(this); //Set connection callback
}
public void Start() {
trd.InitConnect("127.0.0.1", (ushort)11111, false);
}
public void OnInitConnect(FTAPI_Conn client, long errCode, String desc)
{
Console.Write("Trd onInitConnect: ret={0} desc={1} connID={2}\n", errCode, desc, client.GetConnectID());
}
public void OnDisconnect(FTAPI_Conn client, long errCode) {
Console.Write("Trd onDisConnect: {0}\n", errCode);
}
public static void Main(String[] args) {
FTAPI.Init();
Program trd = new Program();
trd.Start();
while (true)
Thread.Sleep(1000 * 600);
}
}
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
- Output
Trd onInitConnect: ret=0 desc= connID=6826804060126078029
# Destroy the connection
void Close()
Description
Destroy connection
Example
FTAPI_Trd trd = new FTAPI_Trd();
trd.InitConnect("127.0.0.1", (ushort)11111, false);
trd.Close();
2
3
# Create and initialize the connection
boolean initConnect(String ip, short port, boolean isEnableEncrypt)
Description
Create transaction object and initialize transaction connection
Parameters
Parameter Type Description ip str OpenD listening address. port int OpenD listening port. bEnableEncrypt bool Whether to enable encryption. Return
- ret: Whether to start execution. Does not represent the connection result, the result is called back through onInitConnect
Example
public class TrdDemo implements FTSPI_Trd, FTSPI_Conn {
FTAPI_Conn_Trd trd = new FTAPI_Conn_Trd();
public TrdDemo() {
trd.setClientInfo("javaclient", 1); //Set client information
trd.setConnSpi(this); //Set connection callback
}
public void start() throws IOException {
trd.initConnect("127.0.0.1", (short)11111, false);
}
@Override
public void onInitConnect(FTAPI_Conn client, long errCode, String desc)
{
System.out.printf("Trd onInitConnect: ret=%b desc=%s connID=%d\n", errCode, desc, client.getConnectID());
}
@Override
public void onDisconnect(FTAPI_Conn client, long errCode) {
System.out.printf("Trd onDisConnect: %d\n", errCode);
}
public static void main(String[] args) throws IOException {
FTAPI.init();
TrdDemo trd = new TrdDemo();
trd.start();
while (true) {
try {
Thread.sleep(1000 * 600);
} catch (InterruptedException exc) {
}
}
}
}
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
# Destroy the connection
void close()
Description
Destroy connection
Example
FTAPI_Conn_Trd trd = new FTAPI_Conn_Trd();
trd.initConnect("127.0.0.1", (short)11111, false);
trd.close();
2
3
# Create and initialize the connection
FTAPI_Trd* CreateTrdApi();
bool InitConnect(const char* szIPAddr, Futu::u16_t nPort, bool bEnableEncrypt);
Description
Create transaction object and initialize transaction connection
Parameters
Parameter Type Description szIPAddr str OpenD listening address. nPort int OpenD listening port. bEnableEncrypt bool Whether to enable encryption. Example
FTAPI_Trd *m_pTrdApi = FTAPI::CreateTrdApi();
m_pTrdApi->InitConnect("127.0.0.1", 11111, false);
FTAPI::ReleaseTrdApi(m_pTrdApi);
2
3
# Destroy the connection
void ReleaseTrdApi(FTAPI_Trd* pTrd);
Description
Destroy connection
Return
- pTrd: Connection example
Example
FTAPI_Trd *m_pTrdApi = FTAPI::CreateTrdApi();
m_pTrdApi->InitConnect("127.0.0.1", 11111, false);
FTAPI::ReleaseTrdApi(m_pTrdApi);
2
3
# Create and initialize the connection
start(ip, port, ssl, key)
Description
Create transaction object and initialize transaction connection
Parameters
Parameter Type Description ip str OpenD listening WebSocket address. port int OpenD listening WebSocket port. ssl bool Whether to enable SSL encryption, refer to WebSocket related. key str The connection private key, otherwise the connection will time out. The key is configurable in OpenD, and the visualization version will randomly specify one if the key is not specified. Example
import ftWebSocket from "@/components/ft-websocket/main.js";
class Example {
example() {
this.websocket = new ftWebSocket();
this.websocket.start("127.0.0.1", 33333, false, null);
}
}
2
3
4
5
6
7
# Close the connection
close()
Description
Close the connection
Example
import ftWebSocket from "@/components/ft-websocket/main.js";
class Example {
example() {
this.websocket = new ftWebSocket();
this.websocket.start("127.0.0.1", 33333, false, null);
this.websocket.close();
}
}
2
3
4
5
6
7
8
- Python
- Proto
- C#
- Java
- C++
- JavaScript
# Create the connection
OpenSecTradeContext(filter_trdmarket=TrdMarket.HK, host='127.0.0.1', port=11111, is_encrypt=None, security_firm=SecurityFirm.FUTUSECURITIES)
OpenFutureTradeContext(host='127.0.0.1', port=11111, is_encrypt=None, security_firm=SecurityFirm.FUTUSECURITIES)
Description
Create transaction object and initialize transaction connection.
Select the corresponding transaction objects according to the transaction varieties.Examples Transaction Varieties OpenSecTradeContext Securities Market This transaction object supports following transaction varieties: Securities, Options.OpenFutureTradeContext Futures Market This transaction object supports the transaction variety: Futures.OpenHKTradeContext HK Market (Obsolete) We recommend using OpenSecTradeContext to create securities transaction connection.OpenUSTradeContext US Market (Obsolete) We recommend using OpenSecTradeContext to create securities transaction connection.OpenHKCCTradeContext HKCC Market (Obsolete) We recommend using OpenSecTradeContext to create securities transaction connection.OpenCNTradeContext A-share Market (Obsolete) We recommend using OpenSecTradeContext to create securities transaction connection.Parameters
Parameter Type Description filter_trdmarket TrdMarket Filter accounts according to the transaction market authority. - This parameter is only available for OpenSecTradeContext.
- This parameter is only used to filter accounts and will not affect transaction connections.
host str The IP listened by OpenD. port int The port listened by OpenD. is_encrypt bool Whether to enable encryption. Default None means: use the setting of enable_proto_encrypt.security_firm SecurityFirm Specified security firm Example
from moomoo import *
trd_ctx = OpenSecTradeContext(filter_trdmarket=TrdMarket.HK, host='127.0.0.1', port=11111, is_encrypt=None, security_firm=SecurityFirm.FUTUSECURITIES)
trd_ctx.close() # After using the connection, remember to close it to prevent the number of connections from running out
2
3
# Close the connection
close()
Description
Close the trading object. By default, the threads created inside the moomoo API will prevent the process from exiting, and the process can exit normally only after all Contexts are closed. But through set_all_thread_daemon, all internal threads can be set as daemon threads. At this time, even if close of Context is not called, the process can exit normally.
Example
from moomoo import *
trd_ctx = OpenSecTradeContext(filter_trdmarket=TrdMarket.HK, host='127.0.0.1', port=11111)
trd_ctx.close() # After using the connection, remember to close it to prevent the number of connections from running out
2
3
# InitConnect.proto
Description
Create transaction object and initialize transaction connection
Parameters
message C2S
{
required int32 clientVer = 1; //Client version number. clientVer = number before "." * 100 + number after ".". For example: clientVer = 1 * 100 + 1 = 101 for version 1.1 , and clientVer = 2 * 100 + 21 = 221 for version 2.21.
required string clientID = 2; //The unique identifier for client, no specific generation rules, the client can guarantee the uniqueness
optional bool recvNotify = 3; //Whether this connection receives notifications of market status or events that transaction needs to be re-unlocked. If True, OpenD will push these notifications to this connection, otherwise false means not receiving or pushing
//If the communication is to be encrypted, you must first configure the RSA key in both OpenD and the client. If it is not configured, it will not be encrypted.
//It will be encrypted if the RSA key is configured and the specified encryption algorithm is not PacketEncAlgo_None. (Even if it is not set here and the RSA key is configured, the default encryption method will be used), and the FTAES_ECB algorithm is used by default
optional int32 packetEncAlgo = 4; //Specify the packet encryption algorithm, see the enumeration definition of Common.PacketEncAlgo
optional int32 pushProtoFmt = 5; //Specify the push protocol format on this connection, if not specified, use the push_proto_type configuration item
optional string programmingLanguage = 6; //Interface programming language, used for statistical language preference
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- Return
message S2C
{
required int32 serverVer = 1; //OpenD version number
required uint64 loginUserID = 2; //OpenD login user ID
required uint64 connID = 3; //The connection ID of this connection, the unique identifier of the connection
required string connAESKey = 4; //The key for subsequent AES encrypted communication of this connection is fixed as a 16-byte long string
required int32 keepAliveInterval = 5; //Heartbeat keepalive interval
optional string aesCBCiv = 6; //The iv of AES encrypted communication CBC encryption mode is fixed as a 16-byte long string
}
message Response
{
required int32 retType = 1 [default = -400]; //Returned result, see the enumeration definition of Common.RetType
optional string retMsg = 2; //Description of returned result
optional int32 errCode = 3; //Error code. The client usually uses retType and retMsg to judge the results and details. ErrCode only logs and accounts only when individual protocols fail.
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- For interface result, refer to RetType
Protocol ID
1001
# Create and initialize the connection
bool InitConnect(String ip, short port, bool isEnableEncrypt)
Description
Create transaction object and initialize transaction connection
Parameters
Parameter Type Description ip str OpenD listening address. port int OpenD listening port. bEnableEncrypt bool Whether to enable encryption. Return
- ret: Whether the execution is started, it does not represent the connection result, and the result is called back through OnInitConnect
Example
public class Program : MMSPI_Trd, MMSPI_Conn {
MMAPI_Trd trd = new MMAPI_Trd();
public Program() {
trd.SetClientInfo("csharp", 1); //Set client information
trd.SetConnCallback(this); //Set connection callback
}
public void Start() {
trd.InitConnect("127.0.0.1", (ushort)11111, false);
}
public void OnInitConnect(MMAPI_Conn client, long errCode, String desc)
{
Console.Write("Trd onInitConnect: ret={0} desc={1} connID={2}\n", errCode, desc, client.GetConnectID());
}
public void OnDisconnect(MMAPI_Conn client, long errCode) {
Console.Write("Trd onDisConnect: {0}\n", errCode);
}
public static void Main(String[] args) {
MMAPI.Init();
Program trd = new Program();
trd.Start();
while (true)
Thread.Sleep(1000 * 600);
}
}
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
- Output
Trd onInitConnect: ret=0 desc= connID=6826804060126078029
# Destroy the connection
void Close()
Description
Destroy connection
Example
MMAPI_Trd trd = new MMAPI_Trd();
trd.InitConnect("127.0.0.1", (ushort)11111, false);
trd.Close();
2
3
# Create and initialize the connection
boolean initConnect(String ip, short port, boolean isEnableEncrypt)
Description
Create transaction object and initialize transaction connection
Parameters
Parameter Type Description ip str OpenD listening address. port int OpenD listening port. bEnableEncrypt bool Whether to enable encryption. Return
- ret: Whether to start execution. Does not represent the connection result, the result is called back through onInitConnect
Example
public class TrdDemo implements MMSPI_Trd, MMSPI_Conn {
MMAPI_Conn_Trd trd = new MMAPI_Conn_Trd();
public TrdDemo() {
trd.setClientInfo("javaclient", 1); //Set client information
trd.setConnSpi(this); //Set connection callback
}
public void start() throws IOException {
trd.initConnect("127.0.0.1", (short)11111, false);
}
@Override
public void onInitConnect(MMAPI_Conn client, long errCode, String desc)
{
System.out.printf("Trd onInitConnect: ret=%b desc=%s connID=%d\n", errCode, desc, client.getConnectID());
}
@Override
public void onDisconnect(MMAPI_Conn client, long errCode) {
System.out.printf("Trd onDisConnect: %d\n", errCode);
}
public static void main(String[] args) throws IOException {
MMAPI.init();
TrdDemo trd = new TrdDemo();
trd.start();
while (true) {
try {
Thread.sleep(1000 * 600);
} catch (InterruptedException exc) {
}
}
}
}
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
# Destroy the connection
void close()
Description
Destroy connection
Example
MMAPI_Conn_Trd trd = new MMAPI_Conn_Trd();
trd.initConnect("127.0.0.1", (short)11111, false);
trd.close();
2
3
# Create and initialize the connection
MMAPI_Trd* CreateTrdApi();
bool InitConnect(const char* szIPAddr, moomoo::u16_t nPort, bool bEnableEncrypt);
Description
Create transaction object and initialize transaction connection
Parameters
Parameter Type Description szIPAddr str OpenD listening address. nPort int OpenD listening port. bEnableEncrypt bool Whether to enable encryption. Example
MMAPI_Trd *m_pTrdApi = MMAPI::CreateTrdApi();
m_pTrdApi->InitConnect("127.0.0.1", 11111, false);
MMAPI::ReleaseTrdApi(m_pTrdApi);
2
3
# Destroy the connection
void ReleaseTrdApi(MMAPI_Trd* pTrd);
Description
Destroy connection
Return
- pTrd: Connection example
Example
MMAPI_Trd *m_pTrdApi = MMAPI::CreateTrdApi();
m_pTrdApi->InitConnect("127.0.0.1", 11111, false);
MMAPI::ReleaseTrdApi(m_pTrdApi);
2
3
# Create and initialize the connection
start(ip, port, ssl, key)
Description
Create transaction object and initialize transaction connection
Parameters
Parameter Type Description ip str OpenD listening WebSocket address. port int OpenD listening WebSocket port. ssl bool Whether to enable SSL encryption, refer to WebSocket related. key str The connection private key, otherwise the connection will time out. The key is configurable in OpenD, and the visualization version will randomly specify one if the key is not specified. Example
import mmWebSocket from "@/components/mm-websocket/main.js";
class Example {
example() {
this.websocket = new mmWebSocket();
this.websocket.start("127.0.0.1", 33333, false, null);
}
}
2
3
4
5
6
7
# Close the connection
close()
Description
Close the connection
Example
import mmWebSocket from "@/components/mm-websocket/main.js";
class Example {
example() {
this.websocket = new mmWebSocket();
this.websocket.start("127.0.0.1", 33333, false, null);
this.websocket.close();
}
}
2
3
4
5
6
7
8