# Get Account Funds
- Python
- Proto
- C#
- Java
- C++
- JavaScript
accinfo_query(trd_env=TrdEnv.REAL, acc_id=0, acc_index=0, refresh_cache=False, currency=Currency.HKD)
Description
Query fund data such as net asset value, securities market value, cash, and purchasing power of trading accounts.
Parameters
Parameter Type Description trd_env TrdEnv Trading environment acc_id int Trading account ID. - When acc_id is 0, the account specified by acc_index is chosen.
- When acc_id is set the ID number (not 0), the account specified by acc_id is chosen.
- Using acc_id to query and trade is strongly recommended, acc_index will change when adding/closing an account, result in the account you specify is inconsistent with the actual trading account.
acc_index int The account number in the trading account list. The default is 0, which means the first trading account.refresh_cache bool Whether to refresh the cache. - True: Re-request data from the Futu server immediately, without using the OpenD cache. At this time, it will be restricted by the interface frequency limit.
- False: Use OpenD's cache (The cache needs to be refreshed if it is not updated in rare circumstances.
currency Currency The display currency of the funds. - Only applicable to universal securities accounts and futures accounts, other single-market accounts will ignore this parameter.
- In the returned DataFrame, all fund-related fields can be converted with this parameter, except for the fields that explicitly specify the currency.
Return
Field Type Description ret RET_CODE Interface result. data pd.DataFrame If ret == RET_OK, fund data is returned. str If ret != RET_OK, error description is returned. - Fund data format as follows:
Field Type Description power float Maximum Buying Power. - This field is the approximate value calculated according to the marginable initial margin of 50%. But in fact, this ratio of each financial contract is not the same. We recommend using Buy on margin, returned by Query the Maximum Quantity that Can be Bought or Sold, to get the maximum quantity can buy.
max_power_short float Short Buying Power. - This field is the approximate value calculated according to the shortable initial margin of 60%. But in fact, this ratio of each financial contract is not the same. We recommend using Short sell, returned by Query the Maximum Quantity that Can be Bought or Sold, to get the maximum quantity can be shorted.
net_cash_power float Cash Buying Power. total_assets float Total Net Assets. Total Net Assets = Security Assets + Fund Assets + Bond Assetssecurities_assets float Security Assets Minimum OpenD version requirements: 8.2.4218.funds_assets float Fund Assets - Universal accounts will return the total fund assets value. Currently, it does not support for HKD fund and USD fund assets value.
- Minimum OpenD version requirements: 8.2.4218.
bonds_assets float Bond Assets Minimum OpenD version requirements: 8.2.4218.cash float Cash. market_val float Securities Market Value. Only applicable to securities accounts.long_mv float Long Market Value. short_mv float Short Market Value. pending_asset float Asset in Transit. interest_charged_amount float Interest Charged Amount. frozen_cash float Funds on Hold. avl_withdrawal_cash float Withdrawable Cash. Only applicable to securities accounts.max_withdrawal float Maximum Withdrawal. - Only applicable to securities accounts of Futu HK
currency Currency The currency used for this query. Only applicable to universal securities accounts and futures accounts.available_funds float Available funds. Only applicable to securities accounts.unrealized_pl float Unrealized gain or loss. Only applicable to futures accounts.realized_pl float Realized gain or loss. Only applicable to futures accounts.risk_level CltRiskLevel Risk control level. Only applicable to futures accounts. It is recommanded to use risk_status field to get the risk status of securities accounts or futures accounts.risk_status CltRiskStatus Risk status. - Applicable to securities accounts and futures accounts.
- Divided into 9 grades,
LEVEL1
is the safest andLEVEL9
is the most dangerous.
initial_margin float Initial Margin. - Only applicable to futures accounts.
margin_call_margin float Margin-call Margin. maintenance_margin float Maintenance Margin. hk_cash float HKD Cash. Only applicable to futures accounts.hk_avl_withdrawal_cash float HKD Withdrawable Cash. Only applicable to futures accounts.us_cash float USD Cash. Only applicable to futures accounts.us_avl_withdrawal_cash float USD Withdrawable Cash. Only applicable to futures accounts.jp_cash float JPY Cash. - Only applicable to futures accounts.
- Minimum Futu API version requirements: 5.8.2008
jp_avl_withdrawal_cash float JPY Withdrawable Cash. - Only applicable to futures accounts.
- Minimum Futu API version requirements: 5.8.2008
pdt_seq string Day Trades Left. Only applicable to securities accounts of Moomoo US.
Minimum OpenD version requirements: 5.8.2008.is_pdt bool Is it marked as a PDT. True: It is a PDT. False: Not a PDT.
Only applicable to securities accounts of Moomoo US.
Minimum OpenD version requirements: 5.8.2008.beginning_dtbp float Beginning DTBP. Only applicable to securities accounts of Moomoo US marked as a PDT.
Minimum OpenD version requirements: 5.8.2008.remaining_dtbp float Remaining DTBP. Only applicable to securities accounts of Moomoo US marked as a PDT.
Minimum OpenD version requirements: 5.8.2008.dt_call_amount float Day-trading Call Amount. Only applicable to securities accounts of Moomoo US marked as a PDT.
Minimum OpenD version requirements: 5.8.2008.dt_status DtStatus Day-trading Status. Only applicable to securities accounts of Moomoo US marked as a PDT.
Minimum OpenD version requirements: 5.8.2008.cn_cash float CNH Cash. - Only applicable to universal securities accounts and futures accounts.
- This field will not be displayed as other currencies.
cn_avl_withdrawal_cash float CNH Withdrawable Cash. - Only applicable to universal securities accounts and futures accounts.
- This field will not be displayed as other currencies.
sg_cash float SGD Cash. - Only applicable to universal securities accounts.
- This field will not be displayed as other currencies.
sg_avl_withdrawal_cash float SGD Withdrawable Cash. - Only applicable to universal securities accounts.
- This field will not be displayed as other currencies.
au_cash float AUD Cash. - Only applicable to universal securities accounts.
- Minimum Futu API version requirements: 5.8.2008
au_avl_withdrawal_cash float AUD Withdrawable Cash. - Only applicable to universal securities accounts.
- Minimum Futu API version requirements: 5.8.2008
- Fund data format as follows:
Example
from futu import *
trd_ctx = OpenSecTradeContext(filter_trdmarket=TrdMarket.HK, host='127.0.0.1', port=11111, security_firm=SecurityFirm.FUTUSECURITIES)
ret, data = trd_ctx.accinfo_query()
if ret == RET_OK:
print(data)
print(data['power'][0]) # Get the first buying power
print(data['power'].values.tolist()) # convert to list
else:
print('accinfo_query error: ', data)
trd_ctx.close() # Close the current connection
2
3
4
5
6
7
8
9
10
- Output
power max_power_short net_cash_power total_assets securities_assets fund_assets bond_assets cash market_val long_mv short_mv pending_asset interest_charged_amount frozen_cash avl_withdrawal_cash max_withdrawal currency available_funds unrealized_pl realized_pl risk_level risk_status initial_margin margin_call_margin maintenance_margin hk_cash hk_avl_withdrawal_cash us_cash us_avl_withdrawal_cash
0 55926.96 46605.8 5880.48 50080.48 50080.48 N/A N/A 5880.48 44200.0 44200.0 0.0 0.0 0.0 0.0 5880.48 25755.18 N/A N/A N/A N/A N/A LEVEL2 22083.0 19874.7 15458.1 N/A N/A N/A N/A
55926.96
[55926.96]
2
3
4
# Trd_GetFunds.proto
Description
Query fund data such as net asset value, securities market value, cash, and purchasing power of trading accounts.
Parameters
message C2S
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional bool refreshCache = 2; //If explicitly set to true, force to update the cache of OpenD. Otherwise just return the local cached data of OpenD.
//Under normal circumstances, the cached data of OpenD is in sync with server, so there is no need to force an update. It's faster to use the local cached data, and will not cause any pressure on server.
//If you encounter packet loss, etc., the cached data may be inconsistent with the server. If the user finds that the data is not the latest, they can set this flag to true to update the cache and get latest data.
optional int32 currency = 3; //Currency type, see Trd_Common.Currency. Only required for universal securities accounts and futures accounts, other accounts are ignored
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Return
message S2C
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional Trd_Common.Funds funds = 2; //Account funds
}
message Response
{
//The following 3 fields are available in all protocols, and the notes are in 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
15
Protocol ID
2101
uint GetFunds(TrdGetFunds.Request req);
virtual void OnReply_GetFunds(FTAPI_Conn client, uint nSerialNo, ${proto_name.Response rsp);
Description
Query fund data such as net asset value, securities market value, cash, and purchasing power of trading accounts.
Parameters
message C2S
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional bool refreshCache = 2; //If explicitly set to true, force to update the cache of OpenD. Otherwise just return the local cached data of OpenD.
//Under normal circumstances, the cached data of OpenD is in sync with server, so there is no need to force an update. It's faster to use the local cached data, and will not cause any pressure on server.
//If you encounter packet loss, etc., the cached data may be inconsistent with the server. If the user finds that the data is not the latest, they can set this flag to true to update the cache and get latest data.
optional int32 currency = 3; //Currency type, see Trd_Common.Currency. Only required for universal securities accounts and futures accounts, other accounts are ignored
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Return
message S2C
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional Trd_Common.Funds funds = 2; //Account funds
}
message Response
{
//The following 3 fields are available in all protocols, and the notes are in 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
15
- 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
trd.SetTrdCallback(this); //Set transaction 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());
if (errCode != 0)
return;
TrdCommon.TrdHeader header = TrdCommon.TrdHeader.CreateBuilder()
.SetAccID(281756455988247915L)
.SetTrdEnv((int)TrdCommon.TrdEnv.TrdEnv_Real)
.SetTrdMarket((int)TrdCommon.TrdMarket.TrdMarket_HK)
.Build();
TrdGetFunds.C2S c2s = TrdGetFunds.C2S.CreateBuilder()
.SetHeader(header)
.Build();
TrdGetFunds.Request req = TrdGetFunds.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = trd.GetFunds(req);
Console.Write("Send TrdGetFunds: {0}\n", seqNo);
}
public void OnDisconnect(FTAPI_Conn client, long errCode) {
Console.Write("Trd onDisConnect: {0}\n", errCode);
}
public void OnReply_GetFunds(FTAPI_Conn client, uint nSerialNo, TrdGetFunds.Response rsp)
{
Console.Write("Reply: TrdGetFunds: {0}\n", nSerialNo);
Console.Write("accID: {0}\n", rsp.S2C.Header.AccID);
}
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
52
53
54
55
- Output
Trd onInitConnect: ret=0 desc= connID=6826811710045588886
Send TrdGetFunds: 3
Reply: TrdGetFunds: 3
accID: 281756455988247915
2
3
4
int getFunds(TrdGetFunds.Request req);
void onReply_GetFunds(FTAPI_Conn client, int nSerialNo, ${proto_name.Response rsp);
Description
Query fund data such as net asset value, securities market value, cash, and purchasing power of trading accounts. data. Obtain fund data such as the net asset value of the account, the market value of securities, cash, and purchasing power.
Parameters
message C2S
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional bool refreshCache = 2; //If explicitly set to true, force to update the cache of OpenD. Otherwise just return the local cached data of OpenD.
//Under normal circumstances, the cached data of OpenD is in sync with server, so there is no need to force an update. It's faster to use the local cached data, and will not cause any pressure on server.
//If you encounter packet loss, etc., the cached data may be inconsistent with the server. If the user finds that the data is not the latest, they can set this flag to true to update the cache and get latest data.
optional int32 currency = 3; //Currency type, see Trd_Common.Currency. Only required for universal securities accounts and futures accounts, other accounts are ignored
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Return
message S2C
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional Trd_Common.Funds funds = 2; //Account funds
}
message Response
{
//The following 3 fields are available in all protocols, and the notes are in 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
15
- 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
trd.setTrdSpi(this); //Set transaction callback
}
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;
TrdCommon.TrdHeader header = TrdCommon.TrdHeader.newBuilder()
.setAccID(281756455988247915L)
.setTrdEnv(TrdCommon.TrdEnv.TrdEnv_Real_VALUE)
.setTrdMarket(TrdCommon.TrdMarket.TrdMarket_HK_VALUE)
.build();
TrdGetFunds.C2S c2s = TrdGetFunds.C2S.newBuilder()
.setHeader(header)
.build();
TrdGetFunds.Request req = TrdGetFunds.Request.newBuilder().setC2S(c2s).build();
int seqNo = trd.getFunds(req);
System.out.printf("Send TrdGetFunds: %d\n", seqNo);
}
@Override
public void onDisconnect(FTAPI_Conn client, long errCode) {
System.out.printf("Trd onDisConnect: %d\n", errCode);
}
@Override
public void onReply_GetFunds(FTAPI_Conn client, int nSerialNo, TrdGetFunds.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("TrdGetFunds failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive TrdGetFunds: %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
65
66
67
- Output
Send TrdGetFunds: 2
Receive TrdGetFunds: {
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"header": {
"trdEnv": 1,
"accID": "281756455988247915",
"trdMarket": 1
},
"funds": {
"power": 535202.51221,
"totalAssets": 0.0,
"securitiesAssets": 0.0,
"fundAssets": 0.0,
"bondAssets": 0.0,
"cash": 0.0,
"marketVal": 0.0,
"frozenCash": 0.0,
"debtCash": 0.0,
"avlWithdrawalCash": 0.0,
"initialMargin": 221955.2062,
"maintenanceMargin": 155362.6396,
"maxPowerShort": 446002.09351,
"netCashPower": 0.0,
"longMv": 0.0,
"shortMv": 0.0,
"pendingAsset": 0.0,
"maxWithdrawal": 0.0,
"riskStatus": 2,
"marginCallMargin": 199751.9652
}
}
}
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
Futu::u32_t GetFunds(const Trd_GetFunds::Request &stReq);
virtual void OnReply_GetFunds(Futu::u32_t nSerialNo, const Trd_GetFunds::Response &stRsp) = 0;
Description
Query fund data such as net asset value, securities market value, cash, and purchasing power of trading accounts.
Parameters
message C2S
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional bool refreshCache = 2; //If explicitly set to true, force to update the cache of OpenD. Otherwise just return the local cached data of OpenD.
//Under normal circumstances, the cached data of OpenD is in sync with server, so there is no need to force an update. It's faster to use the local cached data, and will not cause any pressure on server.
//If you encounter packet loss, etc., the cached data may be inconsistent with the server. If the user finds that the data is not the latest, they can set this flag to true to update the cache and get latest data.
optional int32 currency = 3; //Currency type, see Trd_Common.Currency. Only required for universal securities accounts and futures accounts, other accounts are ignored
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Return
message S2C
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional Trd_Common.Funds funds = 2; //Account funds
}
message Response
{
//The following 3 fields are available in all protocols, and the notes are in 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
15
- 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;
// construct request message
Trd_GetFunds::Request req;
Trd_GetFunds::C2S *c2s = req.mutable_c2s();
Trd_Common::TrdHeader *header = c2s->mutable_header();
header->set_accid(3637840);
header->set_trdenv(0);
header->set_trdmarket(1);
m_GetFundsSerialNo = m_pTrdApi->GetFunds(req);
cout << "Request GetFunds SerialNo: " << m_GetFundsSerialNo << endl;
}
virtual void OnReply_GetFunds(Futu::u32_t nSerialNo, const Trd_GetFunds::Response &stRsp){
if(nSerialNo == m_GetFundsSerialNo)
{
cout << "OnReply_GetFunds SerialNo: " << nSerialNo << endl;
// print response
// ProtoBufToBodyData and UTF8ToLocal refer to tool.h in Samples
string resp_str;
ProtoBufToBodyData(stRsp, resp_str);
cout << UTF8ToLocal(resp_str) << endl;
}
}
protected:
FTAPI_Trd *m_pTrdApi;
Futu::u32_t m_GetFundsSerialNo;
};
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 GetFunds SerialNo: 4
OnReply_GetFunds SerialNo: 4
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"header": {
"trdEnv": 0,
"accID": "3637840",
"trdMarket": 1
},
"funds": {
"power": 0,
"totalAssets": 994327.11,
"securitiesAssets": 994327.11,
"fundAssets": 0,
"bondAssets": 0,
"cash": 814027.11,
"marketVal": 180300,
"frozenCash": 0,
"debtCash": 0,
"avlWithdrawalCash": 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
GetFunds(req);
Description
Query fund data such as net asset value, securities market value, cash, and purchasing power of trading accounts.
Parameters
message C2S
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional bool refreshCache = 2; //If explicitly set to true, force to update the cache of OpenD. Otherwise just return the local cached data of OpenD.
//Under normal circumstances, the cached data of OpenD is in sync with server, so there is no need to force an update. It's faster to use the local cached data, and will not cause any pressure on server.
//If you encounter packet loss, etc., the cached data may be inconsistent with the server. If the user finds that the data is not the latest, they can set this flag to true to update the cache and get latest data.
optional int32 currency = 3; //Currency type, see Trd_Common.Currency. Only required for universal securities accounts and futures accounts, other accounts are ignored
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Return
message S2C
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional Trd_Common.Funds funds = 2; //Account funds
}
message Response
{
//The following 3 fields are available in all protocols, and the notes are in 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
15
- 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";
TrdGetFunds(){
const { RetType } = Common
const { TrdEnv } = 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) {
websocket.GetAccList({
c2s: {
userID: 0,
},
}).then((res) => {
let { retType, s2c: { accList } } = res
if(retType == RetType.RetType_Succeed){
let acc = accList.filter((item)=>{
return item.trdEnv == TrdEnv.TrdEnv_Simulate && item.trdMarketAuthList.some((auth)=>{ return auth == TrdMarket.TrdMarket_HK})
})[0]; // The sample takes the first HK paper trading environment account
const req = {
c2s: {
header: {
trdEnv: acc.trdEnv,
accID: acc.accID,
trdMarket: acc.trdMarketAuthList[0],
}
},
};
websocket.GetFunds(req)
.then((res) => {
let { errCode, retMsg, retType,s2c } = res
console.log("GetFunds: 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);
});
}
})
.catch((error) => {
console.log("GetAccList error:", error);
});
} else {
console.log("error", msg);
}
};
websocket.start(addr, port, enable_ssl, key);
// After using the connection, remember to close it to prevent the number of connections from running out
setTimeout(()=>{
websocket.stop();
console.log("stop");
}, 5000); // Set the script to receive OpenD push duration to 5 seconds
}
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
- Output
GetFunds: errCode 0, retMsg , retType 0
{
"header": {
"trdEnv": 0,
"accID": "6684972",
"trdMarket": 1
},
"funds": {
"power": 0,
"totalAssets": 1063162.9,
"securitiesAssets": 1063162.9,
"fundAssets": 0,
"bondAssets": 0,
"cash": 132162.9,
"marketVal": 931000,
"frozenCash": 0,
"debtCash": 0,
"avlWithdrawalCash": 0
}
}
stop
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Interface Limitations
- A maximum of 10 requests per 30 seconds
- It will be restricted by the frequency limit for this interface, only when refresh_cache is True
- Python
- Proto
- C#
- Java
- C++
- JavaScript
accinfo_query(trd_env=TrdEnv.REAL, acc_id=0, acc_index=0, refresh_cache=False, currency=Currency.HKD)
Description
Query fund data such as net asset value, securities market value, cash, and purchasing power of trading accounts.
Parameters
Parameter Type Description trd_env TrdEnv Trading environment acc_id int Trading account ID. - When acc_id is 0, the account specified by acc_index is chosen.
- When acc_id is set the ID number (not 0), the account specified by acc_id is chosen.
- Using acc_id to query and trade is strongly recommended, acc_index will change when adding/closing an account, result in the account you specify is inconsistent with the actual trading account.
acc_index int The account number in the trading account list. The default is 0, which means the first trading account.refresh_cache bool Whether to refresh the cache. - True: Re-request data from the moomoo server immediately, without using the OpenD cache. At this time, it will be restricted by the interface frequency limit.
- False: Use OpenD's cache (The cache needs to be refreshed if it is not updated in rare circumstances.
currency Currency The display currency of the funds. - Only applicable to universal securities accounts and futures accounts, other single-market accounts will ignore this parameter.
- In the returned DataFrame, all fund-related fields can be converted with this parameter, except for the fields that explicitly specify the currency.
Return
Field Type Description ret RET_CODE Interface result. data pd.DataFrame If ret == RET_OK, fund data is returned. str If ret != RET_OK, error description is returned. - Fund data format as follows:
Field Type Description power float Maximum Buying Power. - This field is the approximate value calculated according to the marginable initial margin of 50%. But in fact, this ratio of each financial contract is not the same. We recommend using Buy on margin, returned by Query the Maximum Quantity that Can be Bought or Sold, to get the maximum quantity can buy.
max_power_short float Short Buying Power. - This field is the approximate value calculated according to the shortable initial margin of 60%. But in fact, this ratio of each financial contract is not the same. We recommend using Short sell, returned by Query the Maximum Quantity that Can be Bought or Sold, to get the maximum quantity can be shorted.
net_cash_power float Cash Buying Power. total_assets float Net Assets. Total Net Assets = Security Assets + Fund Assets + Bond Assetssecurities_assets float Security Assets Minimum OpenD version requirements: 8.2.4218.funds_assets float Fund Assets - Universal accounts will return the total fund assets value. Currently, it does not support for HKD fund and USD fund assets value.
- Minimum OpenD version requirements: 8.2.4218.
bonds_assets float Bond Assets Minimum OpenD version requirements: 8.2.4218.cash float Cash. market_val float Securities Market Value. Only applicable to securities accounts.long_mv float Long Market Value. short_mv float Short Market Value. pending_asset float Asset in Transit. interest_charged_amount float Interest Charged Amount. frozen_cash float Funds on Hold. avl_withdrawal_cash float Withdrawable Cash. Only applicable to securities accounts.max_withdrawal float Maximum Withdrawal. - Only applicable to securities accounts of Futu HK
currency Currency The currency used for this query. Only applicable to universal securities accounts and futures accounts.available_funds float Available funds. Only applicable to securities accounts.unrealized_pl float Unrealized gain or loss. Only applicable to futures accounts.realized_pl float Realized gain or loss. Only applicable to futures accounts.risk_level CltRiskLevel Risk control level. Only applicable to futures accounts. It is recommanded to use risk_status field to get the risk status of securities accounts or futures accounts.risk_status CltRiskStatus Risk status. - Applicable to securities accounts and futures accounts.
- Divided into 9 grades,
LEVEL1
is the safest andLEVEL9
is the most dangerous.
initial_margin float Initial Margin. - Only applicable to futures accounts.
margin_call_margin float Margin-call Margin. maintenance_margin float Maintenance Margin. hk_cash float HKD Cash. Only applicable to futures accounts.hk_avl_withdrawal_cash float HKD Withdrawable Cash. Only applicable to futures accounts.us_cash float USD Cash. Only applicable to futures accounts.us_avl_withdrawal_cash float USD Withdrawable Cash. Only applicable to futures accounts.jp_cash float JPY Cash. - Only applicable to futures accounts.
- Minimum Futu API version requirements: 5.8.2008
jp_avl_withdrawal_cash float JPY Withdrawable Cash. - Only applicable to futures accounts.
- Minimum Futu API version requirements: 5.8.2008
pdt_seq string Day Trades Left. Only applicable to securities accounts of Moomoo US.
Minimum OpenD version requirements: 5.8.2008.is_pdt bool Is it marked as a PDT. True: It is a PDT. False: Not a PDT.
Only applicable to securities accounts of Moomoo US.
Minimum OpenD version requirements: 5.8.2008.beginning_dtbp float Beginning DTBP. Only applicable to securities accounts of Moomoo US marked as a PDT.
Minimum OpenD version requirements: 5.8.2008.remaining_dtbp float Remaining DTBP. Only applicable to securities accounts of Moomoo US marked as a PDT.
Minimum OpenD version requirements: 5.8.2008.dt_call_amount float Day-trading Call Amount. Only applicable to securities accounts of Moomoo US marked as a PDT.
Minimum OpenD version requirements: 5.8.2008.dt_status DtStatus Day-trading Status. Only applicable to securities accounts of Moomoo US marked as a PDT.
Minimum OpenD version requirements: 5.8.2008.cn_cash float CNH Cash. - Only applicable to universal securities accounts and futures accounts.
- This field will not be displayed as other currencies.
cn_avl_withdrawal_cash float CNH Withdrawable Cash. - Only applicable to universal securities accounts and futures accounts.
- This field will not be displayed as other currencies.
sg_cash float SGD Cash. - Only applicable to universal securities accounts.
- This field will not be displayed as other currencies.
sg_avl_withdrawal_cash float SGD Withdrawable Cash. - Only applicable to universal securities accounts.
- This field will not be displayed as other currencies.
- Fund data format as follows:
Example
from moomoo import *
trd_ctx = OpenSecTradeContext(filter_trdmarket=TrdMarket.HK, host='127.0.0.1', port=11111, security_firm=SecurityFirm.FUTUSECURITIES)
ret, data = trd_ctx.accinfo_query()
if ret == RET_OK:
print(data)
print(data['power'][0]) # Get the first buying power
print(data['power'].values.tolist()) # convert to list
else:
print('accinfo_query error: ', data)
trd_ctx.close() # Close the current connection
2
3
4
5
6
7
8
9
10
Output
Output
power max_power_short net_cash_power total_assets securities_assets fund_assets bond_assets cash market_val long_mv short_mv pending_asset interest_charged_amount frozen_cash avl_withdrawal_cash max_withdrawal currency available_funds unrealized_pl realized_pl risk_level risk_status initial_margin margin_call_margin maintenance_margin hk_cash hk_avl_withdrawal_cash us_cash us_avl_withdrawal_cash
0 55926.96 46605.8 5880.48 50080.48 50080.48 N/A N/A 5880.48 44200.0 44200.0 0.0 0.0 0.0 0.0 5880.48 25755.18 N/A N/A N/A N/A N/A LEVEL2 22083.0 19874.7 15458.1 N/A N/A N/A N/A
55926.96
[55926.96]
2
3
4
# Trd_GetFunds.proto
Description
Query fund data such as net asset value, securities market value, cash, and purchasing power of trading accounts.
Parameters
message C2S
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional bool refreshCache = 2; //If explicitly set to true, force to update the cache of OpenD. Otherwise just return the local cached data of OpenD.
//Under normal circumstances, the cached data of OpenD is in sync with server, so there is no need to force an update. It's faster to use the local cached data, and will not cause any pressure on server.
//If you encounter packet loss, etc., the cached data may be inconsistent with the server. If the user finds that the data is not the latest, they can set this flag to true to update the cache and get latest data.
optional int32 currency = 3; //Currency type, see Trd_Common.Currency. Only required for universal securities accounts and futures accounts, other accounts are ignored
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Return
message S2C
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional Trd_Common.Funds funds = 2; //Account funds
}
message Response
{
//The following 3 fields are available in all protocols, and the notes are in 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
15
Protocol ID
2101
uint GetFunds(TrdGetFunds.Request req);
virtual void OnReply_GetFunds(MMAPI_Conn client, uint nSerialNo, ${proto_name.Response rsp);
Description
Query fund data such as net asset value, securities market value, cash, and purchasing power of trading accounts.
Parameters
message C2S
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional bool refreshCache = 2; //If explicitly set to true, force to update the cache of OpenD. Otherwise just return the local cached data of OpenD.
//Under normal circumstances, the cached data of OpenD is in sync with server, so there is no need to force an update. It's faster to use the local cached data, and will not cause any pressure on server.
//If you encounter packet loss, etc., the cached data may be inconsistent with the server. If the user finds that the data is not the latest, they can set this flag to true to update the cache and get latest data.
optional int32 currency = 3; //Currency type, see Trd_Common.Currency. Only required for universal securities accounts and futures accounts, other accounts are ignored
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Return
message S2C
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional Trd_Common.Funds funds = 2; //Account funds
}
message Response
{
//The following 3 fields are available in all protocols, and the notes are in 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
15
- 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
trd.SetTrdCallback(this); //Set transaction 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());
if (errCode != 0)
return;
TrdCommon.TrdHeader header = TrdCommon.TrdHeader.CreateBuilder()
.SetAccID(281756455988247915L)
.SetTrdEnv((int)TrdCommon.TrdEnv.TrdEnv_Real)
.SetTrdMarket((int)TrdCommon.TrdMarket.TrdMarket_HK)
.Build();
TrdGetFunds.C2S c2s = TrdGetFunds.C2S.CreateBuilder()
.SetHeader(header)
.Build();
TrdGetFunds.Request req = TrdGetFunds.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = trd.GetFunds(req);
Console.Write("Send TrdGetFunds: {0}\n", seqNo);
}
public void OnDisconnect(MMAPI_Conn client, long errCode) {
Console.Write("Trd onDisConnect: {0}\n", errCode);
}
public void OnReply_GetFunds(MMAPI_Conn client, uint nSerialNo, TrdGetFunds.Response rsp)
{
Console.Write("Reply: TrdGetFunds: {0}\n", nSerialNo);
Console.Write("accID: {0}\n", rsp.S2C.Header.AccID);
}
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
52
53
54
55
- Output
Trd onInitConnect: ret=0 desc= connID=6826811710045588886
Send TrdGetFunds: 3
Reply: TrdGetFunds: 3
accID: 281756455988247915
2
3
4
int getFunds(TrdGetFunds.Request req);
void onReply_GetFunds(MMAPI_Conn client, int nSerialNo, ${proto_name.Response rsp);
Description
Query fund data such as net asset value, securities market value, cash, and purchasing power of trading accounts. data. Obtain fund data such as the net asset value of the account, the market value of securities, cash, and purchasing power.
Parameters
message C2S
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional bool refreshCache = 2; //If explicitly set to true, force to update the cache of OpenD. Otherwise just return the local cached data of OpenD.
//Under normal circumstances, the cached data of OpenD is in sync with server, so there is no need to force an update. It's faster to use the local cached data, and will not cause any pressure on server.
//If you encounter packet loss, etc., the cached data may be inconsistent with the server. If the user finds that the data is not the latest, they can set this flag to true to update the cache and get latest data.
optional int32 currency = 3; //Currency type, see Trd_Common.Currency. Only required for universal securities accounts and futures accounts, other accounts are ignored
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Return
message S2C
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional Trd_Common.Funds funds = 2; //Account funds
}
message Response
{
//The following 3 fields are available in all protocols, and the notes are in 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
15
- 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
trd.setTrdSpi(this); //Set transaction callback
}
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;
TrdCommon.TrdHeader header = TrdCommon.TrdHeader.newBuilder()
.setAccID(281756455988247915L)
.setTrdEnv(TrdCommon.TrdEnv.TrdEnv_Real_VALUE)
.setTrdMarket(TrdCommon.TrdMarket.TrdMarket_HK_VALUE)
.build();
TrdGetFunds.C2S c2s = TrdGetFunds.C2S.newBuilder()
.setHeader(header)
.build();
TrdGetFunds.Request req = TrdGetFunds.Request.newBuilder().setC2S(c2s).build();
int seqNo = trd.getFunds(req);
System.out.printf("Send TrdGetFunds: %d\n", seqNo);
}
@Override
public void onDisconnect(MMAPI_Conn client, long errCode) {
System.out.printf("Trd onDisConnect: %d\n", errCode);
}
@Override
public void onReply_GetFunds(MMAPI_Conn client, int nSerialNo, TrdGetFunds.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("TrdGetFunds failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive TrdGetFunds: %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
65
66
67
- Output
Send TrdGetFunds: 2
Receive TrdGetFunds: {
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"header": {
"trdEnv": 1,
"accID": "281756455988247915",
"trdMarket": 1
},
"funds": {
"power": 535202.51221,
"totalAssets": 0.0,
"securitiesAssets": 0.0,
"fundAssets": 0.0,
"bondAssets": 0.0,
"cash": 0.0,
"marketVal": 0.0,
"frozenCash": 0.0,
"debtCash": 0.0,
"avlWithdrawalCash": 0.0,
"initialMargin": 221955.2062,
"maintenanceMargin": 155362.6396,
"maxPowerShort": 446002.09351,
"netCashPower": 0.0,
"longMv": 0.0,
"shortMv": 0.0,
"pendingAsset": 0.0,
"maxWithdrawal": 0.0,
"riskStatus": 2,
"marginCallMargin": 199751.9652
}
}
}
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
moomoo::u32_t GetFunds(const Trd_GetFunds::Request &stReq);
virtual void OnReply_GetFunds(moomoo::u32_t nSerialNo, const Trd_GetFunds::Response &stRsp) = 0;
Description
Query fund data such as net asset value, securities market value, cash, and purchasing power of trading accounts.
Parameters
message C2S
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional bool refreshCache = 2; //If explicitly set to true, force to update the cache of OpenD. Otherwise just return the local cached data of OpenD.
//Under normal circumstances, the cached data of OpenD is in sync with server, so there is no need to force an update. It's faster to use the local cached data, and will not cause any pressure on server.
//If you encounter packet loss, etc., the cached data may be inconsistent with the server. If the user finds that the data is not the latest, they can set this flag to true to update the cache and get latest data.
optional int32 currency = 3; //Currency type, see Trd_Common.Currency. Only required for universal securities accounts and futures accounts, other accounts are ignored
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Return
message S2C
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional Trd_Common.Funds funds = 2; //Account funds
}
message Response
{
//The following 3 fields are available in all protocols, and the notes are in 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
15
- 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;
// construct request message
Trd_GetFunds::Request req;
Trd_GetFunds::C2S *c2s = req.mutable_c2s();
Trd_Common::TrdHeader *header = c2s->mutable_header();
header->set_accid(3637840);
header->set_trdenv(0);
header->set_trdmarket(1);
m_GetFundsSerialNo = m_pTrdApi->GetFunds(req);
cout << "Request GetFunds SerialNo: " << m_GetFundsSerialNo << endl;
}
virtual void OnReply_GetFunds(moomoo::u32_t nSerialNo, const Trd_GetFunds::Response &stRsp){
if(nSerialNo == m_GetFundsSerialNo)
{
cout << "OnReply_GetFunds SerialNo: " << nSerialNo << endl;
// print response
// ProtoBufToBodyData and UTF8ToLocal refer to tool.h in Samples
string resp_str;
ProtoBufToBodyData(stRsp, resp_str);
cout << UTF8ToLocal(resp_str) << endl;
}
}
protected:
MMAPI_Trd *m_pTrdApi;
moomoo::u32_t m_GetFundsSerialNo;
};
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 GetFunds SerialNo: 4
OnReply_GetFunds SerialNo: 4
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"header": {
"trdEnv": 0,
"accID": "3637840",
"trdMarket": 1
},
"funds": {
"power": 0,
"totalAssets": 994327.11,
"securitiesAssets": 994327.11,
"fundAssets": 0,
"bondAssets": 0,
"cash": 814027.11,
"marketVal": 180300,
"frozenCash": 0,
"debtCash": 0,
"avlWithdrawalCash": 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
GetFunds(req);
Description
Query fund data such as net asset value, securities market value, cash, and purchasing power of trading accounts.
Parameters
message C2S
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional bool refreshCache = 2; //If explicitly set to true, force to update the cache of OpenD. Otherwise just return the local cached data of OpenD.
//Under normal circumstances, the cached data of OpenD is in sync with server, so there is no need to force an update. It's faster to use the local cached data, and will not cause any pressure on server.
//If you encounter packet loss, etc., the cached data may be inconsistent with the server. If the user finds that the data is not the latest, they can set this flag to true to update the cache and get latest data.
optional int32 currency = 3; //Currency type, see Trd_Common.Currency. Only required for universal securities accounts and futures accounts, other accounts are ignored
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Return
message S2C
{
required Trd_Common.TrdHeader header = 1; //Transaction common header
optional Trd_Common.Funds funds = 2; //Account funds
}
message Response
{
//The following 3 fields are available in all protocols, and the notes are in 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
15
- 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";
TrdGetFunds(){
const { RetType } = Common
const { TrdEnv } = 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) {
websocket.GetAccList({
c2s: {
userID: 0,
},
}).then((res) => {
let { retType, s2c: { accList } } = res
if(retType == RetType.RetType_Succeed){
let acc = accList.filter((item)=>{
return item.trdEnv == TrdEnv.TrdEnv_Simulate && item.trdMarketAuthList.some((auth)=>{ return auth == TrdMarket.TrdMarket_HK})
})[0]; // The sample takes the first HK paper trading environment account
const req = {
c2s: {
header: {
trdEnv: acc.trdEnv,
accID: acc.accID,
trdMarket: acc.trdMarketAuthList[0],
}
},
};
websocket.GetFunds(req)
.then((res) => {
let { errCode, retMsg, retType,s2c } = res
console.log("GetFunds: 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);
});
}
})
.catch((error) => {
console.log("GetAccList error:", error);
});
} else {
console.log("error", msg);
}
};
websocket.start(addr, port, enable_ssl, key);
// After using the connection, remember to close it to prevent the number of connections from running out
setTimeout(()=>{
websocket.stop();
console.log("stop");
}, 5000); // Set the script to receive OpenD push duration to 5 seconds
}
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
- Output
GetFunds: errCode 0, retMsg , retType 0
{
"header": {
"trdEnv": 0,
"accID": "6684972",
"trdMarket": 1
},
"funds": {
"power": 0,
"totalAssets": 1063162.9,
"securitiesAssets": 1063162.9,
"fundAssets": 0,
"bondAssets": 0,
"cash": 132162.9,
"marketVal": 931000,
"frozenCash": 0,
"debtCash": 0,
"avlWithdrawalCash": 0
}
}
stop
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Interface Limitations
- A maximum of 10 requests per 30 seconds
- It will be restricted by the frequency limit for this interface, only when refresh_cache is True