Files
dabo/dabo/functions/webpage.sh
2023-10-30 16:33:53 +01:00

309 lines
15 KiB
Bash

function webpage {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
# Get charts.css
[ -e ../charts.min.css ] || wget -q https://raw.githubusercontent.com/ChartsCSS/charts.css/main/dist/charts.min.css -O ../charts.min.css
# create status webpage
echo "<html>
<head>
<meta charset='UTF-8'>
<meta http-equiv='refresh' content='${INTERVAL}'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' href='/browser.css'>
<link rel='stylesheet' type='text/css' href='/charts.min.css'>
<title>Dabo! on ${STOCK_EXCHANGE} - ${URL}</title>
</head>
<body>
<h1>State of Dabo-Bot! on ${STOCK_EXCHANGE} - ${URL} (ReadOnly)</h1>
<h1>Last update $(date '+%F %T')</h1>" >../index.html.tmp
local f_SPOT_BALANCE=$(tail -n1 "asset-histories/BALANCESPOT${CURRENCY}.history.csv" | cut -d, -f2)
local f_COMPLETE_BALANCE=$(tail -n1 "asset-histories/BALANCECOMPLETE${CURRENCY}.history.csv" | cut -d, -f2)
echo '<h2>Overview</h2>' >>../index.html.tmp
echo "<table>
<tr>
<td><b>Overall Balance:</b></td>
<td><font color=green><b>${CURRENCY} ${f_COMPLETE_BALANCE}</b></font></td>
</td>
<tr>
<td>SPOT Balance (invested):</td>
<td><font color=blue>${CURRENCY} $f_SPOT_BALANCE</font></td>
</tr>
<tr>
<td>Free Balance (not invested):</td>
<td><font color=yellow>${CURRENCY} ${CURRENCY_BALANCE}</font></td>
</tr>
</table>" >>../index.html.tmp
echo "<h2>Trade Performance (overall closed trades)</h2>" >>../index.html.tmp
echo "<table>
<tr>
<td><b>Performance</b></td>
<td><b>Average (investment portion $INVEST%)</b></td>
<td><b>Added</b></td>
<tr>
<tr>
<td>Day (last 24 hours):</td>
<td> $(find trade-histories -name "*-closed.history.csv.result" -mtime -1 -exec cat {} \; | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM/100*${INVEST})}")%</td>
<td> $(find trade-histories -name "*-closed.history.csv.result" -mtime -1 -exec cat {} \; | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM)}")%</td>
<tr>
<td>Week (last 7 days):</td>
<td> $(find trade-histories -name "*-closed.history.csv.result" -mtime -7 -exec cat {} \; | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM/100*${INVEST})}")%</td>
<td> $(find trade-histories -name "*-closed.history.csv.result" -mtime -7 -exec cat {} \; | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM)}")%</td>
</tr>
<tr>
<td>Month (last 30 days):</td>
<td> $(find trade-histories -name "*-closed.history.csv.result" -mtime -30 -exec cat {} \; | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM/100*${INVEST})}")%</td>
<td> $(find trade-histories -name "*-closed.history.csv.result" -mtime -30 -exec cat {} \; | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM)}")%</td>
</tr>
<tr>
<td>3 Month (last 91 days):</td>
<td> $(find trade-histories -name "*-closed.history.csv.result" -mtime -91 -exec cat {} \; | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM/100*${INVEST})}")%</td>
<td> $(find trade-histories -name "*-closed.history.csv.result" -mtime -91 -exec cat {} \; | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM)}")%</td>
</tr>
<tr>
<td>6 Month (last 183 days):</td>
<td> $(find trade-histories -name "*-closed.history.csv.result" -mtime -183 -exec cat {} \; | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM/100*${INVEST})}")%</td>
<td> $(find trade-histories -name "*-closed.history.csv.result" -mtime -183 -exec cat {} \; | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM)}")%</td>
</tr>
<tr>
<td>Year</td>
<td> $(find trade-histories -name "*-closed.history.csv.result" -mtime -365 -exec cat {} \; | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM/100*${INVEST})}")%</td>
<td> $(find trade-histories -name "*-closed.history.csv.result" -mtime -365 -exec cat {} \; | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM)}")%</td>
</tr>
<tr>
<td>Performance complete):</td>
<td> $(find trade-histories -name "*-closed.history.csv.result" -exec cat {} \; | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM/100*${INVEST}) }")%</td>
<td> $(find trade-histories -name "*-closed.history.csv.result" -exec cat {} \; | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM)}")%</td>
</tr>
</table>" >>../index.html.tmp
echo '<h2>Open Trades (Invested Assets)</h2>' >>../index.html.tmp
echo "<table width='100%'>" >>../index.html.tmp
local line
cat EXCHANGE_GET_BALANCES_CMD_OUT | grep -v ${CURRENCY} | sort | while read line
do
local spot_balances=($(echo $line | sed 's/,/ /g'))
echo "<tr>
<td><a href=\"botdata/asset-histories/${spot_balances[0]}${CURRENCY}.history.csv\">${spot_balances[0]}</a> <a href=\"https://www.coingecko.com/de/munze/$(egrep -i ^$(echo ${spot_balances[0]} | sed "s/${CURRENCY}$//"), COINGECKO_IDS | cut -d, -f2 )\">🔗</a></td>
<td>${spot_balances[1]}</td>
<td>${CURRENCY} ${spot_balances[2]} ( $(cat DIFF_BUY_PRICE_${spot_balances[0]}${CURRENCY})%)</td>
</tr>" >>../index.html.tmp
done
echo "</table>" >>../index.html.tmp
echo "<h2>Market Performance ( $(cat MARKET_PERFORMANCE_LATEST)%)</h2>" >>../index.html.tmp
#echo "<table width='100%'><tr><td><details><summary>Charts</summary>" >>../index.html.tmp
echo "<details><summary>Charts</summary>" >>../index.html.tmp
echo "Krypto" >>../index.html.tmp
genchart MARKET_PERFORMANCE.csv 50 2,3,4,5,6,7 red-or-green,gold,royalblue,lightyellow,MediumSlateBlue,Sienna >>../index.html.tmp
echo "Commodities" >>../index.html.tmp
genchart MARKET_PERFORMANCE.csv 50 2,12,10,13 red-or-green,gold,SandyBrown,Sienna >>../index.html.tmp
echo "World Economic data" >>../index.html.tmp
genchart MARKET_PERFORMANCE.csv 50 2,11,15 red-or-green,Yellow,Sienna >>../index.html.tmp
echo "US Economic data" >>../index.html.tmp
genchart MARKET_PERFORMANCE.csv 50 2,8,17,16,18,19,20,21,14,9 red-or-green,OliveDrab,Yellow,Orange,DeepSkyBlue,DarkMagenta,PeachPuff,PaleTurquoise,Pink,PapayaWhip >>../index.html.tmp
#echo "</details></td></tr></table>" >>../index.html.tmp
echo "</details>" >>../index.html.tmp
echo "<details><summary>Table</summary><table width='100%'>" >>../index.html.tmp
echo "<tr>" >>../index.html.tmp
head -n1 MARKET_PERFORMANCE.csv | perl -pe 's/,/\n/g' | tr [:lower:] [:upper:] | while read f_mperfcol
do
echo "<td><b>${f_mperfcol}</b></td>" >>../index.html.tmp
done
echo "</tr>" >>../index.html.tmp
egrep "^[0-9][0-9]" MARKET_PERFORMANCE.csv | tail -n10 | while read f_mperfline
do
f_mperfline="$(echo ${f_mperfline} | perl -pe 's/ /_/g; s/,/ /g')"
echo "<tr>" >>../index.html.tmp
#echo "${mperfline}" | perl -pe 's/,/\n/g' | while read f_mperfcol
for f_mperfcol in ${f_mperfline}
do
if echo "${f_mperfcol}" | grep -q ":"
then
f_mperfcol="$(echo ${f_mperfcol} | perl -pe 's/_/ /')"
echo "<td><b>${f_mperfcol}</b></td>" >>../index.html.tmp
else
echo "<td> ${f_mperfcol}%</td>" >>../index.html.tmp
fi
done
echo "</tr>" >>../index.html.tmp
done
echo "</table></details>" >>../index.html.tmp
echo "<a href=\"botdata/MARKET_PERFORMANCE.csv\">Complete list</a>" >>../index.html.tmp
echo '<h2>Latest trades</h2>' >>../index.html.tmp
echo '<h3>Open</h3>' >>../index.html.tmp
echo "<table width='100%'><tr>" >>../index.html.tmp
echo "<td>Date</td>
<td>Asset</td>
<td>Action</td>
<td>${CURRENCY} Quantity</td>
<td>${CURRENCY} Price</td>
<td>Commission</td>
<td>Comment</td></tr>" >>../index.html.tmp
local f_trade_file
for f_trade_file in $(ls -t trade-histories/trade-*-open.history.csv 2>/dev/null)
do
local tradeline=$(tail -n1 ${f_trade_file})
local asset=$(echo ${f_trade_file} | cut -d. -f3 | cut -d- -f1)
local interimfile=$(echo ${f_trade_file} | sed 's/open\.history\.csv/interim\.history\.csv/')
echo "<td>$(echo ${tradeline} | cut -d, -f1)</td>
<td><a href=\"botdata/asset-histories/${asset}.history.csv\">${asset}</a> <a href=\"https://www.coingecko.com/de/munze/$(egrep -i ^$(echo ${asset} | sed "s/${CURRENCY}$//"), COINGECKO_IDS | cut -d, -f2 )\">🔗</a></td>
<td>$(echo ${tradeline} | cut -d, -f2)</td>
<td>$(echo ${tradeline} | cut -d, -f4)</td>
<td>$(echo ${tradeline} | cut -d, -f5)</td>
<td>$(echo ${tradeline} | cut -d, -f6)</td>
<td>$(echo ${tradeline} | cut -d, -f7,8,9,10,11,12,13,14,15)</td>
</tr>" >>../index.html.tmp
echo "<tr><td colspan='7'><details><summary>Chart</summary>" >>../index.html.tmp
genchart "$interimfile" >>../index.html.tmp
echo "</details></td></tr>" >>../index.html.tmp
done
echo "</table>" >>../index.html.tmp
echo '<h3>Closed</h3>' >>../index.html.tmp
echo "<table width='100%'><tr>" >>../index.html.tmp
echo "<td>Date</td>
<td>Asset</td>
<td>Action</td>
<td>${CURRENCY} Quantity</td>
<td>${CURRENCY} Price (result)</td>
<td>Commission</td>
<td>Comment</td></tr>" >>../index.html.tmp
for f_trade_file in $(ls -t trade-histories/trade-*-closed.history.csv 2>/dev/null | head -n 50)
do
local tradeline
cat ${f_trade_file} | while read tradeline
do
local f_action=$(echo ${tradeline} | cut -d, -f2)
local f_price=$(echo ${tradeline} | cut -d, -f5)
local tradedate=$(echo ${tradeline} | cut -d, -f1 | perl -pe 's/_([0-9][0-9])-([0-9][0-9])-([0-9][0-9])/ $1:$2/')
if echo ${f_action} | grep -q buy
then
echo ${f_price} >${g_tmp}/buyprice
local tradedatebuy=${tradedate}
fi
if echo ${f_action} | grep -q sell
then
if [ -s "${f_trade_file}.result" ]
then
local f_profit=$(cat "${f_trade_file}.result")
f_price="${f_price} ( ${f_profit}%)"
else
local f_profit=$(g_percentage-diff $(cat ${g_tmp}/buyprice) ${f_price})
f_price="${f_price} ( ${f_profit}%)"
echo "${f_profit}" >"${f_trade_file}.result"
fi
fi
local asset=$(echo ${f_trade_file} | cut -d. -f3 | cut -d- -f1)
interimfile=$(echo ${f_trade_file} | sed 's/closed\.history\.csv/interim\.history\.csv/')
echo "<td>${tradedate}</td>
<td><a href=\"botdata/asset-histories/${asset}.history.csv\">${asset}</a> <a href=\"https://www.coingecko.com/de/munze/$(egrep -i ^$(echo ${asset} | sed "s/${CURRENCY}$//"), COINGECKO_IDS | cut -d, -f2 )\">🔗</a></td>
<td>${f_action}</td>
<td>$(echo ${tradeline} | cut -d, -f4)</td>
<td>${f_price}</td>
<td>$(echo ${tradeline} | cut -d, -f6)</td>
<td>$(echo ${tradeline} | cut -d, -f7,8,9,10,11,12,13,14,15)</td> ">>../index.html.tmp
if echo ${f_action} | grep -q sell
then
echo "<tr><td colspan='7'><details><summary>Charts</summary>" >>../index.html.tmp
local tradeintervals=$(cat ${interimfile} | wc -l)
head -n1 asset-histories/${asset}.history.csv >${g_tmp}/trade.csv
grep -A ${tradeintervals} "${tradedatebuy}:" asset-histories/${asset}.history.csv >>${g_tmp}/trade.csv
# cache old trade charts
if ! [ -s "${interimfile}.chart" ]
then
echo "Price, EMA, Levels" >${interimfile}.chart
genchart "${g_tmp}/trade.csv" ${tradeintervals} 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 >>${interimfile}.chart
echo "MACD" >>${interimfile}.chart
genchart "${g_tmp}/trade.csv" ${tradeintervals} 8,6,7 >>${interimfile}.chart
echo "RSIs" >>${interimfile}.chart
genchart "${g_tmp}/trade.csv" ${tradeintervals} 10,11,12,14,15,16,17,13 >>${interimfile}.chart
fi
cat "${interimfile}.chart" >>../index.html.tmp
echo "</details></td></tr>" >>../index.html.tmp
else
echo "</tr>" >>../index.html.tmp
fi
done
done
echo "</table>" >>../index.html.tmp
#echo "<h2>Current config</h2>" >>../index.html.tmp
#echo "<pre>$(cat ../../dabo-bot.conf | perl -pe 's/\</&#60;/g; s/\>/&#62;/g;')</pre>" >>../index.html.tmp
echo '<h2>Available Assets and histories</h2>' >>../index.html.tmp
echo "<table width='100%'><tr>" >>../index.html.tmp
echo "<tr><td>Asset</td><td>Date</td><td>Price ${CURRENCY}</td><td>24h change (USD)</td></tr>" >>../index.html.tmp
local asset
cat ASSETS | egrep -v "${BLACKLIST}" | sort | while read asset
do
[ -s asset-histories/${asset}.history.csv ] || continue
echo "<td><a href=\"botdata/asset-histories/${asset}.history.csv\">${asset}</a> <a href=\"https://www.coingecko.com/de/munze/$(egrep -i ^$(echo ${asset} | sed "s/${CURRENCY}$//"), COINGECKO_IDS | cut -d, -f2 )\">🔗</a></td>" >>../index.html.tmp
kcurrency=$(echo ${asset} | sed "s/${CURRENCY}//")
#get_rate_percentage_min_before_and_now ${kcurrency} ${CURRENCY} 1440
local assetin=($(cat asset-histories/${asset}.history.csv | egrep -v "0.00000000$" | tail -n2 | head -n1 | sed 's/,/ /g'))
echo "<td>${assetin[0]} ${assetin[1]}</td>
<td>${CURRENCY} ${assetin[2]}</td>
<td> $(grep "^$kcurrency," ASSET_PRICE_CHANGE_PERCENTAGE_24H | cut -d, -f2)%</td>
</tr><tr><td colspan='4'>" >>../index.html.tmp
echo "<details><summary>${asset} Charts</summary>" >>../index.html.tmp
echo "Price, EMA, Levels" >>../index.html.tmp
genchart asset-histories/${asset}.history.csv 50 2,25,26,27,28,29,30,31,32,33,34,35,5,36,37,38 green,DarkSlateGrey,DarkSlateGrey,Gold,DarkOrange,DarkOrange,GoldenRod,GoldenRod,GoldenRod,GoldenRod,DarkOrange,DarkOrange,MidnightBlue,Indigo,DarkSlateBlue,DodgerBlue,DeepSkyBlue >>../index.html.tmp
echo "MACD" >>../index.html.tmp
genchart asset-histories/${asset}.history.csv 50 8,6,7 >>../index.html.tmp
echo "RSIs" >>../index.html.tmp
genchart asset-histories/${asset}.history.csv 50 10,11,12,14,15,16,17,13 >>../index.html.tmp
echo "</details></td></tr>" >>../index.html.tmp
done
echo "</table>" >>../index.html.tmp
echo "<h2>Top/Flop</h2>" >>../index.html.tmp
ls -1 trade-histories/*.result | cut -d . -f3-6 | sort -u | while read f_asset_results
do
f_asset=$(echo ${f_asset_results} | cut -d\- -f1)
echo $(cat trade-histories/*${f_asset_results} | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM)}") >trade-histories/${f_asset}.complete_result
done
echo "<h3>Top 10</h3>" >>../index.html.tmp
grep [0-9] trade-histories/*.complete_result | sort -t: -k2 -rn | head -n 10 | cut -d/ -f2 | perl -pe 's/\.complete_result:/ /; s/$/%<br>/' >>../index.html.tmp
echo "<h3>Flop 10</h3>" >>../index.html.tmp
grep [0-9] trade-histories/*.complete_result | sort -t: -k2 -n | head -n 10 | cut -d/ -f2 | perl -pe 's/\.complete_result:/ /; s/$/%<br>/' >>../index.html.tmp
echo '<h2>Complete trading histories</h2>' >>../index.html.tmp
echo "<table>" >>../index.html.tmp
find trade-histories -type f -name *.history.csv | cut -d/ -f2 | cut -d. -f1 | sort | while read asset
do
[ -s trade-histories/${asset}.history.csv ] || continue
echo "<tr><td><a href=\"botdata/trade-histories/${asset}.history.csv\">${asset}</a> <a href=\"https://www.coingecko.com/de/munze/$(egrep -i ^$(echo ${asset} | sed "s/${CURRENCY}$//"), COINGECKO_IDS | cut -d, -f2 )\">🔗</a></td></tr>" >>../index.html.tmp
done
echo "</table>" >>../index.html.tmp
# color magic
cat ../index.html.tmp | perl -pe 's/ (\-[0-9]+\.[0-9]+\%)/<font color=red>$1<\/font>/g; s/ ([0-9]+\.[0-9]+\%)/<font color=green>$1<\/font>/g;' >../index.html
#mv ../index.html.tmp ../index.html
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@ finished"
}