dabo/dabo/analyze.sh

183 lines
6.8 KiB
Bash
Raw Normal View History

2023-05-02 10:53:48 +02:00
#!/bin/bash
. /etc/bash/gaboshlib.include
g_nice
function g_echo_note {
[ -z "$1" ] && return 0
echo -en "\033[97m$(tail -n1 ${g_tmp}/$tmpfile | cut -d, -f1) \033[36mNOTE:"
cat <<< "$@"
echo -en "\033[0m"
}
function analyze {
local file=$1
tmpfile=$(basename "${file}")
. /etc/bash/gaboshlib/g_percentage-diff.bashfunc
2023-05-10 09:29:53 +02:00
. dabo/functions/check_buy_conditions.sh
. dabo/functions/check_sell_conditions.sh
. dabo/functions/get_vars_from_csv.sh
2023-05-13 20:44:16 +02:00
. dabo/dabo-bot.conf
2023-05-09 17:04:02 +02:00
. dabo-bot.conf
2023-05-02 10:53:48 +02:00
. analyze.conf
2023-05-10 09:40:57 +02:00
[ "${ANALYZE_VERBOSE}" -eq "0" ] || g_echo "Analyzing file: $file"
2023-05-02 10:53:48 +02:00
# cleanup
f_SELL=1
f_BUY=""
>${g_tmp}/${tmpfile}
>${g_tmp}/result-${tmpfile}
rm -f ${g_tmp}/open-${tmpfile}
rm -f ${g_tmp}/interim-${tmpfile}
rm -f ${g_tmp}/output-${tmpfile}
ORIGIFS="$IFS"
IFS=$'\n'
2023-05-10 09:29:53 +02:00
local f_strategy
2023-05-10 14:44:08 +02:00
2023-05-17 11:35:29 +02:00
#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
2023-05-10 14:44:08 +02:00
2023-05-17 11:35:29 +02:00
for line in $(egrep "^${ANALYZE_TIME}" "$file" | grep -v ',,')
2023-05-02 10:53:48 +02:00
do
IFS="$ORIGIFS"
current=$(echo $line | cut -d, -f2)
time=$(echo $line | cut -d, -f1 | cut -d: -f1,2)
echo "$line" >>${g_tmp}/${tmpfile}
if [ -f "${g_tmp}/open-${tmpfile}" ]
then
2023-05-10 09:29:53 +02:00
for f_strategy in $(find strategies -name "sell.*.conf" -type f)
do
if [ "${ANALYZE_VERBOSE}" -eq "0" ]
then
2023-05-19 08:59:09 +02:00
check_sell_conditions ${g_tmp}/${tmpfile} "${f_strategy}" >>${g_tmp}/output-${tmpfile} 2>&1
2023-05-10 09:29:53 +02:00
else
check_sell_conditions ${g_tmp}/${tmpfile} "${f_strategy}" 2>&1 | tee -a ${g_tmp}/output-${tmpfile}
fi
done
2023-05-02 10:53:48 +02:00
fi
if ! [ -f "${g_tmp}/open-${tmpfile}" ]
then
2023-05-10 09:29:53 +02:00
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
2023-05-19 08:59:09 +02:00
check_buy_conditions ${g_tmp}/${tmpfile} "${f_strategy}" >>${g_tmp}/output-${tmpfile} 2>&1 || break
2023-05-10 09:29:53 +02:00
else
2023-05-19 08:12:18 +02:00
check_buy_conditions ${g_tmp}/${tmpfile} "${f_strategy}" 2>&1 | tee -a ${g_tmp}/output-${tmpfile}
2023-05-10 09:29:53 +02:00
fi
done
2023-05-02 10:53:48 +02:00
fi
done
# sell at the end to have a final result.
if [ -f ${g_tmp}/open-${tmpfile} ]
then
f_SELL="SELL ${f_ASSET}: End of file/data"
echo "SELL: $(tail -n1 ${g_tmp}/${tmpfile} | cut -d, -f1) === ${f_SELL}" >>${g_tmp}/output-${tmpfile} 2>&1
result=$(g_percentage-diff ${BUY_PRICE} ${current})
result=$(echo "${result}-${FEE}" | bc | sed 's/^\./0./; s/^-\./-0./')
echo "$result" >>${g_tmp}/result-${tmpfile}
echo "RESULT: ${result}% (${BUY_PRICE} -> ${current})" >>${g_tmp}/output-${tmpfile}
rm -f ${g_tmp}/open-${tmpfile}
rm -f ${g_tmp}/interim-${tmpfile}
fi
complete_result=0
for result in $(cat ${g_tmp}/result-${tmpfile})
do
complete_result=$(echo "$complete_result+$result" | bc -l | sed 's/^\./0./; s/^-\./-0./' | xargs printf "%.2f")
done
echo "COMPLETE RESULT $file analyze-${analyzedate}/${tmpfile}.log: ${complete_result}%" | tee -a ${g_tmp}/output-${tmpfile}
echo "=====================================" >>${g_tmp}/output-${tmpfile}
echo "${complete_result}" >>${g_tmp}/overall-result-${tmpfile}
cat ${g_tmp}/output-${tmpfile} >"analyze-${analyzedate}/${tmpfile}.log"
}
2023-05-10 15:06:13 +02:00
for conf in dabo-bot.conf analyze.conf
2023-05-10 14:44:08 +02:00
do
. $conf
cat $conf
done
2023-05-02 10:53:48 +02:00
2023-05-10 09:29:53 +02:00
if [ "${ANALYZE_BATCH}" -eq "0" ]
then
cores=$(cat /proc/cpuinfo | grep "^processor.*:" | tail -n1 | perl -pe 's/processor.*: //')
echo -n "parallel -j${cores} bash -c --" >/tmp/parallel-$$
fi
2023-05-02 10:53:48 +02:00
analyzedate="$(date +%Y-%m-%d--%H-%M-%S)"
mkdir "analyze-${analyzedate}"
2023-05-10 09:29:53 +02:00
cp dabo-bot.conf analyze.conf analyze-${analyzedate}/
cp -r strategies analyze-${analyzedate}/
2023-05-02 10:53:48 +02:00
for file in $@
do
2023-05-10 15:19:21 +02:00
echo "${file}" | egrep -q "BALANCE|MSCI-WORLD-INDEX" && continue
2023-05-10 16:26:00 +02:00
lines=$(egrep "^${ANALYZE_TIME}" "$file" | grep -v ',,' | wc -l)
if [ $lines -lt 40 ]
2023-05-10 16:26:00 +02:00
then
g_echo "Only $lines lines for given timeframe (${ANALYZE_TIME}) in $file - ignoring files with less then 40!"
2023-05-10 16:26:00 +02:00
continue
fi
2023-05-10 09:29:53 +02:00
if [ "${ANALYZE_BATCH}" -eq "0" ]
then
echo -n " \"analyze ${file}\"" >>/tmp/parallel-$$
else
analyze ${file}
fi
2023-05-02 10:53:48 +02:00
done
2023-05-10 09:29:53 +02:00
if [ "${ANALYZE_BATCH}" -eq "0" ]
then
export -f g_echo_note
export g_tmp
export analyzedate
. /tmp/parallel-$$
fi
2023-05-02 10:53:48 +02:00
echo "OVERALL RESULT: $(cat ${g_tmp}/overall-result-* | awk '{ SUM += $1 } END { printf("%2.2f", SUM) }')%" | tee analyze-${analyzedate}/overall-result.log
cat analyze-${analyzedate}/*.history.csv.log >analyze-${analyzedate}/analyze-overall.log
2023-05-15 11:51:54 +02:00
echo "" | tee -a analyze-${analyzedate}/overall-result.log
2023-05-17 11:33:13 +02:00
echo "Trades: $(grep "BUY: " analyze-${analyzedate}/analyze-overall.log | wc -l)" | tee -a analyze-${analyzedate}/overall-result.log
2023-05-15 11:51:54 +02:00
echo "Trade results positive: $(grep "^RESULT: " analyze-${analyzedate}/analyze-overall.log | grep ": [0-9]" | wc -l)" | tee -a analyze-${analyzedate}/overall-result.log
echo "Trade results negative: $(grep "^RESULT: " analyze-${analyzedate}/analyze-overall.log | grep ": -" | wc -l)" | tee -a analyze-${analyzedate}/overall-result.log
echo "Trade results neutral: $(grep "^RESULT: " analyze-${analyzedate}/analyze-overall.log | grep ": 0.00" | wc -l)" | tee -a analyze-${analyzedate}/overall-result.log
echo "" | tee -a analyze-${analyzedate}/overall-result.log
echo "Interim results: $(grep "INTERIM RESULT: " analyze-${analyzedate}/analyze-overall.log | wc -l)" | tee -a analyze-${analyzedate}/overall-result.log
echo "Interim results positive: $(grep "INTERIM RESULT: [0-9]" analyze-${analyzedate}/analyze-overall.log | wc -l)" | tee -a analyze-${analyzedate}/overall-result.log
echo "Interim results negative: $(grep "INTERIM RESULT: -" analyze-${analyzedate}/analyze-overall.log | wc -l)" | tee -a analyze-${analyzedate}/overall-result.log
echo "" | tee -a analyze-${analyzedate}/overall-result.log
echo "First interim result after BUY positive: $(grep "BUY: " -A6 analyze-${analyzedate}/analyze-overall.log | grep "INTERIM RESULT: [0-9]" | grep -v "0.00" | wc -l)" | tee -a analyze-${analyzedate}/overall-result.log
echo "First interim result after BUY negative: $(grep "BUY: " -A6 analyze-${analyzedate}/analyze-overall.log | grep "INTERIM RESULT: -" | wc -l)" | tee -a analyze-${analyzedate}/overall-result.log
echo "First interim result after BUY neutral 0.00: $(grep "BUY: " -A6 analyze-${analyzedate}/analyze-overall.log | grep "INTERIM RESULT: -" | wc -l)" | tee -a analyze-${analyzedate}/overall-result.log
2023-05-02 10:53:48 +02:00
echo "
Complete Results" >>analyze-${analyzedate}/overall-result.log
grep "COMPLETE RESULT " analyze-${analyzedate}/analyze-overall.log | grep -v ": 0%" >>analyze-${analyzedate}/overall-result.log
echo "
Trades" >>analyze-${analyzedate}/overall-result.log
egrep "BUY: |SELL: " analyze-${analyzedate}/analyze-overall.log >>analyze-${analyzedate}/overall-result.log