Skip to content

Instantly share code, notes, and snippets.

@mutsuda
Last active November 16, 2020 17:14
Show Gist options
  • Save mutsuda/9a5abb55e38e80f66e22abc921fc5d86 to your computer and use it in GitHub Desktop.
Save mutsuda/9a5abb55e38e80f66e22abc921fc5d86 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("Avenir-Book", 11)
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 totalfont = new Font("Avenir-Heavy",20)
let retfont = new Font("Avenir-Book",17)
let w = new ListWidget()
w.backgroundColor = new Color("#123456")
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()+"%"
amount = w.addText(total_amount)
ret = w.addText(time_return)
amount.font = totalfont
ret.font = retfont
amount.textColor = Color.white()
ret.textColor = Color.white()
ret.textOpacity = 0.7
w.backgroundColor = new Color("#217ec1")
return w
}
@deigote
Copy link

deigote commented Nov 16, 2020

Great idea! I think you can simplify it a bit by getting the total amount from the portfolio, if I'm not mistaken:

#!/bin/bash

ACCOUNT_ID="$1"
TOKEN="$2"
ENDPOINT=performance
METHOD=GET

RESPONSE=`curl https://api.indexacapital.com/accounts/$ACCOUNT_ID/$ENDPOINT \
    -H "X-Auth-Token: $TOKEN" \
    -X $METHOD`

TOTAL=`echo $RESPONSE | jq '.return.total_amount' | cut -d'.' -f1`
INVESTED=`echo $RESPONSE | jq '.return.investment'`
TIME_RETURN=`echo $RESPONSE | jq '.return.time_return'`

echo "$INVESTED - $TOTAL ($TIME_RETURN)"

Sorry for the bash, always end up using that when I'm testing something...

Edit: I actually did that, plus adding the widget a small title, check it out if you're interested - https://gist.github.com/deigote/faf6fc75db9289893c538639f96e66c8 . Again, great idea, I also didn't know about scriptable which looks like fun 😄 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment