Files
dabo/dabo/functions/ccxt.sh
2024-05-26 10:01:30 +02:00

56 lines
1.7 KiB
Bash

function f_ccxt {
# remove old result
unset f_ccxt_result
# lower case
STOCK_EXCHANGE=${STOCK_EXCHANGE,,}
if [ -s /dabo/.${STOCK_EXCHANGE}-secrets ]
then
. /dabo/.${STOCK_EXCHANGE}-secrets
else
g_echo_error "No secrets found (/dabo/.${STOCK_EXCHANGE}-secrets) found"
return 1
fi
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
if [ -z "$f_ccxt_initialized" ]
then
g_python 'import os' || return 1
g_python 'import sys' || return 1
g_python 'sys.path.append("/ccxt/python")' || return 1
g_python 'import ccxt' || return 1
fi
if ! [[ "$f_ccxt_initialized" =~ $STOCK_EXCHANGE ]]
then
local f_exchange_type="swap"
[ -z "$LEVERAGE" ] && f_exchange_type="spot"
g_python "${STOCK_EXCHANGE} = ccxt.${STOCK_EXCHANGE}({'apiKey': '${API_KEY}','secret': '${API_SECRET}','enableRateLimit': True,'options': {'defaultType': '${f_exchange_type}',},})" || return 1
g_python "${STOCK_EXCHANGE}.load_markets()"
f_ccxt_initialized="${f_ccxt_initialized}${STOCK_EXCHANGE},"
fi
# send and receive ccxt command in python - on error kill progress
if ! g_python "$@"
then
kill -9 $(jobs -l | grep python-pipeexec.py | cut -d" " -f 2)
return 1
fi
# reference result to python-result
declare -ng f_ccxt_result=g_python_result
# make the output jq-conform
# avoids errors like: "parse error: Invalid numeric literal at"
f_ccxt_result=${f_ccxt_result//\'/\"}
f_ccxt_result=${f_ccxt_result// None/ null}
f_ccxt_result=${f_ccxt_result// True/ true}
f_ccxt_result=${f_ccxt_result// False/ false}
f_ccxt_result=${f_ccxt_result//,,/,}
}