Skip to content

Instantly share code, notes, and snippets.

@jaybuidl
Last active February 6, 2022 17:25
Show Gist options
  • Save jaybuidl/a8d8cb669c067dfe3acf692aff135c56 to your computer and use it in GitHub Desktop.
Save jaybuidl/a8d8cb669c067dfe3acf692aff135c56 to your computer and use it in GitHub Desktop.
Live prices download from Alpha Vantage
#!/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
@jaybuidl
Copy link
Author

jaybuidl commented Mar 15, 2018

Output example:

$ ./get-prices.sh 
S&P 500
last price: 2747.41
change: -1.40  (-0.05%)

NASDAQ 100
last price: 7482.73
change: -1.98  (-0.03%)

EUR/USD
last price: 1.23060
change: 0.00000  (0.00000%)

Bitcoin/USD
last price: 8125.00
change: 0.00  (0.00%)

Apple
last price: 178.65
change: -0.13  (-0.07%)

Tencent
last price: 59.73
change: 0.01  (0.02%)

@jaybuidl
Copy link
Author

API keys can be generated for free from https://www.alphavantage.co/support/#support

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