119 lines
3.9 KiB
Bash
119 lines
3.9 KiB
Bash
function get_macd_indicator {
|
|
|
|
#g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
|
|
# get histfile
|
|
local f_hist_file="$1"
|
|
|
|
# define or clean gloval macdvars
|
|
f_macd_ema12=""
|
|
f_macd_ema26=""
|
|
f_macd=""
|
|
f_macd_ema9_signal=""
|
|
f_macd_signal=""
|
|
|
|
if ! [ -s "$f_hist_file" ]
|
|
then
|
|
g_echo_warn "${FUNCNAME} $@: Histfile $f_hist_file does not exist or is empty"
|
|
return 1
|
|
fi
|
|
|
|
# read last two lines
|
|
local f_lastline=$(tail -n1 "$f_hist_file" | grep ^2)
|
|
local f_second_lastline=$(tail -n2 "$f_hist_file" | head -n1 | grep ^2)
|
|
|
|
# Try to get current MACD values
|
|
local f_macd_lastprice=$(echo "$f_lastline" | cut -d, -f2)
|
|
f_macd_ema12=$(echo "$f_lastline" | cut -d, -f4)
|
|
f_macd_ema26=$(echo "$f_lastline" | cut -d, -f5)
|
|
f_macd=$(echo "$f_lastline" | cut -d, -f6)
|
|
f_macd_ema9_signal=$(echo "$f_lastline" | cut -d, -f7)
|
|
|
|
# if data is complete
|
|
if [ -n "${f_macd_ema12}" ] && [ -n "${f_macd_ema26}" ] && [ -n "${f_macd}" ] && [ -n "${f_macd_ema9_signal}" ]
|
|
then
|
|
g_echo_note "${FUNCNAME} $@: Looks like current MACD was already calculated"
|
|
return 1
|
|
fi
|
|
|
|
# Try to get last MACD values
|
|
f_macd_last_ema12=$(echo "$f_second_lastline" | cut -d, -f4)
|
|
f_macd_last_ema26=$(echo "$f_second_lastline" | cut -d, -f5)
|
|
f_macd_last=$(echo "$f_second_lastline" | cut -d, -f6)
|
|
f_macd_last_ema9_signal=$(echo "$f_second_lastline" | cut -d, -f7)
|
|
f_macd_last_histogram=$(echo "$f_second_lastline" | cut -d, -f8)
|
|
|
|
# calc EMA12
|
|
get_ema "${f_hist_file}" 2 12 "${f_macd_last_ema12}" "${f_macd_lastprice}"
|
|
if [ -z "${f_ema}" ]
|
|
then
|
|
g_echo_note "${FUNCNAME} $@: Not enough data for calculating EMA12 - waiting for more values"
|
|
echo -n ",,,,,," >>"${f_hist_file}"
|
|
return 0
|
|
fi
|
|
f_macd_ema12=${f_ema}
|
|
echo -n ",${f_macd_ema12}" >>"${f_hist_file}"
|
|
|
|
# calc EMA26
|
|
if [ -z "${f_macd_last_ema12}" ]
|
|
then
|
|
echo -n ",,,,," >>"${f_hist_file}"
|
|
return 0
|
|
fi
|
|
get_ema "${f_hist_file}" 2 26 "${f_macd_last_ema26}" "${f_macd_lastprice}"
|
|
if [ -z "${f_ema}" ]
|
|
then
|
|
g_echo_note "${FUNCNAME} $@: Not enough data for calculating EMA26 - waiting for more values"
|
|
echo -n ",,,,," >>"${f_hist_file}"
|
|
return 0
|
|
fi
|
|
f_macd_ema26=${f_ema}
|
|
echo -n ",${f_macd_ema26}" >>"${f_hist_file}"
|
|
|
|
# calc MACD
|
|
if [ -z "${f_macd_ema26}" ]
|
|
then
|
|
echo -n ",,,," >>"${f_hist_file}"
|
|
return 0
|
|
fi
|
|
#[ -z "${f_macd_ema12}" ] && return 0
|
|
f_macd=$(echo "scale=8; ${f_macd_ema12}-${f_macd_ema26}" | bc | sed 's/^\./0./; s/^-\./-0./')
|
|
echo -n ",${f_macd}" >>"${f_hist_file}"
|
|
|
|
# calc MACD Signal
|
|
if [ -z "${f_macd}" ]
|
|
then
|
|
echo -n ",,," >>"${f_hist_file}"
|
|
return 0
|
|
fi
|
|
get_ema "${f_hist_file}" 6 9 "${f_macd_last_ema9_signal}" "${f_macd}"
|
|
if [ -z "${f_ema}" ]
|
|
then
|
|
g_echo_note "${FUNCNAME} $@: Not enough data for calculating EMA9 Signal - waiting for more values"
|
|
echo -n ",,," >>"${f_hist_file}"
|
|
return 0
|
|
fi
|
|
f_macd_ema9_signal=${f_ema}
|
|
echo -n ",${f_macd_ema9_signal}" >>"${f_hist_file}"
|
|
|
|
# calc MACD Histogram
|
|
f_macd_histogram=$(echo "scale=10; ${f_macd}-(${f_macd_ema9_signal})" | bc | sed 's/^\./0./; s/^-\./-0./')
|
|
echo -n ",${f_macd_histogram}" >>"${f_hist_file}"
|
|
|
|
# check for MACD buy or sell signal
|
|
echo ${f_macd_histogram} | grep -q "^-" && echo ${f_macd_last_histogram} | grep -q "^[0-9]" && f_macd_signal="sell"
|
|
echo ${f_macd_histogram} | grep -q "^[0-9]" && echo ${f_macd_last_histogram} | grep -q "^-" && f_macd_signal="buy"
|
|
|
|
# calculate MACD Histogram relation
|
|
if [ $(tail -n36 "${f_hist_file}" | wc -l) -ge 35 ]
|
|
then
|
|
local f_macd_histogram_max=$(tail -n60 "${f_hist_file}" | cut -d"," -f8 | egrep "^[0-9]|^-[0-9]" | sed 's/^-//' | sort -n | tail -n1)
|
|
local f_macd_histogram_relation=$(echo "100+$(g_percentage-diff ${f_macd_histogram_max} ${f_macd_histogram})" | bc | sed 's/^\./0./; s/^-\./-0./' | cut -d\. -f1)
|
|
fi
|
|
|
|
echo -n ",${f_macd_histogram_relation}|${f_macd_signal}" >>"${f_hist_file}"
|
|
|
|
|
|
|
|
}
|
|
|