From 1e3c389e902808dc274916c949edd980bb9b71ca Mon Sep 17 00:00:00 2001 From: olli Date: Tue, 6 Feb 2024 12:36:27 +0100 Subject: [PATCH] fixes for watch_assets.sh --- dabo/dabo-bot.sh | 2 +- dabo/functions/get_coingecko_data.sh | 26 +++++--- dabo/functions/watch_assets.sh | 97 +++++++++++++++++++--------- dabo/watch-assets.csv | 1 + 4 files changed, 83 insertions(+), 43 deletions(-) create mode 100644 dabo/watch-assets.csv diff --git a/dabo/dabo-bot.sh b/dabo/dabo-bot.sh index 90982ce..4d3ad7b 100755 --- a/dabo/dabo-bot.sh +++ b/dabo/dabo-bot.sh @@ -48,7 +48,7 @@ do then rm -f firstloop 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})) g_echo_note "NEXT LOOP - sleeping ${time_to_interval} seconds until next full ${LOOP_INTERVAL} seconds" sleep ${time_to_interval} diff --git a/dabo/functions/get_coingecko_data.sh b/dabo/functions/get_coingecko_data.sh index dc1ea94..72a4cd6 100644 --- a/dabo/functions/get_coingecko_data.sh +++ b/dabo/functions/get_coingecko_data.sh @@ -1,14 +1,22 @@ function get_coingecko_data { # 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 - g_runcmd g_retrycmd sh COINGECKO_GET_ASSETS_CMD >COINGECKO_GET_ASSETS_CMD_OUT_TMP || return 1 - 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} + local f_gecko_currencies="usd eur" + if find COINGECKO_GET_ASSETS_CMD_OUT -mmin +5 | grep -q COINGECKO_GET_ASSETS_CMD_OUT then - mv COINGECKO_GET_ASSETS_CMD_OUT_TMP COINGECKO_GET_ASSETS_CMD_OUT - else - 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)" - return 1 + for f_gecko_currency in ${f_gecko_currencies} + do + 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 + 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 [ ${FULL_LOOP} == 0 ] && return 0 @@ -38,6 +46,4 @@ function get_coingecko_data { return 2 fi fi - - } diff --git a/dabo/functions/watch_assets.sh b/dabo/functions/watch_assets.sh index e4b6f6d..3ef7ba9 100644 --- a/dabo/functions/watch_assets.sh +++ b/dabo/functions/watch_assets.sh @@ -4,9 +4,25 @@ function watch_assets { local f_line local f_price local f_alert + local f_alert_html="

Alerts when values are crossed

" + local f_portfolio_html="

Portfolio

" 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=" + + + + + + + Dabo WATCH ASSETS - ${URL} + + +

Dabo-Bot WATCH ASSETS - ${URL} (ReadOnly)

+

Last update $(date '+%F %T')

" + + + 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[@]}" do g_echo "$f_line" @@ -20,42 +36,59 @@ function watch_assets { local f_selldate=${f_line_array[7]} local f_sellquantity=${f_line_array[8]} local f_comment=${f_line_array[9]} + local f_currency=${f_line_array[10]} - # get current asset price ild last price if not sold - if [ -z "${f_sellprice}" ] + [[ ${f_currency} =~ ^usd ]] && local f_currency_symbol="$" + [[ ${f_currency} =~ ^eur ]] && local f_currency_symbol="€" + + # get current asset price and last price + f_price="" + if [[ ${f_asset} =~ ^https ]] then - if [[ ${f_asset} =~ ^https ]] - then - # get asset price from get_marketdata - get_marketdata ${f_asset} - f_price=${f_get_marketdata_price} - readarray -d " " -t f_asset_array < <(echo -n "${f_asset}") - f_asset=${f_asset_array[1]} - else - # get token price from coingecko - f_price=$(jq -r ".[] |select(.symbol==\"${f_asset}\")|\"\\(.current_price)\"" COINGECKO_GET_ASSETS_CMD_OUT) - fi - [ -s WATCH_ASSETS_${f_asset}_LAST_PRICE ] && read f_last_price < <(cat WATCH_ASSETS_${f_asset}_LAST_PRICE) - echo ${f_price} >WATCH_ASSETS_${f_asset}_LAST_PRICE - echo g_num_valid_number ${f_price} ${f_last_price} #|| continue + # get asset price from get_marketdata + get_marketdata ${f_asset} + f_price=${f_get_marketdata_price} + readarray -d " " -t f_asset_array < <(echo -n "${f_asset}") + f_asset=${f_asset_array[1]} + else + # get token price from coingecko + f_price=$(jq -r ".[] |select(.symbol==\"${f_asset}\")|\"\\(.current_price)\"" COINGECKO_GET_ASSETS_${f_currency}_CMD_OUT) + fi + [ -s WATCH_ASSETS_${f_asset}_${f_currency}_LAST_PRICE ] && read f_last_price < <(cat WATCH_ASSETS_${f_asset}_${f_currency}_LAST_PRICE) + echo ${f_price} >WATCH_ASSETS_${f_asset}_${f_currency}_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 - 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 + # Notify on alert + readarray -d "|" -t f_alerts_array < <(echo -n "${f_alerts}") + for f_alert in "${f_alerts_array[@]}" + do + f_alert_html+="" + 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+="" + fi - - done + + f_alert_html+="
${f_asset}${f_alert}
${f_asset} (${f_comment})${f_buyquantity_price}${f_currency_symbol} (${f_buyprice}${f_currency_symbol})${f_price}${f_currency_symbol} ${g_percentage_diff_result}%
" + f_portfolio_html+="" + echo ${f_html} ${f_portfolio_html} ${f_alert_html} | perl -pe 's/ (\-[0-9]+\.[0-9]+\%)/$1<\/font>/g; s/ ([0-9]+\.[0-9]+\%)/$1<\/font>/g;' >../watch.html + } diff --git a/dabo/watch-assets.csv b/dabo/watch-assets.csv new file mode 100644 index 0000000..86afd13 --- /dev/null +++ b/dabo/watch-assets.csv @@ -0,0 +1 @@ +ASSET,ALERTS,BUYPRICE,BUYDATE,BUYQUANTITY,SELLPRICE,SELLDATE,SELLQUANTITY,COMMENT