dabo/dabo/functions/get_rsi_indicator.sh
2023-05-19 08:02:34 +02:00

80 lines
2.6 KiB
Bash

function get_rsi_indicator {
#g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
# get histfile
local f_hist_file="$1"
local f_last_minutes="$2"
# calculate change of lastest f_last_minutes+1 prices for calculating rsi over the last f_last_minutes periods
local f_price
local f_last_price
local f_positive_sum=0
local f_negative_sum=0
f_rsi=""
local f_period=$((f_last_minutes+1))
local f_period_sum=$(tail -n${f_period} "${f_hist_file}" | cut -d, -f2 | grep "^[0-9]" | wc -l)
if ! [ ${f_period_sum} -ge ${f_period} ]
then
g_echo_note "${FUNCNAME} $@: Not enough data - waiting for more values and defaulting to 50. (${f_period} needed; ${f_period_sum} given)"
echo -n ",50" >>"${f_hist_file}"
return 0
fi
# for f_price in $(tail -n${f_period} "${f_hist_file}" | cut -d"," -f2)
# do
# # not for the first line because a comparative value is missing
# if [ -z "${f_last_price}" ]
# then
# f_last_price=${f_price}
# continue
# fi
#
# # calculate each change
# local f_price_change=$(g_percentage-diff ${f_last_price} ${f_price})
# f_last_price=${f_price}
#
# # add positive/negative changes
# if echo "${f_price_change}" | grep -q '^-'
# then
# f_negative_sum=$(echo "${f_negative_sum}+${f_price_change}" | bc -l)
# else
# f_positive_sum=$(echo "${f_positive_sum}+${f_price_change}" | bc -l)
# fi
# done
f_positive_sum=$(tail -n${f_period} "${f_hist_file}" | cut -d"," -f3 | grep "^[0-9]" | awk "{ SUM += \$1} END { printf(\"%10.10f\", SUM/${f_period}) }")
f_negative_sum=$(tail -n${f_period} "${f_hist_file}" | cut -d"," -f3 | grep "^-[0-9]" | awk "{ SUM += \$1} END { printf(\"%10.10f\", SUM/${f_period}) }")
# if one of both is "0" then fix results
if [ ${f_negative_sum} == "0.0000000000" ]
then
f_rsi=100
#g_echo_note "RSI-Indicator RSI: ${f_rsi}%"
echo -n ",${f_rsi}" >>"${f_hist_file}"
return 0
fi
if [ ${f_positive_sum} == "0.0000000000" ]
then
f_rsi=0
#g_echo_note "RSI-Indicator RSI: ${f_rsi}%"
echo -n ",${f_rsi}" >>"${f_hist_file}"
return 0
fi
# calculate positive/negative change averages
local f_negative_sum_average=$(echo "${f_negative_sum}/${f_last_minutes}" | bc -l | sed 's/-//')
local f_positive_sum_average=$(echo "${f_positive_sum}/${f_last_minutes}" | bc -l)
# calculate RS and RSI
local f_rs=$(echo "${f_positive_sum_average}/${f_negative_sum_average}" | bc -l)
f_rsi=$(echo "100-(100/(1+${f_rs}))" | bc -l | xargs printf "%.0f")
echo -n ",${f_rsi}" >>"${f_hist_file}"
#g_echo_note "RSI-Indicator RSI: ${f_rsi}%"
}