Last active
February 6, 2022 17:25
-
-
Save jaybuidl/a8d8cb669c067dfe3acf692aff135c56 to your computer and use it in GitHub Desktop.
Live prices download from Alpha Vantage
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
#!/bin/bash | |
# precondition: jq installed | |
pkg="jq" | |
if [[ $(dpkg-query -W -f='${Status}' $pkg 2>/dev/null) != "install ok installed" ]]; | |
then | |
echo "$pkg package not found: installing" | |
if sudo apt-get install -y $pkg; | |
then | |
echo "$pkg installation successful, continuing..."; | |
else | |
echo "$pkg installation failed"; | |
exit 1 | |
fi | |
fi | |
if [ -z ${API_KEY} ] | |
then | |
echo "API_KEY env variable is not set, exiting" | |
exit 1 | |
fi | |
function getPrice() #symbol | |
{ | |
local symbol="$1" | |
curl -i -s -H "Accept: application/json" "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&interval=1min&apikey=${API_KEY}&symbol=${symbol}" \ | |
| sed -n '/{/,$p' \ | |
| jq '."Time Series (1min)" | to_entries[] | [.key, .value."4. close"] | .[1]' | |
} | |
function getFxPrice() #fromFx #toFx | |
{ | |
local from="$1" | |
local to="$2" | |
curl -i -s -H "Accept: application/json" "https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&apikey=${API_KEY}&from_currency=${from}&to_currency=${to}" | |
} | |
function calculateChange() #p1 #p2 | |
{ | |
local p1="$1" | |
local p2="$2" | |
local precision="$3" | |
echo "scale = 10; ($p1 - $p2)" | tr -d '"' | bc | |
} | |
function calculateChangePercents() #p1 #p2 | |
{ | |
local p1="$1" | |
local p2="$2" | |
local precision="$3" | |
echo "scale = 10; 100 * ($p1 - $p2) / $p1" | tr -d '"' | bc | |
} | |
function formatPrice() #name #serie #precision | |
{ | |
local name="$1" | |
local prices="$2" | |
local precision="$3" | |
p1="$(echo ${prices} | cut -d ' ' -f2 | tr -d '"')" | |
p2="$(echo ${prices} | cut -d ' ' -f1 | tr -d '"')" | |
change="$(printf "%.${precision}f" $(calculateChange $p2 $p1 2))" | |
percents="$(printf "%.${precision}f" $(calculateChangePercents $p2 $p1 2))" | |
echo "$name" | |
echo "last price: $(printf "%.${precision}f" $p2)" | |
echo "change: $change (${percents}%)" | |
echo "" | |
} | |
formatPrice "S&P 500" "$(getPrice '^GSPC')" 2 | |
sleep 5 | |
formatPrice "NASDAQ 100" "$(getPrice '^IXIC')" 2 | |
sleep 5 | |
formatPrice "EUR/USD" "$(getPrice 'EURUSD')" 5 | |
sleep 5 | |
formatPrice "Bitcoin/USD" "$(getPrice 'BTCUSD')" 2 | |
sleep 5 | |
formatPrice "Apple" "$(getPrice 'AAPL')" 2 | |
sleep 5 | |
formatPrice "Tencent" "$(getPrice 'TCEHY')" 2 | |
#sleep 5 | |
#formatPrice "FTSE 100" "$(getPrice '^FTSE')" 2 | |
#sleep 5 | |
#formatPrice "DAX" "$(getPrice '^GDAXI')" 2 | |
#sleep 5 | |
#formatPrice "Hang Seng" "$(getPrice '^HSI')" 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output example: