switch to CCXT

This commit is contained in:
olli 2024-06-10 20:35:22 +02:00
parent ecd6f3d7ff
commit f13038bf9c
15 changed files with 258 additions and 411 deletions

View File

@ -32,3 +32,6 @@ INVEST="5"
# Stop all trading and sell everything if the complete balance shrinks under this value in ${CURRENCY}
EMERGENCY_STOP="1000"
# Leverage
LEVERAGE=""

View File

@ -82,31 +82,36 @@ do
#[ ${FULL_LOOP} = 1 ] && transactions_overview
# stock data
if [ "${STOCK_EXCHANGE}" = "BINANCE" ]
then
# command for current token infos (function for setting var QUANTITY_LOT_CUT
TOKEN_INFO_CMD="binance_get_token_info"
# command for buying/selling a token
TRADE_CMD='binance-api-call POST /api/v3/order "&symbol=TOKEN&quoteOrderQty=QUANTITY&side=ACTION&type=MARKET"'
elif [ "${STOCK_EXCHANGE}" = "ONETRADING" ]
then
TOKEN_INFO_CMD="onetrading_get_token_info"
TRADE_CMD='onetrading-api-call POST public/v1/account/orders "--header \"Content-Type: application/json\" --data \"{\\\"instrument_code\\\":\\\"TOKEN\\\",\\\"side\\\":\\\"ACTION\\\",\\\"type\\\":\\\"MARKET\\\",\\\"amount\\\":\\\"QUANTITY\\\"}\""'
elif [ "${STOCK_EXCHANGE}" = "NONE" ]
#if [ "${STOCK_EXCHANGE}" = "BINANCE" ]
#then
# # command for current token infos (function for setting var QUANTITY_LOT_CUT
# TOKEN_INFO_CMD="binance_get_token_info"
# # command for buying/selling a token
# TRADE_CMD='binance-api-call POST /api/v3/order "&symbol=TOKEN&quoteOrderQty=QUANTITY&side=ACTION&type=MARKET"'
#elif [ "${STOCK_EXCHANGE}" = "ONETRADING" ]
#then
# TOKEN_INFO_CMD="onetrading_get_token_info"
# TRADE_CMD='onetrading-api-call POST public/v1/account/orders "--header \"Content-Type: application/json\" --data \"{\\\"instrument_code\\\":\\\"TOKEN\\\",\\\"side\\\":\\\"ACTION\\\",\\\"type\\\":\\\"MARKET\\\",\\\"amount\\\":\\\"QUANTITY\\\"}\""'
if [ "${STOCK_EXCHANGE}" = "NONE" ]
then
## stop here if STOCK_EXCHANGE not present
continue
fi
# Get current symbols
[ ${FULL_LOOP} = 1 ] && get_symbols
# Get current assets
get_assets || continue
# Sell something?
check_for_sell
#check_for_sell
# Get current balances
[ ${FULL_LOOP} = 1 ] && get_ccxt_balances || continue
[ ${FULL_LOOP} = 1 ] && get_balances || continue
# Get current balance
[ ${FULL_LOOP} = 1 ] && get_balance || continue
# Get current positions
[ ${FULL_LOOP} = 1 ] && get_positions || continue
# Buy something?
[ ${FULL_LOOP} = 1 ] && check_for_buy

View File

@ -1,5 +1,10 @@
# functions
# Export all functions and vars
set -a
BASEPATH=/dabo/htdocs
# load functions
for bashfunc in $(find ${BASEPATH}/../functions -type f -name "*.sh")
do
. "$bashfunc"
@ -7,10 +12,12 @@ done
. /etc/bash/gaboshlib.include
# vars
export LANGUAGE="en_US"
LANGUAGE="en_US"
g_tries=13
g_tries_delay=23
g_wget_opts="--timeout 10 --tries=2 --user-agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'"
set +a
# prepare directories
mkdir -p ${BASEPATH}/botdata/asset-histories

View File

