← All EA examples
1020 / MQL5 / USDⓈ-M FUTURES

How can MQL5 query Binance account configuration for a symbol?

Call ufQuerySymbolConfiguration for the chart contract. This example reads symbol, marginType, isAutoAddMargin, leverage and maxNotionalValue from the first response-array item. It queries configuration without changing leverage or margin mode.

What do you need to use this example?

  • Install and configure MT5 Binance and API credentials. Make mt5binance.mqh, JAson.mqh and the account permissions required by this example available.
  • Use the intended chart symbol and account configuration access. The data describes this account’s settings for that symbol rather than exchange-wide rules for all symbols.

How does the source execute?

  1. On the first tick, call ufQuerySymbolConfiguration(handles, Symbol(), recvWindow, resultChar).
  2. Convert the synchronous response to a string and parse it as CJAVal.
  3. Read configuration fields at index 0 and set isRun=true; the source does not traverse all array items.

Run on the first tick of this attachment; isRun then prevents repetition. Reloading may execute it again.

Parameters and execution behavior

ParameterSource value or behavior
Main interfaceufQuerySymbolConfiguration
SymbolSymbol()
Array index0
Margin settingsmarginType / isAutoAddMargin
Leverage settingsleverage / maxNotionalValue
Request timing windowrecvWindow = 3000 ms
Execution triggerRun on the first tick of this attachment; isRun then prevents repetition. Reloading may execute it again.

Related guides and examples

Full MQL5 source code

//+------------------------------------------------------------------+
//|币安U本位期货查看交易对配置binanceUSDMarginFutureQuerySymbolConfiguration.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 MT5 binance API header file 引入MT5币安API头文件
// 查询当前合约的保证金模式、杠杆及名义价值配置。
// Query margin mode, leverage, and notional configuration for the current symbol.
// 引入币安 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反序列化币安返回的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/binance-spot-api-docs/rest-api/endpoint-security-type#timing-security
//设置时间同步安全 https://developers.binance.com/docs/zh-CN/binance-spot-api-docs/rest-api/endpoint-security-type#%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.
static char resultChar[];

// 执行标记,只有在后续代码检查并更新此变量时才限制重复运行。
// Execution flag. It limits repeated execution only where later code checks and updates it.
bool isRun=false;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
// EA 加载时由 MT5 调用,执行本例所需的初始化。
// MT5 calls this handler when the EA is loaded to perform initialization required by this example.
int OnInit()
  {
   // 初始化配置中已设置的币安 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.
void OnDeinit(const int reason)
  {
//---
   // 释放本例创建的 curl 请求句柄。
   // Release the curl request handles created by this example.
   curlCleanUp(handles);

   printf("curlCleanUp");
  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
// 收到行情时检查执行标记,按函数体中的标记控制演示是否重复运行。
// On each tick, check the execution flag and use the flag logic in the function body to control repeated execution.
void OnTick()
  {
   if(isRun==false)
     {
      //Get current account symbol configuration 查询交易对上的基础配置
      // 查询当前图表合约的账户配置,包括保证金模式、自动追加保证金和杠杆倍数。
      // Query account configuration for the current chart symbol, including margin mode, automatic margin addition, and leverage.
      // 此处使用 handles 发起请求,并通过最后一个引用参数 resultChar 接收同步响应。
      // Use handles for the request and receive the synchronous response through the final reference parameter resultChar.
      ufQuerySymbolConfiguration(handles, Symbol(),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 jSymbolConfiguration;
      // 解析 JSON 供后续按字段名读取;本例此处未检查解析函数的返回值。
      // Deserialize JSON for subsequent field access. This example does not check the parser return value here.
      jSymbolConfiguration.Deserialize(result);

      // 读取 symbol 字段,表示交易对,使用 ToStr 转换。
      // Read symbol for trading symbol using ToStr.
      string symbol=jSymbolConfiguration[0]["symbol"].ToStr();
      // 读取 marginType 字段,表示保证金模式,使用 ToStr 转换。
      // Read marginType for margin mode using ToStr.
      string marginType=jSymbolConfiguration[0]["marginType"].ToStr();
      // 读取 isAutoAddMargin 字段,表示是否自动追加保证金,使用 ToBool 转换。
      // Read isAutoAddMargin for whether automatic margin addition is enabled using ToBool.
      bool isAutoAddMargin=jSymbolConfiguration[0]["isAutoAddMargin"].ToBool();
      // 读取 leverage 字段,表示杠杆倍数,使用 ToStr 转换。
      // Read leverage for leverage using ToStr.
      string leverage=jSymbolConfiguration[0]["leverage"].ToStr();
      // 读取 maxNotionalValue 字段,表示允许的最大名义价值,使用 ToStr 转换。
      // Read maxNotionalValue for maximum permitted notional value using ToStr.
      string maxNotionalValue=jSymbolConfiguration[0]["maxNotionalValue"].ToStr();

      // 标记本次演示已执行;此处未按请求成功与否决定是否重试。
      // Mark this demonstration as executed. This assignment does not select retries based on request success.
      isRun=true;
     }
  }

//+------------------------------------------------------------------+