dabo/functions/check_for_sell.sh

231 lines
10 KiB
Bash
Raw Normal View History

2023-04-28 17:09:15 +02:00
function check_for_sell {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
## sell earlier if market is bad
if [ $(echo "${f_market_performance} > ${GOOD_MARKET_PERFORMANCE_INDEX}" | bc -l) -eq 0 ]
then
g_echo_note "SELL market performance ${f_market_performance}% bad - sell earlier"
#SELL_PERCENTAGE_FROM_LAST_PURCHASE_NEGATIVE="${SELL_PERCENTAGE_FROM_LAST_PURCHASE_NEGATIVE_BAD_MARKET}"
#SELL_PERCENTAGE_FROM_LAST_PURCHASE_POSITIVE="${SELL_PERCENTAGE_FROM_LAST_PURCHASE_POSITIVE_BAD_MARKET}"
else
g_echo_note "SELL market performance ${f_market_performance}% ok"
fi
# Check all balances
for f_EXCHANGE_GET_BALANCES_CMD_OUT in $(cat EXCHANGE_GET_BALANCES_CMD_OUT | egrep -v "^${CURRENCY},")
do
f_ASSET=$(echo ${f_EXCHANGE_GET_BALANCES_CMD_OUT} | cut -d, -f1)
f_QUANTITY=$(echo ${f_EXCHANGE_GET_BALANCES_CMD_OUT} | cut -d, -f2)
f_QUANTITY_CURRENCY=$(echo ${f_EXCHANGE_GET_BALANCES_CMD_OUT} | cut -d, -f3)
f_LAST_EXCHANGE_RATE=$(echo ${f_EXCHANGE_GET_BALANCES_CMD_OUT} | cut -d, -f4)
f_ASSET_HIST_FILE="asset-histories/${f_ASSET}${CURRENCY}.history.csv"
# State for currency
g_echo "SELL ${f_ASSET}: Balance: $f_QUANTITY ($f_QUANTITY_CURRENCY $CURRENCY)"
# check for emergency stop
local f_COMPLETE_BALANCE=$(tail -n1 "asset-histories/BALANCECOMPLETE${CURRENCY}.history.csv" | cut -d, -f2)
if [ $(echo "${f_COMPLETE_BALANCE} < ${EMERGENCY_STOP}" | bc -l) -ne 0 ]
then
local f_msg="ATTENTION! EMERGENCY STOP DUE TO POOR PERFORMANCE: BALANCE (${f_COMPLETE_BALANCE}) LOWER THEN EMERGENCY_STOP-VALUE (${EMERGENCY_STOP})"
g_echo_error "$f_msg"
do_trade ${f_ASSET} ${CURRENCY} ${f_QUANTITY_CURRENCY} sell "${f_msg}"
continue
fi
check_sell_conditions ${f_ASSET_HIST_FILE}
#local f_perf
#for f_perf in ${f_perf_week} ${f_perf_month}
#do
# [ -z "${f_perf}" ] && continue
# if [ $(echo "${f_perf} < ${EMERGENCY_STOP}" | bc -l) -ne 0 ]
# then
# local f_msg="ATTENTION! EMERGENCY STOP DUE TO POOR PERFORMANCE: LOWER THEN EMERGENCY_STOP-VALUE (${EMERGENCY_STOP}) (WEEK: ${f_perf_week}; MONTH: ${f_perf_month})"
# g_echo_error "$f_msg"
# do_trade ${f_ASSET} ${CURRENCY} ${f_QUANTITY_CURRENCY} sell "${f_msg}"
# continue 2
# fi
#done
## check for DONT_SELL_IF_GROWTH_OVER
#local f_condition
#for f_condition in $DONT_SELL_IF_GROWTH_OVER
#do
# local f_min=$(echo ${f_condition} | cut -d'>' -f1)
# local f_percentage=$(echo ${f_condition} | cut -d'>' -f1)
# get_rate_percentage_min_before_and_now ${f_ASSET} ${CURRENCY} ${f_min} || continue
# if [ $(echo "${f_exchange_rate_diff_percentage} > ${f_percentage}" | bc -l) -ne 0 ]
# then
# g_echo_note "SELL ${f_ASSET}: growth rate last ${f_min} minute(s) was over ${f_percentage}% (${f_exchange_rate_diff_percentage})"
# continue 2
# fi
#done
### from here: check for defined state to sell
# Reset g_SELL for next round
# f_SELL=""
#
#
# ## check current result
# f_TRADE_HIST_FILE="trade-histories/${f_ASSET}${CURRENCY}.history.csv"
# if [ -s "${f_TRADE_HIST_FILE}" ]
# then
# f_BUY_PRICE=$(grep ",buy," $f_TRADE_HIST_FILE | tail -n1 | cut -d, -f5)
# f_BUY_PRICE_LAST_RATE_DIFF=$(g_percentage-diff $f_BUY_PRICE $f_LAST_EXCHANGE_RATE)
# # Store for overview
# echo ${f_BUY_PRICE_LAST_RATE_DIFF} >DIFF_BUY_PRICE_${f_ASSET}${CURRENCY}
#
#
# # 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="Selling ${f_QUANTITY_CURRENCY} ${CURRENCY} - SELL_PERCENTAGE_FROM_LAST_PURCHASE_NEGATIVE(${SELL_PERCENTAGE_FROM_LAST_PURCHASE_NEGATIVE}%) > Last rate (${f_BUY_PRICE_LAST_RATE_DIFF}%)"
# do_trade ${f_ASSET} ${CURRENCY} ${f_QUANTITY_CURRENCY} sell "${f_SELL}"
# continue
# else
# g_echo_note "SELL ${f_ASSET}: compare current price $f_LAST_EXCHANGE_RATE and price at the time of purchase $f_BUY_PRICE (${f_BUY_PRICE_LAST_RATE_DIFF}): over SELL_PERCENTAGE_FROM_LAST_PURCHASE_NEGATIVE(${SELL_PERCENTAGE_FROM_LAST_PURCHASE_NEGATIVE}%)"
# fi
# # check for SELL_PERCENTAGE_FROM_LAST_PURCHASE_POSITIVE
# if [ $(echo "${f_BUY_PRICE_LAST_RATE_DIFF} < ${SELL_PERCENTAGE_FROM_LAST_PURCHASE_POSITIVE}" | bc -l) -ne 0 ]
# then
# g_echo_note "SELL ${f_ASSET}: Not doing further checking - compare current price $f_LAST_EXCHANGE_RATE and price at the time of purchase $f_BUY_PRICE (${f_BUY_PRICE_LAST_RATE_DIFF}): under SELL_PERCENTAGE_FROM_LAST_PURCHASE_POSITIVE(${SELL_PERCENTAGE_FROM_LAST_PURCHASE_POSITIVE}%)"
# continue
# elif [ $(echo "${f_market_performance} > ${GOOD_MARKET_PERFORMANCE_INDEX}" | bc -l) -eq 0 ]
# then
# # check for SELL_PERCENTAGE_FROM_LAST_PURCHASE_POSITIVE if on bad market
# local f_msg="SELL ${f_ASSET}: Sell because over SELL_PERCENTAGE_FROM_LAST_PURCHASE_POSITIVE_BAD_MARKET (${SELL_PERCENTAGE_FROM_LAST_PURCHASE_POSITIVE_BAD_MARKET}) $f_BUY_PRICE (${f_BUY_PRICE_LAST_RATE_DIFF} in bad market"
# g_echo_note "$f_msg"
# do_trade ${f_ASSET} ${CURRENCY} ${f_QUANTITY_CURRENCY} sell "${f_msg}"
# continue
# fi
# # check for SELL_PERCENTAGE_FROM_LAST_PURCHASE_SOFT
# if [ $(echo "${f_BUY_PRICE_LAST_RATE_DIFF} > ${SELL_PERCENTAGE_FROM_LAST_PURCHASE_SOFT}" | bc -l) -ne 0 ]
# then
# f_SELL="Selling ${f_QUANTITY_CURRENCY} ${CURRENCY} - SELL_PERCENTAGE_FROM_LAST_PURCHASE_SOFT(${SELL_PERCENTAGE_FROM_LAST_PURCHASE_SOFT}%) > Last rate (${f_BUY_PRICE_LAST_RATE_DIFF}%)"
# else
# g_echo_note "SELL ${f_ASSET}: compare current price $f_LAST_EXCHANGE_RATE and price at the time of purchase $f_BUY_PRICE (${f_BUY_PRICE_LAST_RATE_DIFF}) not over SELL_PERCENTAGE_FROM_LAST_PURCHASE_SOFT(${SELL_PERCENTAGE_FROM_LAST_PURCHASE_SOFT}%)"
# fi
# else
# g_echo_warn "SELL ${f_ASSET}: Missing TRADE_HIST_FILE (${f_TRADE_HIST_FILE})"
# fi
# ## check for SELL_PERCENTAGE_HARD
# get_rate_percentage_min_before_and_now $f_ASSET $CURRENCY 15 || continue
# if [ $(echo "${f_exchange_rate_diff_percentage} < ${SELL_PERCENTAGE_HARD}" | bc -l) -ne 0 ]
# then
# f_SELL="SELLING ${f_QUANTITY_CURRENCY} ${CURRENCY} - SELL_PERCENTAGE_HARD(${SELL_PERCENTAGE_HARD}%) > 15 minutes rate (${f_exchange_rate_diff_percentage}%)"
# else
# g_echo_note "SELL ${f_ASSET}: Rate change 15 minutes ago (${f_exchange_rate_diff_percentage}) not over SELL_PERCENTAGE_HARD(${SELL_PERCENTAGE_HARD}%)"
# fi
## check for SELL_PERCENTAGE_SOFT
# go through time intervals
# for f_min in ${MINUTE_STEPS_SELL}
# do
# # calculate rate change last defined minutes
# get_rate_percentage_min_before_and_now $f_ASSET $CURRENCY $f_min || continue
# if [ $(echo "${f_exchange_rate_diff_percentage} < ${SELL_PERCENTAGE_SOFT}" | bc -l) -ne 0 ]
# then
# f_SELL="SELLING ${f_QUANTITY_CURRENCY} ${CURRENCY} - SELL_PERCENTAGE_SOFT(${SELL_PERCENTAGE_SOFT}%) > $f_min minutes rate (${f_exchange_rate_diff_percentage}%)"
# break
# else
# g_echo_note "SELL ${f_ASSET}: Rate change $f_min ago (${f_exchange_rate_diff_percentage}) not over SELL_PERCENTAGE_SOFT(${SELL_PERCENTAGE_SOFT}%)"
# fi
# done
# dont sell if lock exists
#if [ -s "${f_ASSET}${CURRENCY}-sell.lock" ]
#then
# g_echo_note "SELL ${f_ASSET}: Not doing further checking - ${f_ASSET}${CURRENCY}-sell.lock exists and not older then 30 minutes"
# continue
#fi
# check last 5 minutes - if growth over 0.5% then hold (important for rally prices
#get_rate_percentage_min_before_and_now ${f_ASSET} ${CURRENCY} 5 || continue
#[ $(echo "${f_exchange_rate_diff_percentage} > 0.5" | bc -l) -ne 0 ] && continue
# # check last 10 minutes - if loss over -1% then sell
# get_rate_percentage_min_before_and_now ${f_ASSET} ${CURRENCY} 10
# if [ $(echo "${f_exchange_rate_diff_percentage} < -1" | bc -l) -ne 0 ]
# then
# f_SELL="SELL ${f_ASSET}: 10min Loss ${f_exchange_rate_diff_percentage} higher then -1%"
# g_echo_note "$f_SELL"
# fi
#
# # Sell on RSI higher then RSI_SELL_SIGNAL_FROM_FORCE
# local f_rsi=$(tail -n1 ${f_ASSET_HIST_FILE} | cut -d, -f3)
# if [ ${f_rsi} -ge ${RSI_SELL_SIGNAL_FROM_FORCE} ] && [ ${f_rsi} -lt 100 ]
# then
# f_SELL="SELL ${f_ASSET}: RSI: $f_rsi"
# g_echo_note "$f_SELL"
# fi
#
#
# # check MACD and RSI
# local f_macd=$(tail -n1 ${f_ASSET_HIST_FILE} | cut -d, -f7)
# g_echo_note "SELL ${f_ASSET}: Checking for indicators: RSI $f_rsi ; MACD: $f_macd"
# if [ $(echo "${f_macd} < 0" | bc -l) -ne 0 ]
# then
# g_echo_note "SELL ${f_ASSET}: trend change RSI $f_rsi ; MACD: $f_macd"
# if [ $(echo "${f_rsi} > 60" | bc -l) -ne 0 ]
# then
# f_SELL="SELL ${f_ASSET}: RSI: $f_rsi ; MACD: $f_macd"
# g_echo_note "$f_SELL"
# fi
# fi
# # check for SELL_CONDITION and RSI Indicator
# local f_condition
# local f_loopcount=0
# local f_conditioncount=0
# for f_condition in $SELL_CONDITION
# do
# local f_min=$(echo ${f_condition} | cut -d'<' -f1)
# local f_percentage=$(echo ${f_condition} | cut -d'<' -f2)
# get_rate_percentage_min_before_and_now ${f_ASSET} ${CURRENCY} ${f_min} || continue 2
# if [ $(echo "${f_exchange_rate_diff_percentage} < ${f_percentage}" | bc -l) -ne 0 ]
# then
# g_echo_note "SELL ${f_ASSET}: Price change met SELL_CONDITION (decrease of ${f_exchange_rate_diff_percentage}% higher then ${f_percentage}% in ${f_min} minutes."
# ((f_conditioncount=f_conditioncount+1))
# else
# g_echo_note "SELL ${f_ASSET}: Price change didn't meet SELL_CONDITION (decrease of ${f_exchange_rate_diff_percentage}% lower then ${f_percentage}% in ${f_min} minutes."
# fi
# ((f_loopcount=f_loopcount+1))
# done
# if [ $f_conditioncount -eq $f_loopcount ]
# then
# if [ ${f_rsi} -ge ${RSI_SELL_SIGNAL_FROM} ]
# then
# f_SELL="SELL ${f_ASSET}: Price change met all SELL_CONDITIONs ${SELL_CONDITION} ($f_conditioncount of $f_loopcount) with RSI ${f_rsi}"
# g_echo_note "$f_SELL"
# fi
# else
# g_echo_note "SELL ${f_ASSET}: Price change didn't meet SELL_CONDITIONs ${SELL_CONDITION} ($f_conditioncount of $f_loopcount)"
# fi
# # Sell or not sell?
# if [ -n "$f_SELL" ]
# then
# do_trade ${f_ASSET} ${CURRENCY} ${f_QUANTITY_CURRENCY} sell "${f_SELL}"
# fi
done
}