manhdn (OP)
Newbie
Offline
Activity: 62
Merit: 0
|
|
July 12, 2017, 04:17:05 AM Last edit: July 12, 2017, 04:32:59 AM by manhdn |
|
Hi, I am creating a trading tool with google spreadsheets and google apps scripts (very similar to Javascript). Everything seems to be ok (check balances, get addresses,...) but the buy/sell command. When I place an order by this command: = poloniex("buy","USDT_ZEC",100,0.01) and the api response: {"error":"Total must be at least 0.0001."}. I tried to change the param to other values but always get the same results (even when I change the pair name to a wrong name like BTC_XXX, it answered the same error). PLease help me to address this. Thanks in advance!
|
|
|
|
manhdn (OP)
Newbie
Offline
Activity: 62
Merit: 0
|
|
July 12, 2017, 09:32:36 AM |
|
Here is my code: // work in progress // you need a poloniex API key and secret with trading option enabled // you can test it with: // = poloniex ("returnBalances","BTC") // or // = poloniex ("returnBalances") function poloniex(command,parameter,subparam) { // I assume that all the keys are in the "keys" spreadsheet. The key is in cell A1 and the secret in cell A2 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("keys"); var key = sheet.getRange("A1").getValue(); var secret = sheet.getRange("A2").getValue(); var nonce = 1495932972127042 + new Date().getTime(); var payload = { "nonce": nonce, "command": command } 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 = ""; for (i = 0; i < signature.length; i++) { var byte = signature ; if (byte < 0) byte += 256; var byteStr = byte.toString(16); if (byteStr.length == 1) byteStr = '0'+byteStr; stringSignature += byteStr; } 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()); //Edited 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] } //end edited }
|
|
|
|
yg10
Newbie
Offline
Activity: 88
Merit: 0
|
|
July 14, 2017, 01:56:37 AM |
|
Can you print the value of payloadEncoded variable?
I do not see that you pass field names "currencyPair", "rate", and "amount" to your function.
|
|
|
|
HeRetiK
Legendary
Offline
Activity: 3080
Merit: 2166
Playgram - The Telegram Casino
|
|
July 14, 2017, 01:54:33 PM |
|
1) Your function call poloniex("buy","USDT_ZEC",100,0.01) includes 4 arguments, your function poloniex(command,parameter,subparam) only accepts 3.
2) Your function poloniex doesn't actually do anything with the arguments you receive. You're only sending the command parameter (and the nonce), no currency pair, rate or total. You need to add those to the payload object.
|
|
|
|
▄▄███████▄▄███████ ▄███████████████▄▄▄▄▄ ▄████████████████████▀░ ▄█████████████████████▄░ ▄█████████▀▀████████████▄ ██████████████▀▀█████████ █████████████████████████ ██████████████▄▄█████████ ▀█████████▄▄████████████▀ ▀█████████████████████▀░ ▀████████████████████▄░ ▀███████████████▀▀▀▀▀ ▀▀███████▀▀███████ | ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ Playgram.io ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ | ▄▄▄░░ ▀▄ █ █ █ █ █ █ █ ▄▀ ▀▀▀░░
| │ | ▄▄▄███████▄▄▄ ▄▄███████████████▄▄ ▄███████████████████▄ ▄██████████████▀▀█████▄ ▄██████████▀▀███▄██▐████▄ ██████▀▀████▄▄▀▀█████████ ████▄▄███▄██▀█████▐██████ ██████████▀██████████████ ▀███████▌▐██▄████▐██████▀ ▀███████▄▄███▄████████▀ ▀███████████████████▀ ▀▀███████████████▀▀ ▀▀▀███████▀▀▀ | | │ | ██████▄▄███████▄▄████████ ███▄███████████████▄░░▀█▀ ███████████░█████████░░█ ░█████▀██▄▄░▄▄██▀█████░█ █████▄░▄███▄███▄░▄██████ ████████████████████████ ████████████████████████ ██░▄▄▄░██░▄▄▄░██░▄▄▄░███ ██░░░█░██░░░█░██░░░█░████ ██░░█░░██░░█░░██░░█░░████ ██▄▄▄▄▄██▄▄▄▄▄██▄▄▄▄▄████ ███████████████████████ ███████████████████████ | | │ | ► | |
|
|
|
AlexOko
Newbie
Offline
Activity: 9
Merit: 0
|
|
July 14, 2017, 06:09:19 PM |
|
Very good answer. I have polonex.
|
|
|
|
manhdn (OP)
Newbie
Offline
Activity: 62
Merit: 0
|
|
July 16, 2017, 01:24:57 AM |
|
Thank you all for your helps!
|
|
|
|
manhdn (OP)
Newbie
Offline
Activity: 62
Merit: 0
|
|
July 16, 2017, 01:38:55 AM |
|
1) Your function call poloniex("buy","USDT_ZEC",100,0.01) includes 4 arguments, your function poloniex(command,parameter,subparam) only accepts 3.
2) Your function poloniex doesn't actually do anything with the arguments you receive. You're only sending the command parameter (and the nonce), no currency pair, rate or total. You need to add those to the payload object.
I tried to add those to the payload, but it even cannot run. Can you help me to get this run? I will be very happy to send you money just to say thank. (If you offer a service to do that, pls give me the price ).
|
|
|
|
HeRetiK
Legendary
Offline
Activity: 3080
Merit: 2166
Playgram - The Telegram Casino
|
|
July 16, 2017, 08:36:23 AM |
|
1) Your function call poloniex("buy","USDT_ZEC",100,0.01) includes 4 arguments, your function poloniex(command,parameter,subparam) only accepts 3.
2) Your function poloniex doesn't actually do anything with the arguments you receive. You're only sending the command parameter (and the nonce), no currency pair, rate or total. You need to add those to the payload object.
I tried to add those to the payload, but it even cannot run. Can you help me to get this run? I will be very happy to send you money just to say thank. (If you offer a service to do that, pls give me the price ). Try the following (code changes in green): // work in progress // you need a poloniex API key and secret with trading option enabled // you can test it with: // = poloniex ("returnBalances","BTC") // or // = poloniex ("returnBalances") function poloniex(command,options) { // I assume that all the keys are in the "keys" spreadsheet. The key is in cell A1 and the secret in cell A2 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("keys"); var key = sheet.getRange("A1").getValue(); var secret = sheet.getRange("A2").getValue(); var nonce = 1495932972127042 + new Date().getTime(); var payload = options || {}; payload.nonce = nonce; payload.command = command; 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 = ""; for (i = 0; i < signature.length; i++) { var byte = signature; if (byte < 0) byte += 256; var byteStr = byte.toString(16); if (byteStr.length == 1) byteStr = '0'+byteStr; stringSignature += byteStr; } 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()); //Edited return JSON.stringify(dataAll) } //end edited} Note that you now have to call buy, sell, differently. eg. you should now be able to call buy like this: poloniex("buy", { currencyPair: "USDT_ZEC", amount: 100, rate: 0.01 }); The parameter names within the object {} correspond directly to the expected poloniex API POST parameters. I haven't actually run it, but send me a PM if it works and you feel like tipping me.
|
|
|
|
▄▄███████▄▄███████ ▄███████████████▄▄▄▄▄ ▄████████████████████▀░ ▄█████████████████████▄░ ▄█████████▀▀████████████▄ ██████████████▀▀█████████ █████████████████████████ ██████████████▄▄█████████ ▀█████████▄▄████████████▀ ▀█████████████████████▀░ ▀████████████████████▄░ ▀███████████████▀▀▀▀▀ ▀▀███████▀▀███████ | ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ Playgram.io ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ | ▄▄▄░░ ▀▄ █ █ █ █ █ █ █ ▄▀ ▀▀▀░░
| │ | ▄▄▄███████▄▄▄ ▄▄███████████████▄▄ ▄███████████████████▄ ▄██████████████▀▀█████▄ ▄██████████▀▀███▄██▐████▄ ██████▀▀████▄▄▀▀█████████ ████▄▄███▄██▀█████▐██████ ██████████▀██████████████ ▀███████▌▐██▄████▐██████▀ ▀███████▄▄███▄████████▀ ▀███████████████████▀ ▀▀███████████████▀▀ ▀▀▀███████▀▀▀ | | │ | ██████▄▄███████▄▄████████ ███▄███████████████▄░░▀█▀ ███████████░█████████░░█ ░█████▀██▄▄░▄▄██▀█████░█ █████▄░▄███▄███▄░▄██████ ████████████████████████ ████████████████████████ ██░▄▄▄░██░▄▄▄░██░▄▄▄░███ ██░░░█░██░░░█░██░░░█░████ ██░░█░░██░░█░░██░░█░░████ ██▄▄▄▄▄██▄▄▄▄▄██▄▄▄▄▄████ ███████████████████████ ███████████████████████ | | │ | ► | |
|
|
|
manhdn (OP)
Newbie
Offline
Activity: 62
Merit: 0
|
|
July 16, 2017, 03:33:16 PM Last edit: July 16, 2017, 11:24:28 PM by manhdn |
|
1) Your function call poloniex("buy","USDT_ZEC",100,0.01) includes 4 arguments, your function poloniex(command,parameter,subparam) only accepts 3.
2) Your function poloniex doesn't actually do anything with the arguments you receive. You're only sending the command parameter (and the nonce), no currency pair, rate or total. You need to add those to the payload object.
I tried to add those to the payload, but it even cannot run. Can you help me to get this run? I will be very happy to send you money just to say thank. (If you offer a service to do that, pls give me the price ). Try the following (code changes in green): // work in progress // you need a poloniex API key and secret with trading option enabled // you can test it with: // = poloniex ("returnBalances","BTC") // or // = poloniex ("returnBalances") function poloniex(command,options) { // I assume that all the keys are in the "keys" spreadsheet. The key is in cell A1 and the secret in cell A2 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("keys"); var key = sheet.getRange("A1").getValue(); var secret = sheet.getRange("A2").getValue(); var nonce = 1495932972127042 + new Date().getTime(); var payload = options || {}; payload.nonce = nonce; payload.command = command; 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 = ""; for (i = 0; i < signature.length; i++) { var byte = signature; if (byte < 0) byte += 256; var byteStr = byte.toString(16); if (byteStr.length == 1) byteStr = '0'+byteStr; stringSignature += byteStr; } 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()); //Edited return JSON.stringify(dataAll) } //end edited} Note that you now have to call buy, sell, differently. eg. you should now be able to call buy like this: poloniex("buy", { currencyPair: "USDT_ZEC", amount: 100, rate: 0.01 }); The parameter names within the object {} correspond directly to the expected poloniex API POST parameters. I haven't actually run it, but send me a PM if it works and you feel like tipping me. When I run your code, it said: Limit Exceeded: URLFetch header size. (line 57, file "Code") (Line 57: var response = UrlFetchApp.fetch(uri, params); ) Can you give me your email, I will share the sample spreadsheet with script embled so you can easily help me to fix it? **I also want to process buy/sell command by placing a function in a cell inside the Spreadsheet (not a function in the script). Can I do that? Honestly, I dont master in this so certainly I will really appreciate your help with an amount of btc. Thanks
|
|
|
|
yg10
Newbie
Offline
Activity: 88
Merit: 0
|
|
July 17, 2017, 09:08:04 AM |
|
Can you please provide the value of variable payloadEncoded. Also it will be helpful to put the following var req = UrlFetchApp.getRequest(url, params) before calling UrlFetchApp.fetch(uri, params); and to provide the value of variable req
|
|
|
|
manhdn (OP)
Newbie
Offline
Activity: 62
Merit: 0
|
|
July 17, 2017, 01:53:47 PM |
|
Can you please provide the value of variable payloadEncoded. Also it will be helpful to put the following var req = UrlFetchApp.getRequest(url, params) before calling UrlFetchApp.fetch(uri, params); and to provide the value of variable req I dont know much about google script so I made some changes but it does not work. Can you edit the code directly and make some experiments of the spreadsheet? I can send you the spread sheet with the code embled so you can try.
|
|
|
|
yg10
Newbie
Offline
Activity: 88
Merit: 0
|
|
July 20, 2017, 07:04:51 AM |
|
I could not answer for last few days. It looks like in the latest version URL encoding happens twice. Why don't you try the following function poloniex(command,options) {
// I assume that all the keys are in the "keys" spreadsheet. The key is in cell A1 and the secret in cell A2
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("keys");
var key = sheet.getRange("A1").getValue(); var secret = sheet.getRange("A2").getValue(); var nonce = 1495932972127042 + new Date().getTime();
var payload = options || {}; payload.nonce = nonce; payload.command = command;
var uri = "https://poloniex.com/tradingApi";
var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, payloadEncoded, secret);
var stringSignature = ""; for (i = 0; i < signature.length; i++) { var byte = signature; if (byte < 0) byte += 256; var byteStr = byte.toString(16); if (byteStr.length == 1) byteStr = '0'+byteStr; stringSignature += byteStr; }
var headers = { "key": key, "sign": stringSignature }
var params = { "method": "post", "headers": headers, "payload": payload }
// To see the request that is send to Poloniex uncomment following lines. // var req = getRequest(url, params) // for(i in req) { // Logger.log(i + ": " + req[i]); // } var response = UrlFetchApp.fetch(uri, params);
var dataAll = JSON.parse(response.getContentText()); process.stdout.write(params)
return JSON.stringify(dataAll)
}
poloniex("buy", { currencyPair: "USDT_ZEC", amount: 100, rate: 0.01 });
|
|
|
|
|