-
-
Save deigote/faf6fc75db9289893c538639f96e66c8 to your computer and use it in GitHub Desktop.
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
// 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("Avenir-Book", 11) | |
Script.setWidget(w) | |
Script.complete() | |
return | |
} | |
let performance = await loadItems("performance", parameters) | |
let widget = createWidget(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 performance info | |
function createWidget(recperformance) | |
{ | |
let totalfont = new Font("Avenir-Heavy",20) | |
let retfont = new Font("Avenir-Book",17) | |
let lastUpdatedFont = new Font("AvenirNext-Italic", 12) | |
let w = new ListWidget() | |
w.backgroundColor = new Color("#123456") | |
let takes = 0 | |
let cgs = 0 | |
let total = 0 | |
total_amount = (recperformance["return"]["total_amount"]).toFixed(2).toString()+"€" | |
total_invested = (recperformance["return"]["investment"]).toFixed(2).toString()+"€" | |
time_return_annual = (recperformance["return"]["time_return_annual"] * 100).toFixed(2).toString()+"%" | |
title = w.addText("Indexa") | |
title.font = retfont | |
title.textColor = Color.white() | |
amount = w.addText(total_amount + " " + total_invested) | |
amount.font = totalfont | |
amount.textColor = Color.white() | |
ret = w.addText(time_return_annual) | |
ret.textColor = Color.white() | |
ret.font = retfont | |
ret.textOpacity = 0.7 | |
lastUpdated = w.addText(new Date().toLocaleTimeString()) | |
lastUpdated.textColor = Color.white() | |
lastUpdated.font = lastUpdatedFont | |
lastUpdated.textOpacity = 0.7 | |
w.backgroundColor = new Color("#217ec1") | |
return w | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment