moving strategies to check_[buy|sell]_conditions.sh

This commit is contained in:
olli 2023-05-19 11:03:35 +02:00
parent 4510794f32
commit a28b442e5c
5 changed files with 191 additions and 158 deletions

View File

@ -38,14 +38,6 @@ function analyze {
ORIGIFS="$IFS"
IFS=$'\n'
local f_strategy
#lines=$(egrep "^${ANALYZE_TIME}" "$file" | grep -v ',,' | wc -l)
#if [ $lines -lt 40 ]
#then
# g_echo "Only $lines lines for given timeframe (${ANALYZE_TIME}) in $file - ignoring files with less then 40!"
# return 1
#fi
for line in $(egrep "^${ANALYZE_TIME}" "$file" | grep -v ',,')
do
@ -55,28 +47,22 @@ function analyze {
echo "$line" >>${g_tmp}/${tmpfile}
if [ -f "${g_tmp}/open-${tmpfile}" ]
then
for f_strategy in $(find strategies -name "sell.*.conf" -type f)
do
if [ "${ANALYZE_VERBOSE}" -eq "0" ]
then
check_sell_conditions ${g_tmp}/${tmpfile} "${f_strategy}" >>${g_tmp}/output-${tmpfile} 2>&1
else
check_sell_conditions ${g_tmp}/${tmpfile} "${f_strategy}" 2>&1 | tee -a ${g_tmp}/output-${tmpfile}
fi
done
if [ "${ANALYZE_VERBOSE}" -eq "0" ]
then
check_sell_conditions ${g_tmp}/${tmpfile} >>${g_tmp}/output-${tmpfile} 2>&1
else
check_sell_conditions ${g_tmp}/${tmpfile} 2>&1
fi
fi
if ! [ -f "${g_tmp}/open-${tmpfile}" ]
then
f_market_performance=$(grep "^$time" data/botdata/MARKET_PERFORMANCE | tail -n1 | cut -d: -f4 | cut -d"%" -f1 | sed 's/ *//')
for f_strategy in $(find strategies -name "buy.*.conf" -type f)
do
if [ "${ANALYZE_VERBOSE}" -eq "0" ]
then
check_buy_conditions ${g_tmp}/${tmpfile} "${f_strategy}" >>${g_tmp}/output-${tmpfile} 2>&1 || break
else
check_buy_conditions ${g_tmp}/${tmpfile} "${f_strategy}" 2>&1 | tee -a ${g_tmp}/output-${tmpfile}
fi
done
if [ "${ANALYZE_VERBOSE}" -eq "0" ]
then
check_buy_conditions ${g_tmp}/${tmpfile} >>${g_tmp}/output-${tmpfile} 2>&1
else
check_buy_conditions ${g_tmp}/${tmpfile} 2>&1
fi
fi
done

View File

@ -1,16 +1,18 @@
function check_buy_conditions {
local f_ASSET_HIST_FILE="$1"
local f_strategy="$2"
f_ASSET=$(basename ${f_ASSET_HIST_FILE} | cut -d\. -f1)
if [ -n "${BOT}" ]
if ! [ "$2" == "SELL" ]
then
# ignore already invested asset
if ls trade-histories/trade-*${f_ASSET}-open.history.csv >/dev/null 2>&1
if [ -n "${BOT}" ]
then
g_echo_note "BUY ${f_ASSET}@${CURRENCY}: $f_ASSET Already invested - ignoring for more diversification"
return 0
# 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
@ -18,8 +20,96 @@ function check_buy_conditions {
get_vars_from_csv "${f_ASSET_HIST_FILE}" || return 1
### from here: check for defined state to buy
f_BUY="${f_last_line},${f_market_performance}"
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}" ]
@ -36,7 +126,7 @@ function check_buy_conditions {
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 0
return 1
fi
local f_priceXago=$(tail -n${BUY_MINGROWTH_PERIOD} ${f_ASSET_HIST_FILE} | grep ^[0-9] | head -n1 | cut -d, -f2)
@ -45,19 +135,19 @@ function check_buy_conditions {
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 0
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 0
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 0
return 1
fi
## no negative or empty values
@ -67,11 +157,11 @@ function check_buy_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 0
# return 1
# fi
#else
# g_echo_note "${f_echo_prefix}f_macd_histogram_relation empty or negative"
# return 0
# return 1
#fi
# RSI
@ -96,7 +186,7 @@ function check_buy_conditions {
echo "${f_echo_prefix}RSI conditions met" >/dev/null
else
g_echo_note "${f_echo_prefix}RSI conditions NOT met"
return 0
return 1
fi
# Price change
@ -109,7 +199,7 @@ function check_buy_conditions {
echo "${f_echo_prefix}Price change conditions met" >/dev/null
else
g_echo_note "${f_echo_prefix}Price change conditions NOT met"
return 0
return 1
fi
# if [ -n "$f_BUY" ]
@ -118,7 +208,7 @@ function check_buy_conditions {
# 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 0
# return 1
# fi
# # Check for continious growing price
@ -130,7 +220,7 @@ function check_buy_conditions {
# [ $(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 0
# return 1
# fi
@ -149,66 +239,9 @@ function check_buy_conditions {
# [ $(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
# return 1
# fi
# 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
fi
}

View File

@ -30,11 +30,7 @@ function check_for_buy {
if [ $(tail -n 6 "$f_ASSET_HIST_FILE" | egrep -v ",,|,$" | wc -l) -ge 5 ]
then
local f_strategy
for f_strategy in $(find ../../strategies -name "buy.*.conf" -type f)
do
check_buy_conditions "${f_ASSET_HIST_FILE}" "${f_strategy}"
done
check_buy_conditions "${f_ASSET_HIST_FILE}" "${f_strategy}"
else
g_echo_note "BUY $f_ASSET_HIST_FILE not enough data - waiting for complete values"
fi

View File

@ -22,12 +22,6 @@ function check_for_sell {
do_trade ${f_ASSET} ${CURRENCY} ${f_QUANTITY_CURRENCY} sell "${f_msg}"
continue
fi
local f_strategy
for f_strategy in $(find ../../strategies -name "sell.*.conf" -type f)
do
check_sell_conditions "${f_ASSET_HIST_FILE}" "${f_strategy}"
done
check_sell_conditions "${f_ASSET_HIST_FILE}" "${f_strategy}"
done
}

View File

@ -1,17 +1,16 @@
function check_sell_conditions {
local f_ASSET_HIST_FILE="$1"
local f_strategy="$2"
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
local f_echo_prefix="SELL ${f_ASSET}@${CURRENCY}:${f_price}:${f_strategy} - "
f_echo_prefix="SELL ${f_ASSET}@${CURRENCY}:${f_price} - "
### check current result
if [ -n "${BOT}" ]
then
@ -36,9 +35,72 @@ function check_sell_conditions {
echo "INTERIM RESULT: ${f_BUY_PRICE_LAST_RATE_DIFF}%"
fi
# store new interim result
echo ${f_BUY_PRICE_LAST_RATE_DIFF} >>${f_TRADE_HIST_FILE_INTERIM}
echo ${f_BUY_PRICE_LAST_RATE_DIFF} >>${f_TRADE_HIST_FILE_INTERIM}
# Check for filled buy conditions - if filled don't sell
check_buy_conditions ${f_ASSET_HIST_FILE} SELL || return 0
# run strategies
local f_strategy_path=../../strategies
[ -z "${BOT}" ] && f_strategy_path=strategies
for f_strategy in $(find ${f_strategy_path} -name "sell.*.conf" -type f)
do
f_echo_prefix="SELL ${f_ASSET}@${CURRENCY}:${f_price}:${f_strategy} - "
check_sell_conditions_strategy ${f_ASSET_HIST_FILE} ${f_strategy} ${f_TRADE_HIST_FILE} ${f_TRADE_HIST_FILE_INTERIM} ${f_BUY_PRICE} ${f_BUY_PRICE_LAST_RATE_DIFF}
if [ -n "$f_SELL" ]
then
f_SELL="${f_echo_prefix}${f_SELL}"
break
fi
done
### 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)"
echo "${csv_headline}
${f_last_line}
${f_SELL}" | perl -pe 's/((?<=,)|(?<=^)),/ ,/g;' | column -t -s,
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
}
function check_sell_conditions_strategy {
local f_ASSET_HIST_FILE="$1"
local f_strategy="$2"
local f_TRADE_HIST_FILE="$3"
local f_TRADE_HIST_FILE_INTERIM="$4"
local f_BUY_PRICE="$5"
local f_BUY_PRICE_LAST_RATE_DIFF="$6"
# run strategy
if [ -s "${f_strategy}" ]
then
@ -49,11 +111,10 @@ function check_sell_conditions {
fi
g_echo_note "${f_echo_prefix}Running SELL checks"
# 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_BUY_PRICE_2ND_LAST_RATE_DIFF=$(tail -n2 ${f_TRADE_HIST_FILE_INTERIM} | head -n1)
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
@ -72,7 +133,6 @@ function check_sell_conditions {
fi
fi
# Sell on MACD Condition
if [ $(echo "${f_macd_histogram_relation} < ${SELL_MACD_RELATION_FROM}" | bc -l) -ne 0 ]
then
@ -120,40 +180,4 @@ function check_sell_conditions {
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)"
echo "${csv_headline}
${f_last_line}
${f_SELL}" | perl -pe 's/((?<=,)|(?<=^)),/ ,/g;' | column -t -s,
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
}