Skip to content

Instantly share code, notes, and snippets.

@josepmartins
Forked from mutsuda/indexaCapitalWidget.js
Last active November 14, 2020 19:20
Show Gist options
  • Save josepmartins/3e20a063c2fd92c2e9282cf7979420df to your computer and use it in GitHub Desktop.
Save josepmartins/3e20a063c2fd92c2e9282cf7979420df to your computer and use it in GitHub Desktop.
// Read parameter from widget
let parameter = args.widgetParameter
var parameters = "";
if (parameter) parameters = parameter.split(";")
else
{
// If no parameters were found, ask the user to put them
let w = new ListWidget()
error = w.addText("Introduce tu cuenta y token de IndexaCapital separados por un ; en la configuración del Widget")
error.font = new Font("Courier-Bold", 12)
Script.setWidget(w)
Script.complete()
return
}
let portfolio = await loadItems("portfolio", parameters)
let performance = await loadItems("performance", parameters)
let widget = createWidget(portfolio, performance)
Script.setWidget(widget)
Script.complete()
// Get the data from the API in the given endpoint
async function loadItems(endpoint, parameters)
{
const url = "https://api.indexacapital.com/accounts/" + parameters[0] + "/" + endpoint
const request = new Request(url)
request.headers = { 'X-Auth-Token':parameters[1] }
const response = await request.loadJSON()
return response
}
// Create the widget with the portfolio and performance info
function createWidget(recportfolio, recperformance)
{
let titlefont = new Font("Verdana",24)
let amountfont = new Font("Courier-Bold",20)
let retfont = new Font("Courier",16)
let w = new ListWidget()
let takes = 0
let cgs = 0
let total = 0
total_amount = (recportfolio["portfolio"]["total_amount"]).toFixed(2).toString()+"€"
time_return = (recperformance["return"]["time_return"] * 100).toFixed(2).toString()+"%"
title = w.addText("Indexa")
amount = w.addText(total_amount)
ret = w.addText(time_return)
title.font = titlefont
amount.font = amountfont
ret.font = retfont
title.textColor = Color.white()
amount.textColor = Color.white()
ret.textColor = Color.white()
ret.textOpacity = 0.5
w.backgroundColor = new Color("#2D2D2D")
return w
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment