pnl, tax, transactions

This commit is contained in:
2025-03-01 15:28:50 +01:00
parent 06bfc85485
commit a2848481eb
2 changed files with 111 additions and 93 deletions

View File

@@ -54,19 +54,20 @@ function calc_fifo_pnl {
g_num_exponential2normal $f_fee_amount
f_fee_amount=$g_num_exponential2normal_result
echo "f_fiat_amount=$f_fiat_amount"
## Debug
#echo "f_fiat_amount=$f_fiat_amount"
# convert f_fiat_currency/f_fiat_amount to TRANSFER_CURRENCY/f_fiat_amount_tax_currency if they are not equal
if ! [ "$f_fiat_currency" == "$TRANSFER_CURRENCY" ] && [ "$f_fiat_amount" != "0" ]
then
currency_converter $f_fiat_amount "$f_fiat_currency" $TRANSFER_CURRENCY "$f_date" >/dev/null
f_fiat_amount_tax_currency=$f_currency_converter_result
echo "currency_converter: $f_fiat_amount "$f_fiat_currency" $TRANSFER_CURRENCY "$f_date" -> $f_currency_converter_result"
else
f_fiat_amount_tax_currency=$f_fiat_amount
fi
echo "f_fiat_amount_tax_currency=$f_fiat_amount_tax_currency"
## Debug
#echo "f_fiat_amount_tax_currency=$f_fiat_amount_tax_currency"
# convert f_fee_currency/f_fee_amount to TRANSFER_CURRENCY/f_fiat_amount_tax_currency if present
if [ -n "$f_fee_amount" ]
@@ -78,7 +79,8 @@ function calc_fifo_pnl {
f_fiat_amount_tax_currency=$g_calc_result
fi
echo "f_fiat_amount_tax_currency=$f_fiat_amount_tax_currency"
## Debug
#echo "f_fiat_amount_tax_currency=$f_fiat_amount_tax_currency"
# no space in date (prevent problems mit $f_holdings)
f_date="${f_date/ /T}"
@@ -86,7 +88,8 @@ function calc_fifo_pnl {
# get current holfings for determining if this is a long or short trade (f_holdings_amount)
get_holdings_amount
echo "f_crypto_amount=$f_crypto_amount"
## Debug
#echo "f_crypto_amount=$f_crypto_amount"
# Process fundingfee action
if [[ $f_action == "fundingfee" ]]
@@ -98,12 +101,14 @@ function calc_fifo_pnl {
if g_num_is_higher_equal $f_holdings_amount 0
then
# long
echo process_buy "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date"
## Debug
#echo process_buy "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date"
process_buy "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date"
elif g_num_is_lower_equal "$f_holdings_amount" "-$f_crypto_amount"
then
# short
echo process_sell "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" "$f_year" short
## Debug
#echo process_sell "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" "$f_year" short
process_sell "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" "$f_year" short
else
# long+short (partial)
@@ -119,10 +124,12 @@ function calc_fifo_pnl {
f_crypto_amount_short=$g_calc_result
# part short-sell
# part long-sell
echo PART: process_sell process_sell "$f_symbol" "$f_crypto_amount_long" "$f_fiat_amount_tax_currency_long" "$f_date" "$f_year"
## Debug
#echo PART: process_sell process_sell "$f_symbol" "$f_crypto_amount_long" "$f_fiat_amount_tax_currency_long" "$f_date" "$f_year"
process_sell "$f_symbol" "$f_crypto_amount_long" "$f_fiat_amount_tax_currency_long" "$f_date" "$f_year"
echo PART: process_buy "$f_symbol" "$f_crypto_amount_long" "$f_fiat_amount_tax_currency_long" "$f_date" "$f_year"
## Debug
#echo PART: process_buy "$f_symbol" "$f_crypto_amount_long" "$f_fiat_amount_tax_currency_long" "$f_date" "$f_year"
process_buy "$f_symbol" "$f_crypto_amount_long" "$f_fiat_amount_tax_currency_long" "$f_date" "$f_year"
fi
# Process sell actions
@@ -132,7 +139,8 @@ function calc_fifo_pnl {
if g_num_is_higher_equal "$f_holdings_amount" "$f_crypto_amount"
then
# long
echo process_sell "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" "$f_year"
## Debug
#echo process_sell "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" "$f_year"
process_sell "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" "$f_year"
elif g_num_is_higher "$f_holdings_amount" 0
then
@@ -148,27 +156,31 @@ function calc_fifo_pnl {
g_calc "$f_crypto_amount/100*($g_percentage_diff_result*-1)"
f_crypto_amount_short=$g_calc_result
# part long-sell
echo PART: process_sell "$f_symbol" "$f_crypto_amount_long" "$f_fiat_amount_tax_currency_long" "$f_date" "$f_year"
## Debug
#echo PART: process_sell "$f_symbol" "$f_crypto_amount_long" "$f_fiat_amount_tax_currency_long" "$f_date" "$f_year"
process_sell "$f_symbol" "$f_crypto_amount_long" "$f_fiat_amount_tax_currency_long" "$f_date" "$f_year"
# part short-sell
echo PART: process_buy "$f_symbol" "$f_crypto_amount_short" "$f_fiat_amount_tax_currency_short" "$f_date" short
## Debug
#echo PART: process_buy "$f_symbol" "$f_crypto_amount_short" "$f_fiat_amount_tax_currency_short" "$f_date" short
process_buy "$f_symbol" "$f_crypto_amount_short" "$f_fiat_amount_tax_currency_short" "$f_date" short
elif [[ $f_action == "liquidation" ]]
then
# short sell/liquidation
echo process_sell "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" $f_year short
## Debug
#echo process_sell "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" $f_year short
process_sell "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" $f_year short
else
# short buy
echo process_buy "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" short
## Debug
#echo process_buy "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" short
process_buy "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" short
fi
fi
## DEBUG output
get_holdings_amount
echo "f_holdings_amount=$f_holdings_amount"
echo "============================"
#get_holdings_amount
#echo "f_holdings_amount=$f_holdings_amount"
#echo "============================"
done < "$f_csv_file"
}
@@ -184,7 +196,8 @@ function process_buy {
then
f_holdings[$f_symbol]+="-$f_amount:$f_price:$f_date "
f_action="${f_action}-short"
echo ACTION:$f_action
## Debug
#echo ACTION:$f_action
elif [[ $f_action == "reward-staking" ]]
then
f_tax_type="Sonst-Einkünfte-Staking"
@@ -232,17 +245,20 @@ function process_sell {
[ -n "$f_short" ] && g_num_is_higher $f_remaining_sell $f_buy_amount && f_sell_from_holding=$f_remaining_sell
# calculate sell percentage of buy trade
echo "f_sell_from_holding=$f_sell_from_holding"
## Debug
#echo "f_sell_from_holding=$f_sell_from_holding"
[ -z "$f_short" ] && g_percentage-diff $f_buy_amount $f_sell_from_holding
[ -n "$f_short" ] && g_percentage-diff $f_buy_amount $f_sell_from_holding
g_calc "100+$g_percentage_diff_result"
f_percentage_of_buy=${g_calc_result#-}
echo "f_percentage_of_buy=$f_percentage_of_buy"
## Debug
#echo "f_percentage_of_buy=$f_percentage_of_buy"
# Calculate profit/loss (pnl)
echo "f_sell_price=$f_sell_price"
echo "f_buy_price=$f_buy_price"
## Debug
#echo "f_sell_price=$f_sell_price"
#echo "f_buy_price=$f_buy_price"
# if not first iteration (f_pnl is already set from previous iteration) and partial sell
if [ -n "$f_pnl" ]
then
@@ -253,7 +269,8 @@ function process_sell {
g_calc "$f_sell_price - ($f_buy_price/100*$f_percentage_of_buy)"
fi
local f_pnl=$g_calc_result
echo "f_pnl=$f_pnl"
## Debug
#echo "f_pnl=$f_pnl"
# Check if trade is tax-free (held for more than a year)
local f_is_taxable=true
@@ -266,8 +283,9 @@ function process_sell {
# Update remaining sell amount and holdings
g_calc "$f_remaining_sell - $f_sell_from_holding"
f_remaining_sell=$g_calc_result
echo "f_remaining_sell=$f_remaining_sell"
echo "HOLDINGS1: ${f_holdings[$f_symbol]}"
## Debug
#echo "f_remaining_sell=$f_remaining_sell"
#echo "HOLDINGS1: ${f_holdings[$f_symbol]}"
f_holdings[$f_symbol]="${f_holdings[$f_symbol]#* }"
# If there's remaining amount in the holding, add it back
@@ -275,7 +293,8 @@ function process_sell {
[ -n "$f_short" ] && g_calc "$f_buy_amount + $f_sell_from_holding"
g_calc "$f_buy_amount - $f_sell_from_holding"
local f_remaining_buy_amount=$g_calc_result
echo "f_remaining_buy_amount=$g_calc_result"
## Debug
#echo "f_remaining_buy_amount=$g_calc_result"
#if g_num_is_higher $f_remaining_buy_amount 0
if [ "$f_remaining_buy_amount" != "0" ]
then
@@ -283,7 +302,8 @@ function process_sell {
f_remaining_buy_price=$g_calc_result
f_holdings[$f_symbol]="$f_remaining_buy_amount:$f_remaining_buy_price:$f_buy_date ${f_holdings[$f_symbol]}"
fi
echo "HOLDINGS2: ${f_holdings[$f_symbol]}"
## Debug
#echo "HOLDINGS2: ${f_holdings[$f_symbol]}"
done
# Update profit/loss
@@ -309,15 +329,16 @@ function process_sell {
fi
## DEBUG output
get_holdings_amount
echo "f_holdings_amount=$f_holdings_amount"
echo "Result: $f_trade_result ; taxable=$f_is_taxable ; REMAINING: $f_holdings_amount"
#get_holdings_amount
#echo "f_holdings_amount=$f_holdings_amount"
#echo "Result: $f_trade_result ; taxable=$f_is_taxable ; REMAINING: $f_holdings_amount"
# write to csv
if [ -n "$f_short" ]
then
f_action="${f_action}-short"
echo ACTION:$f_action
## Debug
#echo ACTION:$f_action
fi
[ "$f_trade_tax" == "0" ] && [ "$f_tax_type" == "Veräußerungsgeschäft" ] && f_tax_type="Veräußerungsgeschäft Spekulationsfrist > 1 Jahr"
echo "$f_date,$f_exchange,$f_action,$f_symbol,-$f_sell_amount,$f_fiat_currency,$f_sell_price,,,,,,$f_tax_type,$f_trade_tax,$f_sell_price,,$f_trade_result,,,,,,," >>ALL_TRANSACTIONS_OVERVIEW.csv.tmp
@@ -347,8 +368,9 @@ function get_holdings_amount {
function process_fundingfee {
local f_symbol="$1" f_amount="$2" f_fiat_amount_tax_currency="$3" f_date="$4" f_year="$5"
echo "adding fundingfee: $f_fiat_amount_tax_currency"
## Debug
#echo "adding fundingfee: $f_fiat_amount_tax_currency"
## add fundingfee
[[ $f_fiat_amount_tax_currency == -* ]] && f_tax="${f_fiat_amount_tax_currency#-}"
@@ -357,61 +379,61 @@ function process_fundingfee {
echo "$f_date,$f_exchange,$f_action,$f_symbol,$f_amount,,,,,,,,Kapitalertrag-Derivat,$f_tax,$f_tax,,$f_tax,,,,,,," >>ALL_TRANSACTIONS_OVERVIEW.csv.tmp
}
function transaction_csv_validity_ckecks {
local f_buy f_sell f_liquidation f_liquidation_short
local f_complete_result=0
declare -A transaction_csv_validity_ckeck_buy_sell_diff
f_symbols=$(cut -d, -f3 $f_csv_file | sort -u)
local f_buy_amount f_sell_amount f_tax_type
# go through symbols and male some pre-checks
for f_symbol in $f_symbols
do
## check asset amount
g_echo_note "Initial checks for $f_symbol"
# add all buys and sells of a symbols amount
f_buy=$(\
egrep "buy,${f_symbol},|,reward-staking,${f_symbol}|,giveaway,${f_symbol},instant_trade_bonus,${f_symbol}" "$f_csv_file" | \
cut -d, -f4 | \
awk '{ SUM += $1} END { printf("%.12f\n", SUM) }' \
)
f_sell=$(\
egrep "sell,${f_symbol}," "$f_csv_file" | \
cut -d, -f4 | \
awk '{ SUM += $1} END { printf("%.12f\n", SUM) }' \
)
f_liquidation=$(\
egrep "liquidation,${f_symbol}," "$f_csv_file" | \
grep -v ",short" | \
cut -d, -f4 | \
awk '{ SUM += $1} END { printf("%.12f\n", SUM) }' \
)
f_liquidation_short=$(\
egrep "liquidation,${f_symbol},.+,short" "$f_csv_file" | \
cut -d, -f4 | \
awk '{ SUM += $1} END { printf("%.12f\n", SUM) }' \
)
# add liquidations to sell
# long
g_calc "$f_sell + $f_liquidation - $f_liquidation_short"
f_sell=$g_calc_result
# buy should be same as sell sum to be fine - if not:
g_calc "$f_buy == $f_sell"
if ! [[ $g_calc_result == 1 ]]
then
g_echo_note "buy ($f_buy) and sell ($f_sell) amount sums are different for ${f_symbol}. Open Positions!?"
g_calc "$f_sell - ($f_buy)"
transaction_csv_validity_ckecks[$f_symbol]=$g_calc_result
else
transaction_csv_validity_ckeck_buy_sell_diff[$f_symbol]=0
fi
done
}
#function transaction_csv_validity_ckecks {
# local f_buy f_sell f_liquidation f_liquidation_short
# local f_complete_result=0
# declare -A transaction_csv_validity_ckeck_buy_sell_diff
#
# f_symbols=$(cut -d, -f3 $f_csv_file | sort -u)
# local f_buy_amount f_sell_amount f_tax_type
#
# # go through symbols and male some pre-checks
# for f_symbol in $f_symbols
# do
#
# ## check asset amount
# g_echo_note "Initial checks for $f_symbol"
# # add all buys and sells of a symbols amount
# f_buy=$(\
# egrep "buy,${f_symbol},|,reward-staking,${f_symbol}|,giveaway,${f_symbol},instant_trade_bonus,${f_symbol}" "$f_csv_file" | \
# cut -d, -f4 | \
# awk '{ SUM += $1} END { printf("%.12f\n", SUM) }' \
# )
# f_sell=$(\
# egrep "sell,${f_symbol}," "$f_csv_file" | \
# cut -d, -f4 | \
# awk '{ SUM += $1} END { printf("%.12f\n", SUM) }' \
# )
# f_liquidation=$(\
# egrep "liquidation,${f_symbol}," "$f_csv_file" | \
# grep -v ",short" | \
# cut -d, -f4 | \
# awk '{ SUM += $1} END { printf("%.12f\n", SUM) }' \
# )
# f_liquidation_short=$(\
# egrep "liquidation,${f_symbol},.+,short" "$f_csv_file" | \
# cut -d, -f4 | \
# awk '{ SUM += $1} END { printf("%.12f\n", SUM) }' \
# )
#
# # add liquidations to sell
# # long
# g_calc "$f_sell + $f_liquidation - $f_liquidation_short"
# f_sell=$g_calc_result
#
# # buy should be same as sell sum to be fine - if not:
# g_calc "$f_buy == $f_sell"
# if ! [[ $g_calc_result == 1 ]]
# then
# g_echo_note "buy ($f_buy) and sell ($f_sell) amount sums are different for ${f_symbol}. Open Positions!?"
# g_calc "$f_sell - ($f_buy)"
# transaction_csv_validity_ckecks[$f_symbol]=$g_calc_result
# else
# transaction_csv_validity_ckeck_buy_sell_diff[$f_symbol]=0
# fi
#
# done
#}
function print_results {

View File

@@ -56,10 +56,6 @@ function get_phemex_csv_transactions {
grep -q "$f_id" "TRANSACTIONS-phemex.csv" || echo $f_line >>"TRANSACTIONS-phemex.csv"
done
#cat TRANSACTIONS-phemex-LONG.csv.tmp TRANSACTIONS-phemex.csv.tmp TRANSACTIONS-phemex-liquidations-LONG.csv.tmp | sort >TRANSACTIONS-phemex.csv
#cat TRANSACTIONS-phemex-SHORT.csv.tmp TRANSACTIONS-phemex-liquidations-SHORT.csv.tmp | sort >>TRANSACTIONS-phemex.csv
# cleanup
rm -f TRANSACTIONS-phemex-LONG.csv.tmp TRANSACTIONS-phemex-SHORT.csv.tmp TRANSACTIONS-phemex.csv.tmp TRANSACTIONS-phemex-liquidations-LONG.csv.tmp TRANSACTIONS-phemex-liquidations-SHORT.csv.tmp
fi