Skip to content

Instantly share code, notes, and snippets.

@kylorhall
Last active February 26, 2019 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylorhall/59139f99cb018bb1ee9ee44cbb7a6fd6 to your computer and use it in GitHub Desktop.
Save kylorhall/59139f99cb018bb1ee9ee44cbb7a6fd6 to your computer and use it in GitHub Desktop.
var ClientID = "ENTER_CLIENTID_FROM_SHARESIGHT_HERE";
var ClientSecret = "ENTER_CLIENTSECRET_FROM_SHARESIGHT_HERE";
var PortfolioNumber = "ENTER_PORTFOLIO_NUMBER_FROM_SHARESIGHT_HERE";
var isConsolidated = false; // set to true if this is a consolidated view (see ?consolidated=) in your address bar
/* Note to readers, your portfolio number is the number shown in the address
bar when you have Sharesight open. It is five or six digits. This code
can handle combined portfolios, or consolidated views as Sharesight refers
to them */
/* The following code checks if a token has been retrieved from Sharesight
in the last 30 minutes (how long the tokens remain valid for). If one
hasn't, it requests a token using the client ID an client secret that
we stored at the beginning of the code. */
function addtoproperties(SStoken) {
var scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty('SStoken', SStoken);
var d = new Date();
var timeStamp = d.getTime();
scriptProperties.setProperty('issuedtime', timeStamp);
var store = scriptProperties.getProperties();
return;
}
function gettoken() {
var authdetails = {
"grant_type": "client_credentials",
"client_id": ClientID,
"client_secret": ClientSecret
};
var options = {
'method': 'post',
'payload': authdetails
};
var scriptProperties = PropertiesService.getScriptProperties();
var issuetime = scriptProperties.getProperty('issuedtime');
if (issuetime !== null) {
var TokenExpiry = (Number(issuetime) + 1800);
} else {
var TokenExpiry = 0;
}
var d = new Date();
var timeStamp = d.getTime();
if (timeStamp > TokenExpiry) {
var response = UrlFetchApp.fetch("https://api.sharesight.com/oauth2/token", options);
var data = JSON.parse(response.getContentText());
var token = data.access_token;
addtoproperties(token)
} else {
var token = scriptProperties.getProperty('SStoken');
}
return token
}
//This next section requests the JSON data from Sharesight
function getSScombinedportfolio() {
var token = gettoken();
var header = {
'Authorization': 'Bearer ' + token
}
var options = {
'method': 'get',
'headers': header
};
var url = "https://api.sharesight.com/api/v2/portfolios/" + PortfolioNumber + "/valuation.json?consolidated=" + (isConsolidated ? 'true' : 'false');
var response = UrlFetchApp.fetch(url, options);
var data = JSON.parse(response.getContentText());
return data
}
/* And this code parses the JSON into an array with three rows containing
Symbol, Quantity and Value. */
function parsedSSdata() {
var dataSet = getSScombinedportfolio();
var rows = [],
data;
for (i = 0; i < dataSet.holdings.length; i++) {
data = dataSet.holdings[i];
rows.push({
'symbol': data.symbol,
'quantity': data.quantity,
'value': data.value
});
}
return rows
}
/* The following section handles when the portfolio number is actually a
consolidated view of more than one portfolio. It sums any holdings with
the same symbol and then sorts them largest to smallest. */
function summedSSdatawithKeys() {
var data = parsedSSdata();
var sum = [];
data.forEach(function(o) {
var existing = sum.filter(function(i) {
return i.symbol === o.symbol
})[0];
if (!existing) sum.push(o);
else existing.quantity += o.quantity, existing.value += o.value;
});
// Sort largest value to lowest value
sum.sort(function(a, b) {
return b.value - a.value;
});
return sum;
}
/* And this final section is the function you can call to import the
data into a sheet. If no option is specified it will show symbol (ticker),
quantity and value. Or you can specify either quantity or value.
E.g. =SStotals("value") */
function SStotals(options) {
var dataSet = summedSSdatawithKeys();
var output = [];
switch (options) {
case 'value':
for (var i = 0; i < dataSet.length; ++i) {
var data = dataSet[i];
output.push([data.symbol, data.value]);
}
break;
case 'quantity':
for (var i = 0; i < dataSet.length; ++i) {
var data = dataSet[i];
output.push([data.symbol, data.quantity]);
}
break;
default:
for (var i = 0; i < dataSet.length; ++i) {
var data = dataSet[i];
output.push([data.symbol, data.quantity, data.value]);
}
break;
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment