dabo/functions/check_sell_conditions.sh
2023-04-28 17:09:15 +02:00

144 lines
5.6 KiB
Bash

function check_sell_conditions {
local f_ASSET_HIST_FILE="$1"
f_ASSET=$(basename ${f_ASSET_HIST_FILE} | cut -d\. -f1)
### from here: check for defined state to sell
f_SELL=""
# get data
get_vars_from_csv ${f_ASSET_HIST_FILE} || return 1
### check current result
if [ -n "${BOT}" ]
then
f_TRADE_HIST_FILE="$(ls -1tr trade-histories/trade-*${f_ASSET}-open.history.csv | tail -n1)"
if ! [ -s "${f_TRADE_HIST_FILE}" ]
then
g_echo_note "SELL ${f_ASSET}: No trade history file (${f_TRADE_HIST_FILE}) found - ignoring"
return 0
fi
f_TRADE_HIST_FILE_INTERIM=$(echo ${f_TRADE_HIST_FILE} | sed 's/-open\.history\.csv$/-interim.history.csv/')
local f_BUY_PRICE=$(grep ',BUY' $f_TRADE_HIST_FILE | tail -n1 | cut -d, -f5)
local f_BUY_PRICE_LAST_RATE_DIFF=$(g_percentage-diff ${f_BUY_PRICE} ${f_price})
# Store for overview
echo ${f_BUY_PRICE_LAST_RATE_DIFF} >DIFF_BUY_PRICE_${f_ASSET}
else
f_TRADE_HIST_FILE="${g_tmp}/open-${tmpfile}"
f_TRADE_HIST_FILE_INTERIM="${g_tmp}/interim-${tmpfile}"
f_BUY_PRICE_LAST_RATE_DIFF=$(g_percentage-diff ${BUY_PRICE} ${f_price})
f_BUY_PRICE=${BUY_PRICE}
result=${f_BUY_PRICE_LAST_RATE_DIFF}
echo "${f_last_line}"
echo "INTERIM RESULT: ${f_BUY_PRICE_LAST_RATE_DIFF}%"
fi
# check if the result (profit/loss until now) is lowering and sell if too low ()
if [ -f ${f_TRADE_HIST_FILE_INTERIM} ]
then
local f_BUY_PRICE_2ND_LAST_RATE_DIFF=$(tail -n1 ${f_TRADE_HIST_FILE_INTERIM})
local f_diff_result=$(echo "${f_BUY_PRICE_LAST_RATE_DIFF} - (${f_BUY_PRICE_2ND_LAST_RATE_DIFF})" | bc | sed 's/^\./0./; s/^-\./-0./')
if [ $(echo "${f_diff_result} < ${SELL_IF_LAST_RATE_LOWER_THEN}" | bc -l) -ne 0 ] && [ $(echo "${f_BUY_PRICE_LAST_RATE_DIFF} > ${FEE}" | bc -l) -ne 0 ]
then
f_SELL="Loss between last (${f_BUY_PRICE_LAST_RATE_DIFF}%) and 2nd last (${f_BUY_PRICE_2ND_LAST_RATE_DIFF}%) rate/result more then ${SELL_IF_LAST_RATE_LOWER_THEN}% (${f_diff_result}%)"
fi
# Sell if the last X time units is lower then FEE
if [ $(cat ${f_TRADE_HIST_FILE_INTERIM} | wc -l) -ge ${SELL_IF_LOWER_THEN_FEE_AFTER_PERIOD} ]
then
local f_BUY_PRICE_XND_LAST_RATE_DIFF=$(tail -n${SELL_IF_LOWER_THEN_FEE_AFTER_PERIOD} ${f_TRADE_HIST_FILE_INTERIM} | head -n1)
local f_diff_result=$(echo "${f_BUY_PRICE_LAST_RATE_DIFF} - (${f_BUY_PRICE_XND_LAST_RATE_DIFF})" | bc)
if [ $(echo "${f_diff_result} < ${FEE}" | bc -l) -ne 0 ]
then
f_SELL="Loss between last rate (${f_BUY_PRICE_LAST_RATE_DIFF}%) in ${SELL_IF_LOWER_THEN_FEE_AFTER_PERIOD} time units before more then ${FEE} (${f_diff_result}%)"
fi
fi
fi
# store new interim result
echo ${f_BUY_PRICE_LAST_RATE_DIFF} >>${f_TRADE_HIST_FILE_INTERIM}
# Sell on MACD Condition
if [ $(echo "${f_macd_histogram_relation} < ${SELL_MACD_RELATION_FROM}" | bc -l) -ne 0 ]
then
f_SELL="MACD condition met"
fi
# Sell on RSI conditions
if \
[ ${f_rsi5} -le ${SELL_RSI5_SIGNAL_UNTIL} ] && \
[ ${f_rsi14} -le ${SELL_RSI14_SIGNAL_UNTIL} ] && \
[ ${f_rsi21} -le ${SELL_RSI21_SIGNAL_UNTIL} ] && \
[ ${f_rsi60} -le ${SELL_RSI60_SIGNAL_UNTIL} ] && \
[ ${f_rsi120} -le ${SELL_RSI120_SIGNAL_UNTIL} ] && \
[ ${f_rsi240} -le ${SELL_RSI240_SIGNAL_UNTIL} ] && \
[ ${f_rsi420} -le ${SELL_RSI420_SIGNAL_UNTIL} ] && \
[ ${f_rsi720} -le ${SELL_RSI720_SIGNAL_UNTIL} ]&& \
[ ${f_rsi5} -ge ${SELL_RSI5_SIGNAL_FROM} ] && \
[ ${f_rsi14} -ge ${SELL_RSI14_SIGNAL_FROM} ] && \
[ ${f_rsi21} -ge ${SELL_RSI21_SIGNAL_FROM} ] && \
[ ${f_rsi60} -ge ${SELL_RSI60_SIGNAL_FROM} ] && \
[ ${f_rsi120} -ge ${SELL_RSI120_SIGNAL_FROM} ] && \
[ ${f_rsi240} -ge ${SELL_RSI240_SIGNAL_FROM} ] && \
[ ${f_rsi420} -ge ${SELL_RSI420_SIGNAL_FROM} ] && \
[ ${f_rsi720} -ge ${SELL_RSI720_SIGNAL_FROM} ]
then
f_SELL="RSI conditions met"
fi
# hold on negative result
if [ "${SELL_HOLD_IF_RESULT_NEGATIVE}" -gt 0 ]
then
if [ $(echo "(${f_BUY_PRICE_LAST_RATE_DIFF} - ${FEE}) < 0" | bc -l) -ne 0 ]
then
g_echo_note "Result negative - holding (INTERIM RESULT ${f_BUY_PRICE_LAST_RATE_DIFF}% - FEE ${FEE}% < 0"
f_SELL=""
fi
fi
# check for SELL_PERCENTAGE_FROM_LAST_PURCHASE_NEGATIVE
if [ $(echo "${f_BUY_PRICE_LAST_RATE_DIFF} < ${SELL_PERCENTAGE_FROM_LAST_PURCHASE_NEGATIVE}" | bc -l) -ne 0 ]
then
f_SELL="SELL_PERCENTAGE_FROM_LAST_PURCHASE_NEGATIVE(${SELL_PERCENTAGE_FROM_LAST_PURCHASE_NEGATIVE}%) > Last rate (${f_BUY_PRICE_LAST_RATE_DIFF}%)"
fi
[ -n "$f_SELL" ] && f_SELL="SELL ${f_ASSET}@${CURRENCY}:${f_price}: $f_SELL"
### Sell or not sell?
# BOT
if [ -n "$f_SELL" ] && [ -n "${BOT}" ]
then
g_echo_note "$f_SELL"
echo "${f_last_line},${f_ASSET}" >>trade.log
f_ASSET=$(echo ${f_ASSET} | sed "s/${CURRENCY}//")
# binance_convert ${f_ASSET} ${CURRENCY} ${f_QUANTITY} sell "${f_SELL}"
if [ ${STOCK_EXCHANGE} = "BINANCE" ]
then
binance_convert ${f_ASSET} ${CURRENCY} ${f_QUANTITY} sell "${f_SELL}" ${f_QUANTITY_CURRENCY} || \
do_trade ${f_ASSET} ${CURRENCY} ${f_QUANTITY_CURRENCY} sell "${f_SELL}"
else
do_trade ${f_ASSET} ${CURRENCY} ${f_QUANTITY_CURRENCY} sell "${f_SELL}"
fi
fi
# ANALYZE
if [ -n "${f_SELL}" ] && [ -z "${BOT}" ]
then
echo "SELL: $(tail -n1 ${f_ASSET_HIST_FILE} | cut -d, -f1) === $f_SELL"
result=$(g_percentage-diff ${BUY_PRICE} ${current})
result=$(echo "${result}-${FEE}" | bc | sed 's/^\./0./; s/^-\./-0./')
echo "${result}" >>${g_tmp}/result-${tmpfile}
rm -f "${f_TRADE_HIST_FILE}"
rm -f "${f_TRADE_HIST_FILE_INTERIM}"
echo "RESULT: ${result}% (${BUY_PRICE} -> ${current})"
fi
}