Binance EA Connection Library MT4 Documentation – Other – 17 March 2024


Product URL  : https://www.mql5.com/en/market/product/114646

This library will allow you to manage trades using any of your EA and its very easy to integrate on any EA which you can do yourself with the script code which is mentioned in description and also demo examples on video which shows the complete process which is for mt5 but works for mt4 with listed API calls avaialble for MT4.

– Place Limit, SL Limit and Take Profit Limit Orders

– Place Market, SL-Market, TP-Market orders

– Modify Limit order

– Cancel Order

– Query Orders

– Change Leverage, margin

– Get Position info

and more…

URL:

Spot:

  • For testnet API is : testnet.binance.vision
  • For LIVE MODE API is: api.binance.com
  • Futures:

  • For testnet API is testnet.binancefuture.com
  • For LIVE MODE API is: fapi.binance.com
  • Tutorial 1 : Trading Operations, Please note : Symbols creation or History is not available in MT4 version

    Script Demo :

    
    
    
    
    
    #property copyright "Copyright 2024, Rajesh Kumar Nait"
    #property link      "https://www.mql5.com/en/users/rajeshnait/seller"
    #property version   "1.00"
    #property description "Uncomment code as required"
    
    
    struct BinanceConfig {
       string            api_url;
       string            api_key;
       string            api_secret;
       string            api_suffix;
       string            symbol_prefix;
       bool              debug;
    };
    
    
    #import "..\Libraries\Library_Binance_mt4.ex5"
    void Binance_Init(BinanceConfig &config);
    string orderLimit(string symbol, string side, double quantity, double price, string timeInForce_, string recvWindow_,  string reduceOnly_);
    string modifyorderLimit(string symbol, long oid, string side, double quantity, double price, string recWindow_,  string reduceOnly_);
    string orderMarket(string symbol, string side, double quantity, string recWindow_,  string reduceOnly_);
    string orderStopMarket(string symbol,string side,double quantity,double stopPrice, string workingtype, string recWindow_, string reduceOnly_, string priceProtect_ );
    string orderTakeProfitMarket(string symbol,string side,double quantity,double stopPrice, string workingtype, string recWindow_,  string reduceOnly_, string priceProtect_ );
    string orderStopLimit(string symbol,string side,double quantity,double stopPrice,double price, string workingtype, string recWindow_, string reduceOnly_, string priceProtect_ );
    string orderTakeProfitLimit(string symbol,string side,double quantity,double stopPrice,double price, string workingtype, string recWindow_,  string reduceOnly_, string priceProtect_ );
    string CancelOrder(string symbol, long orderId, string recWindow_ );
    string CancelAllOpenOrder(string symbol, string recWindow_ );
    string QueryPositionMode(string recWindow_ );
    string QueryMultiAssetMode(string recWindow_);
    string QueryOrderwithID(string symbol, long orderid, string recWindow_);
    string CurrentOpenOrder(string symbol,long orderid, string recWindow_);
    string CurrentAllOpenOrders(string symbol,string recWindow_);
    string GetFuturesBalance(string recWindow_);
    string GetAccount(string recWindow_);
    string changeLeverage(string symbol,int leverage, string recWindow_);
    string changeMargin(string symbol,string marginType, string recWindow_ );
    string positionRisk(string symbol, string recWindow_);
    #import
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    bool   Binance_debug   = true;
    string Binance_Key     = ""; 
    string Binance_Secret  = ""; 
    string Binance_URL     = "https://testnet.binancefuture.com"; 
    string Binance_suffix  = "/fapi/v1/";
    string Binance_SymbolPrefix = "a_";
    
    
    
    
    
    string timeInForce="GTC";   
    string reduceOnly="true";   
    string priceProtect="true"; 
    string recvWindow="2000";   
    string working_Type = "CONTRACT_PRICE"; 
    
    
    
    
    
    datetime MaxDate= D'2023-12-20 00:00:00';
    
    
    BinanceConfig config;
    
    
    
    
    void OnStart() {
    
    
       config.api_url = Binance_URL;
       config.api_key = Binance_Key;
       config.api_secret = Binance_Secret;
       config.api_suffix = Binance_suffix;
       config.symbol_prefix = Binance_SymbolPrefix;
       config.debug = Binance_debug;
    
       Binance_Init(config);
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    FAQs :

    1. How to remove symbol prefix from my symbol when sending order to binance? if symbol prefix is a_BTCUSDT, when sending order via API you should send “BTCUSDT” only

    here is the script tutorial to remove prefix from your symbol

    void OnStart() {
    
       string sym = _Symbol;
       string prefix = "bs_"; 
       int length = 3; 
       string resultString;
    
    
       if (StringSubstr(sym, 0, length) == prefix) {
          
          resultString = StringSubstr(sym, length);
       } else {
          
          resultString = sym;
       }
    
    
       Print("Original Symbol: ", sym);
       Print("Result String: ", resultString);
    }

    2. I am getting wrong Lot size calculation while calculating lot size for my EA, I use Google to calculate USD rates. What I am doing wrong?

    You should calculate Lot size as per Binance rules only. Here are links to Binance Leveraged Margin Table which mentions Maintenance Margin as per your position size. You should deduct Maintenance margin from your position size. Also you must be interested in Liquidation Calculation logic to be implemented in your EA

    Example : First go to Binance App and select leverage you wish e.g. 125x

    then lets suppose your account balance is $33.69 and BTCUSDT price is 59169.3

    then check how much qty you can buy, in this case as per price in screenshot you can see I can buy 0.071 max qty

    so make sure your calculation is either 0.071 or below then your EA will be placing order without any issue.

    ——————————————————————————————

    5. How do i check my USDT balance using EA Connector library?

    Here is the script demo

    
    
    
    
    
    #property copyright "Copyright 2024, MetaQuotes Ltd."
    #property link      "https://www.mql5.com"
    #property version   "1.00"
    
    #include <JAson.mqh>
    CJAVal jv_(NULL, jtUNDEF);
    
    
    struct BinanceConfig {
       string            api_url;
       string            api_key;
       string            api_secret;
       string            api_suffix;
       string            symbol_prefix;
       bool              debug;
    };
    
    
    #import "..\Libraries\Library_Binance.ex5"
    void Binance_Init(BinanceConfig &config);
    string orderLimit(string symbol, string side, double quantity, double price, string timeInForce_, string recvWindow_,  string reduceOnly_);
    string modifyorderLimit(string symbol, long oid, string side, double quantity, double price, string recWindow_,  string reduceOnly_);
    string orderMarket(string symbol, string side, double quantity, string recWindow_,  string reduceOnly_);
    string orderStopMarket(string symbol,string side,double quantity,double stopPrice, string workingtype, string recWindow_, string reduceOnly_, string priceProtect_);
    string orderTakeProfitMarket(string symbol,string side,double quantity,double stopPrice, string workingtype, string recWindow_,  string reduceOnly_, string priceProtect_);
    string orderStopLimit(string symbol,string side,double quantity,double stopPrice,double price, string workingtype, string recWindow_, string reduceOnly_, string priceProtect_);
    string orderTakeProfitLimit(string symbol,string side,double quantity,double stopPrice,double price, string workingtype, string recWindow_,  string reduceOnly_, string priceProtect_);
    string CancelOrder(string symbol, long orderId, string recWindow_);
    string CancelAllOpenOrder(string symbol, string recWindow_);
    string QueryPositionMode(string recWindow_);
    string QueryMultiAssetMode(string recWindow_);
    string QueryOrderwithID(string symbol, long orderid, string recWindow_);
    string CurrentOpenOrder(string symbol,long orderid, string recWindow_);
    string CurrentAllOpenOrders(string symbol,string recWindow_);
    string GetFuturesBalance(string recWindow_);
    string GetAccount(string recWindow_);
    string changeLeverage(string symbol,int leverage, string recWindow_);
    string changeMargin(string symbol,string marginType, string recWindow_);
    string positionRisk(string symbol, string recWindow_);
    #import
    
    bool Binance_debug     = true;
    
    
    string Binance_Key     = ""; 
    string Binance_Secret  = ""; 
    string Binance_URL     = "https://fapi.binance.com"; 
    
    
    string Binance_suffix  = "/fapi/v1/";
    string Binance_SymbolPrefix = "a_";
    
    
    
    
    string timeInForce="GTC";   
    string reduceOnly="true";   
    string priceProtect="true"; 
    string recvWindow="2000";   
    string working_Type = "CONTRACT_PRICE"; 
    
    
    BinanceConfig config;
    
    
    
    void OnStart() {
    
       config.api_url = Binance_URL;
       config.api_key = Binance_Key;
       config.api_secret = Binance_Secret;
       config.api_suffix = Binance_suffix;
       config.symbol_prefix = Binance_SymbolPrefix;
       config.debug = Binance_debug;
    
       Binance_Init(config);
    
    
       string s = GetFuturesBalance(recvWindow);
       jv_.Deserialize(s);
    
       for(int i=0; i<jv_.Size(); i++) {
    
          string asset = jv_[i]["asset"].ToStr();
          double bal = jv_[i]["balance"].ToDbl();
    
    
          if(asset=="USDT"){ 
             double balance = NormalizeDouble(bal,2);
             Print("Balance ",balance);
           }
       }
    
    }
    
    



    Source link

    Register at Binance

    Scroll to Top