Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Stock Widget for iOS using Scriptable
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-blue; icon-glyph: book; share-sheet-inputs: plain-text;
// Stock Ticker Widget
let stocksInfo = await getStockData()
let widget = await createWidget()
if (config.runsInWidget) {
// The script runs inside a widget, so we pass our instance of ListWidget to be shown inside the widget on the Home Screen.
Script.setWidget(widget)
} else {
// The script runs inside the app, so we preview the widget.
widget.presentSmall()
}
// Calling Script.complete() signals to Scriptable that the script have finished running.
// This can speed up the execution, in particular when running the script from Shortcuts or using Siri.
Script.complete()
async function createWidget(api) {
let upticker = SFSymbol.named("chevron.up");
let downticker = SFSymbol.named("chevron.down");
let widget = new ListWidget()
// Add background gradient
let gradient = new LinearGradient()
gradient.locations = [0, 1]
gradient.colors = [
new Color("141414"),
new Color("13233F")
]
widget.backgroundGradient = gradient
for(j=0; j<stocksInfo.length; j++)
{
let currentStock = stocksInfo[j];
let row1 = widget.addStack();
// Add Stock Symbol
let stockSymbol = row1.addText(currentStock.symbol);
stockSymbol.textColor = Color.white();
stockSymbol.font = Font.boldMonospacedSystemFont(12);
//Add Current Price
row1.addSpacer();
let symbolPrice = row1.addText(currentStock.price);
symbolPrice.textColor = Color.white();
symbolPrice.font = Font.boldMonospacedSystemFont(12);
//Second Row
widget.addSpacer(2)
let row2= widget.addStack();
// Add Company name
let companyName= row2.addText(currentStock.name);
companyName.textColor = Color.white();
companyName.textOpacity = 0.7;
companyName.font = Font.boldMonospacedSystemFont(9);
//Add Today's change in price
row2.addSpacer();
let changeValue = row2.addText(currentStock.changevalue);
if(currentStock.changevalue < 0) {
changeValue.textColor = Color.red();
} else {
changeValue.textColor = Color.green();
}
changeValue.font = Font.boldMonospacedSystemFont(9);
// Add Ticker icon
row2.addSpacer(2);
let ticker = null;
if(currentStock.changevalue < 0){
ticker = row2.addImage(downticker.image);
ticker.tintColor = Color.red();
} else {
ticker = row2.addImage(upticker.image);
ticker.tintColor = Color.green();
}
ticker.imageSize = new Size(8,8);
widget.addSpacer(6);
}
return widget
}
async function getStockData() {
let stocks = null;
// Read from WidgetParameter if present or use hardcoded values
// Provide values in Widget Parameter as comma seperated list
if(args.widgetParameter == null) {
stocks = ["PRGS", "AAPL", "INR=X", "XRP-USD"];
} else {
stocks = args.widgetParameter.split(",");
}
let stocksdata = [];
for(i=0; i< stocks.length; i++)
{
let stkdata = await queryStockData(stocks[i].trim());
let price = stkdata.quoteSummary.result[0].price;
let priceKeys = Object.keys(price);
let data = {};
data.symbol = price.symbol;
data.changepercent = (price.regularMarketChangePercent.raw * 100).toFixed(2);
data.changevalue = price.regularMarketChange.raw.toFixed(2);
data.price = price.regularMarketPrice.raw.toFixed(2);
data.high = price.regularMarketDayHigh.raw.toFixed(2);
data.low = price.regularMarketDayLow.raw.toFixed(2);
data.prevclose = price.regularMarketPreviousClose.raw.toFixed(2);
data.name = price.shortName;
stocksdata.push(data);
}
return stocksdata;
}
async function queryStockData(symbol) {
let url = "https://query1.finance.yahoo.com/v10/finance/quoteSummary/" + encodeURIComponent(symbol) + "?modules=price"
let req = new Request(url)
return await req.loadJSON()
}
@saiteja09

This comment has been minimized.

Copy link
Owner Author

@saiteja09 saiteja09 commented Oct 17, 2020