@ -14,8 +14,8 @@ function f_ccxt {
return 1
fi
unset g_ccxt_jobs
local g_ccxt_jobs
#unset g_ccxt_jobs
#local g_ccxt_jobs
mapfile -t g_ccxt_jobs < <(jobs -r)
# Initialize ccxt in python if not initialized
[[ ${g_ccxt_jobs[*]} =~ *python-pipeexec.py* ]] || unset f_ccxt_initialized
@ -44,6 +44,12 @@ function f_ccxt {
# reference result to python-result
declare -ng f_ccxt_result=g_python_result
# Check for json output
f_ccxt_json_out=""
[[ $f_ccxt_result =~ ^\[ ]] && [[ $f_ccxt_result =~ \]$ ]] && f_ccxt_json_out=1
[[ $f_ccxt_result =~ ^\{ ]] && [[ $f_ccxt_result =~ \}$ ]] && f_ccxt_json_out=1
if [ -n "$f_ccxt_json_out" ]
then
# make the output jq-conform
# avoids errors like: "parse error: Invalid numeric literal at"
f_ccxt_result=${f_ccxt_result//\'/\"}
@ -51,5 +57,11 @@ function f_ccxt {
f_ccxt_result=${f_ccxt_result// True/ true}
f_ccxt_result=${f_ccxt_result// False/ false}
f_ccxt_result=${f_ccxt_result//,,/,}
fi
return 0
}

View File

@ -8,7 +8,7 @@ function get_asset {
[ -f "${f_ASSET_HIST_FILE}" ] || echo "Date and Time,Price" >"${f_ASSET_HIST_FILE}"
local f_line="${f_timestamp},$(egrep "^${f_ASSET}," EXCHANGE_GET_ASSETS_CMD_OUT | cut -d, -f2)"
local f_line="${f_timestamp},$(grep "^${f_ASSET}," CCXT_TICKERS | cut -d, -f2)"
echo "${f_line}" >>${f_ASSET_HIST_FILE}
local f_linecount=0

View File

@ -3,41 +3,17 @@ function get_assets {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
## determine assets with prices
local f_filename="EXCHANGE_GET_ASSETS_CMD"
# Try to collect assets
# get data from API
if [ ${STOCK_EXCHANGE} = "BINANCE" ]
then
binance-api-call GET /api/v3/ticker/price || return 1
# parse API output
cat ${g_tmp}/API_CMD_OUT | jq -r '.[] | .symbol + "," + .price' | grep "${CURRENCY}," | egrep -v "${TRANSFER_CURRENCY},|,0[\.][0]*$" >${f_filename}_OUT.tmp
elif [ ${STOCK_EXCHANGE} = "ONETRADING" ]
then
onetrading-api-call GET "public/v1/market-ticker" || return 1
#cat ${g_tmp}/API_CMD_OUT | jq -r '.[] | select(.state=="ACTIVE") | .instrument_code + "," + .last_price' | sed 's/_//' | grep "${CURRENCY}," | egrep -v "${TRANSFER_CURRENCY},|,0[\.][0]*$" >${f_filename}_OUT.tmp
cat ${g_tmp}/API_CMD_OUT | jq -r '.[] | .instrument_code + "," + .last_price' | sed 's/_//' | grep "${CURRENCY}," | egrep -v "${TRANSFER_CURRENCY},|,0[\.][0]*$" >${f_filename}_OUT.tmp
elif [ ${STOCK_EXCHANGE} = "NONE" ]
if [ ${STOCK_EXCHANGE} = "NONE" ]
then
f_no_exchange=1
else
#f_ccxt "print(exchange.fetch_tickers())" && echo $f_ccxt_result >CCXT_TICKERS
return 0
fi
cat ${g_tmp}/API_CMD_OUT >PRICETICKER
# timestamp for data
#f_timestamp=$(g_date_print)
# check output
if [ -s ${f_filename}_OUT.tmp ] && egrep -q "^[A-Z]+${CURRENCY},[0-9]*\.[0-9]+$" ${f_filename}_OUT.tmp
then
mv ${f_filename}_OUT.tmp ${f_filename}_OUT
else
g_echo_warn "${f_filename}_OUT.tmp has wrong Syntax. - Not updating ${f_filename}_OUT $(tail -n 10 ${f_filename}_OUT.tmp)"
return 2
f_ccxt "print(${STOCK_EXCHANGE}.fetch_tickers())" && [ -n "$f_ccxt_json_out" ] && echo $f_ccxt_result >CCXT_TICKERS_RAW
fi
jq -r '.[] | .symbol + "," + (.last|tostring)' CCXT_TICKERS_RAW | sed "s/:${CURRENCY},/,/; s/\///" | grep "${CURRENCY}," >CCXT_TICKERS
# Write file with asset list and ignore marketcap under LARGEST_MARKETCAP
cat EXCHANGE_GET_ASSETS_CMD_OUT | cut -d"," -f1 | while read f_ASSET
cat CCXT_TICKERS | cut -d"," -f1 | while read f_ASSET
do
# ignore marketcap under LARGEST_MARKETCAP
f_assetwocurrency=$(echo ${f_ASSET} | sed "s/$CURRENCY$//")
@ -59,7 +35,6 @@ function get_assets {
#get_asset "${f_ASSET}"
echo -n " \"get_asset ${f_ASSET}\"" >>/tmp/parallel
done
export -f get_asset
export f_timestamp
export csv_headline
. /tmp/parallel

View File

@ -0,0 +1,32 @@
function get_balance {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
f_ccxt "print(${STOCK_EXCHANGE}.fetch_balance ({\"currency\": \"$CURRENCY\"}))" && [ -n "$f_ccxt_json_out" ] && echo $f_ccxt_result >CCXT_BALANCE
# get current investmentbalance
f_CURRENCY_BALANCE=$(jq -r ".${CURRENCY}.free" CCXT_BALANCE)
if g_num_valid_number "${f_CURRENCY_BALANCE}"
then
g_echo_note "=== Investmentbudget: $f_CURRENCY_BALANCE $CURRENCY"
printf -v CURRENCY_BALANCE %.2f ${f_CURRENCY_BALANCE}
else
g_echo_warn "Could not determine CURRENCY_BALANCE (${f_CURRENCY_BALANCE} ${CURRENCY}) from file CCXT_BALANCE $(tail -n 10 CCXT_BALANCE)"
return 3
fi
f_USED_BALANCE=$(jq -r ".${CURRENCY}.used" CCXT_BALANCE)
printf -v USED_BALANCE %.2f ${f_USED_BALANCE}
f_COMPLETE_BALANCE=$(jq -r ".${CURRENCY}.total" CCXT_BALANCE)
printf -v COMPLETE_BALANCE %.2f ${f_COMPLETE_BALANCE}
# write balance history
g_echo_note "=== Total Balance: $f_COMPLETE_BALANCE $CURRENCY"
g_echo_note "=== Free Balance: $f_CURRENCY_BALANCE $CURRENCY"
g_echo_note "=== Used Balance: $f_USED_BALANCE $CURRENCY"
echo "$f_timestamp,$COMPLETE_BALANCE" >>"asset-histories/BALANCECOMPLETE${CURRENCY}.history.csv"
echo "$f_timestamp,$USED_BALANCE" >>"asset-histories/BALANCEUSED${CURRENCY}.history.csv"
echo "$f_timestamp,$CURRENCY_BALANCE" >>"asset-histories/BALANCE${CURRENCY}.history.csv"
}

View File

@ -1,132 +0,0 @@
function get_balances {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
local f_filename="EXCHANGE_GET_BALANCES_CMD"
# get data from API
if [ ${STOCK_EXCHANGE} = "BINANCE" ]
then
binance_convert_dust
binance-api-call GET sapi/v1/capital/config/getall || return 1
# parse outout
cat ${g_tmp}/API_CMD_OUT | jq -r '.[] | .coin + "," + .free' | egrep -v ",0$|,0[\.][0]*$|${TRANSFER_CURRENCY}" | sort >${f_filename}_OUT.tmp_raw
elif [ ${STOCK_EXCHANGE} = "ONETRADING" ]
then
onetrading-api-call GET public/v1/account/balances || return 1
cat ${g_tmp}/API_CMD_OUT | jq -r '.balances[] | .currency_code + "," + .available' | egrep -v ",0$|,0[\.][0]*$|${TRANSFER_CURRENCY}" | sort >${f_filename}_OUT.tmp_raw
else
return 0
fi
if ! [ -s ${f_filename}_OUT.tmp_raw ] && egrep -q "^[A-Z]+,[0-9]*\.[0-9]+$" ${f_filename}_OUT.tmp_raw
then
g_echo_warn "${f_filename}_OUT.tmp_raw has wrong Syntax or is empty. - Not updating ${f_filename}_OUT $(tail -n 10 ${f_filename}_OUT.tmp_raw)"
return 2
fi
# CURRENCY is defined
if [ -n "${CURRENCY}" ]
then
# put balance of (main) currency in global CURRENCY_BALANCE
local f_CURRENCY_BALANCE=$(egrep "^${CURRENCY}," ${f_filename}_OUT.tmp_raw | cut -d"," -f2)
if echo "${f_CURRENCY_BALANCE}" | egrep -q "^[0-9]*\.[0-9]*"
then
CURRENCY_BALANCE=${f_CURRENCY_BALANCE}
g_echo_note "Investmentbudget: $CURRENCY_BALANCE $CURRENCY"
else
g_echo_warn "Could not determine CURRENCY_BALANCE (${f_CURRENCY_BALANCE} ${CURRENCY}) from file ${f_filename}_OUT.tmp_raw $(cat ${f_filename}_OUT.tmp_raw)"
return 3
fi
# add balance in CURRNCY
local f_line
for f_line in $(cat ${f_filename}_OUT.tmp_raw)
do
local f_ASSET=$(echo ${f_line} | cut -d, -f1)
local f_QUANTITY=$(echo ${f_line} | cut -d, -f2)
if [ "${f_ASSET}" = "${CURRENCY}" ]
then
g_echo_note "own currency line $f_line"
echo "${f_ASSET},${f_QUANTITY},$f_QUANTITY,1.00000000" >>${f_filename}_OUT.tmp
continue
fi
local f_ASSET_HIST_FILE="asset-histories/${f_ASSET}${CURRENCY}.history.csv"
if ! [ -s ${f_ASSET_HIST_FILE} ]
then
g_echo_note "No history file (${f_ASSET_HIST_FILE}) found for asset (${f_ASSET}) - ignoring $f_line."
continue
fi
# ignore non traded currencies by bot
ls trade-histories/trade-*.${f_ASSET}${CURRENCY}-open.history.csv >/dev/null 2>&1 || continue
# get last prices
local f_LAST_EXCHANGE_PRICE=$(tail -n1 ${f_ASSET_HIST_FILE} | head -n1 | cut -d, -f2)
if ! echo "$f_LAST_EXCHANGE_PRICE" | egrep -q "^[0-9]"
then
g_echo_warn "didn't get latest price from ${f_ASSET_HIST_FILE}"
continue
fi
# calculate quantity in CURRENCY
local f_QUANTITY_CURRENCY=$(echo "scale=8; $f_LAST_EXCHANGE_PRICE*$f_QUANTITY" | bc -l | sed 's/^\./0./;')
if ! echo "$f_QUANTITY_CURRENCY" | egrep -q "^[0-9]"
then
g_echo_warn "could not calculate quantity in ${CURRENCY}"
continue
fi
# ignore low values
if [ $(echo "$f_QUANTITY_CURRENCY < 2" | bc -l) -ne 0 ]
then
g_echo_note "${f_ASSET}: Ignore low quantity asset ${f_ASSET}(${f_QUANTITY})"
continue
else
g_echo_note "${f_ASSET}: Going on with non-low quantity (>1) asset ${f_ASSET}(${f_QUANTITY})"
fi
echo "${f_ASSET},${f_QUANTITY},${f_QUANTITY_CURRENCY},${f_LAST_EXCHANGE_PRICE}" >>${f_filename}_OUT.tmp
done
fi
if [ -s ${f_filename}_OUT.tmp ] && egrep -q "^[A-Z]+,[0-9]*\.[0-9]+,[0-9]*\.[0-9]+,[0-9]*\.[0-9]+$" ${f_filename}_OUT.tmp
then
mv ${f_filename}_OUT.tmp ${f_filename}_OUT
else
g_echo_warn "${f_filename}_OUT.tmp has wrong Syntax or is empty. - Not updating ${f_filename}_OUT $(tail -n 10 ${f_filename}_OUT.tmp)"
return 2
fi
# get complete spot balance
local f_SPOT_BALANCE=0
local f_EXCHANGE_GET_BALANCES_CMD_OUT
for f_EXCHANGE_GET_BALANCES_CMD_OUT in $(cat EXCHANGE_GET_BALANCES_CMD_OUT | grep -v ^$CURRENCY,)
do
#local f_ASSET=$(echo ${f_EXCHANGE_GET_BALANCES_CMD_OUT} | cut -d"," -f1)
local f_QUANTITY_CURRENCY=$(echo ${f_EXCHANGE_GET_BALANCES_CMD_OUT} | cut -d"," -f3)
local f_SPOT_BALANCE=$(echo "scale=2; $f_SPOT_BALANCE+$f_QUANTITY_CURRENCY" | bc -l)
done
# calculate complete balance
local f_COMPLETE_BALANCE=$(echo "scale=2; $CURRENCY_BALANCE+$f_SPOT_BALANCE" | bc -l)
#local f_timestamp=$(g_date_print)
# write balance history
echo "$f_timestamp,$f_COMPLETE_BALANCE" >>"asset-histories/BALANCECOMPLETE${CURRENCY}.history.csv"
echo "$f_timestamp,$f_SPOT_BALANCE" >>"asset-histories/BALANCESPOT${CURRENCY}.history.csv"
echo "$f_timestamp,$CURRENCY_BALANCE" >>"asset-histories/BALANCE${CURRENCY}.history.csv"
### global vars for overall performance last day, week, month
#get_rate_percentage_min_before_and_now BALANCECOMPLETE ${CURRENCY} 1440
#f_perf_day=${f_exchange_rate_diff_percentage}
#get_rate_percentage_min_before_and_now BALANCECOMPLETE ${CURRENCY} 10080
#f_perf_week=${f_exchange_rate_diff_percentage}
#get_rate_percentage_min_before_and_now BALANCECOMPLETE ${CURRENCY} 43200
#f_perf_month=${f_exchange_rate_diff_percentage}
}

View File

@ -1,135 +0,0 @@
function get_ccxt_balances {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
local f_filename="CCXT_GET_BALANCES_CMD"
#f_ccxt "print(${STOCK_EXCHANGE}.fetch_balance ({"currency": "USDT"}))" && echo $f_ccxt_result >CCXT_BALANCES_RAW
f_ccxt "print(${STOCK_EXCHANGE}.fetch_balance ())" && echo $f_ccxt_result >CCXT_BALANCES_RAW
return 0
# get data from API
if [ ${STOCK_EXCHANGE} = "BINANCE" ]
then
binance_convert_dust
binance-api-call GET sapi/v1/capital/config/getall || return 1
# parse outout
cat ${g_tmp}/API_CMD_OUT | jq -r '.[] | .coin + "," + .free' | egrep -v ",0$|,0[\.][0]*$|${TRANSFER_CURRENCY}" | sort >${f_filename}_OUT.tmp_raw
elif [ ${STOCK_EXCHANGE} = "ONETRADING" ]
then
onetrading-api-call GET public/v1/account/balances || return 1
cat ${g_tmp}/API_CMD_OUT | jq -r '.balances[] | .currency_code + "," + .available' | egrep -v ",0$|,0[\.][0]*$|${TRANSFER_CURRENCY}" | sort >${f_filename}_OUT.tmp_raw
fi
if ! [ -s ${f_filename}_OUT.tmp_raw ] && egrep -q "^[A-Z]+,[0-9]*\.[0-9]+$" ${f_filename}_OUT.tmp_raw
then
g_echo_warn "${f_filename}_OUT.tmp_raw has wrong Syntax or is empty. - Not updating ${f_filename}_OUT $(tail -n 10 ${f_filename}_OUT.tmp_raw)"
return 2
fi
# CURRENCY is defined
if [ -n "${CURRENCY}" ]
then
# put balance of (main) currency in global CURRENCY_BALANCE
local f_CURRENCY_BALANCE=$(egrep "^${CURRENCY}," ${f_filename}_OUT.tmp_raw | cut -d"," -f2)
if echo "${f_CURRENCY_BALANCE}" | egrep -q "^[0-9]*\.[0-9]*"
then
CURRENCY_BALANCE=${f_CURRENCY_BALANCE}
g_echo_note "Investmentbudget: $CURRENCY_BALANCE $CURRENCY"
else
g_echo_warn "Could not determine CURRENCY_BALANCE (${f_CURRENCY_BALANCE} ${CURRENCY}) from file ${f_filename}_OUT.tmp_raw $(cat ${f_filename}_OUT.tmp_raw)"
return 3
fi
# add balance in CURRNCY
local f_line
for f_line in $(cat ${f_filename}_OUT.tmp_raw)
do
local f_ASSET=$(echo ${f_line} | cut -d, -f1)
local f_QUANTITY=$(echo ${f_line} | cut -d, -f2)
if [ "${f_ASSET}" = "${CURRENCY}" ]
then
g_echo_note "own currency line $f_line"
echo "${f_ASSET},${f_QUANTITY},$f_QUANTITY,1.00000000" >>${f_filename}_OUT.tmp
continue
fi
local f_ASSET_HIST_FILE="asset-histories/${f_ASSET}${CURRENCY}.history.csv"
if ! [ -s ${f_ASSET_HIST_FILE} ]
then
g_echo_note "No history file (${f_ASSET_HIST_FILE}) found for asset (${f_ASSET}) - ignoring $f_line."
continue
fi
# ignore non traded currencies by bot
ls trade-histories/trade-*.${f_ASSET}${CURRENCY}-open.history.csv >/dev/null 2>&1 || continue
# get last prices
local f_LAST_EXCHANGE_PRICE=$(tail -n1 ${f_ASSET_HIST_FILE} | head -n1 | cut -d, -f2)
if ! echo "$f_LAST_EXCHANGE_PRICE" | egrep -q "^[0-9]"
then
g_echo_warn "didn't get latest price from ${f_ASSET_HIST_FILE}"
continue
fi
# calculate quantity in CURRENCY
local f_QUANTITY_CURRENCY=$(echo "scale=8; $f_LAST_EXCHANGE_PRICE*$f_QUANTITY" | bc -l | sed 's/^\./0./;')
if ! echo "$f_QUANTITY_CURRENCY" | egrep -q "^[0-9]"
then
g_echo_warn "could not calculate quantity in ${CURRENCY}"
continue
fi
# ignore low values
if [ $(echo "$f_QUANTITY_CURRENCY < 2" | bc -l) -ne 0 ]
then
g_echo_note "${f_ASSET}: Ignore low quantity asset ${f_ASSET}(${f_QUANTITY})"
continue
else
g_echo_note "${f_ASSET}: Going on with non-low quantity (>1) asset ${f_ASSET}(${f_QUANTITY})"
fi
echo "${f_ASSET},${f_QUANTITY},${f_QUANTITY_CURRENCY},${f_LAST_EXCHANGE_PRICE}" >>${f_filename}_OUT.tmp
done
fi
if [ -s ${f_filename}_OUT.tmp ] && egrep -q "^[A-Z]+,[0-9]*\.[0-9]+,[0-9]*\.[0-9]+,[0-9]*\.[0-9]+$" ${f_filename}_OUT.tmp
then
mv ${f_filename}_OUT.tmp ${f_filename}_OUT
else
g_echo_warn "${f_filename}_OUT.tmp has wrong Syntax or is empty. - Not updating ${f_filename}_OUT $(tail -n 10 ${f_filename}_OUT.tmp)"
return 2
fi
# get complete spot balance
local f_SPOT_BALANCE=0
local f_EXCHANGE_GET_BALANCES_CMD_OUT
for f_EXCHANGE_GET_BALANCES_CMD_OUT in $(cat EXCHANGE_GET_BALANCES_CMD_OUT | grep -v ^$CURRENCY,)
do
#local f_ASSET=$(echo ${f_EXCHANGE_GET_BALANCES_CMD_OUT} | cut -d"," -f1)
local f_QUANTITY_CURRENCY=$(echo ${f_EXCHANGE_GET_BALANCES_CMD_OUT} | cut -d"," -f3)
local f_SPOT_BALANCE=$(echo "scale=2; $f_SPOT_BALANCE+$f_QUANTITY_CURRENCY" | bc -l)
done
# calculate complete balance
local f_COMPLETE_BALANCE=$(echo "scale=2; $CURRENCY_BALANCE+$f_SPOT_BALANCE" | bc -l)
#local f_timestamp=$(g_date_print)
# write balance history
echo "$f_timestamp,$f_COMPLETE_BALANCE" >>"asset-histories/BALANCECOMPLETE${CURRENCY}.history.csv"
echo "$f_timestamp,$f_SPOT_BALANCE" >>"asset-histories/BALANCESPOT${CURRENCY}.history.csv"
echo "$f_timestamp,$CURRENCY_BALANCE" >>"asset-histories/BALANCE${CURRENCY}.history.csv"
### global vars for overall performance last day, week, month
#get_rate_percentage_min_before_and_now BALANCECOMPLETE ${CURRENCY} 1440
#f_perf_day=${f_exchange_rate_diff_percentage}
#get_rate_percentage_min_before_and_now BALANCECOMPLETE ${CURRENCY} 10080
#f_perf_week=${f_exchange_rate_diff_percentage}
#get_rate_percentage_min_before_and_now BALANCECOMPLETE ${CURRENCY} 43200
#f_perf_month=${f_exchange_rate_diff_percentage}
}

View File

@ -16,6 +16,8 @@ function get_marketdata_yahoo_historic {
[[ $f_item = "TAO-USD" ]] && f_item="TAO22974-USD"
[[ $f_item = "UNI-USD" ]] && f_item="UNI7083-USD"
[[ $f_item = "SUI-USD" ]] && f_item="SUI20947-USD"
[[ $f_item = "BLAZE-USD" ]] && f_item="BLAZE30179-USD"
[[ $f_item = "BEER-USD" ]] && f_item="BEER31337-USD"
# end if already failed the last hour
if [ -f FAILED_YAHOO_${f_name}_HISTORIC_DOWNLOAD ]

View File

@ -0,0 +1,61 @@
function get_positions {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
local f_symbol f_symbols
# build python array of symbols
for f_symbol in "${f_symbols_array[@]}"
do
if [ -z "$LEVERAGE" ]
then
[[ $f_symbol =~ /${CURRENCY}$ ]] && f_symbols+="'$f_symbol', "
else
[[ $f_symbol =~ /${CURRENCY}:${CURRENCY}$ ]] && f_symbols+="'$f_symbol', "
fi
done
[ -z "$f_symbols" ] && return 1
f_ccxt "print($STOCK_EXCHANGE.fetchPositions(symbols=[${f_symbols}]))" && [ -n "$f_ccxt_json_out" ] && echo $f_ccxt_result >CCXT_POSITIONS_RAW
jq -r "
.[] |
select(.entryPrice != 0) |
.symbol + \",\" + (.notional|tostring) + \",\" + (.entryPrice|tostring) + \",\" + (.markPrice|tostring) + \",\" + .side + \",\" + (.leverage|tostring)
" CCXT_POSITIONS_RAW >POSITIONS
get_position_array
}
function get_position_array {
g_array POSITIONS f_get_positions_array
}
function get_position_line_vars {
local f_pos_line=$1
g_array $f_pos_line f_position_array ,
f_position_symbol=${f_position_array[0]}
f_position_currency_amount=${f_position_array[1]}
f_position_entry_price=${f_position_array[2]}
f_position_current_price=${f_position_array[3]}
f_position_side=${f_position_array[4]}
[ -z "$f_position_side" ] && f_position_side="long"
f_position_leverage=${f_position_array[5]}
[ -z "$f_position_leverage" ] && f_position_leverage="1"
g_percentage-diff $f_position_entry_price $f_position_current_price
[ "$f_position_side" = short ] && g_percentage-diff $f_position_current_price $f_position_entry_price
f_position_pnl_percentage=$g_percentage_diff_result
if [ -n $f_position_leverage ]
then
g_calc "$f_position_pnl_percentage*$f_position_leverage"
f_position_pnl_percentage=$g_calc_result
g_calc "$f_position_currency_amount/$f_position_leverage"
f_position_currency_amount=$g_calc_result
g_calc "$f_position_currency_amount/100*$f_position_pnl_percentage"
f_position_pnl=$g_calc_result
fi
}

View File

@ -0,0 +1,28 @@
function get_symbols {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
f_ccxt "print($STOCK_EXCHANGE.symbols)"
if [ -z "$f_ccxt_json_out" ]
then
g_echo_warn "Could not get symbols list - no json output"
return 1
fi
local f_symbols=${f_ccxt_result}
f_symbols=${f_symbols//\"}
f_symbols=${f_symbols//, /+}
f_symbols=${f_symbols//\[}
f_symbols=${f_symbols//\]}
if [ -z "$f_symbols" ]
then
g_echo_warn "Could not get symbols list - empty"
return 1
fi
g_array "$f_symbols" f_symbols_array +
printf '%s\n' "${f_symbols_array[@]}" >SYMBOLS-$STOCK_EXCHANGE
}

View File

@ -2,7 +2,7 @@ function get_transactions {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
local f_exchange f_symbols f_symbol
local f_exchange f_symbol f_symbol_file f_asset f_currency f_leverage f_convert_end_month f_convert_end_year f_symbol_file_csv f_symbol_file_csv_tmp f_start_date f_end_date f_convert_file f_fiat f_fiats
local DEFAULT_STOCK_EXCHANGE=$STOCK_EXCHANGE
for f_exchange in /dabo/.*-secrets;
@ -18,12 +18,13 @@ function get_transactions {
g_echo_note "Exchange: $f_exchange"
f_ccxt "print(${STOCK_EXCHANGE}.symbols)"
f_symbols=${f_ccxt_result}
f_symbols=${f_symbols//\"}
f_symbols=${f_symbols//, /+}
f_symbols=${f_symbols//\[}
f_symbols=${f_symbols//\]}
get_symbols
#f_ccxt "print(${STOCK_EXCHANGE}.symbols)"
#f_symbols=${f_ccxt_result}
#f_symbols=${f_symbols//\"}
#f_symbols=${f_symbols//, /+}
#f_symbols=${f_symbols//\[}
#f_symbols=${f_symbols//\]}
# transfer-dir
mkdir -p "TRANSACTIONS-$f_exchange"
@ -31,13 +32,14 @@ function get_transactions {
touch --time=mtime -t $(date -d "now -1 day" +%Y%m%d%H%M) TRANSACTIONS-TIMESTAMP
# go through symbols
local f_orig_IFS=$IFS
IFS=+
for f_symbol in $f_symbols
#local f_orig_IFS=$IFS
#IFS=+
#for f_symbol in $f_symbols
for f_symbol in "${f_symbols_array[@]}"
do
#echo $f_symbol
#[[ $f_symbol =~ ETH|BTC ]] || continue
IFS=$f_orig_IFS
#IFS=$f_orig_IFS
# binance does not allow derivate trading in germany so ignore because of 400-Error
[[ $f_symbol =~ : ]] && [[ $f_exchange = binance ]] && continue
f_symbol_file="TRANSACTIONS-$f_exchange/${f_symbol//\/}"
@ -182,8 +184,7 @@ function get_transactions {
fi
# Switch sides if Fiat is in Krypto side
local f_fiats="USD EUR"
local f_fiat
f_fiats="USD EUR"
for f_fiat in $f_fiats
do
[ -f TRANSACTIONS-$f_exchange.csv.tmp ] && rm TRANSACTIONS-$f_exchange.csv.tmp

View File

@ -12,7 +12,7 @@ function transactions_overview {
>ALL_TRANSACTIONS_OVERVIEW_WARN.csv.tmp
>TRANSACTIONS_OVERVIEW-trade-result_tax_german_eur.tmp
local f_exchange f_asset f_transactions_array f_transaction f_result f_asset_quantity f_asset_quantity_sold f_currency_quantity f_currency_quantity_sold f_currency_spent f_date f_type f_asset_amount f_currency f_currency_amount f_fee_currency f_fee_amount f_sell_result f_taxable f_tax_type f_one_year_ago f_currency_amount_eur f_currency_spent_eur f_currency_quantity_sold_eur f_note f_asset_quantity_remaining f_currency_remaining f_year
local f_exchange f_asset f_transactions_array f_transaction f_result f_asset_quantity f_asset_quantity_sold f_currency_quantity f_currency_quantity_sold f_currency_spent f_date f_type f_asset_amount f_currency f_currency_amount f_fee_currency f_fee_amount f_sell_result f_taxable f_tax_type f_one_year_ago f_currency_amount_eur f_currency_spent_eur f_currency_quantity_sold_eur f_note f_asset_quantity_remaining f_currency_remaining f_year f_currency_spent_eur_tax f_currency_quantity_sold_eur_tax f_sell_result_percentage f_sell_result_percentage_eur
f_assets_per_exchange=$(egrep -h -v '^DATE,TYPE,ASSET,ASSET_AMOUNT,CURRENCY,CURRENCY_AMOUNT,EXCHANGE|^#|^$|^ +$|^$' TRANSACTIONS-*.csv | cut -d, -f3,7 | sort -u)
@ -62,6 +62,8 @@ function transactions_overview {
f_tax_type=""
f_one_year_ago=""
f_note=""
f_sell_result_percentage=0
f_sell_result_percentage_eur=0
# if there is a fee change to f_currency_amount and deduct f_currency_amount
if [ -n "$f_fee_amount" ]
@ -112,7 +114,6 @@ function transactions_overview {
fi
# round fiat numbers to 2 decimal places
printf -v f_currency_amount_eur %.2f $f_currency_amount_eur
local f_fiats="USD USDT BUSD"
local f_fiat
for f_fiat in $f_fiats
@ -132,6 +133,7 @@ function transactions_overview {
g_calc "$f_currency_spent_eur+$f_currency_amount_eur"
f_currency_spent_eur=$g_calc_result
fi
printf -v f_currency_amount_eur %.2f $f_currency_amount_eur
# what did I spent on asset
if [[ $f_type =~ buy|leverage-buy|reward-staking|instant_trade_bonus|giveaway ]]
@ -146,19 +148,6 @@ function transactions_overview {
# rise result if reward-staking|instant_trade_bonus|giveaway
if [[ $f_type =~ reward-staking|instant_trade_bonus|giveaway ]]
then
# # Calculate/change to Euro if not
# if [[ $f_currency != EUR ]]
# then
# if currency_converter $f_currency_amount $f_currency EUR "${f_date}"
# then
# f_currency_amount=$f_currency_converter_result
# f_currency="EUR"
# else
# g_echo_warn "!!!!!! Could not convert currency $f_currency to EUR"
# continue
# fi
# fi
if [[ $f_type =~ reward-staking ]]
then
f_taxable=$f_currency_amount_eur
@ -290,8 +279,10 @@ function transactions_overview {
f_asset_quantity_sold=0
f_asset_quantity_remaining=0
f_currency_spent=0
printf -v f_currency_spent_eur_tax %.2f $f_currency_spent_eur
f_currency_spent_eur=0
f_currency_quantity_sold=0
printf -v f_currency_quantity_sold_eur_tax %.2f $f_currency_quantity_sold_eur
f_currency_quantity_sold_eur=0
fi
@ -364,7 +355,7 @@ function transactions_overview {
f_tax_type="Verkauf (Einkommenssteuersatz)"
g_calc "$f_asset_amount_tax_able-$f_asset_amount"
f_asset_amount_tax_able=$g_calc_result
echo "$f_date,$f_exchange,$f_currency_spent_eur,$f_currency_quantity_sold_eur" >> "TRANSACTIONS_OVERVIEW-trade-result_tax_german_eur.tmp"
echo "$f_date,$f_exchange,$f_currency_spent_eur_tax,$f_currency_quantity_sold_eur_tax" >> "TRANSACTIONS_OVERVIEW-trade-result_tax_german_eur.tmp"
else
## partially taxable
f_one_year_ago="partially"
@ -383,7 +374,7 @@ function transactions_overview {
f_tax_type="Verkauf (Einkommenssteuersatz)"
g_calc "$f_asset_amount_tax_able-$f_taxable_asset"
echo "$f_date,$f_exchange,$f_currency_spent_eur,$f_currency_quantity_sold_eur,$f_percentage_taxable" >> "TRANSACTIONS_OVERVIEW-trade-result_tax_german_eur.tmp"
echo "$f_date,$f_exchange,$f_currency_spent_eur_tax,$f_currency_quantity_sold_eur_tax,$f_percentage_taxable" >> "TRANSACTIONS_OVERVIEW-trade-result_tax_german_eur.tmp"
fi
fi
@ -412,7 +403,9 @@ function transactions_overview {
# 20 totally mutal currency amount (money for trade) spent in EUR - f_currency_spent_eur
# 21 totally mutal currency amount (money for trade) sold - f_currency_quantity_sold
# 22 totally mutal currency amount (money for trade) sold in EUR - f_currency_quantity_sold_eur
echo "$f_date,$f_exchange,$f_type,$f_asset,$f_asset_amount,$f_currency,$f_currency_amount,$f_one_year_ago,$f_currency_spent,$f_asset_quantity,$f_result,$f_sell_result,$f_tax_type,$f_taxable,$f_currency_amount_eur,$f_result_eur,$f_sell_result_eur,$f_asset_quantity_remaining,$f_note,$f_currency_spent_eur,$f_currency_quantity_sold,$f_currency_quantity_sold_eur" | tee -a ALL_TRANSACTIONS_OVERVIEW.csv.tmp >>ALL_TRANSACTIONS_OVERVIEW.log
# 23 f_sell_result_percentage
# 24 f_sell_result_percentage_eur
echo "$f_date,$f_exchange,$f_type,$f_asset,$f_asset_amount,$f_currency,$f_currency_amount,$f_one_year_ago,$f_currency_spent,$f_asset_quantity,$f_result,$f_sell_result,$f_tax_type,$f_taxable,$f_currency_amount_eur,$f_result_eur,$f_sell_result_eur,$f_asset_quantity_remaining,$f_note,$f_currency_spent_eur,$f_currency_quantity_sold,$f_currency_quantity_sold_eur,$f_sell_result_percentage,$f_sell_result_percentage_eur" | tee -a ALL_TRANSACTIONS_OVERVIEW.csv.tmp >>ALL_TRANSACTIONS_OVERVIEW.log
if [[ $f_type =~ sell|leverage-sell ]]
then

View File

@ -24,7 +24,7 @@ function webpage {
# historic overview
echo '<a href=TRANSACTIONS_OVERVIEWS.html><h2>Historic Overview</h2></a>' >>../index.html.tmp
local f_SPOT_BALANCE=$(tail -n1 "asset-histories/BALANCESPOT${CURRENCY}.history.csv" | cut -d, -f2)
local f_USED_BALANCE=$(tail -n1 "asset-histories/BALANCEUSED${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>
@ -33,8 +33,8 @@ function webpage {
<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>
<td>Used Balance (invested):</td>
<td><font color=blue>${CURRENCY} $f_USED_BALANCE</font></td>
</tr>
<tr>
<td>Free Balance (not invested):</td>
@ -42,70 +42,46 @@ function webpage {
</tr>
</table>" >>../index.html.tmp
echo "<h2>Trade Performance (overall closed trades)</h2>" >>../index.html.tmp
echo "<h2>Balance in- outflows</h2>" >>../index.html.tmp
echo "<table><tr><td><b>Time ago</b><td><b>Balance</b></td><td><b>in/out</b></td><td><b>Percentage</b></td></tr>" >> ../index.html.tmp
for f_balance_date in Day Week Month 3Month Year
do
f_balance_at_date=$(grep "^$(date -d "1 $f_balance_date ago" +"%Y-%m-%d ")" asset-histories/BALANCECOMPLETE${CURRENCY}.history.csv | head -n1 | cut -d, -f2)
if g_num_valid_number "$f_balance_at_date"
then
printf -v f_balance_at_date %.2f $f_balance_at_date
g_calc "$f_COMPLETE_BALANCE-$f_balance_at_date"
printf -v f_balance_diff %.2f $g_calc_result
g_percentage-diff $f_balance_at_date $f_COMPLETE_BALANCE
printf -v f_balance_diff_percentage %.2f $g_percentage_diff_result
echo "<tr><td>$f_balance_date<td>$f_balance_at_date</td><td>$f_balance_diff</td><td> ${f_balance_diff_percentage}%</td></tr>" >> ../index.html.tmp
fi
done
echo "</table>" >>../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 Positions</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
echo "<table width='100%'><tr><td>Symbol</td><td>Aount</td><td>Entry Price</td><td>Current Price</td><td>Profit/Loss</td><td>Notes</td></tr>" >>../index.html.tmp
for f_position in "${f_get_positions_array[@]}"
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
get_position_line_vars "$f_position"
echo "<tr><td>$f_position_symbol</td><td>$f_position_currency_amount</td><td>$f_position_entry_price</td><td>$f_position_current_price</td><td>$f_position_pnl ( ${f_position_pnl_percentage}%)</td><td>$f_position_side ${f_position_leverage}X</td></tr>" >>../index.html.tmp
done
echo "</table>" >>../index.html.tmp
## Open Positions
echo "<h2>Open Positions - From Trade Histories</h2>" >>../index.html.tmp
echo "<h2>Open Positions - From Trade Histories (daily refresh only)</h2>" >>../index.html.tmp
echo "<table width='100%'>" >>../index.html.tmp
echo "<tr><td>Amount</td><td>Spent Amount</td><td>Sold Amount</td><td>Profit/Loss</td><td>Asset Amount</td><td>Exchange</td></tr>" >>../index.html.tmp
rm -f ../index.html.tmp.tmp
cat ALL_TRANSACTIONS_OVERVIEW.csv | cut -d, -f2,4 | sort -u | while read f_asset_per_exchange
local f_result_complete=0
local f_spent_complete=0
local f_currency_amount_complete=0
local f_sold_complete=0
local f_result_percent_complete=0
local f_asset f_exchange f_amount f_spent f_sold f_currency_amount f_result_percent
for f_asset_per_exchange in $(cat ALL_TRANSACTIONS_OVERVIEW.csv | cut -d, -f2,4 | sort -u)
do
mapfile -d, -t f_asset_per_exchange_array < <(echo $f_asset_per_exchange)
f_asset=${f_asset_per_exchange_array[1]%$'\n'}
@ -132,9 +108,28 @@ function webpage {
fi
g_calc "$f_currency_amount-($f_spent)"
f_result=$g_calc_result
### Calc Complete values
g_calc "$f_result_complete+($f_result)"
f_result_complete=$g_calc_result
g_calc "$f_currency_amount_complete+($f_currency_amount)"
f_currency_amount_complete=$g_calc_result
g_calc "$f_spent_complete+($f_spent)"
f_spent_complete=$g_calc_result
g_calc "$f_sold_complete+($f_sold)"
f_sold_complete=$g_calc_result
echo "<tr><td>$f_currency_amount $TRANSFER_CURRENCY</td><td>$f_spent $TRANSFER_CURRENCY</td><td>$f_sold $TRANSFER_CURRENCY</td><td>$f_result $TRANSFER_CURRENCY ( ${f_result_percent}%)</td><td>$f_amount $f_asset</td><td>$f_exchange</td></tr>" >>../index.html.tmp.tmp
fi
done
g_percentage-diff $f_spent_complete $f_currency_amount_complete
f_result_percent_complete=$g_percentage_diff_result
echo "<tr><td>$f_currency_amount_complete $TRANSFER_CURRENCY</td><td>$f_spent_complete $TRANSFER_CURRENCY</td><td>$f_sold_complete $TRANSFER_CURRENCY</td><td>$f_result_complete $TRANSFER_CURRENCY ( ${f_result_percent_complete}%)</td><td>ALL</td><td>ALL</td></tr>" >>../index.html.tmp
sort -n -k3 -t'>' -r ../index.html.tmp.tmp >>../index.html.tmp
rm ../index.html.tmp.tmp
echo "</table>" >>../index.html.tmp