Bitcoin Forum
April 25, 2024, 06:50:23 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Need help with Poloniex API (buy/sell command errors)  (Read 1077 times)
manhdn (OP)
Newbie
*
Offline Offline

Activity: 62
Merit: 0


View Profile WWW
July 12, 2017, 04:17:05 AM
Last edit: July 12, 2017, 04:32:59 AM by manhdn
 #1

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!
1714071023
Hero Member
*
Offline Offline

Posts: 1714071023

View Profile Personal Message (Offline)

Ignore
1714071023
Reply with quote  #2

1714071023
Report to moderator
You get merit points when someone likes your post enough to give you some. And for every 2 merit points you receive, you can send 1 merit point to someone else!
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714071023
Hero Member
*
Offline Offline

Posts: 1714071023

View Profile Personal Message (Offline)

Ignore
1714071023
Reply with quote  #2

1714071023
Report to moderator
manhdn (OP)
Newbie
*
Offline Offline

Activity: 62
Merit: 0


View Profile WWW
July 12, 2017, 09:32:36 AM
 #2

Here is my code:

Quote
// 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 Offline

Activity: 88
Merit: 0


View Profile
July 14, 2017, 01:56:37 AM
 #3

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 Offline

Activity: 2912
Merit: 2080


Cashback 15%


View Profile
July 14, 2017, 01:54:33 PM
 #4

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.

.
.HUGE.
▄██████████▄▄
▄█████████████████▄
▄█████████████████████▄
▄███████████████████████▄
▄█████████████████████████▄
███████▌██▌▐██▐██▐████▄███
████▐██▐████▌██▌██▌██▌██
█████▀███▀███▀▐██▐██▐█████

▀█████████████████████████▀

▀███████████████████████▀

▀█████████████████████▀

▀█████████████████▀

▀██████████▀▀
█▀▀▀▀











█▄▄▄▄
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
.
CASINSPORTSBOOK
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀▀█











▄▄▄▄█
AlexOko
Newbie
*
Offline Offline

Activity: 9
Merit: 0


View Profile
July 14, 2017, 06:09:19 PM
 #5

Very good answer. I have polonex.
manhdn (OP)
Newbie
*
Offline Offline

Activity: 62
Merit: 0


View Profile WWW
July 16, 2017, 01:24:57 AM
 #6

Thank you all for your helps!
manhdn (OP)
Newbie
*
Offline Offline

Activity: 62
Merit: 0


View Profile WWW
July 16, 2017, 01:38:55 AM
 #7

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 Smiley ).
HeRetiK
Legendary
*
Offline Offline

Activity: 2912
Merit: 2080


Cashback 15%


View Profile
July 16, 2017, 08:36:23 AM
 #8

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 Smiley ).

Try the following (code changes in green):

Quote
// 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:

Code:
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.

.
.HUGE.
▄██████████▄▄
▄█████████████████▄
▄█████████████████████▄
▄███████████████████████▄
▄█████████████████████████▄
███████▌██▌▐██▐██▐████▄███
████▐██▐████▌██▌██▌██▌██
█████▀███▀███▀▐██▐██▐█████

▀█████████████████████████▀

▀███████████████████████▀

▀█████████████████████▀

▀█████████████████▀

▀██████████▀▀
█▀▀▀▀











█▄▄▄▄
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
.
CASINSPORTSBOOK
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀▀█











▄▄▄▄█
manhdn (OP)
Newbie
*
Offline Offline

Activity: 62
Merit: 0


View Profile WWW
July 16, 2017, 03:33:16 PM
Last edit: July 16, 2017, 11:24:28 PM by manhdn
 #9

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 Smiley ).

Try the following (code changes in green):

Quote
// 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:

Code:
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 Offline

Activity: 88
Merit: 0


View Profile
July 17, 2017, 09:08:04 AM
 #10

Can you please provide the value of variable payloadEncoded.

Also it will be helpful to put the following

Code:
var req = UrlFetchApp.getRequest(url, params) 
before calling  UrlFetchApp.fetch(uri, params); and to provide the value of variable req

manhdn (OP)
Newbie
*
Offline Offline

Activity: 62
Merit: 0


View Profile WWW
July 17, 2017, 01:53:47 PM
 #11

Can you please provide the value of variable payloadEncoded.

Also it will be helpful to put the following

Code:
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 Offline

Activity: 88
Merit: 0


View Profile
July 20, 2017, 07:04:51 AM
 #12

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
Code:
 
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
});

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!