get via CCXT all (historic) transactions

This commit is contained in:
olli 2024-05-26 10:00:34 +02:00
parent 03ebb54e38
commit acccc2a463

View File

@ -0,0 +1,205 @@
function get_transactions {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
local f_exchange f_symbols f_symbol
local DEFAULT_STOCK_EXCHANGE=$STOCK_EXCHANGE
for f_exchange in /dabo/.*-secrets;
do
# extract ccxt exchange from filename
g_basename $f_exchange
f_exchange=${g_basename_result/-secrets}
f_exchange=${f_exchange/.}
STOCK_EXCHANGE=$f_exchange
[[ $f_exchange = bitpanda ]] && continue
[[ $f_exchange = onetrading ]] && continue
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//\]}
# transfer-dir
mkdir -p "TRANSACTIONS-$f_exchange"
# create timestamp file
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
do
#echo $f_symbol
#[[ $f_symbol =~ ETH|BTC ]] || continue
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//\/}"
# remove file older then 1 day and refetch
[ "$f_symbol_file" -ot TRANSACTIONS-TIMESTAMP ] && rm -f "$f_symbol_file"
# fetch only if not exists
[ -f "$f_symbol_file" ] && continue
g_echo_note "fetchng closed orders of $f_symbol"
f_ccxt "print(${STOCK_EXCHANGE}.fetchMyTrades(symbol='$f_symbol', limit=500, params={'paginate': True}))"
# write to file
#cat ${g_python_out} >"$f_symbol_file"
[[ $f_ccxt_result = \[\] ]] && f_ccxt_result=""
echo -n $f_ccxt_result >"$f_symbol_file"
# continue if no trade
[ -z "$f_ccxt_result" ] && continue
# get f_asset+f_currency from symbol (BTC/USDT)
g_array $f_symbol f_symbol_array /
f_asset=${f_symbol_array[0]}
f_currency=${f_symbol_array[1]}
f_symbol_file_csv_tmp="${f_symbol_file}.csv.tmp"
f_symbol_file_csv="${f_symbol_file}.csv"
>"${f_symbol_file_csv_tmp}"
# Check for contract/leverage Trade
f_leverage=""
if [[ $f_currency =~ : ]]
then
# mark
f_leverage="leverage-"
# get funding fees
f_ccxt "print(${STOCK_EXCHANGE}.fetchFundingHistory('$f_symbol', limit=200, params={'paginate': True}))"
[[ $f_ccxt_result = \[\] ]] || echo -n $f_ccxt_result >"${f_symbol_file}.FundingFees"
cat ${f_symbol_file}.FundingFees | jq -r "
.[] |
.datetime + \",fundingfee,$f_asset,0,\" + .code + \",0\" + \",$f_exchange,\" + .code + \",\" + (.amount|tostring)
" >>$f_symbol_file_csv_tmp
# remove the ':' in f_currency
g_array "$f_currency" f_currency_array :
f_currency=${f_currency_array[0]}
fi
# generate csv
# get longs (posSide=="1")
cat "$f_symbol_file" | jq -r "
.[] |
select(.side==\"buy\" or .side==\"sell\") |
select(.info.posSide==\"1\") |
select(.symbol!= null) |
.datetime + \",$f_leverage\" + .side + \",$f_asset,\" + (.amount|tostring) + \",$f_currency,\" + (.cost|tostring) + \",$f_exchange,\" + .fee.currency + \",\" + (.fee.cost|tostring)
" >>"$f_symbol_file_csv_tmp"
# get shorts (posSide=="2") sell first, then buy (https://github.com/ccxt/ccxt/issues/22518)
cat "$f_symbol_file" | jq -r "
.[] |
select(.side==\"sell\") |
select(.info.posSide==\"2\") |
select(.symbol!= null) |
.side = \"sell\" |
.datetime + \",$f_leverage\" + .side + \",$f_asset,\" + (.amount|tostring) + \",$f_currency,\" + (.cost|tostring) + \",$f_exchange,\" + .fee.currency + \",\" + (.fee.cost|tostring) + \",short\"
" >>"$f_symbol_file_csv_tmp"
cat "$f_symbol_file" | jq -r "
.[] |
select(.side==\"buy\") |
select(.info.posSide==\"2\") |
select(.symbol!= null) |
.side = \"buy\" |
.datetime + \",$f_leverage\" + .side + \",$f_asset,\" + (.amount|tostring) + \",$f_currency,\" + (.cost|tostring) + \",$f_exchange,\" + .fee.currency + \",\" + (.fee.cost|tostring) + \",short\"
" >>"$f_symbol_file_csv_tmp"
if [ -s "$f_symbol_file_csv_tmp" ]
then
if [ -s "$f_symbol_file_csv" ]
then
cat "$f_symbol_file_csv_tmp" "$f_symbol_file_csv" | sort -u >"${f_symbol_file_csv}.sorted"
mv "${f_symbol_file_csv}.sorted" "$f_symbol_file_csv"
rm "$f_symbol_file_csv_tmp"
else
mv "$f_symbol_file_csv_tmp" "$f_symbol_file_csv"
fi
else
rm "$f_symbol_file_csv_tmp"
fi
done
## Get converts on supported exchanges since 2022
if [[ $f_exchange = binance ]]
then
printf -v f_convert_end_year '%(%Y)T'
for ((y=2022;y<=$f_convert_end_year;y++))
do
f_convert_end_month=12
[[ $y == $f_convert_end_year ]] && printf -v f_convert_end_month '%(%m)T'
for ((m=1;m<=$f_convert_end_month;m++))
do
f_start_date="$(date -d "$y-$m-1" +%s)001"
em=$((m+1))
[ $em = 13 ] && em=1
f_end_date="$(date -d "$y-$em-1" +%s)000"
f_convert_file="TRANSACTIONS-$f_exchange/CONVERT-$y-$m"
[ -s "${f_convert_file}.csv" ] && continue
f_ccxt "print(${f_exchange}.fetchConvertTradeHistory(since=${f_start_date}, params={'until': ${f_end_date}}))"
[[ $f_ccxt_result = \[\] ]] && f_ccxt_result=""
echo -n $f_ccxt_result >"$f_convert_file"
if [ -s "$f_convert_file" ]
then
cat "$f_convert_file" | jq -r "
.[] |
select(.info.side==\"BUY\") |
.datetime + \",\" + (.info.side|ascii_downcase) + \",\" + .toCurrency + \",\" + (.toAmount|tostring) + \",\" + .fromCurrency + \",\" + (.fromAmount|tostring) + \",$f_exchange\"
" >"${f_convert_file}.csv"
cat "$f_convert_file" | jq -r "
.[] |
select(.info.side==\"SELL\") |
.datetime + \",\" + (.info.side|ascii_downcase) + \",\" + .fromCurrency + \",\" + (.fromAmount|tostring) + \",\" + .toCurrency + \",\" + (.toAmount|tostring) + \",$f_exchange\"
" >>"${f_convert_file}.csv"
fi
done
done
fi
# put all sorted n one file
if [ -s TRANSACTIONS-$f_exchange.csv ]
then
cat "TRANSACTIONS-$f_exchange/"*.csv TRANSACTIONS-$f_exchange.csv | sort -u >TRANSACTIONS-$f_exchange.csv.tmp
mv TRANSACTIONS-$f_exchange.csv.tmp TRANSACTIONS-$f_exchange.csv
else
cat "TRANSACTIONS-$f_exchange/"*.csv | sort -u >TRANSACTIONS-$f_exchange.csv
fi
# Switch sides if Fiat is in Krypto side
local f_fiats="USD EUR"
local f_fiat
for f_fiat in $f_fiats
do
[ -f TRANSACTIONS-$f_exchange.csv.tmp ] && rm TRANSACTIONS-$f_exchange.csv.tmp
grep -h ",sell,$f_fiat," TRANSACTIONS-$f_exchange.csv | awk -F, '{print $1",buy,"$5","$6","$3","$4","$7","$8","$9}' >>TRANSACTIONS-$f_exchange.csv.tmp
grep -h ",buy,$f_fiat," TRANSACTIONS-$f_exchange.csv | awk -F, '{print $1",sell,"$5","$6","$3","$4","$7","$8","$9}' >>TRANSACTIONS-$f_exchange.csv.tmp
if [ -s TRANSACTIONS-$f_exchange.csv.tmp ]
then
g_echo_note "Switched some fiat/krypto sides"
#cat TRANSACTIONS-$f_exchange.csv.tmp
cat TRANSACTIONS-$f_exchange.csv | egrep -v ",sell,$f_fiat,|,buy,$f_fiat," >>TRANSACTIONS-$f_exchange.csv.tmp
cat TRANSACTIONS-$f_exchange.csv.tmp | sort >TRANSACTIONS-$f_exchange.csv
fi
done
done
STOCK_EXCHANGE=$DEFAULT_STOCK_EXCHANGE
}