fixes for watch_assets.sh

This commit is contained in:
olli 2024-02-06 12:36:27 +01:00
parent 535dff8d0a
commit 1e3c389e90
4 changed files with 83 additions and 43 deletions

View File

@ -48,7 +48,7 @@ do
then then
rm -f firstloop rm -f firstloop
else else
LOOP_INTERVAL=60 # 60s max free coinhecko API interval LOOP_INTERVAL=30 # 60s max free coingecko API interval + 30s puffer
time_to_interval=$((${LOOP_INTERVAL} - $(date +%s) % ${LOOP_INTERVAL})) time_to_interval=$((${LOOP_INTERVAL} - $(date +%s) % ${LOOP_INTERVAL}))
g_echo_note "NEXT LOOP - sleeping ${time_to_interval} seconds until next full ${LOOP_INTERVAL} seconds" g_echo_note "NEXT LOOP - sleeping ${time_to_interval} seconds until next full ${LOOP_INTERVAL} seconds"
sleep ${time_to_interval} sleep ${time_to_interval}

View File

@ -1,14 +1,22 @@
function get_coingecko_data { function get_coingecko_data {
# get data from coingecko # get data from coingecko
echo "curl -s -X 'GET' \"https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=250&page=1&sparkline=false&price_change_percentage=1h,24h,7d,14d,30d,1y\" -H 'accept: application/json'" >COINGECKO_GET_ASSETS_CMD local f_gecko_currencies="usd eur"
g_runcmd g_retrycmd sh COINGECKO_GET_ASSETS_CMD >COINGECKO_GET_ASSETS_CMD_OUT_TMP || return 1 if find COINGECKO_GET_ASSETS_CMD_OUT -mmin +5 | grep -q COINGECKO_GET_ASSETS_CMD_OUT
local f_test_query=$(jq -r ".[] |select(.symbol==\"btc\")|\"\\(.current_price)\"" COINGECKO_GET_ASSETS_CMD_OUT_TMP)
if g_num_valid_number ${f_test_query}
then then
mv COINGECKO_GET_ASSETS_CMD_OUT_TMP COINGECKO_GET_ASSETS_CMD_OUT for f_gecko_currency in ${f_gecko_currencies}
else do
find COINGECKO_GET_ASSETS_CMD_OUT -mmin +${LOOP_INTERVAL} && g_echo_warn "Coingecko data older then ${LOOP_INTERVAL}s: $(ls -l COINGECKO_GET_ASSETS_CMD_OUT)" echo "curl -s -X 'GET' \"https://api.coingecko.com/api/v3/coins/markets?vs_currency=${f_gecko_currency}&order=market_cap_desc&per_page=250&page=1&sparkline=false&price_change_percentage=1h,24h,7d,14d,30d,1y\" -H 'accept: application/json'" >COINGECKO_GET_ASSETS_${f_gecko_currency}_CMD
return 1 g_runcmd g_retrycmd sh COINGECKO_GET_ASSETS_${f_gecko_currency}_CMD >COINGECKO_GET_ASSETS_${f_gecko_currency}_CMD_OUT_TMP || return 1
local f_test_query=$(jq -r ".[] |select(.symbol==\"btc\")|\"\\(.current_price)\"" COINGECKO_GET_ASSETS_${f_gecko_currency}_CMD_OUT_TMP)
if g_num_valid_number ${f_test_query}
then
mv COINGECKO_GET_ASSETS_${f_gecko_currency}_CMD_OUT_TMP COINGECKO_GET_ASSETS_${f_gecko_currency}_CMD_OUT
[[ ${f_gecko_currency} =~ ^usd ]] && cat COINGECKO_GET_ASSETS_${f_gecko_currency}_CMD_OUT >COINGECKO_GET_ASSETS_CMD_OUT
else
find COINGECKO_GET_ASSETS_${f_gecko_currency}_CMD_OUT -mmin +15 | grep -q COINGECKO_GET_ASSETS_${f_gecko_currency}_CMD_OUT && g_echo_warn "Coingecko data older then 15min: $(ls -l COINGECKO_GET_ASSETS_${f_gecko_currency}_CMD_OUT)"
return 1
fi
done
fi fi
[ ${FULL_LOOP} == 0 ] && return 0 [ ${FULL_LOOP} == 0 ] && return 0
@ -38,6 +46,4 @@ function get_coingecko_data {
return 2 return 2
fi fi
fi fi
} }

View File

@ -4,9 +4,25 @@ function watch_assets {
local f_line local f_line
local f_price local f_price
local f_alert local f_alert
local f_alert_html="<h2>Alerts when values are crossed</h2><table>"
local f_portfolio_html="<h2>Portfolio</h2><table>"
local f_last_price local f_last_price
mapfile -t f_watch_assets_array < <(grep -v ^ASSET,ALERTS,BUYPRICE,BUYDATE,BUYQUANTITY,SELLPRICE,SELLDATE,SELLQUANTITY /dabo/watch-assets.csv) local f_html="<html>
<head>
<meta charset='UTF-8'>
<meta http-equiv='refresh' content='${INTERVAL}'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' href='/browser.css'>
<link rel='stylesheet' type='text/css' href='/charts.min.css'>
<title>Dabo WATCH ASSETS - ${URL}</title>
</head>
<body>
<h1>Dabo-Bot WATCH ASSETS - ${URL} (ReadOnly)</h1>
<h1>Last update $(date '+%F %T')</h1>"
mapfile -t f_watch_assets_array < <(egrep -v '^ASSET,ALERTS,BUYPRICE,BUYDATE,BUYQUANTITY,SELLPRICE,SELLDATE,SELLQUANTITY|^#|^$|^ +$' /dabo/watch-assets.csv)
for f_line in "${f_watch_assets_array[@]}" for f_line in "${f_watch_assets_array[@]}"
do do
g_echo "$f_line" g_echo "$f_line"
@ -20,42 +36,59 @@ function watch_assets {
local f_selldate=${f_line_array[7]} local f_selldate=${f_line_array[7]}
local f_sellquantity=${f_line_array[8]} local f_sellquantity=${f_line_array[8]}
local f_comment=${f_line_array[9]} local f_comment=${f_line_array[9]}
local f_currency=${f_line_array[10]}
# get current asset price ild last price if not sold [[ ${f_currency} =~ ^usd ]] && local f_currency_symbol="$"
if [ -z "${f_sellprice}" ] [[ ${f_currency} =~ ^eur ]] && local f_currency_symbol="€"
# get current asset price and last price
f_price=""
if [[ ${f_asset} =~ ^https ]]
then then
if [[ ${f_asset} =~ ^https ]] # get asset price from get_marketdata
then get_marketdata ${f_asset}
# get asset price from get_marketdata f_price=${f_get_marketdata_price}
get_marketdata ${f_asset} readarray -d " " -t f_asset_array < <(echo -n "${f_asset}")
f_price=${f_get_marketdata_price} f_asset=${f_asset_array[1]}
readarray -d " " -t f_asset_array < <(echo -n "${f_asset}") else
f_asset=${f_asset_array[1]} # get token price from coingecko
else f_price=$(jq -r ".[] |select(.symbol==\"${f_asset}\")|\"\\(.current_price)\"" COINGECKO_GET_ASSETS_${f_currency}_CMD_OUT)
# get token price from coingecko fi
f_price=$(jq -r ".[] |select(.symbol==\"${f_asset}\")|\"\\(.current_price)\"" COINGECKO_GET_ASSETS_CMD_OUT) [ -s WATCH_ASSETS_${f_asset}_${f_currency}_LAST_PRICE ] && read f_last_price < <(cat WATCH_ASSETS_${f_asset}_${f_currency}_LAST_PRICE)
fi echo ${f_price} >WATCH_ASSETS_${f_asset}_${f_currency}_LAST_PRICE
[ -s WATCH_ASSETS_${f_asset}_LAST_PRICE ] && read f_last_price < <(cat WATCH_ASSETS_${f_asset}_LAST_PRICE) echo g_num_valid_number ${f_price} ${f_last_price} #|| continue
echo ${f_price} >WATCH_ASSETS_${f_asset}_LAST_PRICE
echo g_num_valid_number ${f_price} ${f_last_price} #|| continue # Notify on alert
readarray -d "|" -t f_alerts_array < <(echo -n "${f_alerts}")
for f_alert in "${f_alerts_array[@]}"
do
f_alert_html+="<tr><td>${f_asset}</td><td>${f_alert}</td></tr>"
if g_num_is_higher ${f_price} ${f_alert} && g_num_is_lower_equal ${f_last_price} ${f_alert}
then
g_signal-notify "${f_asset} Price ${f_price} switched over ${f_alert}! Comment: ${f_comment}"
fi
if g_num_is_lower ${f_price} ${f_alert} && g_num_is_higher_equal ${f_last_price} ${f_alert}
then
g_signal-notify "${f_asset} Price ${f_price} switched under ${f_alert}! Comment: ${f_comment}"
fi
done
# List portfolio
if [ -n "${f_buyprice}" ]
then
g_calc "${f_buyprice}*${f_buyquantity}"
f_buyquantity_price=${g_calc_result}
g_percentage-diff ${f_buyprice} ${f_price}
local f_portfolio_html+="<tr><td>${f_asset} (${f_comment})</td><td>${f_buyquantity_price}${f_currency_symbol} (${f_buyprice}${f_currency_symbol})</td><td>${f_price}${f_currency_symbol}</td><td> ${g_percentage_diff_result}%</td></tr>"
# Notify on alert
readarray -d "|" -t f_alerts_array < <(echo -n "${f_alerts}")
for f_alert in "${f_alerts_array[@]}"
do
if g_num_is_higher_equal ${f_price} ${f_alert} && g_num_is_lower_equal ${f_last_price} ${f_alert}
then
g_signal-notify "${f_asset} Price ${f_price} switched over alert ${f_alert}! Comment: ${f_comment}"
fi
if g_num_is_lower_equal ${f_price} ${f_alert} && g_num_is_higher_equal ${f_last_price} ${f_alert}
then
g_signal-notify "${f_asset} Price ${f_price} switched under alert ${f_alert}! Comment: ${f_comment}"
fi
done
fi fi
done done
f_alert_html+="</table>"
f_portfolio_html+="</table>"
echo ${f_html} ${f_portfolio_html} ${f_alert_html} | perl -pe 's/ (\-[0-9]+\.[0-9]+\%)/<font color=red>$1<\/font>/g; s/ ([0-9]+\.[0-9]+\%)/<font color=green>$1<\/font>/g;' >../watch.html
} }

1
dabo/watch-assets.csv Normal file
View File

@ -0,0 +1 @@
ASSET,ALERTS,BUYPRICE,BUYDATE,BUYQUANTITY,SELLPRICE,SELLDATE,SELLQUANTITY,COMMENT
1 ASSET ALERTS BUYPRICE BUYDATE BUYQUANTITY SELLPRICE SELLDATE SELLQUANTITY COMMENT