-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstockprice
executable file
·33 lines (27 loc) · 1.09 KB
/
stockprice
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/sh
# stockprice: fetch live stock prices from yahoo finance
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "usage: stockprice [symbol...]"
exit 0
fi
while [ $# -gt 0 ]; do
symbol="$(printf '%s' "$1" | tr '[:lower:]' '[:upper:]')"
shift
curl --silent --location "https://query2.finance.yahoo.com/v8/finance/chart/$symbol" \
| jq --raw-output '.chart | .result[0].meta as $meta | [ $meta.regularMarketPrice, $meta.previousClose, $meta.currency, .error.description ] | @tsv' \
| LC_ALL=C.UTF-8 awk -F '\t' -v "symbol=$symbol" '
BEGIN {
time = strftime("%Y-%m-%dT%H:%M:%S%z")
}
$NF {
printf("%s|||||%s|%s\n", symbol, time, $NF)
}
!$NF {
change = $1 - $2
sign = change > 0 ? "+" : ""
perc = change / $2 * 100
printf("%s|%.2f|%s%.2f|%s%.2f%%|%s|%s\n", symbol, $1, sign, change, sign, perc, $3, time)
}' \
&
done | sort | column --table --table-right 2,3,4 --separator '|'
wait