This commit is contained in:
2024-01-09 18:27:02 +01:00
parent 14ac62e43f
commit ab75694176
3 changed files with 118 additions and 70 deletions

View File

@@ -6,13 +6,11 @@
Based from [ticker.sh](https://github.com/pstadler/ticker.sh)
![ticker.sh](https://raw.githubusercontent.com/pstadler/ticker.sh/master/screenshot.png)
![stocks.sh](https://gitea.maboiteverte.fr/shell/stocks_mbv/raw/branch/master/screenshot.jpg)
![stocks.sh](https://gitea.maboiteverte.fr/shell/stocks_mbv/src/commit/c8e904fa4e3594ab8f878565faf61062d6401e6f/screenshot.jpg)
## Install
@@ -23,10 +21,40 @@ $ curl -o ticker.sh https://raw.githubusercontent.com/pstadler/ticker.sh/master/
## Requirements
- [jq](https://stedolan.github.io/jq/), a versatile command-line JSON processor.
- [yq](https://github.com/kislyuk/yq), a versatile command-line YAML processor (Python version).
- [yq](https://github.com/mikefarah/yq/), a versatile command-line YAML processor.
## Usage
#### Preferences:
```bash
# Preference file:
$ nano ~/.stocks.yaml
watchlist:
- ^FCHI
- AC.PA
- ALCLS.PA
- ATO.PA
- AUB.PA
- CS.PA
- DG.PA
- ORA.PA
- VIRP.PA
- WLN.PA
lots:
- symbol: "AUB.PA"
quantity: 600
unit_cost: 32.164
- symbol: "ORA.PA"
quantity: 700
unit_cost: 9.426
```
#### Running:
```sh
# Single symbol:
$ ./ticker.sh AAPL
@@ -34,23 +62,16 @@ $ ./ticker.sh AAPL
# Multiple symbols:
$ ./ticker.sh AAPL MSFT GOOG BTC-USD
# Read from file:
$ echo "AAPL MSFT GOOG BTC-USD" > ~/.ticker.conf
$ ./ticker.sh $(cat ~/.ticker.conf)
# Run stocks.sh:
$ ./stocks.sh
# Use different colors:
$ COLOR_BOLD="\e[38;5;248m" \
COLOR_GREEN="\e[38;5;154m" \
COLOR_RED="\e[38;5;202m" \
./ticker.sh AAPL
# Disable colors:
$ NO_COLOR=1 ./ticker.sh AAPL
# Update every five seconds:
$ watch -n 5 -t -c ./ticker.sh AAPL MSFT GOOG BTC-USD
# Or if `watch` is not available:
$ while true; do clear; ./ticker.sh AAPL MSFT GOOG BTC-USD; sleep 5; done
$ while true; do clear; ./ticker.sh; sleep 5; done
# Or
./run_stocks.sh
```

View File

@@ -1,5 +1,18 @@
#!/usr/bin/env bash
refresh="60"
refresh="600"
sleep-with-countdown() {
secs=$1
while [ $secs -gt 0 ]; do
printf "\rProchaine dans $secs\033[0K s"
sleep 1
: $((secs--))
done
printf "\n"
}
# sleep-with-countdown $refresh;
#while true; do clear; ./stocks.sh $(cat ~/.ticker.conf); sleep $refresh; done
while true; do clear; ./stocks.sh; sleep $refresh; done
while true; do clear; ./stocks.sh $(cat ~/.ticker.conf); sleep $refresh; done

View File

@@ -14,6 +14,8 @@ reset="\033[0m"
LANG=C
LC_NUMERIC=C
stocks_list=~/.stocks.yaml
query() {
echo "$obj" | jq -r ".$1"
}
@@ -36,18 +38,24 @@ fi
# Read stocks from .stocks.yaml config file
a=$(cat ~/.stocks.yaml | yq .watchlist)
b=$(cat ~/.stocks.yaml | yq .lots)
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=$(echo "$obj" | jq -r)
stock=${obj:2}
list+="$stock,"
done < <(echo "$a" | jq -c '.[]')
done < <(echo "$a")
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
@@ -61,7 +69,6 @@ fi
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')
#echo "$results"
# Display stocks data
@@ -82,7 +89,7 @@ while IFS= read -r obj; do
fi
if [ $t == "0" ]; then
printf "${bold}%-10s | %-23s | %-7s | " "Symbole" "Entreprise" "Cours"
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"
@@ -116,13 +123,16 @@ while IFS= read -r obj; do
fi
# Stocks owned
z=$(jq -c '.[] | select(.symbol == "'${symbol}'")' <<< $b)
#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=$(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)
@@ -154,14 +164,18 @@ while IFS= read -r obj; do
# Displaying
if [ "$price" != "null" ]; then
printf "${color}%-10s | %-23s | ${reset}${cours_color}%7.2f${reset}${color} | " $symbol "$longName" $price
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 ""
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}"