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 get_vars_from_csv "${f_ASSET_HIST_FILE}" || return 1 ### from here: check for defined state to buy f_BUY="" # run strategies local f_strategy_path=../../strategies [ -z "${BOT}" ] && f_strategy_path=strategies for f_strategy in $(find ${f_strategy_path} -name "buy.*.conf" -type f) 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!!! ${f_last_line},${f_market_performance}" 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 f_BUY="${f_echo_prefix}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}//") 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 [ $(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 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_echo_prefix} All BUY conditions met!!!" echo "${csv_headline} ${f_BUY}" | perl -pe 's/((?<=,)|(?<=^)),/ ,/g;' | column -t -s, echo "$f_BUY" >${g_tmp}/open-${tmpfile} BUY_PRICE=$current f_BUY="" fi } function check_buy_conditions_strategy { # load strategy local f_echo_prefix="BUY ${f_ASSET}@${CURRENCY}:${f_price}:${f_strategy} - " if [ -s "${f_strategy}" ] then . "${f_strategy}" || return 1 else g_echo_note "${f_echo_prefix}Strategy file not found" return 1 fi [ -n "${BOT}" ] && g_echo_note "${f_echo_prefix}Running BUY checks" # Check market-performance if [ $(echo "${f_market_performance} > ${GOOD_MARKET_PERFORMANCE_INDEX}" | bc -l) -eq 0 ] then g_echo_note "${f_echo_prefix}BUY market performance ${f_market_performance}% looks bad - Dont buy anything" return 1 fi 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 "${f_echo_prefix}With ${f_pricediff} under ${BUY_MINGROWTH}% growth in the last ${BUY_MINGROWTH_PERIOD} time periods" return 1 fi # Check MACD if [ $(echo "${f_macd_histogram_relation} < ${BUY_MACD_RELATION_FROM}" | bc -l) -ne 0 ] then g_echo_note "${f_echo_prefix}MACD Relation with ${f_macd_histogram_relation}% under BUY_MACD_RELATION_FROM ${BUY_MACD_RELATION_FROM}%" return 1 fi if [ $(echo "${f_macd_histogram_relation} > ${BUY_MACD_RELATION_TO}" | bc -l) -ne 0 ] then g_echo_note "${f_echo_prefix}MACD Relation with ${f_macd_histogram_relation}% over BUY_MACD_RELATION_TO ${BUY_MACD_RELATION_TO}%" return 1 fi ## no negative or empty values #if echo "${f_macd_histogram_relation}" | grep -q "^[0-9]" #then # # 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 "${f_echo_prefix}MACD conditions NOT met" # return 1 # fi #else # g_echo_note "${f_echo_prefix}f_macd_histogram_relation empty or negative" # return 1 #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 "${f_echo_prefix}RSI conditions met" >/dev/null else g_echo_note "${f_echo_prefix}RSI conditions NOT met" return 1 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 "${f_echo_prefix}Price change conditions met" >/dev/null else g_echo_note "${f_echo_prefix}Price change conditions NOT met" return 1 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 "${f_echo_prefix}MACD Trend not near (3 timeframes) the beginning buy signal" # return 1 # fi # # Check for continious growing price # local f_price_before=$(tail -n2 ${f_ASSET_HIST_FILE} | head -n1 | cut -d, -f2) # local f_price_2before=$(tail -n3 ${f_ASSET_HIST_FILE} | head -n1 | cut -d, -f2) # local f_price_3before=$(tail -n4 ${f_ASSET_HIST_FILE} | head -n1 | cut -d, -f2) # if [ $(echo "${f_price} < ${f_price_before}" | bc -l) -ne 0 ] || \ # [ $(echo "${f_price_before} < ${f_price_2before}" | bc -l) -ne 0 ] || \ # [ $(echo "${f_price_2before} < ${f_price_3before}" | bc -l) -ne 0 ] # then # g_echo_note "${f_echo_prefix}Last 3 prices not continiously growing" # return 1 # 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 1 # fi # fi }