Skip to content

Instantly share code, notes, and snippets.

@learntheropes
Last active June 2, 2019 01:18
Show Gist options
  • Select an option

  • Save learntheropes/ee51e015ed5149995c4e9b8ce7394432 to your computer and use it in GitHub Desktop.

Select an option

Save learntheropes/ee51e015ed5149995c4e9b8ce7394432 to your computer and use it in GitHub Desktop.
Poloniex private API for Google Apps Script (Google Sheet)
// 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)
}
@BernardMyburgh
Copy link
Copy Markdown

Hey, thanks for this! Noticed that "returnCompleteBalances" doesn't work, since you need a second parameter, e.g. "onOrders". Right now you can check your balance, but will see 0 if your balance is on orders. So here is the fix:
Setup the function with an additional subparameter:
function poloniex(command,parameter,subparam) {

Then change the return function at the end to consider this new parameter:
if (parameter === undefined) { Logger.log(JSON.stringify(dataAll)) return JSON.stringify(dataAll) } else if(parameter != undefined && subparam === undefined) { return dataAll[parameter] } else if (parameter != undefined && subparam != undefined) { return dataAll[parameter][subparam] }

Then use it like so:
=poloniex ("returnCompleteBalances","ETH","onOrders")

Hope that helps someone.

@rigelrozanski
Copy link
Copy Markdown

rigelrozanski commented Jul 10, 2017

poloniex was saying that the nonce was too small, changed the nonce line to the following and now it works

var nonce = 1495932972127042 + new Date().getTime();

@cwcgunbot-com
Copy link
Copy Markdown

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!

@learntheropes
Copy link
Copy Markdown
Author

learntheropes commented Jul 17, 2017

@duongngocmanh most of the functions are still missing. This is basically the draft of the authentication function.

@learntheropes
Copy link
Copy Markdown
Author

learntheropes commented Jul 17, 2017

Both comments changes are in the code, the old version is commented.

@cwcgunbot-com
Copy link
Copy Markdown

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.

@learntheropes
Copy link
Copy Markdown
Author

@duongngocmanh see the updated code with buy and sell. I would not use it as is. You have a telegram.

@ilhan09
Copy link
Copy Markdown

ilhan09 commented Aug 10, 2017

I could not run it. can you help me ?
ReferenceError: "command" is not defined. (Line: 15, file: "poloniex_private_api")

@learntheropes
Copy link
Copy Markdown
Author

learntheropes commented Aug 11, 2017

@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)

@ilhan09
Copy link
Copy Markdown

ilhan09 commented Aug 12, 2017

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"

@learntheropes
Copy link
Copy Markdown
Author

@ilhan09 are you sure the name of the sheet is "keys"?

@ilhan09
Copy link
Copy Markdown

ilhan09 commented Aug 14, 2017

sorry . 👍

It does not give an error.
But nothing is happening.

@learntheropes
Copy link
Copy Markdown
Author

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.

@citadella
Copy link
Copy Markdown

citadella commented Aug 25, 2017

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?

@cinderblue
Copy link
Copy Markdown

I have the same issue as citadella

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment