Files
dabo/dabo/functions/check_buy_conditions.sh
2023-11-28 17:10:57 +01:00

150 lines
4.5 KiB
Bash

function check_buy_conditions {
local f_ASSET_HIST_FILE="$1"
f_ASSET=$(basename ${f_ASSET_HIST_FILE} | cut -d\. -f1)
if ! [ "$2" == "SELL" ]
then
if [ -n "${BOT}" ]
then
# ignore already invested asset
if ls trade-histories/trade-*${f_ASSET}-open.history.csv >/dev/null 2>&1
then
g_echo_note "BUY ${f_ASSET}@${CURRENCY}: $f_ASSET Already invested - ignoring for more diversification"
return 0
fi
fi
fi
# get asset vars
if [ -n "${BOT}" ]
then
get_vars_from_csv "${f_ASSET_HIST_FILE}" || return 1
fi
### from here: check for defined state to buy
f_BUY=""
# run strategies
if [ -z "$f_buy_strategies_array" ]
then
local f_strategy_path=../../strategies
[ -z "${BOT}" ] && f_strategy_path=strategies
mapfile -t f_buy_strategies_array < <(find ${f_strategy_path} -name "buy.*.conf" -type f)
fi
#for f_strategy in $(find ${f_strategy_path} -name "buy.*.conf" -type f)
for f_strategy in "${f_buy_strategies_array[@]}"
do
f_echo_prefix="BUY ${f_ASSET}@${CURRENCY}:${f_price}:${f_strategy} - "
if check_buy_conditions_strategy ${f_ASSET_HIST_FILE} ${f_strategy}
then
f_BUY="${f_echo_prefix} All BUY conditions met!!!"
break
fi
done
# if this checks came from sell function
if [ "$2" == "SELL" ]
then
[ -n "$f_BUY" ] && return 1
return 0
fi
### Buy or not buy?
# BOT
if [ -n "$f_BUY" ] && [ -n "${BOT}" ]
then
echo "${f_last_line},${f_ASSET}" >>trade.log
g_echo_note "${f_BUY}"
# calculate quantity from balance for potentially invest
g_calc "scale=2; ${CURRENCY_BALANCE}/100*${INVEST}"
local f_INVEST_QUANTITY=$(echo "${g_calc_result}" | cut -d\. -f1)
g_echo_note "BUY current investment quantity is ${f_INVEST_QUANTITY} ${CURRENCY}"
# remove CURRENCY from asset
f_ASSET=$(echo ${f_ASSET} | sed "s/${CURRENCY}//")
if [ ${STOCK_EXCHANGE} = "BINANCE" ]
then
# get stock exchange specific infos for trade (e.g. MIN_NOTIONAL)
${TOKEN_INFO_CMD} ${f_ASSET} ${CURRENCY}
# use MIN_NOTIONAL+5% as INVEST_QUANTITY if INVEST_QUANTITY is under MIN_NOTIONAL
# +5% in spite of MIN_NOTIONAL to be able to sell when the price falls a little bit
#[ $(g_calc "${f_INVEST_QUANTITY} < ${f_MIN_NOTIONAL}") -eq 0 ] || f_INVEST_QUANTITY=$(g_calc "scale=2; $f_MIN_NOTIONAL/100*105")
if ! g_num_is_lower.sh ${f_INVEST_QUANTITY} ${f_MIN_NOTIONAL}
then
g_calc "scale=2; ${f_MIN_NOTIONAL}/100*105"
f_INVEST_QUANTITY=${g_calc_result}
fi
# if there is not enough balance for buying because ${f_MIN_NOTIONAL} needed for buying to sell (workaround)
g_calc "${CURRENCY_BALANCE} < ${f_MIN_NOTIONAL}*2"
if [ ${g_calc_result} -ne 0 ]
then
g_echo_note "BUY ${f_ASSET} not enough balance ${CURRENCY_BALANCE} for buying because of MIN_NOTIONAL (${f_MIN_NOTIONAL}*2) needed for buying-to-sell (workaround)"
return 1
fi
# continue if not balance enough for lowest quantity (MIN_NOTIONAL)
g_calc "${CURRENCY_BALANCE} > ${f_INVEST_QUANTITY}"
if [ ${g_calc_result} -eq 0 ]
then
g_echo_note "BUY ${f_ASSET} not enough balance (${CURRENCY_BALANCE}) for lowest quantity (MIN_NOTIONAL - ${f_INVEST_QUANTITY})"
return 1
fi
binance_convert ${f_ASSET} ${CURRENCY} ${f_INVEST_QUANTITY} buy "$f_BUY" || \
do_trade ${f_ASSET} ${CURRENCY} ${f_INVEST_QUANTITY} buy "$f_BUY"
else
do_trade ${f_ASSET} ${CURRENCY} ${f_INVEST_QUANTITY} buy "$f_BUY"
fi
f_BUY=""
fi
# # ANALYZE
# if [ -n "$f_BUY" ] && [ -z "${BOT}" ]
# then
# echo "BUY: ${f_BUY}"
##echo "${csv_headline},Marketperformance
##${f_last_line}" | cut -d, -f 2-22 | perl -pe 's/([0-9].[0-9][0-9][0-9][0-9][0-9][0-9])[0-9]+/$1/g' | perl -pe 's/((?<=,)|(?<=^)),/ ,/g;' | column -t -s,
# f_open_trade=1
# #echo "${f_echo_prefix}${f_BUY}" >${g_tmp}/open-${tmpfile}
# BUY_PRICE=$f_price
# f_BUY=""
# fi
}
function check_buy_conditions_strategy {
# load strategy
local f_echo_prefix="BUY ${f_ASSET}@${CURRENCY}:${f_price}:${f_strategy} - "
f_do_buy=""
[ -n "${BOT}" ] && g_echo_note "${f_echo_prefix}Running BUY checks"
#if [ -s "${f_strategy}" ]
#then
. "${f_strategy}" || return 1
#else
# g_echo_note "${f_echo_prefix}Strategy file not found"
# return 1
#fi
# Check buy signal from strategy
if [ -n "${f_do_buy}" ]
then
echo " ${f_echo_prefix} Strategy buy signal: ${f_do_buy}"
return 0
fi
return 1
}