python ccxt

This commit is contained in:
olli 2024-03-26 14:09:35 +01:00
parent 24fc7249a0
commit 5b4c2a9500

View File

@ -0,0 +1,135 @@
function get_ccxt_balances {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
local f_filename="CCXT_GET_BALANCES_CMD"
#f_ccxt 'print(exchange.fetch_balance ({"currency": "USDT"}))' && echo $f_ccxt_result >CCXT_BALANCES_RAW
f_ccxt 'print(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} = "BITPANDA" ]
then
bitpanda-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}
}