Trabajo finalizado
Tarea técnica
I have created skeleton EA, which goal is to open order and check the balance though Binance.com REST API, but it doesnt work. There are two layers of problems. First is described in this forum topic:
https://www.mql5.com/en/forum/253049
Apply to this job only if you know how to solve that. The Webrequest doesn't seem to properly connect to most binance api addresses. The only one that reports anything other than 404 is address for opening orders.
1. The EA needs to successfully connect to the REST API and open a waiting order (amount 1.0 ETH, price 0.001, pair ETHBTC). It needs to display result as Alert() and store it in string variable.
2. The EA must read "account" properly and display it's result as an Alert() and store it in string variable.
Currently the EA reports authorization error 401 result1 = {"code":-2014,"msg":"API-key format invalid."} for ORDER function and 404 not found error for ACCOUNT function. (404 also for any other function than order)
Documentation to Binance REST API:
https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md
Here is my skeleton EA, that needs to be corrected:
#property copyright "Copyright 2017,Grzegorz Korycki" #property version "1.00" #property strict #include <MQLMySQL.mqh> #include <SHA256.mqh> string api_key = "erased1"; string api_secret = "erased2"; int private_api_timeout = 2000; int last_api_call =0; string result1 = ""; int first_run = 1; double API_Balance(string symbol_temp) { bool failed = false; Alert("executing API Balance"); bool value_returned = false; char secret_char[]; string data2 = ""; int timestamp = (int)TimeGMT(); string for_encode = "timestamp="+(string)timestamp; char output_char[]; string output; SHA256 hash256; string hash = hash256.hmac(for_encode,api_secret); output = hash; Alert("output = "+output); string uri = "https://api.binance.com/api/v3/account"; string body = for_encode+"&signature="+output; char body_char[]; StringToCharArray(body, body_char, 0, WHOLE_ARRAY, CP_UTF8); ArrayResize(body_char, ArraySize(body_char)-1); ResetLastError(); char result_char[]; string result_headers; string headers = "Sign: "+output+"\r\nKey: "+api_key; headers = "X-MBX-APIKEY: "+api_key; Alert("headers = "+headers); int res=WebRequest("POST",uri,"",headers,5000,body_char,ArraySize(body_char),result_char, result_headers); Alert("Executing URL:"+uri); result1 = CharArrayToString(result_char,0,WHOLE_ARRAY); if(res!=200) { Alert("Authorization error #"+(string)res+", LastError="+(string)GetLastError()); Alert("result1 = "+ result1); failed = true; return -1; } else { Alert("balance loaded !"); Alert("result1 = "+ result1); return 1; } } bool API_Open_Trade(int trade_direction, string pair_name, double trade_size, double price_temp, ) { bool failed = false; Alert("executing API Open Trade"); bool value_returned = false; char secret_char[]; string data2 = ""; int timestamp = (int)TimeGMT(); string for_encode = "symbol=ETHBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.002&recvWindow=5000×tamp="+(string)timestamp; char output_char[]; string output; SHA256 hash256; string hash = hash256.hmac(for_encode,api_secret); output = hash; Alert("output = "+output); string uri = "https://api.binance.com/api/v3/order/test"; string body = for_encode+"&signature="+output; char body_char[]; StringToCharArray(body, body_char, 0, WHOLE_ARRAY, CP_UTF8); ArrayResize(body_char, ArraySize(body_char)-1); ResetLastError(); char result_char[]; string result_headers; string headers = "Sign: "+output+"\r\nKey: "+api_key; headers = "X-MBX-APIKEY: "+api_key; Alert("headers = "+headers); int res=WebRequest("POST",uri,"",headers,5000,body_char,ArraySize(body_char),result_char, result_headers); Alert("Executing URL:"+uri); result1 = CharArrayToString(result_char,0,WHOLE_ARRAY); if(res!=200) { Alert("Authorization error #"+(string)res+", LastError="+(string)GetLastError()); Alert("result1 = "+ result1); failed = true; return -1; } else { Alert("balance loaded !"); Alert("result1 = "+ result1); return 1; } } int OnInit() { API_Open_Trade(1,"ETHBTC", 1, 0.001); API_Balance("ETH"); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { }
Please contact me if you are interested. I will provide you with API keys and SHA256 function. https://api.binance.com needs to be added to allowed addresses in MT4.
Again - apply only if you know how to solve the problem mentioned in the Forum topic - you test the issue in your MT4 in less than a minute.
If solution uses external files, they must allow for usage of HTTP proxy.