Last active
March 3, 2021 08:57
-
-
Save learntheropes/abb5dd9fab96c7c2990a23ef12138778 to your computer and use it in GitHub Desktop.
Bittrex private and public API for Google Apps Script (Google Sheet)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// work in progress | |
// you need a bittrex API key and secret with read account option enabled | |
// I assume that all the keys are in the "keys" spreadsheet. The key is in cell B5 and the secret in cell C5 | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("keys"); | |
var key = sheet.getRange("B5").getValue() | |
var secret = sheet.getRange("C5").getValue(); | |
var baseUrl = 'https://bittrex.com/api/v1.1/'; | |
var nonce = Math.floor(new Date().getTime()/1000); | |
/** | |
* Used to retrieve all balances from your account. | |
* @return the balance of any currency that is not 0. | |
*/ | |
function bittrexGetbalances() { | |
var finals = []; | |
var results = bittrexPrivateRequest("account/getbalances"); | |
Logger.log(results) | |
results.forEach(function(result,index) { | |
finals.push({'currency': result.Currency, 'balance': result.Balance}) | |
}); | |
Logger.log(finals) | |
return json2array_(finals); | |
}; | |
/** | |
* Used to retrieve the balance from your account for a specific currency. | |
* @param {string} a string literal for the currency (ex: LTC). | |
* @return the currency balance. | |
* @customfunction | |
*/ | |
function bittrexGetbalance(currency) { | |
var payload = { "currency" : currency }; | |
var results = bittrexPrivateRequest("account/getbalance",payload); | |
return results.Balance; | |
}; | |
function bittrexPublicRequest(command,payload){ | |
var uri = uriCreator_(command,payload); | |
var response = UrlFetchApp.fetch(uri); | |
var dataAll = JSON.parse(response.getContentText()); | |
if (dataAll.success = true) { | |
return dataAll.result | |
} else { | |
return dataAll.message | |
} | |
} | |
function bittrexPrivateRequest(command,payload){ | |
var uri = uriCreator_(command,payload,true) | |
var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, uri, secret); | |
// Signature copied from comments: | |
var apisign = signature.map(function(byte) { | |
return ('0' + (byte & 0xFF).toString(16)).slice(-2); | |
}).join(''); | |
var headers = { | |
"apisign": apisign | |
} | |
var params = { | |
"method": "get", | |
"headers": headers, | |
"payload": payload | |
} | |
var response = UrlFetchApp.fetch(uri, params); | |
var dataAll = JSON.parse(response.getContentText()); | |
if (dataAll.success = true) { | |
return dataAll.result | |
} else { | |
return dataAll.message | |
} | |
}; | |
function uriCreator_(command,payload,private){ | |
if (payload) { | |
var payloadEncoded = Object.keys(payload).map(function(param) { | |
return encodeURIComponent(param) + '=' + encodeURIComponent(payload[param]); | |
}).join('&'); | |
} | |
if (private) { | |
uri = baseUrl.concat(command + "?apikey=" + key + "&nonce=" + nonce + "&" + payloadEncoded) | |
} else { | |
uri = baseUrl.concat("public/" + command + "?" + payloadEncoded) | |
} | |
return uri | |
}; | |
function json2array_(data){ | |
var results = []; | |
var keys = []; | |
var values = []; | |
for (var i in data){ | |
for (var key in data[i]){ | |
if (i == 0) keys.push(key); | |
values.push(data[i][key]); | |
} | |
if (i == 0){ | |
results.push(keys); | |
keys = []; | |
} | |
results.push(values); | |
values = []; | |
} | |
return results; | |
}; |
ok so looks like
=bittrexGetbalances()
and
=bittrexGetOrderHistoryV1()
is all that working for this script am I correct. Sorry I am learning
@leejie8008 looks like it pulls everything you have a wallet on BTW
Thanks. Pretty helpful
None of you unfortunately answered, how the sheet itself can be generated? I added the keys to key sheet, but don't get any results being displayed. Please help!
Hey could somebody clarify how I would send a buy order with this code?
Ive tried:
bittrexPrivateRequest("Buy",)
but I do not know what to insert in the field "Payload".
I am aware that i have to insert the currency, the amount and the rate; but do not know in which format
Many thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You shouldn't you just in the cell place =bittrexGetbalances and it should go out and download all your balances? Is there more to it. Please let me understand when i do this I get
Error
Unknown range name: 'BITTREXGETBALANCES'