1er commit

This commit is contained in:
2021-04-18 14:16:23 +02:00
commit 31c579e9c9
3 changed files with 201 additions and 0 deletions

3
run_stocks.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/usr/bin/env bash
while true; do clear; ./stocks.sh $(cat ~/.ticker.conf); sleep 60; done

104
stocks.sh Executable file
View File

@@ -0,0 +1,104 @@
#!/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[*]}"
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&region=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 $results | jq -r ".[] | select(.symbol == \"$1\") | .$2"
}
t=0
while IFS= read -r obj; do
marketState=$(echo "$obj" | jq -j '.marketState')
symbol=$(echo "$obj" | jq -j '.symbol')
preMarketChange=$(echo "$obj" | jq -j '.preMarketChange')
postMarketChange=$(echo "$obj" | jq -j '.postMarketChange')
longName=$(echo "$obj" | jq -j '.longName')
if [ "$longName" == "null" ] && [ $symbol == "^FCHI" ]; then
longName="CAC 40"
fi
if [ $t == "0" ]; then
printf "${bold}%-10s | %-25s | %-7s | " "Symbole" "Entreprise" "Cours"
printf "%-6s | %-6s | %-7s | " "Diff" "%" "Veille"
printf "%-7s | %-7s | %-8s | " "+ Haut" "+ Bas" "Volume"
printf "%-9s | %-25s${reset}\n" "Ouverture" "Heure"
fi
t=1
if [ $marketState == "PRE" ] && [ $preMarketChange != "0" ] && [ $preMarketChange != "null" ]; then
nonRegularMarketSign='*'
price=$(echo "$obj" | jq -j '.preMarketPrice')
diff=$preMarketChange
percent=$(echo "$obj" | jq -j '.preMarketChangePercent')
elif [ $marketState != "REGULAR" ] && [ $postMarketChange != "0" ] && [ $postMarketChange != "null" ]; then
nonRegularMarketSign='*'
price=$(echo "$obj" | jq -j '.postMarketPrice')
diff=$postMarketChange
percent=$(echo "$obj" | jq -j '.postMarketChangePercent')
else
nonRegularMarketSign=''
price=$(echo "$obj" | jq -j '.regularMarketPrice')
diff=$(echo "$obj" | jq -j '.regularMarketChange')
percent=$(echo "$obj" | jq -j '.regularMarketChangePercent')
previous=$(echo "$obj" | jq -j '.regularMarketPreviousClose')
volume=$(echo "$obj" | jq -j '.regularMarketVolume')
haut=$(echo "$obj" | jq -j '.regularMarketDayHigh')
bas=$(echo "$obj" | jq -j '.regularMarketDayLow')
ouverture=$(echo "$obj" | jq -j '.regularMarketOpen')
ts=$(echo "$obj" | jq -j '.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")
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 | %-25s | %7.2f | " $symbol "$longName" $price
printf "% 6.2f | % 3.2f%% | %7.2f | " $diff $percent $previous
printf "%7.2f | %7.2f | %8d | " $haut $bas $volume
printf "%9.2f | %25s${reset}\n" $ouverture "$heure"
fi
done < <(echo "$results" | jq -c '.[]')

94
ticker.sh Executable file
View File

@@ -0,0 +1,94 @@
#!/usr/bin/env bash
set -e
LANG=C
LC_NUMERIC=C
SYMBOLS=("$@")
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
FIELDS=(symbol marketState regularMarketPrice regularMarketChange regularMarketChangePercent regularMarketVolume regularMarketPreviousClose \
preMarketPrice preMarketChange preMarketChangePercent postMarketPrice postMarketChange postMarketChangePercent \
shortName longName fullExchangeName)
#API_ENDPOINT="https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com"
API_ENDPOINT="https://query1.finance.yahoo.com/v7/finance/quote?lang=fr-FR&region=FR&corsDomain=finance.yahoo.com"
if [ -z "$NO_COLOR" ]; then
: "${COLOR_BOLD:=\e[1;37m}"
: "${COLOR_GREEN:=\e[32m}"
: "${COLOR_RED:=\e[31m}"
: "${COLOR_RESET:=\e[00m}"
fi
symbols=$(IFS=,; echo "${SYMBOLS[*]}")
echo "$symbols"
fields=$(IFS=,; echo "${FIELDS[*]}")
results=$(curl --silent "$API_ENDPOINT&fields=$fields&symbols=$symbols" | jq '.quoteResponse .result')
#echo "$results"
query () {
echo $results | jq -r ".[] | select(.symbol == \"$1\") | .$2"
}
for symbol in $(IFS=' '; echo "${SYMBOLS[*]}" | tr '[:lower:]' '[:upper:]'); do
marketState="$(query $symbol 'marketState')"
if [ -z $marketState ]; then
printf 'No results for symbol "%s"\n' $symbol
continue
fi
preMarketChange="$(query $symbol 'preMarketChange')"
postMarketChange="$(query $symbol 'postMarketChange')"
longName="$(query $symbol 'longName')"
#regularMarketPreviousClose="$(query $symbol 'regularMarketPreviousClose')"
if [ $marketState == "PRE" ] \
&& [ $preMarketChange != "0" ] \
&& [ $preMarketChange != "null" ]; then
nonRegularMarketSign='*'
price=$(query $symbol 'preMarketPrice')
diff=$preMarketChange
percent=$(query $symbol 'preMarketChangePercent')
elif [ $marketState != "REGULAR" ] \
&& [ $postMarketChange != "0" ] \
&& [ $postMarketChange != "null" ]; then
nonRegularMarketSign='*'
price=$(query $symbol 'postMarketPrice')
diff=$postMarketChange
percent=$(query $symbol 'postMarketChangePercent')
else
nonRegularMarketSign=''
price=$(query $symbol 'regularMarketPrice')
diff=$(query $symbol 'regularMarketChange')
percent=$(query $symbol 'regularMarketChangePercent')
previous=$(query $symbol 'regularMarketPreviousClose')
fi
if [ "$diff" == "0" ] || [ "$diff" == "0.0" ]; then
color=
elif ( echo "$diff" | grep -q ^- ); then
color=$COLOR_RED
else
color=$COLOR_GREEN
fi
if [ "$price" != "null" ]; then
#printf "%-10s$COLOR_BOLD%8.2f$COLOR_RESET" $symbol $price
printf "%-10s%-35s$COLOR_BOLD%8.2f$COLOR_RESET" $symbol "$longName" $price
printf "$color%10.2f%12s$COLOR_RESET%+10s" $diff $(printf "(%.2f%%)" $percent) $previous
printf " %s\n" "$nonRegularMarketSign"
fi
done