dabo/dabo/analyze.sh
2023-06-01 12:55:30 +02:00

175 lines
6.8 KiB
Bash
Executable File

#!/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
. dabo/functions/check_buy_conditions.sh
. dabo/functions/check_sell_conditions.sh
. dabo/functions/get_vars_from_csv.sh
. dabo/dabo-bot.conf
. dabo-bot.conf
. analyze.conf
[ "${ANALYZE_VERBOSE}" -eq "0" ] || g_echo "Analyzing file: $file"
# 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'
for line in $(egrep "^${ANALYZE_TIME}" "$file" | grep -v ',,')
do
IFS="$ORIGIFS"
current=$(echo $line | cut -d, -f2)
time=$(echo $line | cut -d, -f1 | cut -d: -f1,2)
f_market_performance=$(grep "^$time" data/botdata/MARKET_PERFORMANCE | tail -n1 | cut -d: -f4 | cut -d"%" -f1 | sed 's/ *//')
echo "$line" >>${g_tmp}/${tmpfile}
echo "${csv_headline},Marketperformance
${line},${f_market_performance}" | 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, >>${g_tmp}/output-${tmpfile}
if [ -f "${g_tmp}/open-${tmpfile}" ]
then
if [ "${ANALYZE_VERBOSE}" -eq "0" ]
then
check_sell_conditions ${g_tmp}/${tmpfile} >>${g_tmp}/output-${tmpfile}
else
check_sell_conditions ${g_tmp}/${tmpfile}
fi
fi
if ! [ -f "${g_tmp}/open-${tmpfile}" ]
then
if [ "${ANALYZE_VERBOSE}" -eq "0" ]
then
check_buy_conditions ${g_tmp}/${tmpfile} >>${g_tmp}/output-${tmpfile}
else
check_buy_conditions ${g_tmp}/${tmpfile}
fi
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
hodlresult=$(g_percentage-diff $(egrep "^${ANALYZE_TIME}" "$file" | grep -v ',,' | head -n1 | cut -d, -f2) $(egrep "^${ANALYZE_TIME}" "$file" | grep -v ',,' | tail -n1 | cut -d, -f2))
echo "COMPLETE:RESULT:$(basename "$file" | cut -d. -f1):: ${complete_result}% HODL:${hodlresult}% analyze-${analyzedate}/${tmpfile}.log $file" | perl -pe 's/ /\t/g; s/:/ /g' | 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"
}
for conf in dabo-bot.conf analyze.conf
do
. $conf
cat $conf
done
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
analyzedate="$(date +%Y-%m-%d--%H-%M-%S)"
mkdir "analyze-${analyzedate}"
cp dabo-bot.conf analyze.conf analyze-${analyzedate}/
cp -r strategies analyze-${analyzedate}/
analyzecounter=0
for file in $@
do
echo "${file}" | egrep -q "BALANCE|MSCI-WORLD-INDEX" && continue
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!"
continue
fi
if [ "${ANALYZE_BATCH}" -eq "0" ]
then
echo -n " \"analyze ${file}\"" >>/tmp/parallel-$$
else
analyze ${file}
fi
done
if [ "${ANALYZE_BATCH}" -eq "0" ]
then
export -f g_echo_note
export g_tmp
export analyzedate
. /tmp/parallel-$$
fi
analyzecounter=$(cat ${g_tmp}/overall-result-* | egrep -v "^0$" | wc -l)
echo "OVERALL RESULT (average): $(cat ${g_tmp}/overall-result-* | awk "{ SUM += \$1 / ${analyzecounter} } END { printf(\"%2.2f\", SUM) }")%" | tee analyze-${analyzedate}/overall-result.log
cat analyze-${analyzedate}/*.history.csv.log >analyze-${analyzedate}/analyze-overall.log
echo "" | tee -a analyze-${analyzedate}/overall-result.log
echo "Trades: $(grep "BUY: " analyze-${analyzedate}/analyze-overall.log | wc -l)" | tee -a analyze-${analyzedate}/overall-result.log
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: " -A5 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: " -A5 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: " -A5 analyze-${analyzedate}/analyze-overall.log | grep "^INTERIM RESULT: 0.00" | wc -l)" | tee -a analyze-${analyzedate}/overall-result.log
echo "
Complete Results" >>analyze-${analyzedate}/overall-result.log
grep "COMPLETE:RESULT:" analyze-${analyzedate}/analyze-overall.log >>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