function check_buy_conditions { local f_ASSET_HIST_FILE="$1" f_ASSET=$(basename ${f_ASSET_HIST_FILE} | cut -d\. -f1) 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 get_vars_from_csv "${f_ASSET_HIST_FILE}" || return 1 ### from here: check for defined state to buy f_BUY="${f_last_line}" local f_priceXago=$(tail -n${BUY_MINGROWTH_PERIOD} ${f_ASSET_HIST_FILE} | grep ^[0-9] | head -n1 | cut -d, -f2) local f_pricenow=$(echo ${f_last_line} | grep ^[0-9] | cut -d, -f2) local f_pricediff=$(g_percentage-diff "$f_priceXago" "$f_pricenow") if [ $(echo "${f_pricediff} < ${BUY_MINGROWTH}" | bc -l) -ne 0 ] then g_echo_note "BUY ${f_ASSET}@${CURRENCY}:${f_price}: With ${f_pricediff} under ${BUY_MINGROWTH}% growth in the last ${BUY_MINGROWTH_PERIOD} time periods" return 0 fi #local f_macd_histogram_max=$(tail -n26 "${f_ASSET_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 | cut -d\. -f1) # MACD # no negative or empty values echo "${f_macd_histogram_relation}" | grep -q "^[0-9]" || return 0 # check for conditions if [ ${f_macd_histogram_relation} -le ${BUY_MACD_RELATION_FROM} ] || [ ${f_macd_histogram_relation} -ge ${BUY_MACD_RELATION_TO} ] then g_echo_note "BUY ${f_ASSET}@${CURRENCY}:${f_price}: MACD conditions NOT met" return 0 fi # RSI if \ [ ${f_rsi5} -le ${BUY_RSI5_SIGNAL_UNTIL} ] && \ [ ${f_rsi14} -le ${BUY_RSI14_SIGNAL_UNTIL} ] && \ [ ${f_rsi21} -le ${BUY_RSI21_SIGNAL_UNTIL} ] && \ [ ${f_rsi60} -le ${BUY_RSI60_SIGNAL_UNTIL} ] && \ [ ${f_rsi120} -le ${BUY_RSI120_SIGNAL_UNTIL} ] && \ [ ${f_rsi240} -le ${BUY_RSI240_SIGNAL_UNTIL} ] && \ [ ${f_rsi420} -le ${BUY_RSI420_SIGNAL_UNTIL} ] && \ [ ${f_rsi720} -le ${BUY_RSI720_SIGNAL_UNTIL} ] && \ [ ${f_rsi5} -ge ${BUY_RSI5_SIGNAL_FROM} ] && \ [ ${f_rsi14} -ge ${BUY_RSI14_SIGNAL_FROM} ] && \ [ ${f_rsi21} -ge ${BUY_RSI21_SIGNAL_FROM} ] && \ [ ${f_rsi60} -ge ${BUY_RSI60_SIGNAL_FROM} ] && \ [ ${f_rsi120} -ge ${BUY_RSI120_SIGNAL_FROM} ] && \ [ ${f_rsi240} -ge ${BUY_RSI240_SIGNAL_FROM} ] && \ [ ${f_rsi420} -ge ${BUY_RSI420_SIGNAL_FROM} ] && \ [ ${f_rsi720} -ge ${BUY_RSI720_SIGNAL_FROM} ] then echo "BUY ${f_ASSET}@${CURRENCY}:${f_price}: RSI conditions met" >/dev/null else g_echo_note "BUY ${f_ASSET}@${CURRENCY}:${f_price}: RSI conditions NOT met" return 0 fi # Price change if \ [ $(echo "${f_price_change_1_day} > ${BUY_MIN_PRICE_CHANGE_LAST_1_DAY}" | bc -l) -ne 0 ] && \ [ $(echo "${f_price_change_7_day} > ${BUY_MIN_PRICE_CHANGE_LAST_7_DAY}" | bc -l) -ne 0 ] && \ [ $(echo "${f_price_change_14_day} > ${BUY_MIN_PRICE_CHANGE_LAST_14_DAY}" | bc -l) -ne 0 ] && \ [ $(echo "${f_price_change_30_day} > ${BUY_MIN_PRICE_CHANGE_LAST_30_DAY}" | bc -l) -ne 0 ] then echo "BUY ${f_ASSET}@${CURRENCY}:${f_price}: Price change conditions met" >/dev/null else g_echo_note "BUY ${f_ASSET}@${CURRENCY}:${f_price}: Price change conditions NOT met" return 0 fi if [ -n "$f_BUY" ] then # Check for beginning MACD Trend if ! tail -n3 ${f_ASSET_HIST_FILE} | grep -q '|buy,' then g_echo_note "BUY ${f_ASSET}@${CURRENCY}:${f_price}: MACD Trend not near (3 timeframes) the beginning buy signal" return 0 fi # Check for growing MACD trend local f_macd_histogram_before=$(tail -n2 ${f_ASSET_HIST_FILE} | head -n1 | cut -d, -f8) local f_macd_histogram_2before=$(tail -n3 ${f_ASSET_HIST_FILE} | head -n1 | cut -d, -f8) local f_macd_histogram_3before=$(tail -n4 ${f_ASSET_HIST_FILE} | head -n1 | cut -d, -f8) local f_macd_histogram_4before=$(tail -n5 ${f_ASSET_HIST_FILE} | head -n1 | cut -d, -f8) local f_macd_histogram_5before=$(tail -n6 ${f_ASSET_HIST_FILE} | head -n1 | cut -d, -f8) if \ [ $(echo "${f_macd_histogram} < ${f_macd_histogram_before}" | bc -l) -ne 0 ] && \ [ $(echo "${f_macd_histogram_before} < ${f_macd_histogram_2before}" | bc -l) -ne 0 ] && \ [ $(echo "${f_macd_histogram_2before} < ${f_macd_histogram_3before}" | bc -l) -ne 0 ] && \ [ $(echo "${f_macd_histogram_3before} < ${f_macd_histogram_4before}" | bc -l) -ne 0 ] && \ [ $(echo "${f_macd_histogram_4before} < ${f_macd_histogram_5before}" | bc -l) -ne 0 ] then g_echo_note "BUY ${f_ASSET}@${CURRENCY}:${f_price} MACD Histogram not rising (now ${f_macd_histogram} < 1 before ${f_macd_histogram_before} < 2 before ${f_macd_histogram_2before} < 3 before ${f_macd_histogram_3before} < 4 before ${f_macd_histogram_4before} < 5 before ${f_macd_histogram_5before}) - trend loses strength / not growing - don't buy" return 0 fi fi ### Buy or not buy? # BOT if [ -n "$f_BUY" ] && [ -n "${BOT}" ] then echo "${f_last_line},${f_ASSET}" >>trade.log f_BUY="BUY ${f_ASSET}@${CURRENCY}:${f_price}: All BUY conditions met!!! ${f_BUY}" g_echo_note "${f_BUY}" # calculate quantity from balance for potentially invest local f_INVEST_QUANTITY=$(echo "scale=2; ${CURRENCY_BALANCE}/100*${INVEST}" | bc -l | sed 's/^\./0./;' | 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}//") # 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 [ $(echo "${f_INVEST_QUANTITY} < ${f_MIN_NOTIONAL}" | bc -l) -eq 0 ] || f_INVEST_QUANTITY=$(echo "scale=2; $f_MIN_NOTIONAL/100*105" | bc -l) # if there is not enough balance for buying because ${f_MIN_NOTIONAL} needed for buying to sell (workaround) if [ $(echo "${CURRENCY_BALANCE} < ${f_MIN_NOTIONAL}*2" | bc -l) -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) if [ $(echo "${CURRENCY_BALANCE} > ${f_INVEST_QUANTITY}" | bc -l) -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 #g_echo_note "BUY BUY BUY ${f_ASSET} ${CURRENCY} ${f_INVEST_QUANTITY} buy \"$f_BUY\"" if [ ${STOCK_EXCHANGE} = "BINANCE" ] then 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: $(echo ${f_last_line} | cut -d, -f1) === $f_BUY" echo "$f_BUY" >${g_tmp}/open-${tmpfile} BUY_PRICE=$current fi }