ccxt switch

This commit is contained in:
olli 2024-06-14 19:59:33 +02:00
parent 5c32cd3dd1
commit f93d33a6a1
7 changed files with 0 additions and 352 deletions

View File

@ -1,36 +0,0 @@
#!/bin/bash
function binance-api-call {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
local method=$1
local call=$2
local params=$3
if [ -s /dabo/.binance-secrets ]
then
. /dabo/.binance-secrets
else
g_echo_error "No secrets file found"
return 1
fi
call=$(echo "$call" | sed "s/^\///")
if echo "${call}" | egrep -q "^sapi/|^api/v3/order"
then
params="recvWindow=60000${params}"
local timestamp=$(date +%s000)
params="${params}&timestamp=${timestamp}"
local signature=$(echo -n "${params}" | openssl dgst -sha256 -hmac "${API_SECRET}" | cut -d" " -f2)
params="?${params}&signature=$signature"
fi
echo "curl -s -H \"X-MBX-APIKEY: $API_KEY\" -X \"$method\" \"https://api.binance.com/${call}${params}\"" >${g_tmp}/API_CMD
echo "curl -s -H \"X-MBX-APIKEY: API_KEY\" -X \"$method\" \"https://api.binance.com/${call}${params}\"" >${g_tmp}/API_CMD_WO_KEY
g_runcmd g_retrycmd sh ${g_tmp}/API_CMD >${g_tmp}/API_CMD_OUT 2>&1
if egrep -q -i '^{"code":|error' ${g_tmp}/API_CMD_OUT
then
g_echo_error "$(cat ${g_tmp}/API_CMD_WO_KEY): $(cat ${g_tmp}/API_CMD_OUT)"
return 1
fi
}

View File