Instructions
a. Download the script to your scriptable app and add Scriptable Widget to home screen,
b. Edit Widget by activating the Jiggle mode
c. Choose the script, set it to run script.
d. Parameters: provide stock symbols you need to have on widget, separated by commas
Eg: GOOG, INR=X, BTC-USD, AAPL

Look of the widget -

IMG_0083

Settings -
file

@giuliomagnifico

This comment has been minimized.

Copy link

@giuliomagnifico giuliomagnifico commented Oct 19, 2020

Hi, I found a bug, with the composite indexes the widget shows a ^/green value also if the index is negative.

For example add NDAQ (nasdaq) and now is -0.36% but the widget shows only the value without the symbol +/- and it’s in green like a positive value but should be red.

Probably is due how to indexes work that use point as a value instead of money. I haven’t checked the json query to yahoo and if this is fixable.

Anyway for normal stocks and cryptos works perfectly! Thanks

@rekent89

This comment has been minimized.

Copy link

@rekent89 rekent89 commented Nov 22, 2020

When I run the script I’m receiving the following error.

2020-11-22 15:38:20: Error on line 103:44: TypeError: null is not an object (evaluating 'stkdata.quoteSummary.result[0]')

@BeMaWeRo

This comment has been minimized.

Copy link

@BeMaWeRo BeMaWeRo commented Dec 9, 2020

Hi, is it possible to configure a background image, as is possible in this great calendar script?
24C88F40-40F0-4FCF-BFB2-186DAF5A9EA8

https://github.com/JoeGit42/simple-calendar/blob/main/simple_calender.js

@matthew0624

This comment has been minimized.

Copy link

@matthew0624 matthew0624 commented Dec 21, 2020

hey thanks for using this script. but i want to show the price of stock like 12345$ this script shows decimal? point like 12345.000
i don't want to show under 0 can you fix it

@saiteja09

This comment has been minimized.

Copy link
Owner Author

@saiteja09 saiteja09 commented Dec 22, 2020

@matthew0624

Change line 106 to
data.price = price.regularMarketPrice.raw.toFixed(0);

that will show the price without decimal.

@matthew0624

This comment has been minimized.

Copy link

@matthew0624 matthew0624 commented Dec 22, 2020

@matthew0624

Change line 106 to
data.price = price.regularMarketPrice.raw.toFixed(0);

that will show the price without decimal.

wow thx i love you

@SyedYousha

This comment has been minimized.

Copy link

@SyedYousha SyedYousha commented Jan 9, 2021

Hello, I'd possibly like to toggle this code and personalize it a little more.
I want to be able to input the price that I bought the stock and then have that price be compared to the stock price of the current day and get the difference of the two. Could you possibly help me out with something like this?

@webOSpinn

This comment has been minimized.

Copy link

@webOSpinn webOSpinn commented Feb 17, 2021

I think line 118 should be updated to use encodeURIComponent(). That way you don't have to remember to manually encode a symbol like ^dji to %5Edji

let url = "https://query1.finance.yahoo.com/v10/finance/quoteSummary/" + encodeURIComponent(symbol) + "?modules=price"

@matthew0624

This comment has been minimized.

Copy link

@matthew0624 matthew0624 commented Feb 19, 2021

hey i want to open an external application when click this widget. or open stock application. how can i do it??

@R3n007

This comment has been minimized.

Copy link

@R3n007 R3n007 commented Feb 21, 2021

I love this, Are you able do a crypto version?

Edit: Also how to I show Australian stocks, ie A200

@saiteja09

This comment has been minimized.

Copy link
Owner Author

@saiteja09 saiteja09 commented Feb 21, 2021

I think line 118 should be updated to use encodeURIComponent(). That way you don't have to remember to manually encode a symbol like ^dji to %5Edji

let url = "https://query1.finance.yahoo.com/v10/finance/quoteSummary/" + encodeURIComponent(symbol) + "?modules=price"

Thanks for pointing that out. updated the script.

@saiteja09

This comment has been minimized.

Copy link
Owner Author

@saiteja09 saiteja09 commented Feb 21, 2021

I love this, Are you able do a crypto version?

Edit: Also how to I show Australian stocks, ie A200

This supports crypto as well, all you need to do is add the symbol.

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