Bitcoin Forum
December 17, 2017, 10:42:31 AM *
News: Latest stable version of Bitcoin Core: 0.15.1  [Torrent].
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: Need help with Poloniex API (buy/sell command errors)  (Read 1017 times)
manhdn
Jr. Member
*
Offline Offline

Activity: 30


View Profile
July 12, 2017, 04:17:05 AM
 #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!
1513507351
Hero Member
*
Offline Offline

Posts: 1513507351

View Profile Personal Message (Offline)

Ignore
1513507351
Reply with quote  #2

1513507351
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1513507351
Hero Member
*
Offline Offline

Posts: 1513507351

View Profile Personal Message (Offline)

Ignore
1513507351
Reply with quote  #2

1513507351
Report to moderator
1513507351
Hero Member
*
Offline Offline

Posts: 1513507351

View Profile Personal Message (Offline)

Ignore
1513507351
Reply with quote  #2

1513507351
Report to moderator
1513507351
Hero Member
*
Offline Offline

Posts: 1513507351

View Profile Personal Message (Offline)

Ignore
1513507351
Reply with quote  #2

1513507351
Report to moderator
manhdn
Jr. Member
*
Offline Offline

Activity: 30


View Profile
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
Jr. Member
*
Offline Offline

Activity: 34


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

Activity: 588


the forkings will continue until morale improves


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.

AlexOko
Newbie
*
Offline Offline

Activity: 9

Hi. It's AlexOko


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

Very good answer. I have polonex.

icos.icobox.io/?ref=8895a45b5ca47cc879cb9053
manhdn
Jr. Member
*
Offline Offline

Activity: 30


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

Thank you all for your helps!
manhdn
Jr. Member
*
Offline Offline

Activity: 30


View Profile
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
Hero Member
*****
Offline Offline

Activity: 588


the forkings will continue until morale improves


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.

manhdn
Jr. Member
*
Offline Offline

Activity: 30


View Profile
July 16, 2017, 03:33:16 PM
 #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
Jr. Member
*
Offline Offline

Activity: 34


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
Jr. Member
*
Offline Offline

Activity: 30


View Profile
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
Jr. Member
*
Offline Offline

Activity: 34


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:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!