@ -1,142 +0,0 @@
function binance_convert {
# Info for log
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
# needed vars
local f_ASSET=$1
local f_CURRENCY=$2
local f_QUANTITY=$3
local f_ACTION=$4 # buy or sell
local f_COMMENT=$5
local f_QUANTITY_CURRENCY=$6
local f_DATE=$(date '+%F_%H-%M-%S')
local f_ASSET_HIST_FILE="asset-histories/${f_ASSET}${f_CURRENCY}.history.csv"
local f_market_price=$(tail -n1 ${f_ASSET_HIST_FILE} | cut -d, -f2)
local f_link="https://www.coingecko.com/de/munze/$(egrep -i ^${f_ASSET}, COINGECKO_IDS | cut -d, -f2)"
local f_CMDFILE="trade-histories/${f_DATE}-${f_CURRENCY}-${f_ACTION}-BINANCE_CONVERT-TRADE_CMD"
local f_num_converts=$(find trade-histories/*-*-*-BINANCE_CONVERT-TRADE_CMD -type f -mmin 60 | wc -l)
if [ $f_num_converts -ge 99 ]
then
g_echo_note "Already did 99 or more binance converts last hour."
return 1
fi
# get trade commission for comparison with convert
binance-api-call GET /sapi/v1/asset/tradeFee "&symbol=${f_ASSET}${f_CURRENCY}"
local FEE=$(echo "$(cat $g_tmp/API_CMD_OUT | jq -r .[].takerCommission)*100" | bc -l | sed 's/^\./0./; s/^-\./-0./')
if [ "${f_ACTION}" = "buy" ]
then
# check for enough balance for trade
get_balances
local f_CURRENCY_BALANCE=$(egrep "^${f_CURRENCY}," EXCHANGE_GET_BALANCES_CMD_OUT | cut -d"," -f2)
g_echo_note "Checking for enough balance for trade (${f_CURRENCY_BALANCE} > ${f_QUANTITY})"
if [ $(echo "${f_CURRENCY_BALANCE} > ${f_QUANTITY}" | bc -l) -eq 0 ]
then
local f_note="Not enough balance for trade (${f_CURRENCY_BALANCE} > ${f_QUANTITY}) :${f_COMMENT}
${FUNCNAME} $@"
g_echo_note "$f_note"
g_signal-notify "$f_note"
return 1
fi
# get quote on buy
binance-api-call POST /sapi/v1/convert/getQuote "&fromAsset=${f_CURRENCY}&toAsset=${f_ASSET}&fromAmount=${f_QUANTITY}&walletType=SPOT&validTime=10s" || return 1
cat ${g_tmp}/API_CMD_OUT >${f_CMDFILE}_QUOTE_OUT
# get convert price
local f_convert_price=$(cat ${f_CMDFILE}_QUOTE_OUT | grep '^{' | jq -r .inverseRatio | head -n1)
g_percentage-diff ${f_market_price} ${f_convert_price}
local f_price_diff=${g_percentage_diff_result}
fi
if [ "${f_ACTION}" = "sell" ]
then
# get quote on sell
binance-api-call POST /sapi/v1/convert/getQuote "&fromAsset=${f_ASSET}&toAsset=${f_CURRENCY}&fromAmount=${f_QUANTITY}&walletType=SPOT&validTime=10s" || return 1
cat ${g_tmp}/API_CMD_OUT >${f_CMDFILE}_QUOTE_OUT
# get convert price
local f_convert_price=$(cat ${f_CMDFILE}_QUOTE_OUT | grep '^{' | jq -r .ratio | head -n1)
g_percentage-diff ${f_convert_price} ${f_market_price}
local f_price_diff=${g_percentage_diff_result}
fi
if [ $(echo "${f_price_diff} > ${FEE}" | bc -l) -eq 1 ]
then
local f_note="Price difference between Market Price (${f_market_price} ${f_CURRENCY}) and Binance Convert Price (${f_convert_price} ${f_CURRENCY}) is higher then Trading Fee (${f_price_diff}% > ${FEE}%), so I will better use trade then convert"
g_echo_note "$f_note"
g_signal-notify "$f_note"
return 1
fi
g_echo_note "Price difference between Market Price (${f_market_price}) and Binance Convert Price (${f_convert_price}) is lower then Trading Fee (${f_price_diff} > ${FEE}), so I will use convert"
echo "cat ${g_tmp}/API_CMD_OUT >${f_CMDFILE}_QUOTE_OUT
local f_quoteid=\$(cat ${g_tmp}/API_CMD_OUT | jq -r '.quoteId')
binance-api-call POST /sapi/v1/convert/acceptQuote \"&quoteId=\${f_quoteid}\"
cat ${g_tmp}/API_CMD_OUT >${f_CMDFILE}_OUT
" >${f_CMDFILE}
# convert/trade
g_echo_note "Command: $(cat ${f_CMDFILE})"
. ${f_CMDFILE}
cat ${g_tmp}/API_CMD_OUT >${f_CMDFILE}_OUT
g_echo_note "Command Output: $(cat ${f_CMDFILE}_OUT)"
# Check return and log trade
f_STATUS=$(cat ${f_CMDFILE}_OUT | grep '^{' | jq -r .orderStatus)
local f_trade_info_msg="CONVERT/TRADE - ${f_ACTION} ${f_ASSET}${f_CURRENCY}
${f_link}
Complete Overview: https://${URL}/
Comment: ${f_COMMENT}"
if echo "${f_STATUS}" | egrep -q "PROCESS|ACCEPT_SUCCESS|SUCCESS"
then
g_echo_note "CONVERT/TRADE SUCCESSFUL!"
[ "${f_ACTION}" = "buy" ] && local f_convert_price=$(cat ${f_CMDFILE}_QUOTE_OUT | grep '^{' | jq -r .inverseRatio | head -n1)
[ "${f_ACTION}" = "sell" ] && local f_convert_price=$(cat ${f_CMDFILE}_QUOTE_OUT | grep '^{' | jq -r .ratio | head -n1)
local f_COMMISSION="0"
local f_COMMISSIONASSET="${f_CURRENCY}"
echo "${f_DATE},${f_ACTION},${f_CMDFILE}_OUT,${f_QUANTITY} ${f_CURRENCY},${f_convert_price},${f_COMMISSION} ${f_COMMISSIONASSET},CONVERT ${f_COMMENT}" | head -n1 >>trade-histories/${f_ASSET}${f_CURRENCY}.history.csv
if [ "${f_ACTION}" = "buy" ]
then
echo "${f_DATE},${f_ACTION},${f_CMDFILE}_OUT,${f_QUANTITY} ${f_CURRENCY},${f_convert_price},${f_COMMISSION} ${f_COMMISSIONASSET},CONVERT ${f_COMMENT}" | head -n1 >>trade-histories/trade-$(date +%F.%T. | sed 's/:/_/g')${f_ASSET}${f_CURRENCY}-open.history.csv
fi
if [ "${f_ACTION}" = "sell" ]
then
f_tradehistfile="$(ls trade-histories/trade-*${f_ASSET}${f_CURRENCY}-open.history.csv | tail -n1)"
echo "${f_DATE},${f_ACTION},${f_CMDFILE}_OUT,${f_QUANTITY_CURRENCY} ${f_CURRENCY},${f_convert_price},${f_COMMISSION} ${f_COMMISSIONASSET},${f_COMMENT}" | head -n1 >>${f_tradehistfile}
f_tradehistfileclosed=$(echo ${f_tradehistfile} | sed 's/open.history.csv/closed.history.csv/')
mv ${f_tradehistfile} ${f_tradehistfileclosed}
fi
g_signal-notify "CONVERT/TRADE SUCCESSFUL!
${f_trade_info_msg}
Command stored: ${f_CMDFILE}[_OUT]"
[ -f DIFF_BUY_PRICE_${f_ASSET}${f_CURRENCY} ] && [ "${f_ACTION}" = "sell" ] && rm -f DIFF_BUY_PRICE_${f_ASSET}${f_CURRENCY}
# get new balances
get_balances
return 0
else
g_echo_note "CONVERT/TRADE FAILED!
$(cat ${f_CMDFILE}_OUT)"
g_signal-notify "CONVERT/TRADE FAILED!
${f_trade_info_msg}
Command ${f_CMDFILE}:
$0 $@
$(cat ${f_CMDFILE})
OUTPUT:
$(cat ${f_CMDFILE}_OUT)
"
return 1
fi
}

View File

@ -1,46 +0,0 @@
#!/bin/bash
function binance_convert_dust {
# find BNB Balance of an conversion which ran before - Balance takes a while to be shown on BNB
# get BNB balance
binance-api-call GET sapi/v1/capital/config/getall
local f_bnb_balance=$(cat ${g_tmp}/API_CMD_OUT | jq -r '.[] | .coin + "," + .free' | grep "^BNB," | cut -d, -f2)
# convert BNB to $CURRENCY
#if echo "${f_bnb_balance}" | egrep -q "[0-9]\.[0-9]"
if [ $(echo "${f_bnb_balance} > 0.004" | bc -l) -ne 0 ]
then
binance-api-call POST /sapi/v1/convert/getQuote "&fromAsset=BNB&toAsset=${CURRENCY}&fromAmount=${f_bnb_balance}&walletType=SPOT&validTime=30s"
local f_quoteid=$(cat ${g_tmp}/API_CMD_OUT | jq -r '.quoteId')
g_signal-notify "Converting dust from ${f_bnb_balance} BNB to ${CURRENCY}
$(cat ${g_tmp}/API_CMD_OUT)"
binance-api-call POST /sapi/v1/convert/acceptQuote "&quoteId=${f_quoteid}"
fi
# Only run every 6 houres - binance doesn't allow to do it more often
[ -s BINANCE_LAST_DUST_RUN ] || date >BINANCE_LAST_DUST_RUN
find BINANCE_LAST_DUST_RUN -mmin +362 -delete
[ -s BINANCE_LAST_DUST_RUN ] && return 0
date >BINANCE_LAST_DUST_RUN
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
# find dust
local f_dust_assets=""
local f_dust
binance-api-call POST /sapi/v1/asset/dust-btc
# ignore $CURRENCY and assets in open trades
for f_dust in $(cat ${g_tmp}/API_CMD_OUT | jq -r '.details[].asset' | egrep -v "${CURRENCY}")
do
ls trade-histories/trade-*.${f_dust}${CURRENCY}-open.history.csv >/dev/null 2>&1 && continue
f_dust_assets="${f_dust_assets},$f_dust"
done
f_dust_assets=$(echo ${f_dust_assets} | sed 's/^,//')
# convert dust to BNB
if [ -n "${f_dust_assets}" ]
then
g_signal-notify "Converting dust from ${f_dust_assets} to BNB"
binance-api-call POST /sapi/v1/asset/dust "&asset=${f_dust_assets}"
fi
}

View File

@ -1,38 +0,0 @@
function binance_get_token_info {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
local f_ASSET=$1
local f_CURRENCY=$2
local f_QUANTITY=$3
# cleanup cache
[ -s BINANCE_TOKEN_INFO_CMD_OUT ] && find BINANCE_TOKEN_INFO_CMD_OUT -mmin +60 -or -empty -delete
#echo "$BINANCE_CLI_CMD info" >BINANCE_TOKEN_INFO_CMD
#[ -s BINANCE_TOKEN_INFO_CMD_OUT ] || g_runcmd g_retrycmd sh BINANCE_TOKEN_INFO_CMD >BINANCE_TOKEN_INFO_CMD_OUT
if ! [ -s BINANCE_TOKEN_INFO_CMD_OUT ]
then
binance-api-call GET /api/v3/exchangeInfo >BINANCE_TOKEN_INFO_CMD_OUT
cat ${g_tmp}/API_CMD_OUT >BINANCE_TOKEN_INFO_CMD_OUT
fi
# cut quantity by lot-step-size
if ! [ -z "$f_QUANTITY" ]
then
local f_LOT_STEP_SIZE=$(cat BINANCE_TOKEN_INFO_CMD_OUT | jq -c "[ .symbols[] | select( .symbol==\"${f_ASSET}${f_CURRENCY}\") ]" | jq -c '.[].filters[] | select( .filterType=="LOT_SIZE")' | jq -r '.stepSize' | perl -pe 's/0+$//; s/\.$//')
if [ -z "${f_LOT_STEP_SIZE}" ]
then
g_echo "Got no LOT_STEP_SIZE from BINANCE_TOKEN_INFO_CMD_OUT for ${f_ASSET} - Assuming 0.01"
f_LOT_STEP_SIZE="0.01"
fi
local f_LOT_DIV=$(echo "scale=0; ${f_QUANTITY}/${f_LOT_STEP_SIZE}" | bc -l)
f_QUANTITY_LOT_CUT=$(echo "${f_LOT_DIV}*${f_LOT_STEP_SIZE}" | bc -l | sed 's/^\./0./;')
fi
# get min CURRENCY
f_MIN_NOTIONAL=$(cat BINANCE_TOKEN_INFO_CMD_OUT | jq -c "[ .symbols[] | select( .symbol==\"${f_ASSET}${f_CURRENCY}\") ]" | jq -c '.[].filters[] | select( .filterType=="MIN_NOTIONAL")' | jq -r '.minNotional' | perl -pe 's/0+$//; s/\.$//')
if [ -z "${f_MIN_NOTIONAL=}" ]
then
g_echo "Got no LOT_STEP_SIZE from BINANCE_TOKEN_INFO_CMD_OUT for ${f_ASSET} - Assuming 10.00000000"
f_MIN_NOTIONAL="10.00000000"
fi
}

View File

@ -1,28 +0,0 @@
#!/bin/bash
function onetrading-api-call {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
local method=$1
local call=$2
local params=$3
if [ -s /dabo/.onetrading-secrets ]
then
. /dabo/.onetrading-secrets
else
g_echo_error "No secrets file found"
return 1
fi
echo "${call}" | egrep -q "/account/" && local f_token="-H 'Authorization: Bearer ${API_TOKEN}'"
echo "curl -s -X ${method} --url https://api.onetrading.com/${call} $f_token -H 'Accept: application/json' ${params}" >${g_tmp}/API_CMD
echo "curl -s -X ${method} --url https://api.onetrading.com/${call} -H 'Accept: application/json'" >${g_tmp}/API_CMD_WO_KEY
g_runcmd g_retrycmd sh ${g_tmp}/API_CMD >${g_tmp}/API_CMD_OUT 2>&1
if egrep -q -i '^{"code":|error' ${g_tmp}/API_CMD_OUT
then
g_echo_error "$(cat ${g_tmp}/API_CMD_WO_KEY): $(cat ${g_tmp}/API_CMD_OUT)"
return 1
fi
}

View File

@ -1,32 +0,0 @@
function onetrading_get_token_info {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
local f_ASSET=$1
local f_CURRENCY=$2
f_QUANTITY=$3
# cleanup cache
[ -s ONETRADING_TOKEN_INFO_CMD_OUT ] && find ONETRADING_TOKEN_INFO_CMD_OUT -mmin +60 -or -empty -delete
if ! [ -s ONETRADING_TOKEN_INFO_CMD_OUT ]
then
onetrading-api-call GET public/v1/instruments >ONETRADING_TOKEN_INFO_CMD_OUT
cat ${g_tmp}/API_CMD_OUT >ONETRADING_TOKEN_INFO_CMD_OUT
fi
local f_ASSET_PRECISION=$(cat ONETRADING_TOKEN_INFO_CMD_OUT | jq -r ".[] | select(.state==\"ACTIVE\") | select(.quote.code==\"${f_CURRENCY}\") | select(.base.code==\"${f_ASSET}\") | .amount_precision")
local f_CURRENCY_PRECISION=$(cat ONETRADING_TOKEN_INFO_CMD_OUT | jq -r ".[] | select(.state==\"ACTIVE\") | select(.quote.code==\"${f_CURRENCY}\") | select(.base.code==\"${f_ASSET}\") | .quote.precision")
local f_ASSET_MINSIZE=$(cat ONETRADING_TOKEN_INFO_CMD_OUT | jq -r ".[] | select(.state==\"ACTIVE\") | select(.quote.code==\"${f_CURRENCY}\") | select(.base.code==\"${f_ASSET}\") | .min_size")
local f_ASSET_PRICE=$(tail -n1 "asset-histories/${f_ASSET}${f_CURRENCY}.history.csv" | cut -d"," -f2)
if [ -n "$f_QUANTITY" ] && [ -n "$f_ASSET_PRICE" ]
then
if [ $(echo "${f_ASSET_MINSIZE} < ${f_QUANTITY}" | bc -l) -eq 0 ]
then
f_QUANTITY=$(echo "scale=${f_CURRENCY_PRECISION}; ${f_ASSET_MINSIZE}+1" | bc -l)
fi
f_ASSET_QUANTITY=$(echo "scale=${f_ASSET_PRECISION}; ${f_QUANTITY}/${f_ASSET_PRICE}" | bc -l | sed 's/^\./0./;')
fi
}

View File

@ -1,30 +0,0 @@
#!/bin/bash
# https://github.com/phemex/phemex-api-docs/blob/master/Public-Contract-API-en.md
function phemex-api-call {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
local method=$1
local call=$2
local params=$3
if [ -s /dabo/.phemex-secrets ]
then
. /dabo/.phemex-secrets
else
g_echo_error "No secrets file found"
return 1
fi
echo "${call}" | egrep -q "/account/" && local f_token="-H 'Authorization: Bearer ${API_TOKEN}'"
echo "curl -s -X ${method} --url https://api.phemex.com/${call} $f_token -H 'Accept: application/json' ${params}" >${g_tmp}/API_CMD
echo "curl -s -X ${method} --url https://api.phemex.com/${call} -H 'Accept: application/json'" >${g_tmp}/API_CMD_WO_KEY
g_runcmd g_retrycmd sh ${g_tmp}/API_CMD >${g_tmp}/API_CMD_OUT 2>&1
if egrep -q -i '^{"code":|error' ${g_tmp}/API_CMD_OUT
then
g_echo_error "$(cat ${g_tmp}/API_CMD_WO_KEY): $(cat ${g_tmp}/API_CMD_OUT)"
return 1
fi
}