-
-
Save learntheropes/ee51e015ed5149995c4e9b8ce7394432 to your computer and use it in GitHub Desktop.
| // work in progress | |
| // you need a poloniex API key and secret with trading option enabled | |
| // you can test it with: | |
| // = polo("returnBalances","BTC") | |
| // or | |
| // = polo("returnBalances","all") | |
| // or buy and sell: | |
| // polo("BUY","BTC_LTC", 0.0251, 1) or polo("SELL","BTC_LTC", 0.0251, 1) | |
| function polo(command,currencyPair,rate,amount){ | |
| // fillOrKill, immediateOrCancel, postOnly set to true are missing | |
| poloniex_auth_(command,[{key: "currencyPair",value: currencyPair},{key: "rate",value: rate},{key: "amount",value: amount}], function(result){ | |
| Logger.log(result) | |
| return JSON.stringify(result) | |
| }) | |
| } | |
| function poloniex_auth_(command,params,callback) { | |
| // I assume that all the keys are in the "keys" spreadsheet. The key is in cell B4 and the secret in cell C4. | |
| var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("keys"); | |
| var key = sheet.getRange("B4").getValue(); | |
| var secret = sheet.getRange("C4").getValue(); | |
| var nonce = 1495932972127042 + new Date().getTime(); | |
| var payload = { | |
| "nonce": nonce, | |
| "command": command | |
| } | |
| params.forEach( function(param){ | |
| payload[param.key] = param.value; | |
| }) | |
| var payloadEncoded = Object.keys(payload).map(function(param) { | |
| return encodeURIComponent(param) + '=' + encodeURIComponent(payload[param]); | |
| }).join('&'); | |
| var uri = "https://poloniex.com/tradingApi"; | |
| var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, payloadEncoded, secret); | |
| var stringSignature = signature.map(function(byte) { | |
| return ('0' + (byte & 0xFF).toString(16)).slice(-2); | |
| }).join('') | |
| var headers = { | |
| "key": key, | |
| "sign": stringSignature | |
| } | |
| var params = { | |
| "method": "post", | |
| "headers": headers, | |
| "payload": payloadEncoded | |
| } | |
| var response = UrlFetchApp.fetch(uri, params); | |
| var dataAll = JSON.parse(response.getContentText()); | |
| callback(dataAll) | |
| } |
I could not run it. can you help me ?
ReferenceError: "command" is not defined. (Line: 15, file: "poloniex_private_api")
@ilhan09 in the script, at line 12, replace:
function polo(action,currencyPair,rate,amount)
with
function polo(command,currencyPair,rate,amount)
That should fix the issue (but I can't test it now, sorry)
I can not. Another mistake this time.
Eror line ----var key = sheet.getRange("B4").getValue();
my b4=Q0XYRARL-xxxxxxxx-xxxxxxxx-RRHXNSN4
TypeError: The "getRange" method of the null element can not be called. (Line: 28, file: "poloniex_private_api"
@ilhan09 are you sure the name of the sheet is "keys"?
sorry . 👍
It does not give an error.
But nothing is happening.
I'm sorry but I'm not going to store a key with trading option enabled on google just to test it.
returnBalances is working for me.
So I've gotten the trading bit to work just fine, but I can't seem to get a simple query on my balances to work. I'm trying:
polo("returnBalances","BTC")
But I get the following error message:
Request failed for https://poloniex.com/tradingApi returned code 422. Truncated server response: {"error":"Invalid currencyPair parameter."} (use muteHttpExceptions option to examine full response) (line 66).
Thoughts?
I have the same issue as citadella
@duongngocmanh see the updated code with buy and sell. I would not use it as is. You have a telegram.