Last active
February 11, 2022 03:40
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output: