added column "gain"

shellfolio now displays gains and losses of each coin and in total
This commit is contained in:
Michael Clemens 2018-01-28 19:21:20 +01:00 committed by GitHub
parent 8c51a01c0d
commit d5b35b606d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 13 deletions

View File

@ -5,9 +5,11 @@
minus=$(tput setaf 1) minus=$(tput setaf 1)
plus=$(tput setaf 2) plus=$(tput setaf 2)
neutral=$(tput sgr0) neutral=$(tput sgr0)
colwidths="$neutral%6s %15s %8s %8s %8s %12s %12s\n" colwidths="$neutral%5s %14s %7s %7s %7s %12s %12s %12s\n"
total=0 totalvalue=0
totalgain=0
# checks if value is positive or negative, returns color code
colorize() { colorize() {
if [[ $1 == *"-"* ]]; then if [[ $1 == *"-"* ]]; then
color=$minus color=$minus
@ -17,12 +19,14 @@ colorize() {
echo $color echo $color
} }
# print table header
printf "${colwidths}" "Coin" "USD" "+/- 1h" "+/- 24h" "+/- 7d" "Amount" "Value" "Gain"
printf "${colwidths}" "-----" "--------------" "-------" "-------" "-------" "------------" "------------" "------------"
printf "${colwidths}" "Symbol" "USD" "+/- 1h" "+/- 24h" "+/- 7d" "Amount" "Value" # print one line per coin
printf "${colwidths}" "------" "---------------" "--------" "--------" "--------" "------------" "------------"
for coin in $COINS for coin in $COINS
do do
# fetch data from coinmarketcap.com
result=$(curl -s -XGET "https://api.coinmarketcap.com/v1/ticker/$coin/?convert=usd") result=$(curl -s -XGET "https://api.coinmarketcap.com/v1/ticker/$coin/?convert=usd")
if [[ $currency != *"error"* ]]; then if [[ $currency != *"error"* ]]; then
symbol=$(echo $result | jq ".[0].symbol" | xargs printf "%s\n") symbol=$(echo $result | jq ".[0].symbol" | xargs printf "%s\n")
@ -30,27 +34,45 @@ do
change24h=$(echo $result | jq ".[0].percent_change_24h" | xargs printf "%.*f\n" 2) change24h=$(echo $result | jq ".[0].percent_change_24h" | xargs printf "%.*f\n" 2)
change7d=$(echo $result | jq ".[0].percent_change_7d" | xargs printf "%.*f\n" 2) change7d=$(echo $result | jq ".[0].percent_change_7d" | xargs printf "%.*f\n" 2)
usd=$(echo $result | jq ".[0].price_usd" | xargs printf "%.*f\n" 8) usd=$(echo $result | jq ".[0].price_usd" | xargs printf "%.*f\n" 8)
# set color to green or red
col1h=$(colorize $change1h) col1h=$(colorize $change1h)
col24h=$(colorize $change24h) col24h=$(colorize $change24h)
col7d=$(colorize $change7d) col7d=$(colorize $change7d)
# check if $symbol is set, split into $amount and $paid
if [ -z ${!symbol} ]; then if [ -z ${!symbol} ]; then
amount=0 amount=0
paid=0
else else
amount=${!symbol} IFS=: read -r amount paid <<< "${!symbol}"
fi fi
# calculates value of coins
value=$(echo "scale=2; $amount*$usd" | bc) value=$(echo "scale=2; $amount*$usd" | bc)
usd=$(echo "scale=2; $usd" | bc) usd=$(echo "scale=2; $usd" | bc)
total=$(echo "scale=2; $value+$total" | bc) totalvalue=$(echo "scale=2; $value+$totalvalue" | bc)
printf "$neutral%6s %15.6f $col1h%8s $col24h%8s $col7d%8s $neutral%12s %12.2f\n" "$symbol" "$usd" "$change1h%" "$change24h%" "$change7d%" "$amount" "$value" # calculates gain
if [ "$paid" -eq "0" ]; then
gain=0
else
gain=$(echo "scale=2; $value-$paid" | bc)
fi
colgain=$(colorize $gain)
totalgain=$(echo "scale=2; $gain+$totalgain" | bc)
# prints actual table line
printf "$neutral%5s %14.6f $col1h%7s $col24h%7s $col7d%7s $neutral%12s %12.2f $colgain%12.2f$neutral\n" "$symbol" "$usd" "$change1h%" "$change24h%" "$change7d%" "$amount" "$value" "$gain"
fi fi
done done
printf "${colwidths}" "------" "---------------" "--------" "--------" "--------" "------------" "------------" # prints table footer
printf "%75.2f\n" "$total" printf "${colwidths}" "-----" "--------------" "-------" "-------" "-------" "------------" "------------" "------------"
#prints total coin value and total gain
colgain=$(colorize $totalgain)
printf "%70.2f $colgain%12.2f$neutral\n" "$totalvalue" "$totalgain"
exit 0 exit 0