Files
dabo/dabo/functions/get_rsi_indicator.sh
2023-11-26 17:18:01 +01:00

58 lines
1.9 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
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}%"
}