dabo/dabo/analyze.sh

272 lines
11 KiB
Bash
Raw Normal View History

2023-05-02 10:53:48 +02:00
#!/bin/bash
2023-11-09 17:26:12 +01:00
# cleanup
rm -r /tmp/parallel-* /tmp/overall-result-* /tmp/g_analyze.sh-*
2023-05-02 10:53:48 +02:00
. /etc/bash/gaboshlib.include
2023-10-28 14:38:15 +02:00
# Chart Part
2023-10-28 14:33:21 +02:00
. dabo/functions/genchart.sh
2023-10-28 14:38:15 +02:00
# Chart Part End
2023-10-28 14:33:21 +02:00
2023-05-02 10:53:48 +02:00
g_nice
2023-11-09 14:29:23 +01:00
2023-05-02 10:53:48 +02:00
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}")
2023-11-03 16:58:46 +01:00
. /etc/bash/gaboshlib.include
2023-11-06 18:02:08 +01:00
g_tmp="$2"
2023-11-03 16:58:46 +01:00
#for bashfunc in $(find /etc/bash/gaboshlib -type f -name "g_*.bashfunc" -o -name "g_*.sh")
#do
# . "$bashfunc"
#done
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-11-01 14:49:56 +01:00
. dabo/functions/score.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}
2023-11-12 13:24:43 +01:00
rm -rf ${g_tmp}/open-${tmpfile}
rm -rf ${g_tmp}/interim-${tmpfile}
rm -rf ${g_tmp}/output-${tmpfile}
2023-05-02 10:53:48 +02:00
2023-10-28 14:32:14 +02:00
# Chart Part
2023-10-28 17:42:16 +02:00
echo "$(head -n1 ${file}),Market Perrormance,Score,Buy Score,Sell Score,InTrade,Inetrim Result" >analyze-${analyzedate}/chart-${tmpfile}
2023-10-28 14:32:14 +02:00
# Chart Part End
2023-11-12 14:36:33 +01:00
# create market performance array of timeframe
for f_market_perf_line in $(cat data/botdata/MARKET_PERFORMANCE.csv | cut -d, -f1,2 | sed 's/:[0-9][0-9],/,/; s/ /-/g; s/:/-/g')
do
f_market_perf_date=${f_market_perf_line%,*}
f_market_perf_date=${f_market_perf_date//[^0-9]/}
f_market_perf=${f_market_perf_line#*,}
f_market_performace_array[${f_market_perf_date}]=${f_market_perf}
done
2023-11-15 16:50:59 +01:00
# go through data of time
2023-11-06 22:00:08 +01:00
local f_lines
2023-11-07 16:02:19 +01:00
mapfile -t f_lines <<<$(egrep "^${ANALYZE_TIME}" "$file" | grep -v ',,')
2023-11-15 16:50:59 +01:00
local f_line
# for line in "${f_lines[@]}"
for f_line in "${f_lines[@]}"
2023-05-02 10:53:48 +02:00
do
2023-10-28 17:17:43 +02:00
f_sell_score=""
f_buy_score=""
2023-11-12 17:44:30 +01:00
2023-11-15 16:50:59 +01:00
# ad line to last lines array...
f_last_lines_array+=("${f_line}")
# ... and stop here until 4 elements are present
[ -z "${f_last_lines_array[3]}" ] && continue
2023-11-12 17:44:30 +01:00
echo "$line" >>${g_tmp}/${tmpfile}
2023-11-12 17:55:30 +01:00
# get vars
2023-11-12 18:02:28 +01:00
get_vars_from_csv ${g_tmp}/${tmpfile} || continue
2023-11-12 17:55:30 +01:00
# get time from market_performance
2023-11-12 17:48:49 +01:00
local f_time="${f_date%:*}"
f_time="${f_time//[^0-9]/}"
2023-11-12 17:50:55 +01:00
2023-11-12 17:55:30 +01:00
# get markel_performance from array with time
2023-11-12 17:50:55 +01:00
f_market_performance=${f_market_performace_array[${f_time}]}
2023-11-12 14:47:56 +01:00
[ -z "${f_market_performance}" ] && f_market_performance=${f_market_performance_before}
2023-11-12 17:25:28 +01:00
f_market_performance_before=${f_market_performance}
echo "MARKET_PERF=$f_market_performance"
2023-11-12 14:36:33 +01:00
2023-05-02 10:53:48 +02:00
if [ -f "${g_tmp}/open-${tmpfile}" ]
then
if [ "${ANALYZE_VERBOSE}" -eq "0" ]
then
2023-05-30 17:48:27 +02:00
check_sell_conditions ${g_tmp}/${tmpfile} >>${g_tmp}/output-${tmpfile}
else
2023-05-30 17:48:27 +02:00
check_sell_conditions ${g_tmp}/${tmpfile}
fi
2023-05-02 10:53:48 +02:00
fi
if ! [ -f "${g_tmp}/open-${tmpfile}" ]
then
if [ "${ANALYZE_VERBOSE}" -eq "0" ]
then
2023-05-30 17:48:27 +02:00
check_buy_conditions ${g_tmp}/${tmpfile} >>${g_tmp}/output-${tmpfile}
else
2023-05-30 17:48:27 +02:00
check_buy_conditions ${g_tmp}/${tmpfile}
fi
2023-05-02 10:53:48 +02:00
fi
2023-11-15 16:54:52 +01:00
## Chart Part
#local f_intrade=0
#local f_score=${f_buy_score}
#local f_interim="0"
#[ -z "${f_buy_score}" ] && f_buy_score=0
#if [ -z "${f_sell_score}" ]
#then
# f_sell_score=0
#else
# f_score=${f_sell_score}
# f_intrade=1
# if [ -s ${g_tmp}/interim-${tmpfile} ]
# then
# f_interim=$(tail -n1 ${g_tmp}/interim-${tmpfile})
# f_interim=$(g_calc "${f_interim}-${FEE}")
# fi
#fi
#echo "$f_line,${f_market_performance},${f_score},${f_buy_score},${f_sell_score},${f_intrade},${f_interim}" >>analyze-${analyzedate}/chart-${tmpfile}
2023-10-28 15:16:21 +02:00
# Chart Part End
2023-05-02 10:53:48 +02:00
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
2023-11-12 13:24:43 +01:00
result=$(g_percentage-diff ${BUY_PRICE} ${f_price})
2023-11-03 15:31:49 +01:00
result=$(g_calc "${result}-${FEE}")
2023-05-02 10:53:48 +02:00
echo "$result" >>${g_tmp}/result-${tmpfile}
2023-11-12 13:24:43 +01:00
echo "RESULT: ${result}% (${BUY_PRICE} -> ${f_price})" >>${g_tmp}/output-${tmpfile}
2023-05-02 10:53:48 +02:00
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
2023-11-03 15:31:49 +01:00
complete_result=$(g_calc "$complete_result+$result" | xargs printf "%.2f")
2023-05-02 10:53:48 +02:00
done
2023-05-24 22:19:40 +02:00
2023-11-07 16:02:19 +01:00
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))
2023-05-24 22:19:40 +02:00
2023-05-25 11:28:09 +02:00
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}
2023-05-02 10:53:48 +02:00
echo "=====================================" >>${g_tmp}/output-${tmpfile}
2023-11-03 17:00:07 +01:00
echo "${complete_result}" >>/tmp/overall-result-${tmpfile}
2023-05-24 22:19:40 +02:00
2023-05-02 10:53:48 +02:00
cat ${g_tmp}/output-${tmpfile} >"analyze-${analyzedate}/${tmpfile}.log"
2023-11-01 10:45:11 +01:00
2023-11-01 11:00:45 +01:00
# Chart Part
2023-11-01 10:45:11 +01:00
if [ -s "analyze-${analyzedate}/chart-${tmpfile}" ]
then
2023-11-01 13:22:17 +01:00
#g_echo "generating chart for $file from analyze-${analyzedate}/chart-${tmpfile}"
2023-11-01 10:45:11 +01:00
echo "<html><head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' href='../data/browser.css'>
<link rel='stylesheet' type='text/css' href='../data/charts.min.css'>
<title>analyze.sh ${ANALYZE_TIME}</title>
</head>
<body>
<h1>analyze.sh ${ANALYZE_TIME}</h1>
" >analyze-${analyzedate}/chart-${tmpfile}.html
echo "Price, EMA, Levels" >>analyze-${analyzedate}/chart-${tmpfile}.html
genchart analyze-${analyzedate}/chart-${tmpfile} 500 2,25,26,27,28,29,30,31,32,33,34,35,4,36,37,38,39 green,DarkSlateGrey,DarkSlateGrey,Gold,DarkOrange,DarkOrange,GoldenRod,GoldenRod,GoldenRod,GoldenRod,DarkOrange,DarkOrange,MidnightBlue,Indigo,DarkSlateBlue,DodgerBlue,DeepSkyBlue >>analyze-${analyzedate}/chart-${tmpfile}.html
echo "Score" >>analyze-${analyzedate}/chart-${tmpfile}.html
genchart analyze-${analyzedate}/chart-${tmpfile} 500 42,46,45,41 Green,Red,Orange >>analyze-${analyzedate}/chart-${tmpfile}.html
#echo "Sell Score" >>analyze-${analyzedate}/chart-${tmpfile}.html
#genchart analyze-${analyzedate}/chart-${tmpfile} 500 42 green,blue >>analyze-${analyzedate}/chart-${tmpfile}.html
echo "MACD" >>analyze-${analyzedate}/chart-${tmpfile}.html
genchart analyze-${analyzedate}/chart-${tmpfile} 500 8,6,7 >>analyze-${analyzedate}/chart-${tmpfile}.html
echo "RSIs" >>analyze-${analyzedate}/chart-${tmpfile}.html
genchart analyze-${analyzedate}/chart-${tmpfile} 500 10,11,12,14,15,16,17,13 >>analyze-${analyzedate}/chart-${tmpfile}.html
echo "</body></html>">>analyze-${analyzedate}/chart-${tmpfile}.html
fi
2023-11-01 11:00:45 +01:00
# Chart Part END
2023-05-02 10:53:48 +02:00
}
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
2023-05-22 11:06:51 +02:00
analyzecounter=0
2023-10-28 14:32:14 +02:00
# Chart Part
[ -e data/charts.min.css ] || wget -q https://raw.githubusercontent.com/ChartsCSS/charts.css/main/dist/charts.min.css -O data/charts.min.css
# Chart Part End
2023-05-02 10:53:48 +02:00
for file in $@
do
2023-10-27 12:03:48 +02:00
echo "${file}" | egrep -q "BALANCE|-INDEX" && continue
2023-05-10 16:26:00 +02:00
2023-11-07 16:02:19 +01:00
lines=$(egrep "^${ANALYZE_TIME}" "$file" | grep -v ',,' | wc -l)
2023-06-02 09:53:01 +02:00
if [ $lines -lt 150 ]
2023-05-10 16:26:00 +02:00
then
2023-06-02 09:53:01 +02:00
g_echo "Only $lines lines for given timeframe (${ANALYZE_TIME}) in $file - ignoring files with less then 150!"
2023-05-10 16:26:00 +02:00
continue
fi
2023-05-10 09:29:53 +02:00
if [ "${ANALYZE_BATCH}" -eq "0" ]
then
2023-11-06 17:41:38 +01:00
echo -n " \"analyze ${file} ${g_tmp}\"" >>/tmp/parallel-$$
2023-05-10 09:29:53 +02:00
else
2023-11-06 17:41:38 +01:00
analyze ${file} ${g_tmp}
2023-05-10 09:29:53 +02:00
fi
2023-05-22 11:06:51 +02:00
2023-10-28 14:38:15 +02:00
done
if [ "${ANALYZE_BATCH}" -eq "0" ]
then
export -f g_echo_note
export g_tmp
export analyzedate
. /tmp/parallel-$$
fi
2023-11-03 17:00:07 +01:00
analyzecounter=$(cat /tmp/overall-result-* | egrep -v "^0$" | wc -l)
2023-11-03 17:14:55 +01:00
echo "OVERALL RESULT (average): $(cat /tmp/overall-result-* | awk "{ SUM += \$1 / ${analyzecounter} } END { printf(\"%2.2f\", SUM) }")%" | tee analyze-${analyzedate}/overall-result.log
2023-11-03 17:00:07 +01:00
echo "OVERALL RESULT: $(cat /tmp/overall-result-* | awk '{ SUM += $1 } END { print SUM }')%" | tee analyze-${analyzedate}/overall-result.log
2023-05-02 10:53:48 +02:00
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
2023-05-25 11:06:31 +02:00
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
2023-05-02 10:53:48 +02:00
echo "
Complete Results" >>analyze-${analyzedate}/overall-result.log
2023-05-25 11:28:09 +02:00
grep "COMPLETE:RESULT:" analyze-${analyzedate}/analyze-overall.log >>analyze-${analyzedate}/overall-result.log
2023-05-02 10:53:48 +02:00
echo "
Trades" >>analyze-${analyzedate}/overall-result.log
egrep "BUY: |SELL: " analyze-${analyzedate}/analyze-overall.log >>analyze-${analyzedate}/overall-result.log