Leaderboard
Popular Content
Showing content with the highest reputation on 04/27/2024 in all areas
-
Binance API UDF
fraizor reacted to andybiochem for a topic
Hi All, Firstly, I accept no responsibility for how you use the following UDF, use at your own risk! Below is a very basic UDF that will allow communication with Binance's API (binance.com, not the US version). There is no error handling - add this yourself if you are concerned. If you are going to use this UDF in any meaningful way, carefully check the return of every single call, to make sure there is no error in the data you are receiving back from Binance. e.g. if you are pulling ticker prices, and suddenly you get $NULL returned, this could affect how you go on to place orders etc. I have very intentionally not provided an example of how to place orders using the UDF. If you are technically minded enough to use the UDF and understand how to construct the POST data, this should not be an issue. If you are struggling to work out how to place orders with this, please stop - no good can come from not fully understanding Binance's API and trying to automate it. In any event, please start with POSTing to "/api/v3/order/test" before commiting to your live account! The whole UDF calls directly on winhttp.dll, this was the only method I could find that gave a decent reliability, TCP / INET functions I found quite unreliable. Every UDF return gives you the HTTP Code and API Weight pulled out of the http header. Monitor these carefully if you're making heavy/lots of calls to the API; it's very easy to make a mistake and spam Binance's server, which will lead in a ban. Lastly, if you get "Timestamp for this request is outside of the recvwindow" errors back from Binance, it is because your computer clock is not synced with an NTP properly. Sync your clock, and the error should go away, try rebooting too - BUT, the error will probably return after a few days/weeks as the sync drops. Monitor for this error, and keep on top of it if you can! ;----- API Keys ----- Global $sAPI_Key_Access = "enter your access key" Global $sAPI_Key_Secret = "enter your secret key" ;----- Prepare DLL ----- Global $hDll_WinHTTP = DllOpen("winhttp.dll") ;########## EXAMPLE API CALLS ########## ;----- Ping Binance ----- Global $sRet = _BINANCE_API_Call("api/v3/ping") ConsoleWrite($sRet & @CRLF & @CRLF) ;----- Get Binance's Server Time ----- $sRet = _BINANCE_API_Call("api/v3/time") ConsoleWrite($sRet & @CRLF & @CRLF) ;----- Get BTC/USDT Average Price ----- $sRet = _BINANCE_API_Call("api/v3/avgPrice","symbol=BTCUSDT") ConsoleWrite($sRet & @CRLF & @CRLF) ;----- Get ETH/BTC Live Ticker Price ----- For $i = 1 To 5 Sleep(100) $sRet = _BINANCE_API_Call("api/v3/ticker/price", "symbol=ETHBTC") ConsoleWrite($sRet & @CRLF) Next ConsoleWrite(@CRLF) ;----- Get Last 10 ETH/BTC Trades ----- $sRet = _BINANCE_API_Call("api/v3/trades", "symbol=ETHBTC&limit=10") ConsoleWrite($sRet & @CRLF & @CRLF) ;----- Get Your Account Data ----- $sRet = _BINANCE_API_Call("api/v3/account") ConsoleWrite($sRet & @CRLF & @CRLF) Func _BINANCE_API_Call($sEndPoint, $sParameters = "") ;************************************************** ; Performs Binance API Call Via Native WinHTTP dll ;************************************************** ;----- Vars ----- ; Presume GET, assign POST if needed in switch Local $sGETorPOST = "GET" ;----- Check Endpoint Required ----- ; Some endpoints require signing, other endpoints can be ; added as new cases. Switch $sEndPoint Case "api/v3/account" $sParameters &= "×tamp=" & _TimeStamp() $sParameters &= "&signature=" & _HMAC($sParameters, $sAPI_Key_Secret) Case "api/v3/order" $sGETorPOST = "POST" $sParameters &= "×tamp=" & _TimeStamp() $sParameters &= "&signature=" & _HMAC($sParameters, $sAPI_Key_Secret) Case "api/v3/myTrades" $sParameters &= "×tamp=" & _TimeStamp() $sParameters &= "&signature=" & _HMAC($sParameters, $sAPI_Key_Secret) EndSwitch ;----- Start Session ----- Local $hHTTP_Session = DllCall($hDll_WinHTTP, "handle", "WinHttpOpen", "wstr", "Mozilla/4.0", "dword", 0, "wstr", "", "wstr", "", "dword", 0)[0] ;----- Connect To Binance Server ----- Local $hHTTP_Connection = DllCall($hDll_WinHTTP, "handle", "WinHttpConnect", "handle", $hHTTP_Session, "wstr", "api.binance.com", "dword", 443, "dword", 0)[0] ;----- Prepare Request Data ----- If $sParameters <> "" Then $sParameters = "?" & $sParameters Local $hHTTP_Request = DllCall($hDll_WinHTTP, "handle", "WinHttpOpenRequest", "handle", $hHTTP_Connection, "wstr", $sGETorPOST, "wstr", $sEndPoint & $sParameters, "wstr", "HTTP/1.1", "wstr", "", "ptr", 0, "dword", 0x00800000)[0] ;----- Add Request Header ----- ; Adds API key to header even if not specifically needed, inconsequential DllCall($hDll_WinHTTP, "bool", "WinHttpAddRequestHeaders", "handle", $hHTTP_Request, "wstr", "X-MBX-APIKEY: " & $sAPI_Key_Access, "dword", -1, "dword", 0x10000000) ;----- Send Request To Server ----- DllCall($hDll_WinHTTP, "bool", "WinHttpSendRequest", "handle", $hHTTP_Request, "wstr", "", "dword", 0, "ptr", 0, "dword", 0, "dword", 0, "dword_ptr", 0) ;----- Recieve Response ----- DllCall($hDll_WinHTTP, "bool", "WinHttpReceiveResponse", "handle", $hHTTP_Request, "ptr", 0) ;----- Recieve Headers ----- ; Extract HTTP return code and API weight Local $sHeaders = DllCall($hDll_WinHTTP, "bool", "WinHttpQueryHeaders", "handle", $hHTTP_Request, "dword", 22, "wstr", "", "wstr", "", "dword*", 65536, "dword*", 0)[4] Local $sHTTP_ReturnCode = StringMid($sHeaders, StringInStr($sHeaders, "HTTP/1.1 ") + 9, StringInStr($sHeaders, @CR, 0, 1, StringInStr($sHeaders, "HTTP/1.1 ") + 9) - (StringInStr($sHeaders, "HTTP/1.1 ") + 9)) Local $sAPI_Weight = StringMid($sHeaders, StringInStr($sHeaders, "x-mbx-used-weight: ") + 19, StringInStr($sHeaders, @CR, 0, 1, StringInStr($sHeaders, "x-mbx-used-weight: ") + 19) - (StringInStr($sHeaders, "x-mbx-used-weight: ") + 19)) Local $sAPI_IPBan_RetryAfter_Sec = StringInStr($sHeaders, "Retry-After: ") = 0 ? "" : StringMid($sHeaders, StringInStr($sHeaders, "Retry-After: ") + 13, StringInStr($sHeaders, @CR, 0, 1, StringInStr($sHeaders, "Retry-After: ") + 13) - (StringInStr($sHeaders, "Retry-After: ") + 13)) ;----- Get Data ----- Local $sData = "" Local $iBytesToRead, $hBuffer_Data While 1 ;- Get Bytes To Read In This Loop - $iBytesToRead = DllCall($hDll_WinHTTP, "bool", "WinHttpQueryDataAvailable", "handle", $hHTTP_Request, "dword*", 0)[2] ;- Check If No More Data To Read - If $iBytesToRead <= 0 Then ExitLoop ;- Prep Data Buffer - $hBuffer_Data = DllStructCreate("char[" & $iBytesToRead & "]") ;- Read Data To Buffer - DllCall($hDll_WinHTTP, "bool", "WinHttpReadData", "handle", $hHTTP_Request, "struct*", $hBuffer_Data, "dword", $iBytesToRead, "dword*", 0) ;- Get Data From Buffer - $sData &= DllStructGetData($hBuffer_Data, 1) ;- Release - $hBuffer_Data = "" WEnd ;----- Close Handles ----- DllCall($hDll_WinHTTP, "bool", "WinHttpCloseHandle", "handle", $hHTTP_Request) DllCall($hDll_WinHTTP, "bool", "WinHttpCloseHandle", "handle", $hHTTP_Connection) DllCall($hDll_WinHTTP, "bool", "WinHttpCloseHandle", "handle", $hHTTP_Session) ;----- Return Data ----- ; Include HTTP Code and API Weight to check for overuse, and retry period if banned ; HTTP CODES : 429=over request limit, 418=IP banned due to overuse ; API WEIGHT : over 1200 will lead to ban Return '{"HTTPCode":"' & $sHTTP_ReturnCode & '","APIWeight":"' & $sAPI_Weight & ($sAPI_IPBan_RetryAfter_Sec = "" ? "" : '","Retry-After":"' & $sAPI_IPBan_RetryAfter_Sec) & '"}' & $sData EndFunc ;==>_BINANCE_API_Call Func _HMAC($bData, $bKey) ;************************************************** ; Create HMAC SHA256 Signature ;************************************************** Local $oHMAC = ObjCreate("System.Security.Cryptography.HMAC" & "SHA256") $oHMAC.key = Binary($bKey) Local $bHash = $oHMAC.ComputeHash_2(Binary($bData)) Return StringLower(StringMid($bHash, 3)) EndFunc ;==>_HMAC Func _TimeStamp() ;************************************************** ; Create UNIX-style TimeStamp ;************************************************** ; This is 'unix time', aka UTC time in milliseconds Local $aTimeStamp = DllCall("msvcrt.dll", "int:cdecl", "time", "int", 0) Return ($aTimeStamp[0] * 1000) + @MSEC ;convert to miliseconds EndFunc ;==>_TimeStamp An example return would be: {"HTTPCode":"200 OK","APIWeight":"4"}{"symbol":"ETHBTC","price":"0.07152900"} The first {} block is the HTTP code and API weight, the second {} block is the full Binance JSON reply. The JSON can be queried with a JSON UDF (do a search on the forum), or can be split with regexp. Again, please use with caution! Feel free to donate if you find this useful!: NANO nano_1161rm7egfzwgbtckwe8aoicee55897bbgc8apzrp1s3fzyyn7zqobbtkmfh1 point -
Questions on Comments and Code in Known Working Code - (Moved)
somdcomputerguy reacted to Jos for a topic
Care to share what isn't working and what you have changed, as else we will be stabbing in the dark?1 point -
Script/Text/File Manager - Update 4/6/2015
Musashi reacted to argumentum for a topic
Doubt it. Take a look at ..the rest of the custom stuff I guess you can code yourself.1 point -
; https://www.autoitscript.com/forum/topic/211837-gui-richedit-control-showing-symbols/#comment-1533262 #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <GuiRichEdit.au3> Global $hGUI, $btnRemoveTabs, $editBox Local $sTxt = "" $sTxt &= "Func Example()" & @CRLF $sTxt &= "" & @CRLF $sTxt &= @TAB & "$hGUI = GUICreate(""Remove Tabs"", 1000, 800)" & @CRLF $sTxt &= "" & @CRLF $sTxt &= @TAB & "$editBox = _GUICtrlRichEdit_Create($hGUI,"""", 10, 10, 980, 700)" & @CRLF $sTxt &= @TAB & @TAB & @TAB & "_GUICtrlRichEdit_SetFont($editBox, 12)" & @CRLF $sTxt &= @TAB & "GUICtrlSetData(-1, """")" & @CRLF $sTxt &= "" & @CRLF $sTxt &= @TAB & "$btnRemoveTabs = GUICtrlCreateButton(""Remove Tabs & CRLFs"", 400, 730, 200, 30)" & @CRLF $sTxt &= @TAB & "GUICtrlSetState(-1, $GUI_FOCUS)" & @CRLF $sTxt &= "" & @CRLF $sTxt &= @TAB & "GUISetState(@SW_SHOW)" & @CRLF $sTxt &= "" & @CRLF $sTxt &= @TAB & "While 1" & @CRLF $sTxt &= @TAB & @TAB & "Switch GUIGetMsg()" & @CRLF $sTxt &= @TAB & @TAB & "Case $GUI_EVENT_CLOSE" & @CRLF $sTxt &= @TAB & @TAB & @TAB & "ExitLoop" & @CRLF $sTxt &= @TAB & @TAB & "Case $btnRemoveTabs" & @CRLF $sTxt &= @TAB & @TAB & @TAB & "Local $sInput = GUICtrlRead($editBox)" & @CRLF $sTxt &= @TAB & @TAB & @TAB & "RemoveTabs($sInput)" & @CRLF $sTxt &= @TAB & @TAB & "EndSwitch" & @CRLF $sTxt &= @TAB & "WEnd" & @CRLF $sTxt &= "" & @CRLF $sTxt &= @TAB & "GUIDelete()" & @CRLF $sTxt &= "EndFunc" & @CRLF Example($sTxt) ;-------------------------------------------------------------------------------------------------------------------------------- Func Example($sTxt) $hGUI = GUICreate("Remove Tabs", 1000, 800) $editBox = _GUICtrlRichEdit_Create($hGUI, $sTxt, 10, 10, 980, 700) _GUICtrlRichEdit_SetFont($editBox, 12) GUICtrlSetData(-1, "") $btnRemoveTabs = GUICtrlCreateButton("Remove Tabs & CRLFs", 400, 730, 200, 30) GUICtrlSetState(-1, $GUI_FOCUS) GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $btnRemoveTabs Local $sInput = RemoveTabs(_GUICtrlRichEdit_GetText($editBox)) EndSwitch ConformEdit() Sleep(50) WEnd GUIDelete() EndFunc ;==>Example ;-------------------------------------------------------------------------------------------------------------------------------- Func RemoveTabs($sInput) Local $sOutput = $sInput $sOutput = StringReplace($sOutput, "→", "") $sOutput = StringReplace($sOutput, "↭", "") ClipPut($sOutput) SplashTextOn("Done", "Tabs removed and saved to clipboard!", 300, 50, -1, -1, 3) Sleep(1000) SplashOff() Return $sOutput EndFunc ;==>RemoveTabs ;-------------------------------------------------------------------------------------------------------------------------------- Func ConformEdit() Local Static $sCurTxt Local $sNewTxt, $aiSel $aiSel = _GUICtrlRichEdit_GetSel($editBox) $sNewTxt = _GUICtrlRichEdit_GetText($editBox) If $sCurTxt = $sNewTxt Then Return ConsoleWrite("..." & @CRLF) $sNewTxt = StringReplace($sNewTxt, @TAB, "→") $sNewTxt = StringReplace($sNewTxt, " ", "↭") ; ↭ ⇝ ↝ $sCurTxt = $sNewTxt _GUICtrlRichEdit_SetText($editBox, $sCurTxt) _GUICtrlRichEdit_SetSel($editBox, $aiSel[0], $aiSel[1]) EndFunc ;==>ConformEdit ;--------------------------------------------------------------------------------------------------------------------------------1 point
-
Hi all, here is release 1.7 of the UDF The main focus of this release was to bring in some MSC support, which is mainly used with lighting consoles. MSC can also (potentially) control things like fireworks, and flys/trusses etc. So in the unlikely event anyone is trying to do that, dont! (i.e. read the disclaimer in the helpfile) Changelog: - Added some Midi Show Control (MSC) support - Fixed issue where _midi_CloseOutput failed to close non-stream handles. - Fixed example scripts for sequence functions. - Updated reference list links in the helpfile. The midi.org site has been updated, which broke hyperlinks. - Modified internals for _midi_PackSize1 point