Skip to content

Instantly share code, notes, and snippets.

@kkumar-fk
Last active February 11, 2022 03:40
Show Gist options
  • Save kkumar-fk/42695ef65405ef7928be8a6d05e00a4a to your computer and use it in GitHub Desktop.
Save kkumar-fk/42695ef65405ef7928be8a6d05e00a4a to your computer and use it in GitHub Desktop.
Script to calculate your crypto balance, as well as give (50) historical values. Historical values help in deciding whether to invest or not.
#!/bin/bash
# Number of units of each cryptocurrency.
u_bc=0.73674311
u_eth=0.75795629
u_rip=251.065810
# Get prices of each from https://api.coingecko.com/api/v3/coins/list
bc=`curl "https://api.coingecko.com/api/v3/coins/markets?vs_currency=inr&ids=bitcoin" 2> /dev/null | jq . | grep current_price | awk '{print $NF}' | cut -d, -f1 &`
eth=`curl "https://api.coingecko.com/api/v3/coins/markets?vs_currency=inr&ids=ethereum" 2> /dev/null | jq . | grep current_price | awk '{print $NF}' | cut -d, -f1 &`
rip=`curl "https://api.coingecko.com/api/v3/coins/markets?vs_currency=inr&ids=ripple" 2> /dev/null | jq . | grep current_price | awk '{print $NF}' | cut -d, -f1 &`
wait
# Calculate value of each cryptocurrency
bcv=`echo "$u_bc * $bc" | bc -l | cut -d. -f1`
ethv=`echo "$u_eth * $eth" | bc -l | cut -d. -f1`
ripv=`echo "$u_rip * $rip" | bc -l | cut -d. -f1`
# Calculate previous value of each cryptocurrency and percentage change.
if [ -f prev_output.1 ]
then
p_bc=`grep -w "Bitcoin " prev_output.1 | awk '{print $4}'`
p_bcv=`grep -w "Bitcoin " prev_output.1 | awk '{print $6}'`
p_eth=`grep -w "Ethereum " prev_output.1 | awk '{print $4}'`
p_ethv=`grep -w "Ethereum " prev_output.1 | awk '{print $6}'`
p_rip=`grep -w "Ripple " prev_output.1 | awk '{print $4}'`
p_ripv=`grep -w "Ripple " prev_output.1 | awk '{print $6}'`
bc_perc=`echo "scale=1; ($bcv - $p_bcv) * 100 / $p_bcv" | bc -l`
eth_perc=`echo "scale=1; ($ethv - $p_ethv) * 100 / $p_ethv" | bc -l`
rip_perc=`echo "scale=1; ($ripv - $p_ripv) * 100 / $p_ripv" | bc -l`
else
p_bc=0
p_bcv=0
p_eth=0
p_ethv=0
p_rip=0
p_ripv=0
bc_perc=0
eth_perc=0
rip_perc=0
fi
# Let her rip....
(
printf -- "---------------------------------------------------------------------------------------\n"
printf "%-15s%-15s%-10s%-10s%-10s%-10s%-10s%-10s\n" "Coin" "Quantity" "PRate" "Rate" "PValue" "Value" "Change" "%"
printf -- "---------------------------------------------------------------------------------------\n"
printf "%-15s%-15s%-10s%-10s%-10s%-10s%-10s%-10s\n" "Bitcoin" $u_bc $p_bc $bc $p_bcv $bcv $((bcv-p_bcv)) $bc_perc
printf "%-15s%-15s%-10s%-10s%-10s%-10s%-10s%-10s\n" "Ethereum" $u_eth $p_eth $eth $p_ethv $ethv $((ethv-p_ethv)) $eth_perc
printf "%-15s%-15s%-10s%-10s%-10s%-10s%-10s%-10s\n" "Ripple" $u_rip $p_rip $rip $p_ripv $ripv $((ripv-p_ripv)) $rip_perc
printf -- "---------------------------------------------------------------------------------------\n"
total=`echo "$bcv + $ethv + $ripv" | bc -l`
prev_total=`echo "$p_bcv + $p_ethv + $p_ripv" | bc -l`
printf "%-15s = %-15s" "Total" $total
if [ $prev_total -gt 0 ]
then
change=`echo "$total - $prev_total" | bc`
perc=`echo "scale=2; $change * 100 / $prev_total" | bc -l`
printf "%-15s = %-15s" "Prev Total" $prev_total
printf "%-15s = %-15s (%s%%)" "Change" $change $perc
fi
printf "\n"
) | tee prev_output.0
if [ $# -eq 1 ]
then
echo "TEST-RUN: Not saving/preserving files"
rm -f prev_output.0
exit 0
fi
for i in {49..0}
do
if [ -f prev_output.$i ]
then
j=$((i+1))
mv prev_output.$i prev_output.$j
fi
done
echo "------------------ Recent historical prices ------------------"
for i in {2..50}
do
if [ -f prev_output.$i ]
then
ts=`ls -l prev_output.$i | cut -c 40-51`
echo "##### prev_output.$i ($ts) #####"
egrep "Bitcoin|Ethereum|Ripple" prev_output.$i | awk '{printf "%-20s%10s%%\n", $1, $NF}'
fi
done
exit 0
@kkumar-fk
Copy link
Author

kkumar-fk commented Jan 28, 2022

Sample output:

$ ./cryto_summary.sh
---------------------------------------------------------------------------------------
Coin           Quantity       PRate     Rate      PValue    Value     Change    %
---------------------------------------------------------------------------------------
Bitcoin        0.73674311     2706360   2752272   1993892   2027717   33825     1.6
Ethereum       0.75795629     179484    180247    136041    136619    578       .4
Ripple         251.065810     45.11     44.9      11325     11272     -53       -.4
---------------------------------------------------------------------------------------
Total           = 2175608        Prev Total      = 2141258        Change          = 34350           (1.60%)
------------------ Recent historical prices ------------------
##### prev_output.2 (Jan 27 11:48) #####
Bitcoin             .3%
Ethereum            .1%
Ripple              .6%
##### prev_output.3 (Jan 27 10:17) #####
Bitcoin             -5.7%
Ethereum            -8.4%
Ripple              -5.5%
##### prev_output.4 (Jan 26 23:09) #####
Bitcoin             2.1%
Ethereum            6.0%
Ripple              1.5%
##### prev_output.5 (Jan 26 13:19) #####
Bitcoin             -1.0%
Ethereum            -1.0%
Ripple              -.7%
##### prev_output.6 (Jan 26 11:23) #####
Bitcoin             2.5%
Ethereum            2.0%
Ripple              3.4%
##### prev_output.7 (Jan 25 22:18) #####
Bitcoin             1.5%
Ethereum            1.0%
Ripple              1.1%
##### prev_output.8 (Jan 25 17:40) #####
Bitcoin             .1%
Ethereum            .7%
Ripple              .5%
##### prev_output.11 (Jan 25 09:43) #####
Bitcoin             .9%
Ethereum            1.2%
Ripple              .4%
##### prev_output.12 (Jan 25 07:14) #####
Bitcoin             4.7%
Ethereum            5.7%
Ripple              3.7%
##### prev_output.13 (Jan 24 22:41) #####
Bitcoin             2.3%
Ethereum            2.1%
Ripple              1.7%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment