-
-
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) | |
} |
@duongngocmanh most of the functions are still missing. This is basically the draft of the authentication function.
Both comments changes are in the code, the old version is commented.
Thank you Coinfused,
I need this code to run my google spreadsheet to make a buy/sell tool. Can you help me to get this run? I want it to work like this:
On the spreadsheet, I will enter a function such as =poloniex.buy("USDT_ETH,5,0.01) and the spreadsheet will place a buy order on poloniex. Honestly, I am not good at script, so I would love to pay you money for that. Thanks a lot. If you are interested, pls contact me at @manhdn on telegram.
@duongngocmanh see the updated code with buy and sell. I would not use it as is. You have a telegram.
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
Thank you so much for your code. It works fine with all command like check balances, get address...but the buy/sell command. I enter function: = poloniex ("buy","USDT_ZEC",300,0.01) to buy 0.01 ZEC with USDT but the API always return this error: {"error":"Total must be at least 0.0001."}.
I tried to change the currencies pair name to a wrong name (e.g. USDT_XXX) but it still returns the same error. Can you please take a look and help me to address this. Thanks in advance!