dabo/functions/market_performance.sh

93 lines
5.0 KiB
Bash
Raw Normal View History

2023-04-28 17:09:15 +02:00
function market_performance {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
## function for scoring limits set in config up or down by specific (market) facts
# generates variable f_market_performance
# forecast bitcoin (is quartered because uncertain)
f_url="https://30rates.com/btc-to-usd-forecast-today-dollar-to-bitcoin"
[ -e btc-forecast ] && find btc-forecast -mmin +60 -delete
if ! [ -s btc-forecast ]
then
g_runcmd g_retrycmd curl -s "$f_url" >btc-forecast || return 1
fi
local f_forecast=$(cat btc-forecast | hxnormalize -x | hxselect 'table' | grep "<strong>" | grep -A1 "Price" -m1 | tail -n1 | cut -d'>' -f3 | cut -d'<' -f1)
if ! echo "$f_forecast" | egrep -q '^[0-9]*\.[0-9]*$|^[0-9]*$'
then
g_echo_warn "Didn't get correct forecast from $f_url"
return 1
fi
local f_today=$(cat btc-forecast | hxnormalize -x | hxselect 'table' | egrep '<strong>' | head -n1 | cut -d'>' -f3 | cut -d'<' -f1)
if ! echo "$f_today" | egrep -q '^[0-9]*\.[0-9]*$|^[0-9]*$'
then
g_echo_warn "Didn't get correct forecast from $f_url"
return 1
fi
local f_btc_forecast=$(g_percentage-diff ${f_today} ${f_forecast})
echo $f_btc_forecast | egrep -q '[0-9]\.[0-9]' || return 1
g_echo_note "BTC forecast: $f_btc_forecast"
f_btc_forecast=$(echo "scale=2; ${f_btc_forecast}/3" | bc -l | sed -r 's/^(-?)\./\10./')
# forecast ethereum (is quartered because uncertain)
local f_url="https://30rates.com/ethereum-price-prediction-tomorrow-week-month-eth-forecast"
[ -e eth-forecast ] && find eth-forecast -mmin +60 -delete
if ! [ -s eth-forecast ]
then
g_runcmd g_retrycmd curl -s "$f_url" >eth-forecast || return 1
fi
f_forecast=$(cat eth-forecast | hxnormalize -x | hxselect 'table' | grep "<strong>" | grep -A1 "Price" -m1 | tail -n1 | cut -d'>' -f3 | cut -d'<' -f1)
if ! echo "$f_forecast" | egrep -q '^[0-9]*\.[0-9]*$|^[0-9]*$'
then
g_echo_warn "Didn't get correct forecast $f_forecast from $f_url"
return 1
fi
f_today=$(cat eth-forecast | hxnormalize -x | hxselect 'table' | egrep '<strong>' | head -n1 | cut -d'>' -f3 | cut -d'<' -f1)
if ! echo "$f_today" | egrep -q '^[0-9]*\.[0-9]*$|^[0-9]*$'
then
g_echo_warn "Didn't get correct forecast from $f_url"
return 1
fi
local f_eth_forecast=$(g_percentage-diff ${f_today} ${f_forecast})
echo $f_eth_forecast | egrep -q '[0-9]\.[0-9]' || return 1
g_echo_note "ETH forecast: $f_eth_forecast"
f_eth_forecast=$(echo "scale=2; ${f_eth_forecast}/3" | bc -l | sed -r 's/^(-?)\./\10./')
# price performance msci world (last week and halved because of the long time)
#local f_back=240
#[[ $(date +%u) -eq 6 ]] && f_back=1500
#[[ $(date +%u) -eq 7 ]] && f_back=2940
local f_from=$(tail -n 10080 asset-histories/MSCI-WORLD-INDEX.history.csv | grep -v ^[A-Z] | head -n1 | cut -d, -f2)
local f_to=$(tail -n 1 asset-histories/MSCI-WORLD-INDEX.history.csv | cut -d, -f2)
local f_exchange_rate_diff_percentage=$(g_percentage-diff ${f_from} ${f_to})
#local f_back=10080
#get_rate_percentage_min_before_and_now MSCI-WORLD- INDEX $f_back || return 1
local f_msci_world_performance=$(echo "scale=2; ${f_exchange_rate_diff_percentage}/2" | bc -l | sed -r 's/^(-?)\./\10./')
# price performance bitcoin (last 30 minutes)
local f_from=$(tail -n 30 asset-histories/BTC${CURRENCY}.history.csv | grep -v ^[A-Z] | head -n1 | cut -d, -f2)
local f_to=$(tail -n 1 asset-histories/BTC${CURRENCY}.history.csv | cut -d, -f2)
local f_exchange_rate_diff_percentage=$(g_percentage-diff ${f_from} ${f_to})
#get_rate_percentage_min_before_and_now BTC ${CURRENCY} 360 || return 1
local f_btc_performance=${f_exchange_rate_diff_percentage}
# price performance ethereum (last 30 minutes)
#get_rate_percentage_min_before_and_now ETH ${CURRENCY} 360 || return 1
local f_from=$(tail -n 30 asset-histories/ETH${CURRENCY}.history.csv | grep -v ^[A-Z] | head -n1 | cut -d, -f2)
local f_to=$(tail -n 1 asset-histories/ETH${CURRENCY}.history.csv | cut -d, -f2)
local f_exchange_rate_diff_percentage=$(g_percentage-diff ${f_from} ${f_to})
local f_eth_performance=${f_exchange_rate_diff_percentage}
# hourly price performance over TOP 250 marketcap by coingecko
local f_top250_marketcap_performance=$(jq -r ".[].price_change_percentage_1h_in_currency" COINGECKO_GET_ASSETS_CMD_OUT | awk '{ SUM += $1} END { printf("%.2f", SUM/250) }')
## calculate market performance
f_market_performance=$(echo "scale=2; (${f_btc_forecast} + ${f_eth_forecast} + ${f_msci_world_performance} + ${f_btc_performance} + ${f_eth_performance} + ${f_top250_marketcap_performance})/6" | bc -l | sed -r 's/^(-?)\./\10./')
local f_date=$(g_date_print)
echo "${f_date} Market Performance: ${f_market_performance}%; BTC forecast: ${f_btc_forecast}%; ETH forecast: ${f_eth_forecast}%; MSCI WORLD: ${f_msci_world_performance}%; BTC: ${f_btc_performance}%; ETH: ${f_eth_performance}%; TOP250 Marketcap: ${f_top250_marketcap_performance}%" >>MARKET_PERFORMANCE
echo -n "${f_market_performance}" >MARKET_PERFORMANCE_LATEST
}