Skip to content

Instantly share code, notes, and snippets.

@hesido
Last active February 14, 2020 21:12
Show Gist options
  • Save hesido/867185a25c962fc48e8709b64f312900 to your computer and use it in GitHub Desktop.
Save hesido/867185a25c962fc48e8709b64f312900 to your computer and use it in GitHub Desktop.
TradeOgre ticker script for Google Spreadsheets
/**
* Returns Ticker Price From TradeOgre exchange
* Function accepts ranges as well, please use it to reduce server lookups, so it fetches data only once.
*
* @param {string} ticker name
* @param {string} ticker type
* @return {number} ticker price
* @customfunction
**/
function ReturnTickerPrice(TickerName, TickerType) {
TickerType = TickerType || "bid";
var marketdata = JSON.parse(UrlFetchApp.fetch("https://tradeogre.com/api/v1/markets").getContentText());
if (TickerName.map) return TickerName.map(function(tname) {return TickerPriceForPair(tname);});
else return TickerPriceForPair(TickerName);
function TickerPriceForPair(tickername, tickertype) {
tickertype = tickertype || TickerType;
if(Array.isArray(tickertype) && Array.isArray(tickertype[0])) {
return tickertype[0].map(function(tType) {return TickerPriceForPair(tickername, tType);});
}
var tickerfilter = marketdata.filter(function(tkobj) {return tkobj[tickername];});
var tickerobject = (tickerfilter[0] && tickerfilter[0][tickername]) || null;
return (tickerobject && tickerobject[tickertype]) || (tickerobject && tickertype) || "Ticker not found!";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment