From fdb130dc3a50ff7cd7bb299ada2d0881406bfa54 Mon Sep 17 00:00:00 2001 From: olli Date: Sun, 26 May 2024 09:57:59 +0200 Subject: [PATCH] convert all currencies (via historic data in past too) --- dabo/functions/currency_converter.sh | 118 +++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 dabo/functions/currency_converter.sh diff --git a/dabo/functions/currency_converter.sh b/dabo/functions/currency_converter.sh new file mode 100644 index 0000000..49ff90b --- /dev/null +++ b/dabo/functions/currency_converter.sh @@ -0,0 +1,118 @@ +function currency_converter { + + #g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@" + + local f_currency_amount=$1 + local f_currency=$2 + local f_currency_target=$3 + local f_currency_date=$4 + + unset f_currency_converter_result + local f_reverse=false + local f_line f_rate f_histfile f_date_array f_stablecoin + + # get current date if none given + [ -z "$f_currency_date" ] && printf -v f_currency_date '%(%Y-%m-%d %H:%M:%S)T' + + # rate per minute + f_currency_date_minute=$(date -d "${f_currency_date}" "+%Y-%m-%d.%H:%M") + + # hour failback + f_currency_date_hour=$(date -d "${f_currency_date}" "+%Y-%m-%d.%H") + + # day failback + f_currency_date_day=$(date -d "${f_currency_date}" "+%Y-%m-%d") + + # month failback + f_currency_date_month=$(date -d "${f_currency_date}" "+%Y-%m") + + # path to history files for the converting rate + [ -d asset-histories ] && f_asset_histories="asset-histories/" + + # map USD-Stablecoins to USD + local f_stablecoins="USDT BUSD" + for f_stablecoin in $f_stablecoins + do + # Link USD Stablecoin files to USD + if [ -s ${f_asset_histories}${f_currency}${f_stablecoin}.history-raw.csv ] + then + [ -e ${f_asset_histories}${f_currency}USD.history-raw.csv ] || \ + ln ${f_asset_histories}${f_currency}${f_stablecoin}.history-raw.csv ${f_asset_histories}${f_currency}USD.history-raw.csv + fi + + if [ -s ${f_asset_histories}${f_currency_target}${f_stablecoin}.history-raw.csv ] + then + [ -e ${f_asset_histories}USD${f_currency_target}.history-raw.csv ] || \ + ln ${f_asset_histories}${f_currency_target}${f_stablecoin}.history-raw.csv ${f_asset_histories}USD${f_currency_target}.history-raw.csv + fi + # use USD + if [[ $f_currency_target = $f_stablecoin ]] + then + f_currency_target=USD + fi + if [[ $f_currency = $f_stablecoin ]] + then + f_currency=USD + fi + done + + # if there is no currency change (USD USD or USDT USD) + if [[ $f_currency == $f_currency_target ]] + then + f_currency_converter_result=$f_currency_amount + return 0 + fi + + # try direct pair + get_marketdata_yahoo_historic "${f_currency_target}-${f_currency}" "${f_currency_target}${f_currency}" || get_marketdata_yahoo_historic "${f_currency}-${f_currency_target}" "${f_currency}${f_currency_target}" + local f_histfile_default="${f_asset_histories}${f_currency_target}${f_currency}.history-raw.csv" + local f_histfile_yahoo="${f_asset_histories}${f_currency_target}${f_currency}.history-yahoo.csv" + # reverse as backup + local f_histfile_default_reverse="${f_asset_histories}${f_currency}${f_currency_target}.history-raw.csv" + local f_histfile_yahoo_reverse="${f_asset_histories}${f_currency}${f_currency_target}.history-yahoo.csv" + + # search for rate by date + for f_histfile in "$f_histfile_default" "$f_histfile_default_reverse" "$f_histfile_yahoo" "$f_histfile_yahoo_reverse" + do + # histfile has to exist + if [ -s "$f_histfile" ] + then + # search for most precise date + f_line=$(egrep "^$f_currency_date_minute" "$f_histfile" | tail -n1) + [ -z "$f_line" ] && f_line=$(egrep "^$f_currency_date_hour" "$f_histfile" | tail -n1) + [ -z "$f_line" ] && f_line=$(egrep "^$f_currency_date_day" "$f_histfile" | tail -n1) + [ -z "$f_line" ] && f_line=$(egrep "^$f_currency_date_month" "$f_histfile" | tail -n1) + [ -n "$f_line" ] && f_rate=$(echo "$f_line" | cut -d, -f2) + if [ -n "$f_rate" ] + then + [[ $f_histfile =~ ${f_currency}${f_currency_target} ]] && f_reverse=true + [[ $f_line =~ ^$f_currency_date_hour ]] && break + fi + fi + done + + # end if no rate found + if [ -z "$f_rate" ] + then + # try workaround over USD if EUR + if [[ ${f_currency_target} = EUR ]] && [[ $f_currency != USD ]] + then + #g_echo_note "trying way over USD (workaround)" + if currency_converter $f_currency_amount $f_currency USD $f_currency_date + then + f_currency_amount=$f_currency_converter_result + currency_converter $f_currency_amount USD EUR $f_currency_date + return $? + fi + fi + g_echo_warn "didn't find rate for ${f_currency}-${f_currency_target} - '${FUNCNAME} $@'" + return 1 + fi + + # calculate converted currency and store result + [[ $f_reverse = true ]] && g_calc "${f_currency_amount}*${f_rate}" + [[ $f_reverse = false ]] && g_calc "1/${f_rate}*${f_currency_amount}" + f_currency_converter_result=$g_calc_result + +} +