This commit is contained in:
olli 2023-10-20 17:40:38 +02:00
parent feef4f1cdc
commit 55ec9602c8

View File

@ -1,63 +0,0 @@
function get_rate_percentage_min_before_and_now {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
# get vars
local f_asset=$1
local f_currency=$2
local f_min=$3
# Optional time from min before sould be calculated (to be able to calculate from 5 min before to 10 minutes before)
local f_min_from=$4
# clean up global variable (maybe used in runs before)
f_exchange_rate_diff_percentage=""
local f_asset_hist_file="asset-histories/${f_asset}${f_currency}.history.csv"
if [ -s "f_asset_hist_file" ]
then
g_echo_note "${f_asset}: No or empty histfile(${f_asset_hist_file}) found"
return 1
fi
# prepare date
if [ -n "${f_min_from}" ]
then
local f_min_date_from=$(date "+%F %H:%M:" --date="-${f_min_from} minutes")
f_min=$((f_min+f_min_from))
else
local f_min_date_from=$(date "+%F %H:%M:")
fi
local f_min_date=$(date "+%F %H:%M:" --date="-${f_min} minutes")
if [ -z "${f_min_date}" ]
then
g_echo_warn "${f_asset}: Error while calculating date ${f_min} before"
return 1
fi
# get current exchange rate
local f_current_exchange_rate=$(egrep "^${f_min_date_from}" ${f_asset_hist_file} | cut -d, -f2)
if [ -z "${f_current_exchange_rate}" ]
then
g_echo_note "${f_asset}: From rate $f_min_from ${f_min_date_from} not found in ${f_asset_hist_file}"
return 1
fi
# get exchange rate defined minutes before
local f_min_before_exchange_rate=$(egrep "^${f_min_date}" ${f_asset_hist_file} | cut -d, -f2)
if [ -z "${f_min_before_exchange_rate}" ]
then
g_echo_note "${f_asset}: Rate ${f_min} minutes ago not found in ${f_asset_hist_file}"
return 2
fi
# calculate difference in percentage between current rate and rate defined minutes before
f_exchange_rate_diff_percentage=$(g_percentage-diff ${f_min_before_exchange_rate} ${f_current_exchange_rate})
if [ -z "$f_exchange_rate_diff_percentage" ]
then
g_echo_warn "${f_asset}: Calculating difference between rate ${f_min} minutes ago (${min_before_exchange_rate}) and current (${m_current_exchange_rate}) in percentage failed"
return 3
fi
# give back percentage
g_echo_note "${f_asset}: difference from ${f_current_exchange_rate} from ${f_min_date_from} and ${f_min_date} ${f_min_before_exchange_rate}: ${f_exchange_rate_diff_percentage}%"
return 0
}