Can't recall who I got the function from, originally for GLBSE data. I've heavily modified it to fetch:
Blockchain.info | Bitcoin Address Balance |
MtGox | Weighted average, any currency |
BTCT.co | Ticker data, balance and portfolio data |
BitFunder | Ticker data, balance and portfolio data |
MPEx | Average trading prices |
Havelock Investments | Ticker data, balance and portfolio data |
Bitcoin address balances from blockchain.info
Portfolio data and average trading price from BTCT.co
Average trading price from MPEx
Havelock
MtGox weighted average, any currency
Can easily be further customized.
Demo of self-updating portfolio with stats and etc:
https://docs.google.com/spreadsheet/pub?key=0AvwwyRGyc1WgdGFKVlAtZVAzOGNqS1Brd05MUlFlT2c###################################
Go in code editor and put this code in, replacing the variables
// Namworld's JSON Portfolio Toolkit v0.5
// https://bitcointalk.org/index.php?topic=140427.0
// Variables, set them up to use your account data functions
var BTCT_key = "YourReadOnlyApiKey" // BTCT.co Portfolio via JSON API key (read only)
var BF_key = "YourReadOnlyApiKey" // Bitfunder API Key (read only)
var Havelock_key = "YourReadOnlyApiKey" // Havelock Investment API key. Only give portfolio data permissions! Not orders or anything else.
var cacheTime = 120 // time to cache data in seconds
// Main fetch JSON data function
function fetchCached(url){ // fetch url from cache
var publicCache = CacheService.getPublicCache()
var cached = publicCache.get(url)
if (null == cached) { // If cache is null, fetch url
var response = UrlFetchApp.fetch(url)
if ('undefined' != typeof(response)) {
var code = response.getResponseCode()
if (code == 200) {
cached = response.getContentText()
publicCache.put(url, cached, cacheTime)
}
}
}
return cached
}
function fetchCached_Havelock_Key(url,key){
var publicCache = CacheService.getPublicCache()
var cached = publicCache.get(url)
if (null == cached) { // If cache is null, fetch url
var payload = {"key": key};
var options = {"method" : "post","payload" : payload};
var response = UrlFetchApp.fetch(url,options);
if ('undefined' != typeof(response)) {
var code = response.getResponseCode()
if (code == 200) {
cached = response.getContentText()
publicCache.put(url, cached, cacheTime)
}
}
}
return cached
}
// MtGox #########################################################################
function MtGoxCurrency(currency,data,datatype){
// Fetch currency data from MtGOX API
// Ex: =MtGoxCurrency("USD","avg","value") for weighted average
// https://mtgox.com/api/1/BTCUSD/ticker
var r = fetchCached("https://mtgox.com/api/1/BTC"+ currency +"/ticker")
if ('undefined' == typeof(r))
return 'No data!'
r = Utilities.jsonParse(r)
if ('undefined' == typeof(r))
return 'Malformed JSON!'
r = r['return'][data][datatype]
if ('undefined' == typeof(r))
return 0
if (!isNaN(r)){r = r*1}
return r
}
// Blockchain.info ###############################################################
function BlockchainBalance(btcAddress){
// Fetch balance from Blockchain.info API
// EX: =BlockchainBalance("1Namjies9JeJ4ugCzjkAj6dbaWrtjAFKCk")
// http://blockchain.info/address/1dice9wVtrKZTBbAZqz1XiTmboYyvpD3t?format=json
var r = fetchCached("http://blockchain.info/address/" + btcAddress + "?format=json&limit=0")
if ('undefined' == typeof(r))
return 'No data!'
r = Utilities.jsonParse(r)
if ('undefined' == typeof(r))
return 'Malformed JSON!'
r = r['final_balance'] / 100000000
if ('undefined' == typeof(r))
return 0
if (!isNaN(r)){r = r*1}
return r
}
// BTCT.co #######################################################################
function BTCTsecurity(assetName){
// Fetch live security amount from BTCT.co API
// Ex: =BTCTsecurity("BTC-BOND")
// https://btct.co/api/act?key=InputYourApiKey
var r = fetchCached("https://btct.co/api/act?key=" + BTCT_key)
if ('undefined' == typeof(r))
return 'No data!'
r = Utilities.jsonParse(r)
if ('undefined' == typeof(r))
return 'Malformed JSON!'
if ('undefined' == typeof(r['securities'][assetName]))
return 0
r = r['securities'][assetName]['quantity']
if ('undefined' == typeof(r))
return 'Unkown Error!'
if (!isNaN(r)){r = r*1}
return r
}
function BTCTbalance(){
// Fetch your account balance
// EX: =BTCTbalance()
// https://btct.co/api/act?key=InputYourApiKey
var r = fetchCached("https://btct.co/api/act?key=" + BTCT_key)
if ('undefined' == typeof(r))
return 'No data!'
r = Utilities.jsonParse(r)
if ('undefined' == typeof(r))
return 'Malformed JSON!'
r = r['balance']['BTC']
if ('undefined' == typeof(r))
return 0
if (!isNaN(r)){r = r*1}
return r
}
function BTCTasset(assetName,assetData){
// Fetch public asset ticker data
// Ex: =BTCTasset("BTC-BOND","7d_avg")
// https://btct.co/api/ticker/BTC-BOND
var r = fetchCached("https://btct.co/api/ticker/" + assetName)
if ('undefined' == typeof(r))
return 'No data!'
r = Utilities.jsonParse(r)
if ('undefined' == typeof(r))
return 'Malformed JSON!'
r = r[assetData]
if ('undefined' == typeof(r))
return 0
if ('--' == r)
return 0
if (!isNaN(r)){r = r*1}
return r
}
function BTCTcontract(assetName,assetData){
// Fetch public asset contract data
// Ex: =BTCTcontract("BTC-BOND","Shares Outstanding")
// https://btct.co/api/assetContract/BTC-BOND
var r = fetchCached("https://btct.co/api/assetContract/" + assetName)
if ('undefined' == typeof(r))
return 'No data!'
r = Utilities.jsonParse(r)
if ('undefined' == typeof(r))
return 'Malformed JSON!'
r = r[assetData]
if ('undefined' == typeof(r))
return 0
if (!isNaN(r)){r = r*1}
return r
}
// Bitfunder.com #######################################################################
function BFasset(assetName, assetData){
// Fetch public asset data from Bitfunder's API
// Ex: =BFasset("BitPride","last_price")
// https://api.bitfunder.com/public/market
var r = fetchCached("https://api.bitfunder.com/public/market")
if ('undefined' == typeof(r))
return 'No data!'
r = Utilities.jsonParse(r)
if ('undefined' == typeof(r))
return 'Malformed JSON!'
r = r[assetName][assetData]
if ('undefined' == typeof(r))
return 0
if (!isNaN(r)){r = r*1}
return r
}
function BFportfolio(assetName, assetData){
// Fetch live security amount from Bitfunder's API
// Ex: =BFportfolio("BitPride","amount")
// https://api.bitfunder.com/private/assets?key=InputYourApiKey
var r = fetchCached("https://api.bitfunder.com/private/assets?key=" + BF_key)
if ('undefined' == typeof(r))
return 'No data!'
r = Utilities.jsonParse(r)
if ('undefined' == typeof(r))
return 'Malformed JSON!'
r = r["assets"][assetName][assetData]
if ('undefined' == typeof(r))
return 0
if (!isNaN(r)){r = r*1}
return r
}
function BFbalance(){
// Fetch live security amount from Bitfunder's API
// Ex: =BFbalance()
// https://api.bitfunder.com/private/assets?key=InputYourApiKey
var r = fetchCached("https://api.bitfunder.com/private/balance?key=" + BF_key)
if ('undefined' == typeof(r))
return 'No data!'
r = Utilities.jsonParse(r)
if ('undefined' == typeof(r))
return 'Malformed JSON!'
r = r["account_balance"]
if ('undefined' == typeof(r))
return 'Unkown Error!'
r = r / 100000000
if (!isNaN(r)){r = r*1}
return r
}
// MPEx ##########################################################################
function MPEXaverage(MPSIC,period){
// Fetch public asset contract data
// Ex: =MPEXaverage("S.MPOE","1d")
// http://mpex.co/mpex-vwap.php
var r = fetchCached("http://mpex.co/mpex-vwap.php")
if ('undefined' == typeof(r))
return 'No data!'
r = Utilities.jsonParse(r)
if ('undefined' == typeof(r))
return 'Malformed JSON!'
r = r[MPSIC][period]["avg"] / 100000000
if ('undefined' == typeof(r))
return 0
if (!isNaN(r)){r = r*1}
return r
}
// Havelock Investments ##########################################################################
function HavelockTicker(symbol,data){
// Fetch Havelock ticker
// Ex: "HIM","last"
// https://www.havelockinvestments.com/r/tickerfull
var r = fetchCached("https://www.havelockinvestments.com/r/tickerfull")
if ('undefined' == typeof(r))
return 'No data!'
r = Utilities.jsonParse(r)
if ('undefined' == typeof(r))
return 'Malformed JSON!'
r = r[symbol][data]
if ('undefined' == typeof(r))
return 0
if (!isNaN(r)){r = r*1}
return r
}
function HavelockPeriod(symbol,period,data){
// Fetch Havelock averages and over a period data
// Ex: "HIM","30d","vwap"
// https://www.havelockinvestments.com/r/tickerfull
var r = fetchCached("https://www.havelockinvestments.com/r/tickerfull")
if ('undefined' == typeof(r))
return 'No data!'
r = Utilities.jsonParse(r)
if ('undefined' == typeof(r))
return 'Malformed JSON!'
r = r[symbol][period][data]
if ('undefined' == typeof(r))
return 0
if (!isNaN(r)){r = r*1}
return r
}
function HavelockBalance(){
// Fetch your Havelock balance
// Available data : balance, balanceescrow, balanceavailable
var r = fetchCached_Havelock_Key("https://www.havelockinvestments.com/r/balance",Havelock_key)
if ('undefined' == typeof(r))
return 'No data!'
r = Utilities.jsonParse(r)
if ('undefined' == typeof(r))
return 'Malformed JSON!'
r = r['balance']['balance']
if ('undefined' == typeof(r))
return 0
if (!isNaN(r)){r = r*1}
return r
}
function HavelockPortfolio(symbol,data){
// Fetch your Havelock balance
// Ex: "HIM","quantity"
// Available data : symbol, name, quantity, quantityescrow, lastprice, bookvalue, marketvalue
// https://www.havelockinvestments.com/r/portfolio
var r = fetchCached_Havelock_Key("https://www.havelockinvestments.com/r/portfolio",Havelock_key)
if ('undefined' == typeof(r))
return 'No data!'
r = Utilities.jsonParse(r)
if ('undefined' == typeof(r))
return 'Malformed JSON!'
r = r['portfolio']
if (r == null){return 0}
for(var i=0;i if (r[i]['symbol'] == symbol) {r = r[i]}
}
r = r[data]
if ('undefined' == typeof(r))
return 0
if (!isNaN(r)){r = r*1}
return r
}
Then simply put in your spreadsheet cells, putting one value for each variable for the function, with quotes around the text:
=functionName("value","value")