//+------------------------------------------------------------------+//|币安U本位期货查看账户信息V3binanceUSDMarginFutureQueryAccountV3.mq5 |//| Copyright 2024, MetaQuotes Ltd. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright "Copyright 2024, MetaQuotes Ltd."#property link "https://www.mql5.com"#property version "1.00"//import MQL binance API 引入币安MQL接口的头文件// 读取 V3 账户汇总信息,并分别遍历资产和持仓数组。// Read V3 account totals and iterate through asset and position arrays.// 引入币安 MQL 接口声明,调用方式以本文件现有接口签名为准。// Include the Binance MQL API declarations and use the API signatures present in this file.#include <mt5binance.mqh>//import mql5.com open source json library to deserialize binance reponse json https://www.mql5.com/en/code/13663//引入mql5.com上的开源json library反序列化币安API返回的json数据 import json library open source from mql5.com to deserialize binance API json//https://www.mql5.com/zh/code/13663// 使用 JAson.mqh 的 CJAVal 解析同步响应和异步事件 JSON。// Use CJAVal from JAson.mqh to parse synchronous responses and asynchronous event JSON.#include <JAson.mqh>//setup Timing security https://developers.binance.com/docs/derivatives/usds-margined-futures/general-info#timing-security//设置时间同步安全 https://developers.binance.com/docs/zh-CN/derivatives/usds-margined-futures/general-info#%E6%97%B6%E9%97%B4%E5%90%8C%E6%AD%A5%E5%AE%89%E5%85%A8// 请求时间窗口为 3000 毫秒,币安按请求时间戳与服务器时间检查请求时效。// The request timing window is 3000 milliseconds. Binance validates request timing against its server time.long recvWindow=3000;
//币安MQL接口使用的curl handles// 保存币安请求使用的 curl 句柄,供接口调用复用。// Store curl handles for reuse by Binance API calls.int handles[];
//binance reponse 币安返回数据// 同步响应缓冲区,API 调用将返回数据写入此字符数组。// Synchronous response buffer populated by API calls.staticchar resultChar[];
//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+// EA 加载时由 MT5 调用,执行本例所需的初始化。// MT5 calls this handler when the EA is loaded to perform initialization required by this example.intOnInit()
{
// 初始化配置中已设置的币安 API 凭据,供后续签名请求使用。// Initialize the configured Binance API credentials for subsequent signed requests.initBinanceAPIKey();
// 初始化 curl 全局状态,后续分配句柄数组并创建请求句柄。// Initialize global curl state before allocating the handle array and creating request handles.curlGlobalInit();
ArrayResize(handles, 1);
curlInit(handles);
// 为返回 JSON 分配缓冲区,响应长度需要在分配容量内。// Allocate a response buffer large enough to hold the returned JSON.ArrayResize(resultChar, 10000);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+// EA 卸载或重新初始化前调用,reason 为卸载原因;清理操作以函数体为准。// Called before EA removal or reinitialization. reason identifies the cause, and the function body defines the cleanup performed.voidOnDeinit(constint reason)
{
//---// 释放本例创建的 curl 请求句柄。// Release the curl request handles created by this example.curlCleanUp(handles);
printf("curlCleanUp");
}
//+------------------------------------------------------------------+//| |//+------------------------------------------------------------------+// 每次收到行情都会执行函数体中的请求流程,本函数未使用一次性运行判断。// Each tick executes the request sequence in this handler, which has no one-shot execution guard.voidOnTick()
{
//Account Information V3 账户信息V3// 查询 V3 账户信息,从顶层读取汇总数据,并遍历 assets 和 positions 两个数组。// Query V3 account information, read account totals at the top level, and iterate through the assets and positions arrays.// 此处使用 handles 发起请求,并通过最后一个引用参数 resultChar 接收同步响应。// Use handles for the request and receive the synchronous response through the final reference parameter resultChar.ufAccountInformationV3(handles, recvWindow, resultChar);
// 按 UTF-8 将同步响应字符数组转换为 MQL 字符串。// Convert the synchronous response character array to an MQL string using UTF-8.string result=CharArrayToString(resultChar, 0, -1, CP_UTF8);
CJAVal json;
// 解析 JSON 供后续按字段名读取;本例此处未检查解析函数的返回值。// Deserialize JSON for subsequent field access. This example does not check the parser return value here.
json.Deserialize(result);
// 读取 assets 节点,即资产数组。// Read assets, the asset array.CJAVal JAssets=json["assets"];
// 读取 positions 节点,即持仓数组。// Read positions, the position array.CJAVal JPositions=json["positions"];
// 读取 totalInitialMargin 字段,表示初始保证金总额,使用 ToStr 转换。// Read totalInitialMargin for total initial margin using ToStr.string totalInitialMargin=json["totalInitialMargin"].ToStr();//total initial margin required with current mark price (useless with isolated positions), only for USDT asset 当前所需起始保证金总额(存在逐仓请忽略), 仅计算usdt资产positions), only for USDT asset// 读取 totalMaintMargin 字段,表示维持保证金总额,使用 ToStr 转换。// Read totalMaintMargin for total maintenance margin using ToStr.string totalMaintMargin=json["totalMaintMargin"].ToStr();// total maintenance margin required, only for USDT asset 维持保证金总额, 仅计算usdt资产// 读取 totalWalletBalance 字段,表示钱包余额总额,使用 ToStr 转换。// Read totalWalletBalance for total wallet balance using ToStr.string totalWalletBalance=json["totalWalletBalance"].ToStr();// total wallet balance, only for USDT asset 账户总余额, 仅计算usdt资产// 读取 totalUnrealizedProfit 字段,表示未实现盈亏总额,使用 ToStr 转换。// Read totalUnrealizedProfit for total unrealized profit or loss using ToStr.string totalUnrealizedProfit=json["totalUnrealizedProfit"].ToStr();// total unrealized profit, only for USDT asset 持仓未实现盈亏总额, 仅计算usdt资产// 读取 totalMarginBalance 字段,表示保证金余额总额,使用 ToStr 转换。// Read totalMarginBalance for total margin balance using ToStr.string totalMarginBalance=json["totalMarginBalance"].ToStr();// total margin balance, only for USDT asset 保证金总余额, 仅计算usdt资产// 读取 totalPositionInitialMargin 字段,表示持仓初始保证金总额,使用 ToStr 转换。// Read totalPositionInitialMargin for total position initial margin using ToStr.string totalPositionInitialMargin=json["totalPositionInitialMargin"].ToStr();// initial margin required for positions with current mark price, only for USDT asset 持仓所需起始保证金(基于最新标记价格), 仅计算usdt资产// 读取 totalOpenOrderInitialMargin 字段,表示挂单初始保证金总额,使用 ToStr 转换。// Read totalOpenOrderInitialMargin for total open-order initial margin using ToStr.string totalOpenOrderInitialMargin=json["totalOpenOrderInitialMargin"].ToStr();// initial margin required for open orders with current mark price, only for USDT asset 当前挂单所需起始保证金(基于最新标记价格), 仅计算usdt资产// 读取 totalCrossWalletBalance 字段,表示全仓钱包余额总额,使用 ToStr 转换。// Read totalCrossWalletBalance for total cross wallet balance using ToStr.string totalCrossWalletBalance=json["totalCrossWalletBalance"].ToStr();// crossed wallet balance, only for USDT asset 全仓账户余额, 仅计算usdt资产// 读取 totalCrossUnPnl 字段,表示全仓未实现盈亏总额,使用 ToStr 转换。// Read totalCrossUnPnl for total cross unrealized profit or loss using ToStr.string totalCrossUnPnl=json["totalCrossUnPnl"].ToStr();// unrealized profit of crossed positions, only for USDT asset 全仓持仓未实现盈亏总额, 仅计算usdt资产// 读取 availableBalance 字段,表示可用余额,使用 ToStr 转换。// Read availableBalance for available balance using ToStr.string availableBalance=json["availableBalance"].ToStr();// available balance, only for USDT asset 可用余额, 仅计算usdt资产// 读取 maxWithdrawAmount 字段,表示最大可转出金额,使用 ToStr 转换。// Read maxWithdrawAmount for maximum withdrawable amount using ToStr.string maxWithdrawAmount=json["maxWithdrawAmount"].ToStr();// maximum amount for transfer out, only for USDT asset 最大可转出余额, 仅计算usdt资产// 逐项遍历返回数组,读取每条记录。// Iterate through the returned array and read each record.for(int i=0; i<JAssets.Size(); i++)
{
CJAVal JAsset=JAssets[i];
// 读取 asset 字段,表示资产名称,使用 ToStr 转换。// Read asset for asset name using ToStr.string asset=JAsset["asset"].ToStr();// asset name 资产// 读取 walletBalance 字段,表示钱包余额,使用 ToStr 转换。// Read walletBalance for wallet balance using ToStr.string walletBalance=JAsset["walletBalance"].ToStr();// total wallet balance 余额// 读取 unrealizedProfit 字段,表示未实现盈亏,使用 ToStr 转换。// Read unrealizedProfit for unrealized profit or loss using ToStr.string unrealizedProfit=JAsset["unrealizedProfit"].ToStr();//unrealized profit or loss 未实现盈亏// 读取 marginBalance 字段,表示保证金余额,使用 ToStr 转换。// Read marginBalance for margin balance using ToStr.string marginBalance=JAsset["marginBalance"].ToStr();//margin balance 保证金余额// 读取 maintMargin 字段,表示维持保证金,使用 ToStr 转换。// Read maintMargin for maintenance margin using ToStr.string maintMargin=JAsset["maintMargin"].ToStr();//maintenance margin 维持保证金// 读取 initialMargin 字段,表示初始保证金,使用 ToStr 转换。// Read initialMargin for initial margin using ToStr.string initialMargin=JAsset["initialMargin"].ToStr();//total intial margin required with the latest mark price 当前所需起始保证金// 读取 positionInitialMargin 字段,表示持仓所需初始保证金,使用 ToStr 转换。// Read positionInitialMargin for initial margin required for positions using ToStr.string positionInitialMargin=JAsset["positionInitialMargin"].ToStr();// positions margin required with the latest mark price 持仓所需起始保证金(基于最新标记价格)// 读取 openOrderInitialMargin 字段,表示挂单所需初始保证金,使用 ToStr 转换。// Read openOrderInitialMargin for initial margin required for open orders using ToStr.string openOrderInitialMargin=JAsset["openOrderInitialMargin"].ToStr();//open orders intial margin required with the latest mark price 当前挂单所需起始保证金(基于最新标记价格)// 读取 maxWithdrawAmount 字段,表示最大可转出金额,使用 ToStr 转换。// Read maxWithdrawAmount for maximum withdrawable amount using ToStr.string maxWithdrawAmount=JAsset["maxWithdrawAmount"].ToStr(); // available amount for transfer out 最大可转出余额// 读取 crossWalletBalance 字段,表示全仓钱包余额,使用 ToStr 转换。// Read crossWalletBalance for cross wallet balance using ToStr.string crossWalletBalance=JAsset["crossWalletBalance"].ToStr();// wallet balance for crossed margin 全仓账户余额// 读取 crossUnPnl 字段,表示全仓未实现盈亏,使用 ToStr 转换。// Read crossUnPnl for unrealized profit or loss of cross-margin positions using ToStr.string crossUnPnl=JAsset["crossUnPnl"].ToStr();// total unrealized profit or loss of crossed positions 全仓持仓未实现盈亏// 读取 availableBalance 字段,表示可用余额,使用 ToStr 转换。// Read availableBalance for available balance using ToStr.string availableBalance=JAsset["availableBalance"].ToStr();// available margin balance 可用余额// 读取 updateTime 字段,表示更新时间戳,使用 ToStr 转换。// Read updateTime for update timestamp using ToStr.string updateTime=JAsset["updateTime"].ToStr();// update time 更新时间
}
// 逐项遍历返回数组,读取每条记录。// Iterate through the returned array and read each record.for(int i=0; i<JPositions.Size(); i++)
{
CJAVal JPosition=JPositions[i];
// 读取 symbol 字段,表示交易对,使用 ToStr 转换。// Read symbol for trading symbol using ToStr.string symbol=JPosition["symbol"].ToStr();// 交易对// 读取 positionSide 字段,表示持仓方向,使用 ToStr 转换。// Read positionSide for position side using ToStr.string positionSide=JPosition["positionSide"].ToStr();//position side 持仓方向// 读取 positionAmt 字段,表示持仓数量,使用 ToStr 转换。// Read positionAmt for position amount using ToStr.string positionAmt=JPosition["positionAmt"].ToStr();// 持仓数量// 读取 unrealizedProfit 字段,表示未实现盈亏,使用 ToStr 转换。// Read unrealizedProfit for unrealized profit or loss using ToStr.string unrealizedProfit=JPosition["unrealizedProfit"].ToStr();//unrealized profit 持仓未实现盈亏// 读取 isolatedMargin 字段,表示逐仓保证金,使用 ToStr 转换。// Read isolatedMargin for isolated margin using ToStr.string isolatedMargin=JPosition["isolatedMargin"].ToStr();//// 读取 notional 字段,表示持仓名义价值,使用 ToStr 转换。// Read notional for position notional value using ToStr.string notional=JPosition["notional"].ToStr();// 当前杠杆下用户可用的最大名义价值// 读取 isolatedWallet 字段,表示逐仓钱包余额,使用 ToStr 转换。// Read isolatedWallet for isolated wallet balance using ToStr.string isolatedWallet=JPosition["isolatedWallet"].ToStr();//// 读取 initialMargin 字段,表示初始保证金,使用 ToStr 转换。// Read initialMargin for initial margin using ToStr.string initialMargin=JPosition["initialMargin"].ToStr();//initial margin required with current mark price 当前所需起始保证金(按最新标标记价格)// 读取 maintMargin 字段,表示维持保证金,使用 ToStr 转换。// Read maintMargin for maintenance margin using ToStr.string maintMargin=JPosition["maintMargin"].ToStr();//maintenance margin required 持仓维持保证金// 读取 updateTime 字段,表示更新时间戳,使用 ToStr 转换。// Read updateTime for update timestamp using ToStr.string updateTime=JPosition["updateTime"].ToStr();//
}
}
//+------------------------------------------------------------------+