# 解鎖交易
- Python
- Proto
- C#
- Java
- C++
- JavaScript
unlock_trade(password=None, password_md5=None, is_unlock=True)
介紹
解鎖或鎖定交易
參數
參數 類型 說明 password str 交易密碼 如果 password_md5 不為空,就使用傳入的 password_md5 解鎖;否則使用 password 轉 MD5 得到 password_md5 再解鎖password_md5 str 交易密碼的 32 位 MD5 加密(全小寫) 解鎖交易必須要填密碼,鎖定交易忽略is_unlock bool 解鎖或鎖定 True:解鎖
False:鎖定
回傳
參數 類型 說明 ret RET_CODE 介面執行結果 msg NoneType 當 ret == RET_OK 時,回傳 None str 當 ret != RET_OK 時,回傳錯誤描述 Example
from futu import *
pwd_unlock = '123456'
trd_ctx = OpenSecTradeContext(filter_trdmarket=TrdMarket.HK, host='127.0.0.1', port=11111, security_firm=SecurityFirm.FUTUSECURITIES)
ret, data = trd_ctx.unlock_trade(pwd_unlock)
if ret == RET_OK:
print('unlock success!')
else:
print('unlock_trade failed: ', data)
trd_ctx.close()
2
3
4
5
6
7
8
9
- Output
unlock success!
# Trd_UnlockTrade.proto
介紹
解鎖或鎖定交易
參數
message C2S
{
required bool unlock = 1; //true 解鎖交易,false 鎖定交易
optional string pwdMD5 = 2; //交易密碼的 32 位 MD5 加密(全小寫),解鎖交易必須要填密碼,鎖定交易不需要驗證密碼,可不填
optional int32 securityFirm = 3; //券商標記,取值見 Trd_Common.SecurityFirm
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
- 帳戶所屬券商參見 SecurityFirm
- 回傳
message S2C
{
}
message Response
{
//以下3個字段每條協議都有,註釋說明在 InitConnect.proto 中
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
- 介面執行結果,結構參見 RetType
協議 ID
2005
uint UnlockTrade(TrdUnlockTrade.Request req);
virtual void OnReply_UnlockTrade(FTAPI_Conn client, uint nSerialNo, TrdUnlockTrade.Response rsp);
介紹
解鎖或鎖定交易
參數
message C2S
{
required bool unlock = 1; //true 解鎖交易,false 鎖定交易
optional string pwdMD5 = 2; //交易密碼的 32 位 MD5 加密(全小寫),解鎖交易必須要填密碼,鎖定交易不需要驗證密碼,可不填
optional int32 securityFirm = 3; //券商標記,取值見 Trd_Common.SecurityFirm
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
- 帳戶所屬券商參見 SecurityFirm
- 回傳
message S2C
{
}
message Response
{
//以下3個字段每條協議都有,註釋說明在 InitConnect.proto 中
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
- 介面執行結果,結構參見 RetType
- Example
public class Program : FTSPI_Trd, FTSPI_Conn {
FTAPI_Trd trd = new FTAPI_Trd();
public Program() {
trd.SetClientInfo("csharp", 1); //設定用戶端資訊
trd.SetConnCallback(this); //設定連線回呼
trd.SetTrdCallback(this); //設定交易回呼
}
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());
if (errCode != 0)
return;
TrdUnlockTrade.C2S c2s = TrdUnlockTrade.C2S.CreateBuilder()
.SetPwdMD5("e10adc3949ba59abbe56e057f20f883e")
.SetUnlock(true)
.SetSecurityFirm((int)TrdCommon.SecurityFirm.SecurityFirm_FutuSecurities)
.Build();
TrdUnlockTrade.Request req = TrdUnlockTrade.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = trd.UnlockTrade(req);
Console.Write("Send TrdUnlockTrade: {0}\n", seqNo);
}
public void OnDisconnect(FTAPI_Conn client, long errCode) {
Console.Write("Trd onDisConnect: {0}\n", errCode);
}
public void OnReply_UnlockTrade(FTAPI_Conn client, uint nSerialNo, TrdUnlockTrade.Response rsp)
{
Console.Write("Reply: TrdUnlockTrade: {0}\n", nSerialNo);
Console.Write("retMsg: {0}\n", rsp.RetMsg);
}
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
- Output
Trd onInitConnect: ret=0 desc= connID=6826809697310867898
Send TrdUnlockTrade: 3
Reply: TrdUnlockTrade: 3
retMsg:
2
3
4
int unlockTrade(TrdUnlockTrade.Request req);
void onReply_UnlockTrade(FTAPI_Conn client, int nSerialNo, TrdUnlockTrade.Response rsp);
介紹
解鎖或鎖定交易
參數
message C2S
{
required bool unlock = 1; //true 解鎖交易,false 鎖定交易
optional string pwdMD5 = 2; //交易密碼的 32 位 MD5 加密(全小寫),解鎖交易必須要填密碼,鎖定交易不需要驗證密碼,可不填
optional int32 securityFirm = 3; //券商標記,取值見 Trd_Common.SecurityFirm
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
- 帳戶所屬券商參見 SecurityFirm
- 回傳
message S2C
{
}
message Response
{
//以下3個字段每條協議都有,註釋說明在 InitConnect.proto 中
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
- 介面執行結果,結構參見 RetType
- Example
public class TrdDemo implements FTSPI_Trd, FTSPI_Conn {
FTAPI_Conn_Trd trd = new FTAPI_Conn_Trd();
public TrdDemo() {
trd.setClientInfo("javaclient", 1); //設定用戶端資訊
trd.setConnSpi(this); //設定連線回呼
trd.setTrdSpi(this); //設定交易回呼
}
public void start() {
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());
if (errCode != 0)
return;
TrdUnlockTrade.C2S c2s = TrdUnlockTrade.C2S.newBuilder()
.setPwdMD5("e10adc3949ba59abbe56e057f20f883e")
.setUnlock(true)
.setSecurityFirm(TrdCommon.SecurityFirm.SecurityFirm_FutuSecurities_VALUE)
.build();
TrdUnlockTrade.Request req = TrdUnlockTrade.Request.newBuilder().setC2S(c2s).build();
int seqNo = trd.unlockTrade(req);
System.out.printf("Send TrdUnlockTrade: %d\n", seqNo);
}
@Override
public void onDisconnect(FTAPI_Conn client, long errCode) {
System.out.printf("Trd onDisConnect: %d\n", errCode);
}
@Override
public void onReply_UnlockTrade(FTAPI_Conn client, int nSerialNo, TrdUnlockTrade.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("TrdUnlockTrade failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive TrdUnlockTrade: %s\n", json);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
- Output
Send TrdUnlockTrade: 2
Receive TrdUnlockTrade: {
"retType": 0,
"retMsg": "",
"errCode": 0
}
2
3
4
5
6
Futu::u32_t UnlockTrade(const Trd_UnlockTrade::Request &stReq);
virtual void OnReply_UnlockTrade(Futu::u32_t nSerialNo, const Trd_UnlockTrade::Response &stRsp) = 0;
介紹
解鎖或鎖定交易
參數
message C2S
{
required bool unlock = 1; //true 解鎖交易,false 鎖定交易
optional string pwdMD5 = 2; //交易密碼的 32 位 MD5 加密(全小寫),解鎖交易必須要填密碼,鎖定交易不需要驗證密碼,可不填
optional int32 securityFirm = 3; //券商標記,取值見 Trd_Common.SecurityFirm
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
- 帳戶所屬券商參見 SecurityFirm
- 回傳
message S2C
{
}
message Response
{
//以下3個字段每條協議都有,註釋說明在 InitConnect.proto 中
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
- 介面執行結果,結構參見 RetType
- Example
class Program : public FTSPI_Qot, public FTSPI_Trd, public FTSPI_Conn
{
public:
Program() {
m_pTrdApi = FTAPI::CreateTrdApi();
m_pTrdApi->RegisterTrdSpi(this);
m_pTrdApi->RegisterConnSpi(this);
}
~Program() {
if (m_pTrdApi != nullptr)
{
m_pTrdApi->UnregisterTrdSpi();
m_pTrdApi->UnregisterConnSpi();
FTAPI::ReleaseTrdApi(m_pTrdApi);
m_pTrdApi = nullptr;
}
}
void Start() {
m_pTrdApi->InitConnect("127.0.0.1", 11111, false);
}
virtual void OnInitConnect(FTAPI_Conn* pConn, Futu::i64_t nErrCode, const char* strDesc) {
cout << "connect" << endl;
// 組包
Trd_UnlockTrade::Request req;
Trd_UnlockTrade::C2S *c2s = req.mutable_c2s();
c2s->set_pwdmd5("e10adc3949ba59abbe56e057f20f883e");
c2s->set_unlock(true);
c2s->set_securityfirm(Trd_Common::SecurityFirm::SecurityFirm_FutuSecurities);
m_UnlockTradeSerialNo = m_pTrdApi->UnlockTrade(req);
cout << "Request UnlockTrade SerialNo: " << m_UnlockTradeSerialNo << endl;
}
virtual void OnReply_UnlockTrade(Futu::u32_t nSerialNo, const Trd_UnlockTrade::Response &stRsp){
if(nSerialNo == m_UnlockTradeSerialNo)
{
cout << "OnReply_UnlockTrade SerialNo: " << nSerialNo << endl;
// 解析內部結構列印出來
// ProtoBufToBodyData和UTF8ToLocal函數的定義參見Sample中的tool.h檔案
string resp_str;
ProtoBufToBodyData(stRsp, resp_str);
cout << UTF8ToLocal(resp_str) << endl;
}
}
protected:
FTAPI_Trd *m_pTrdApi;
Futu::u32_t m_UnlockTradeSerialNo;
};
int32_t main(int32_t argc, char** argv)
{
FTAPI::Init();
{
Program program;
program.Start();
getchar();
}
protobuf::ShutdownProtobufLibrary();
FTAPI::UnInit();
return 0;
}
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
- Output
connect
Request UnlockTrade SerialNo: 4
OnReply_UnlockTrade SerialNo: 4
{
"retType": 0,
"retMsg": "",
"errCode": 0
}
2
3
4
5
6
7
8
9
UnlockTrade(req);
介紹
解鎖或鎖定交易
參數
message C2S
{
required bool unlock = 1; //true 解鎖交易,false 鎖定交易
optional string pwdMD5 = 2; //交易密碼的 32 位 MD5 加密(全小寫),解鎖交易必須要填密碼,鎖定交易不需要驗證密碼,可不填
optional int32 securityFirm = 3; //券商標記,取值見 Trd_Common.SecurityFirm
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
- 帳戶所屬券商參見 SecurityFirm
- 回傳
message S2C
{
}
message Response
{
//以下3個字段每條協議都有,註釋說明在 InitConnect.proto 中
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
- 介面執行結果,結構參見 RetType
- Example
import ftWebsocket from "futu-api";
import { ftCmdID } from "futu-api";
import { Common, Qot_Common, Trd_Common } from "futu-api/proto";
import beautify from "js-beautify";
function TrdUnlockTrade(){
const { RetType } = Common
const { SecurityFirm } = Trd_Common
let [addr, port, enable_ssl, key] = ["127.0.0.1", 33333, false, '7522027ccf5a06b1'];
let websocket = new ftWebsocket();
websocket.onlogin = (ret, msg)=>{
if (ret) { // 登錄成功
const req = {
c2s: {
unlock: true,
securityFirm: SecurityFirm.SecurityFirm_FutuSecurities,
pwdMD5: "d0970714757783e6cf17b26fb8e2298f", // 設定為自己帳號的交易密碼MD5
},
};
websocket.UnlockTrade(req)
.then((res) => {
let { errCode, retMsg, retType,s2c } = res
console.log("UnlockTrade: errCode %d, retMsg %s, retType %d", errCode, retMsg, retType);
if(retType == RetType.RetType_Succeed){
let data = beautify(JSON.stringify(s2c), {
indent_size: 2,
space_in_empty_paren: true,
});
console.log(data);
}
})
.catch((error) => {
console.log("error:", error);
});
} else {
console.log("error", msg);
}
};
websocket.start(addr, port, enable_ssl, key);
//關閉行情連線,連線不再使用之後,要關閉,否則佔用不必要資源
//同時OpenD也限制了最多128條連線
//也可以一個頁面或者一個項目維護一條連線,這裏範例請求一次建立一條連線
setTimeout(()=>{
websocket.stop();
console.log("stop");
}, 5000); // 5秒後斷開
}
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
43
44
45
46
47
48
49
50
51
- Output
UnlockTrade: errCode 0, retMsg , retType 0
null
stop
2
3
提示
介面限制
- 單用戶ID 每 30 秒內最多請求 10 次解鎖交易介面
- Python
- Proto
- C#
- Java
- C++
- JavaScript
unlock_trade(password=None, password_md5=None, is_unlock=True)
介紹
解鎖或鎖定交易
參數
參數 類型 說明 password str 交易密碼 如果 password_md5 不為空,就使用傳入的 password_md5 解鎖;否則使用 password 轉 MD5 得到 password_md5 再解鎖password_md5 str 交易密碼的 32 位 MD5 加密(全小寫) 解鎖交易必須要填密碼,鎖定交易忽略is_unlock bool 解鎖或鎖定 True:解鎖
False:鎖定
回傳
參數 類型 說明 ret RET_CODE 介面執行結果 msg NoneType 當 ret == RET_OK 時,回傳 None str 當 ret != RET_OK 時,回傳錯誤描述 Example
from moomoo import *
pwd_unlock = '123456'
trd_ctx = OpenSecTradeContext(filter_trdmarket=TrdMarket.US, host='127.0.0.1', port=11111, security_firm=SecurityFirm.FUTUINC)
ret, data = trd_ctx.unlock_trade(pwd_unlock)
if ret == RET_OK:
print('unlock success!')
else:
print('unlock_trade failed: ', data)
trd_ctx.close()
2
3
4
5
6
7
8
9
- Output
unlock success!
# Trd_UnlockTrade.proto
介紹
解鎖或鎖定交易
參數
message C2S
{
required bool unlock = 1; //true 解鎖交易,false 鎖定交易
optional string pwdMD5 = 2; //交易密碼的 32 位 MD5 加密(全小寫),解鎖交易必須要填密碼,鎖定交易不需要驗證密碼,可不填
optional int32 securityFirm = 3; //券商標記,取值見 Trd_Common.SecurityFirm
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
- 帳戶所屬券商參見 SecurityFirm
- 回傳
message S2C
{
}
message Response
{
//以下3個字段每條協議都有,註釋說明在 InitConnect.proto 中
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
- 介面執行結果,結構參見 RetType
協議 ID
2005
uint UnlockTrade(TrdUnlockTrade.Request req);
virtual void OnReply_UnlockTrade(MMAPI_Conn client, uint nSerialNo, TrdUnlockTrade.Response rsp);
介紹
解鎖或鎖定交易
參數
message C2S
{
required bool unlock = 1; //true 解鎖交易,false 鎖定交易
optional string pwdMD5 = 2; //交易密碼的 32 位 MD5 加密(全小寫),解鎖交易必須要填密碼,鎖定交易不需要驗證密碼,可不填
optional int32 securityFirm = 3; //券商標記,取值見 Trd_Common.SecurityFirm
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
- 帳戶所屬券商參見 SecurityFirm
- 回傳
message S2C
{
}
message Response
{
//以下3個字段每條協議都有,註釋說明在 InitConnect.proto 中
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
- 介面執行結果,結構參見 RetType
- Example
public class Program : MMSPI_Trd, MMSPI_Conn {
MMAPI_Trd trd = new MMAPI_Trd();
public Program() {
trd.SetClientInfo("csharp", 1); //設定用戶端資訊
trd.SetConnCallback(this); //設定連線回呼
trd.SetTrdCallback(this); //設定交易回呼
}
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());
if (errCode != 0)
return;
TrdUnlockTrade.C2S c2s = TrdUnlockTrade.C2S.CreateBuilder()
.SetPwdMD5("e10adc3949ba59abbe56e057f20f883e")
.SetUnlock(true)
.SetSecurityFirm((int)TrdCommon.SecurityFirm.SecurityFirm_FutuSecurities)
.Build();
TrdUnlockTrade.Request req = TrdUnlockTrade.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = trd.UnlockTrade(req);
Console.Write("Send TrdUnlockTrade: {0}\n", seqNo);
}
public void OnDisconnect(MMAPI_Conn client, long errCode) {
Console.Write("Trd onDisConnect: {0}\n", errCode);
}
public void OnReply_UnlockTrade(MMAPI_Conn client, uint nSerialNo, TrdUnlockTrade.Response rsp)
{
Console.Write("Reply: TrdUnlockTrade: {0}\n", nSerialNo);
Console.Write("retMsg: {0}\n", rsp.RetMsg);
}
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
- Output
Trd onInitConnect: ret=0 desc= connID=6826809697310867898
Send TrdUnlockTrade: 3
Reply: TrdUnlockTrade: 3
retMsg:
2
3
4
int unlockTrade(TrdUnlockTrade.Request req);
void onReply_UnlockTrade(MMAPI_Conn client, int nSerialNo, TrdUnlockTrade.Response rsp);
介紹
解鎖或鎖定交易
參數
message C2S
{
required bool unlock = 1; //true 解鎖交易,false 鎖定交易
optional string pwdMD5 = 2; //交易密碼的 32 位 MD5 加密(全小寫),解鎖交易必須要填密碼,鎖定交易不需要驗證密碼,可不填
optional int32 securityFirm = 3; //券商標記,取值見 Trd_Common.SecurityFirm
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
- 帳戶所屬券商參見 SecurityFirm
- 回傳
message S2C
{
}
message Response
{
//以下3個字段每條協議都有,註釋說明在 InitConnect.proto 中
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
- 介面執行結果,結構參見 RetType
- Example
public class TrdDemo implements MMSPI_Trd, MMSPI_Conn {
MMAPI_Conn_Trd trd = new MMAPI_Conn_Trd();
public TrdDemo() {
trd.setClientInfo("javaclient", 1); //設定用戶端資訊
trd.setConnSpi(this); //設定連線回呼
trd.setTrdSpi(this); //設定交易回呼
}
public void start() {
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());
if (errCode != 0)
return;
TrdUnlockTrade.C2S c2s = TrdUnlockTrade.C2S.newBuilder()
.setPwdMD5("e10adc3949ba59abbe56e057f20f883e")
.setUnlock(true)
.setSecurityFirm(TrdCommon.SecurityFirm.SecurityFirm_FutuSecurities_VALUE)
.build();
TrdUnlockTrade.Request req = TrdUnlockTrade.Request.newBuilder().setC2S(c2s).build();
int seqNo = trd.unlockTrade(req);
System.out.printf("Send TrdUnlockTrade: %d\n", seqNo);
}
@Override
public void onDisconnect(MMAPI_Conn client, long errCode) {
System.out.printf("Trd onDisConnect: %d\n", errCode);
}
@Override
public void onReply_UnlockTrade(MMAPI_Conn client, int nSerialNo, TrdUnlockTrade.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("TrdUnlockTrade failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive TrdUnlockTrade: %s\n", json);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
- Output
Send TrdUnlockTrade: 2
Receive TrdUnlockTrade: {
"retType": 0,
"retMsg": "",
"errCode": 0
}
2
3
4
5
6
moomoo::u32_t UnlockTrade(const Trd_UnlockTrade::Request &stReq);
virtual void OnReply_UnlockTrade(moomoo::u32_t nSerialNo, const Trd_UnlockTrade::Response &stRsp) = 0;
介紹
解鎖或鎖定交易
參數
message C2S
{
required bool unlock = 1; //true 解鎖交易,false 鎖定交易
optional string pwdMD5 = 2; //交易密碼的 32 位 MD5 加密(全小寫),解鎖交易必須要填密碼,鎖定交易不需要驗證密碼,可不填
optional int32 securityFirm = 3; //券商標記,取值見 Trd_Common.SecurityFirm
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
- 帳戶所屬券商參見 SecurityFirm
- 回傳
message S2C
{
}
message Response
{
//以下3個字段每條協議都有,註釋說明在 InitConnect.proto 中
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
- 介面執行結果,結構參見 RetType
- Example
class Program : public MMSPI_Qot, public MMSPI_Trd, public MMSPI_Conn
{
public:
Program() {
m_pTrdApi = MMAPI::CreateTrdApi();
m_pTrdApi->RegisterTrdSpi(this);
m_pTrdApi->RegisterConnSpi(this);
}
~Program() {
if (m_pTrdApi != nullptr)
{
m_pTrdApi->UnregisterTrdSpi();
m_pTrdApi->UnregisterConnSpi();
MMAPI::ReleaseTrdApi(m_pTrdApi);
m_pTrdApi = nullptr;
}
}
void Start() {
m_pTrdApi->InitConnect("127.0.0.1", 11111, false);
}
virtual void OnInitConnect(MMAPI_Conn* pConn, moomoo::i64_t nErrCode, const char* strDesc) {
cout << "connect" << endl;
// 組包
Trd_UnlockTrade::Request req;
Trd_UnlockTrade::C2S *c2s = req.mutable_c2s();
c2s->set_pwdmd5("e10adc3949ba59abbe56e057f20f883e");
c2s->set_unlock(true);
c2s->set_securityfirm(Trd_Common::SecurityFirm::SecurityFirm_FutuSecurities);
m_UnlockTradeSerialNo = m_pTrdApi->UnlockTrade(req);
cout << "Request UnlockTrade SerialNo: " << m_UnlockTradeSerialNo << endl;
}
virtual void OnReply_UnlockTrade(moomoo::u32_t nSerialNo, const Trd_UnlockTrade::Response &stRsp){
if(nSerialNo == m_UnlockTradeSerialNo)
{
cout << "OnReply_UnlockTrade SerialNo: " << nSerialNo << endl;
// 解析內部結構列印出來
// ProtoBufToBodyData和UTF8ToLocal函數的定義參見Sample中的tool.h檔案
string resp_str;
ProtoBufToBodyData(stRsp, resp_str);
cout << UTF8ToLocal(resp_str) << endl;
}
}
protected:
MMAPI_Trd *m_pTrdApi;
moomoo::u32_t m_UnlockTradeSerialNo;
};
int32_t main(int32_t argc, char** argv)
{
MMAPI::Init();
{
Program program;
program.Start();
getchar();
}
protobuf::ShutdownProtobufLibrary();
MMAPI::UnInit();
return 0;
}
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
- Output
connect
Request UnlockTrade SerialNo: 4
OnReply_UnlockTrade SerialNo: 4
{
"retType": 0,
"retMsg": "",
"errCode": 0
}
2
3
4
5
6
7
8
9
UnlockTrade(req);
介紹
解鎖或鎖定交易
參數
message C2S
{
required bool unlock = 1; //true 解鎖交易,false 鎖定交易
optional string pwdMD5 = 2; //交易密碼的 32 位 MD5 加密(全小寫),解鎖交易必須要填密碼,鎖定交易不需要驗證密碼,可不填
optional int32 securityFirm = 3; //券商標記,取值見 Trd_Common.SecurityFirm
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
- 帳戶所屬券商參見 SecurityFirm
- 回傳
message S2C
{
}
message Response
{
//以下3個字段每條協議都有,註釋說明在 InitConnect.proto 中
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
- 介面執行結果,結構參見 RetType
- Example
import mmWebsocket from "moomoo-api";
import { mmCmdID } from "moomoo-api";
import { Common, Qot_Common, Trd_Common } from "moomoo-api/proto";
import beautify from "js-beautify";
function TrdUnlockTrade(){
const { RetType } = Common
const { SecurityFirm } = Trd_Common
let [addr, port, enable_ssl, key] = ["127.0.0.1", 33333, false, '7522027ccf5a06b1'];
let websocket = new mmWebsocket();
websocket.onlogin = (ret, msg)=>{
if (ret) { // 登錄成功
const req = {
c2s: {
unlock: true,
securityFirm: SecurityFirm.SecurityFirm_FutuSecurities,
pwdMD5: "d0970714757783e6cf17b26fb8e2298f", // 設定為自己帳號的交易密碼MD5
},
};
websocket.UnlockTrade(req)
.then((res) => {
let { errCode, retMsg, retType,s2c } = res
console.log("UnlockTrade: errCode %d, retMsg %s, retType %d", errCode, retMsg, retType);
if(retType == RetType.RetType_Succeed){
let data = beautify(JSON.stringify(s2c), {
indent_size: 2,
space_in_empty_paren: true,
});
console.log(data);
}
})
.catch((error) => {
console.log("error:", error);
});
} else {
console.log("error", msg);
}
};
websocket.start(addr, port, enable_ssl, key);
//關閉行情連線,連線不再使用之後,要關閉,否則佔用不必要資源
//同時OpenD也限制了最多128條連線
//也可以一個頁面或者一個項目維護一條連線,這裏範例請求一次建立一條連線
setTimeout(()=>{
websocket.stop();
console.log("stop");
}, 5000); // 5秒後斷開
}
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
43
44
45
46
47
48
49
50
51
- Output
UnlockTrade: errCode 0, retMsg , retType 0
null
stop
2
3
提示
介面限制
- 單用戶ID 每 30 秒內最多請求 10 次解鎖交易介面
← 讀取交易業務賬户列表 查詢帳戶資金 →