Rename stocks.sh to stocks_legacy

as stocks.sh no longer work (API finance Yahoo HS)
This commit is contained in:
2025-01-13 20:32:43 +01:00
parent ab75694176
commit d381eb8043

185
stocks_legacy.sh Executable file
View File

@@ -0,0 +1,185 @@
#!/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
stocks_list=~/.stocks.yaml
query() {
echo "$obj" | jq -r ".$1"
}
round() {
echo $(printf %.$2f $(echo "scale=$2;(((10^$2)*$1)+0.5)/(10^$2)" | bc))
}
# Test if jq & yq executables are in $PATH
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 ! $(type yq >/dev/null 2>&1); then
echo "'yq' is not in the PATH. (See: https://github.com/kislyuk/yq"
exit 1
fi
# Read stocks from .stocks.yaml config file
if [ -f $stocks_list ]; then
#a=$(cat ~/.stocks.yaml | yq '.watchlist')
a=$(cat $stocks_list | yq '.watchlist')
b=$(cat $stocks_list | yq '.lots')
else
echo "$stocks_list not present !"
echo ""
fi
while IFS= read -r obj; do
stock=${obj:2}
list+="$stock,"
done < <(echo "$a")
symbols=$(echo "$list" | sed 's/.$//')
# AC.PA ATO.PA AUB.PA CS.PA DG.PA ^FCHI VIRP.PA WLN.PA
#####
if [ -z "$symbols" ]; then
echo "Usage: ./stocks_legacy.sh"
echo " - add stocks to ~/.stocks.yaml file"
exit
fi
# Get stocks data from yahoo finance
API_ENDPOINT="https://query1.finance.yahoo.com/v7/finance/quote?lang=fr-FR&region=FR&corsDomain=finance.yahoo.com"
results=$(curl --silent "$API_ENDPOINT&fields=$fields&symbols=$symbols" | jq '.quoteResponse .result')
# Display stocks data
t=0
echo -e "${bold}stocks_legacy.sh${reset}\n"
#query() {
# echo "$obj" | jq -r ".$1"
#}
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 | %-21s | %-7s | " "Symbole" "Entreprise" "Cours"
printf "%-6s | %-7s | %-7s | " "Diff" "%" "Veille"
printf "%-7s | %-7s | %-8s | " "+ Haut" "+ Bas" "Volume"
printf "%-9s | %-25s | %-18s | " "Ouverture" "Heure" "52 semaines"
printf "%-7s | %-9s | %-9s | %-6s${reset}\n" "Qté" "Valoris." "+/- value" "Perf."
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='' #nok
price=$(query 'regularMarketPrice')
diff=$(query 'regularMarketChange') #nok
percent=$(query 'regularMarketChangePercent') #nok
previous=$(query 'regularMarketPreviousClose') #nok
volume=$(query 'regularMarketVolume')
haut=$(query 'regularMarketDayHigh')
bas=$(query 'regularMarketDayLow')
ouverture=$(query 'regularMarketOpen') #nok
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') #nok
fi
# Stocks owned
#z=$(jq -c '.[] | select(.symbol == "'${symbol}'")' <<<$b)
z=$(yq '.[] | select(.symbol == "'${symbol}'")' <<< "$b")
if [ -n "$z" ]; then
#echo "$z"
#quantity=$(jq -c '.[] | select(.symbol == "'${symbol}'") | .quantity' <<< $b)
#quantity=$(jq -c '.quantity' <<<$z)
#unit_cost=$(jq -c '.unit_cost' <<<$z)
quantity=$(yq '.quantity' <<<$z)
unit_cost=$(yq '.unit_cost' <<<$z)
purchase=$(echo "$quantity * $unit_cost" | bc -l)
valuations=$(echo "$quantity * $price" | bc -l)
profit=$(echo "$valuations - $purchase" | bc -l)
performance=$(echo "($valuations / $purchase - 1) * 100" | bc -l)
performance=$(round $performance 2)
#valuation=$(($quantity * $price))
#profit=$((valuations-purchase))
#profit=$(($valuations-$purchase))
#profit=$(($purchase-$valuations))
#((profit = valuations - purchase))
#echo "Qte: $quantity - PM: $unit_cost - Achat: $purchase - Valorisation: $valuations"
#echo "+-value: $profit - Perf: $performance%"
fi
# Lines colors: + green ; - red ; 0 no color
if [ "$diff" == "0" ] || [ "$diff" == "0.0" ]; then
color=
elif (echo "$diff" | grep -q ^-); then
color=$red
#cours_color="\033[3;31m"
cours_color="\033[1;37;1;41m"
else
color=$green
#cours_color="\033[3;32m"
cours_color="\033[1;37;1;42m"
fi
# Displaying
if [ "$price" != "null" ]; then
printf "${color}%-10s | %-21s | ${reset}${cours_color}%7.2f${reset}${color} | " $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 | " $ouverture "$heure" "$ftweeks"
if [ -n "$z" ]; then
printf "%6d | %9.2f | %9.2f | % 6.2f%%${reset}\n" $quantity $valuations $profit $performance
else
echo ""
fi
fi
done < <(echo "$results" | jq -c '.[]')
d=$(LC_ALL=fr_FR.UTF-8 TZ='Europe/Paris' date +"%c")
echo -e "\n${reset}Dernière mise-à-jour: ${bold}$d${reset}"