Last active
February 2, 2018 22:05
-
-
Save woganmay/b9f485b4e393ede41a8d8af2ebb0ab88 to your computer and use it in GitHub Desktop.
Gscript for Sheets - get crypto tickers and maintain a history
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
// Sample workbook here: | |
// https://docs.google.com/spreadsheets/d/1DPASIiKJw6x0n2eq7t4xRU_FQsizBH8Z8rXOfVk2Kb4/pubhtml | |
// Main function - does everything | |
// Triggered by Google every 15 minutes | |
function getAllTickers() { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Crypto"); | |
// Get all tickers | |
getLatestLunoTicker(sheet, "E2"); | |
getLatestCexTicker(sheet, "E3"); | |
getLatestIce3XTicker(sheet, "E4"); | |
// Update the "Last Updated" date | |
sheet.getRange("F7").setValue(new Date()); | |
incrementHistory(sheet); | |
} | |
// Grab the latest BTC/ZAR ticker from luno.com | |
function getLatestLunoTicker(sheet, range) { | |
var lunoTicker = UrlFetchApp.fetch("https://api.mybitx.com/api/1/ticker?pair=XBTZAR", { | |
"method" : "get" | |
}); | |
lunoTicker = JSON.parse(lunoTicker); | |
sheet.getRange(range).setValue(lunoTicker.bid); | |
} | |
// Grab the latest BTC/USD ticker from cex.io | |
function getLatestCexTicker(sheet, range) { | |
var cexTicker = UrlFetchApp.fetch("https://cex.io/api/ticker/BTC/USD", { | |
"method" : "get" | |
}); | |
cexTicker = JSON.parse(cexTicker); | |
sheet.getRange(range).setValue(cexTicker.bid); | |
} | |
// Grab the latest ETH/ZAR ticker from Ice3X.com | |
function getLatestIce3XTicker(sheet, range) { | |
var ice3xTicker = UrlFetchApp.fetch("https://ice3x.com/api/ajax/eth-zar", { | |
"method" : "get" | |
}); | |
ice3xTicker = JSON.parse(ice3xTicker); | |
sheet.getRange(range).setValue(ice3xTicker.orders.buy[0].price); | |
} | |
// Increment the History columns | |
function incrementHistory(sheet) { | |
// Find today's row | |
var history = sheet.getRange("H1:J100").getValues(); | |
var today = new Date(); | |
var todaysDate = Utilities.formatDate(today, "Africa/Johannesburg", "yyyy-MM-dd"); | |
var highestBlankRow = 0; | |
var targetRow = 0; | |
var i = 0; | |
for(row in history) | |
{ | |
i++; | |
// Skip the first 2 rows (headers) | |
if (i <= 2) continue; | |
if (highestBlankRow == 0 || targetRow == 0) | |
{ | |
if (history[row][0] != "") | |
{ | |
// Non-blank cell, compare the date | |
var thisDate = new Date(history[row][0]); | |
var thisDateStr = Utilities.formatDate(thisDate, "Africa/Johannesburg", "yyyy-MM-dd"); | |
if (thisDateStr == todaysDate) | |
{ | |
// This row contains today's date | |
targetRow = i; | |
} | |
} | |
// If it's a blank row, record it | |
if (history[row][0] == "" && highestBlankRow == 0) | |
{ | |
highestBlankRow = i; | |
} | |
} | |
} | |
// If we couldn't find today's date, use the highest | |
// blank row | |
if (targetRow == 0) targetRow = highestBlankRow; | |
var dateCell = "H".concat(targetRow); | |
var lowestCell = "I".concat(targetRow); | |
var highestCell = "J".concat(targetRow); | |
// The total value after having been calculated by the sheet | |
var totalValue = sheet.getRange("F6").getValue(); | |
// Populate the date if it's blank | |
if (sheet.getRange(dateCell).getValue() == "") sheet.getRange(dateCell).setValue(todaysDate); | |
// Populate the High and Low with the totalValue if they're blank | |
if (sheet.getRange(lowestCell).getValue() == "") sheet.getRange(lowestCell).setValue(totalValue); | |
if (sheet.getRange(highestCell).getValue() == "") sheet.getRange(highestCell).setValue(totalValue); | |
// Update Highest/Lowest fields | |
if (totalValue < sheet.getRange(lowestCell).getValue()) sheet.getRange(lowestCell).setValue(totalValue); | |
if (totalValue > sheet.getRange(highestCell).getValue()) sheet.getRange(highestCell).setValue(totalValue); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment