diff --git a/stocks.sh b/stocks.sh index 6c81573..139af79 100755 --- a/stocks.sh +++ b/stocks.sh @@ -14,32 +14,56 @@ 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 +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 [ -z "$SYMBOLS" ]; then - echo "Usage: ./ticker.sh AAPL MSFT GOOG BTC-USD" - exit +if ! $(type yq > /dev/null 2>&1); then + echo "'yq' is not in the PATH. (See: https://github.com/kislyuk/yq" + exit 1 fi -API_ENDPOINT="https://query1.finance.yahoo.com/v7/finance/quote?lang=fr-FR®ion=FR&corsDomain=finance.yahoo.com" +# Read stocks from .stocks.yaml config file -symbols=$(IFS=,; echo "${SYMBOLS[*]}") +a=$(cat ~/.stocks.yaml | yq .watchlist) +b=$(cat ~/.stocks.yaml | yq .lots) + +while IFS= read -r obj; do + stock=$(echo "$obj" | jq -r) + list+="$stock," +done < <(echo "$a" | jq -c '.[]') + +symbols=$(echo "$list" | sed 's/.$//') #echo "$symbols" +# AC.PA ATO.PA AUB.PA CS.PA DG.PA ^FCHI VIRP.PA WLN.PA +##### + +if [ -z "$symbols" ]; then + echo "Usage: ./stocks.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®ion=FR&corsDomain=finance.yahoo.com" + results=$(curl --silent "$API_ENDPOINT&fields=$fields&symbols=$symbols" | jq '.quoteResponse .result') #echo "$results" -query () { - echo "$obj" | jq -r ".$1" -} - +# Display stocks data t=0 @@ -61,7 +85,8 @@ while IFS= read -r obj; do 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" + printf "%-9s | %-25s | %-18s | " "Ouverture" "Heure" "52 semaines" + printf "%-7s | %-9s | %-9s | %-6s${reset}\n" "Qté" "Valoris." "+/- value" "Perf." fi t=1 @@ -89,20 +114,54 @@ while IFS= read -r obj; do 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 + + # Stocks owned + z=$(jq -c '.[] | 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) + 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 | %-23s | %7.2f | " $symbol "$longName" $price + printf "${color}%-10s | %-23s | ${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${reset}\n" $ouverture "$heure" "$ftweeks" + 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 '.[]')