109 lines
3.0 KiB
Bash
Executable File
109 lines
3.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -e
|
|
|
|
italic="\033[3m"
|
|
underline="\033[4m"
|
|
ita_under="\033[3;4m"
|
|
bgd="\033[1;4;31m"
|
|
red="\033[1;31m"
|
|
green="\033[1;32m"
|
|
bold="\033[1m"
|
|
box="\033[1;41m"
|
|
reset="\033[0m"
|
|
|
|
LANG=C
|
|
LC_NUMERIC=C
|
|
|
|
SYMBOLS=("$@")
|
|
#echo "${SYMBOLS[*]}"
|
|
#^FCHI AC.PA ALCLS.PA ATO.PA AUB.PA CS.PA DG.PA ORA.PA VIRB.PA WLN.PA
|
|
|
|
if ! $(type jq > /dev/null 2>&1); then
|
|
echo "'jq' is not in the PATH. (See: https://stedolan.github.io/jq/)"
|
|
exit 1
|
|
fi
|
|
|
|
if [ -z "$SYMBOLS" ]; then
|
|
echo "Usage: ./ticker.sh AAPL MSFT GOOG BTC-USD"
|
|
exit
|
|
fi
|
|
|
|
API_ENDPOINT="https://query1.finance.yahoo.com/v7/finance/quote?lang=fr-FR®ion=FR&corsDomain=finance.yahoo.com"
|
|
|
|
symbols=$(IFS=,; echo "${SYMBOLS[*]}")
|
|
#echo "$symbols"
|
|
|
|
results=$(curl --silent "$API_ENDPOINT&fields=$fields&symbols=$symbols" | jq '.quoteResponse .result')
|
|
#echo "$results"
|
|
|
|
query () {
|
|
echo "$obj" | jq -r ".$1"
|
|
}
|
|
|
|
|
|
t=0
|
|
|
|
echo -e "${bold}stocks.sh${reset}\n"
|
|
|
|
while IFS= read -r obj; do
|
|
marketState=$(query 'marketState')
|
|
symbol=$(query 'symbol')
|
|
|
|
preMarketChange=$(query 'preMarketChange')
|
|
postMarketChange=$(query 'postMarketChange')
|
|
|
|
longName=$(query 'longName')
|
|
if [ "$longName" == "null" ] && [ $symbol == "^FCHI" ]; then
|
|
longName="CAC 40"
|
|
fi
|
|
|
|
if [ $t == "0" ]; then
|
|
printf "${bold}%-10s | %-23s | %-7s | " "Symbole" "Entreprise" "Cours"
|
|
printf "%-6s | %-7s | %-7s | " "Diff" "%" "Veille"
|
|
printf "%-7s | %-7s | %-8s | " "+ Haut" "+ Bas" "Volume"
|
|
printf "%-9s | %-25s | %-18s${reset}\n" "Ouverture" "Heure" "52 semaines"
|
|
fi
|
|
t=1
|
|
|
|
if [ $marketState == "PRE" ] && [ $preMarketChange != "0" ] && [ $preMarketChange != "null" ]; then
|
|
nonRegularMarketSign='*'
|
|
price=$(query 'preMarketPrice')
|
|
diff=$preMarketChange
|
|
percent=$(query 'preMarketChangePercent')
|
|
elif [ $marketState != "REGULAR" ] && [ $postMarketChange != "0" ] && [ $postMarketChange != "null" ]; then
|
|
nonRegularMarketSign='*'
|
|
price=$(query 'postMarketPrice')
|
|
diff=$postMarketChange
|
|
percent=$(query 'postMarketChangePercent')
|
|
else
|
|
nonRegularMarketSign=''
|
|
price=$(query 'regularMarketPrice')
|
|
diff=$(query 'regularMarketChange')
|
|
percent=$(query 'regularMarketChangePercent')
|
|
previous=$(query 'regularMarketPreviousClose')
|
|
volume=$(query 'regularMarketVolume')
|
|
haut=$(query 'regularMarketDayHigh')
|
|
bas=$(query 'regularMarketDayLow')
|
|
ouverture=$(query 'regularMarketOpen')
|
|
ts=$(query 'regularMarketTime')
|
|
heure=$(LC_ALL=fr_FR.UTF-8 date -d @$ts +"%c" 2>/dev/null || LC_ALL=fr_FR.UTF-8 date -r $ts +"%c")
|
|
ftweeks=$(query 'fiftyTwoWeekRange')
|
|
fi
|
|
|
|
if [ "$diff" == "0" ] || [ "$diff" == "0.0" ]; then
|
|
color=
|
|
elif ( echo "$diff" | grep -q ^- ); then
|
|
color=$red
|
|
else
|
|
color=$green
|
|
fi
|
|
|
|
if [ "$price" != "null" ]; then
|
|
printf "${color}%-10s | %-23s | %7.2f | " $symbol "$longName" $price
|
|
printf "% 6.2f | % 6.2f%% | %7.2f | " $diff $percent $previous
|
|
printf "%7.2f | %7.2f | %8d | " $haut $bas $volume
|
|
printf "%9.2f | %25s | %18s${reset}\n" $ouverture "$heure" "$ftweeks"
|
|
fi
|
|
|
|
done < <(echo "$results" | jq -c '.[]')
|