Heya,
Thought i'd share an API snippet to see if there is interest in collaboration or just help out someone who's trying to make a trading bot.
For the script to work you'd need to create an account on Phemex aswell as an API key, the script then connects to display your trading wallet balance.
#include <Date.au3>
#include <Array.au3>
#include <File.au3>
;Enter your API Key and API secret
Global $APIkey = "Enter your API key here"
Global $ApiSecret = "Enter your API secret here"
;General variables
Global $URI = "https://api.phemex.com"
Global $Logfile = "log.txt"
;Object variables
Global $oMyRet[2]
Global $oMyError = ObjEvent("AutoIt.Error","MyErrFunc")
$WalletBalance = GetWalletBalance($APIkey, $ApiSecret)
If not @error then MsgBox(0, "Balance", "Your wallet holds "&$WalletBalance/100000000&" BTC")
Func GetWalletBalance($APIkey, $ApiSecret)
$Method = "GET"
$Action = "/accounts/accountPositions"
$Parameters = "currency=BTC"
$ConnectionRetry = 0
While 1
$Expires = _TimeGetStamp()+60
$Signature = _HashHMAC("SHA256", $Action&$Parameters&$Expires, $ApiSecret)
$objHTTP = ObjCreate("MSXML2.ServerXMLHTTP")
$objReturn = ObjCreate("Msxml2.DOMdocument.3.0")
$objHTTP.open ($Method, $URI & $Action &"?"& $Parameters, False)
$objHTTP.setRequestHeader ("Content-Type", "application/json")
$objHTTP.setRequestHeader ("x-phemex-access-token", $APIkey)
$objHTTP.setRequestHeader ("x-phemex-request-expiry", $Expires)
$objHTTP.setRequestHeader ("x-phemex-request-signature", $Signature)
$objHTTP.send ()
If @error then
If $oMyRet[0] = 80020009 then
$ConnectionRetry = $ConnectionRetry+1
If $ConnectionRetry = 60 then
_FileWriteLog($Logfile, 'Error - GetWalletBalance - COM Error: '&$oMyRet[1])
Exit
EndIf
Sleep (1000)
ContinueLoop
EndIf
EndIf
If $ConnectionRetry > 1 then _FileWriteLog($Logfile, "Warning - Connection to API was lost for "&$ConnectionRetry&" seconds but ok now")
$strStat = $objHTTP.status
If $strStat = 200 then
$strReturn = $objHTTP.responseText
If NOT StringInStr($strReturn, '"code":0') then
_FileWriteLog($Logfile, 'Error - GetWalletBalance - API Status '&$strStat&@CR&$strReturn)
SetError (2)
Return 0
EndIf
$ReturnArray = StringSplit($strReturn, ":")
$WalletbalanceSearch = _ArraySearch($ReturnArray, "accountBalanceEv", 0, 0, 0, 1)
$WalletBalanceArray = StringSplit($ReturnArray[$WalletbalanceSearch+1],",")
$WalletBalance = $WalletBalanceArray[1]
Return $WalletBalance
ElseIf $strStat = 500 OR $strStat = 502 OR $strStat = 503 OR $strStat = 401 Then
_FileWriteLog($Logfile, "Warning - Call returned status "&$strStat&", retrying call", 1)
sleep (1000)
ContinueLoop
Else
$strReturn = $objHTTP.responseText
$ReturnArray = StringSplit($strReturn, ":")
_FileWriteLog($Logfile, 'Error - GetWalletBalance - API Status "'&$strStat&@CR&$strReturn)
Exit
EndIf
WEnd
EndFunc
Func _TimeGetStamp()
Local $av_Time
$av_Time = DllCall('CrtDll.dll', 'long:cdecl', 'time', 'ptr', 0)
If @error Then
SetError(99)
Return False
EndIf
Return $av_Time[0]
EndFunc
Func _HashHMAC($sAlgorithm, $bData, $bKey, $bRaw_Output = False)
Local $oHashHMACErrorHandler = ObjEvent("AutoIt.Error", "_HashHMACErrorHandler")
Local $oHMAC = ObjCreate("System.Security.Cryptography.HMAC" & $sAlgorithm)
If @error Then SetError(1, 0, "")
$oHMAC.key = Binary($bKey)
Local $bHash = $oHMAC.ComputeHash_2(Binary($bData))
Return SetError(0, 0, $bRaw_Output ? $bHash : StringLower(StringMid($bHash, 3)))
EndFunc
Func _HashHMACErrorHandler($oError)
;Dummy Error Handler
EndFunc
Func MyErrFunc()
$HexNumber = Hex($oMyError.number, 8)
$oMyRet[0] = $HexNumber
$oMyRet[1] = StringStripWS($oMyError.description, 3)
_FileWriteLog($Logfile, "### COM Error ! Number: " & $HexNumber & " ScriptLine: " & $oMyError.scriptline & " Description: " & $oMyRet[1] & @LF )
SetError(1)
Return
EndFunc