From 9b6c5a1408b0a407743257d4a37bbfa910406fe3 Mon Sep 17 00:00:00 2001 From: olli Date: Fri, 19 Apr 2024 11:05:41 +0200 Subject: [PATCH] Transactions Overview with German Tax --- dabo/functions/transactions_overview.sh | 264 ++++++++++++++++++++++++ 1 file changed, 264 insertions(+) create mode 100644 dabo/functions/transactions_overview.sh diff --git a/dabo/functions/transactions_overview.sh b/dabo/functions/transactions_overview.sh new file mode 100644 index 0000000..5ff24c9 --- /dev/null +++ b/dabo/functions/transactions_overview.sh @@ -0,0 +1,264 @@ +function transactions_overview { + + g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@" + + get_bitpanda_api_transactions + get_justtrade_csv_transactions + + >ALL_TRANSACTIONS_OVERVIEW.csv.tmp + + local f_exchange f_transactions_array f_transaction + + #f_seconds_one_year_ago=$(date -d "1 year ago" +%s) + + f_tax_rate="32" + + f_assets_per_exchange=$(egrep -h -v '^DATE,TYPE,ASSET,ASSET_AMOUNT,CURRENCY,CURRENCY_AMOUNT,EXCHANGE|^#|^$|^ +$|^$' TRANSACTIONS-*.csv | cut -d, -f3,7 | sort -u) + + f_trade_result=0 + + for f_asset_per_exchange in ${f_assets_per_exchange} + do + f_asset_quantity=0 + f_currency_spent=0 + f_result=0 + f_asset_amount_tax_free=0 + f_asset_amount_tax_able=0 + f_trade_result_asset=0 + mapfile -d, -t f_asset_per_exchange_array < <(echo $f_asset_per_exchange) + f_asset=${f_asset_per_exchange_array[0]} + f_exchange=${f_asset_per_exchange_array[1]%$'\n'} + echo "=== Asset $f_asset on Exchange $f_exchange" + mapfile -t f_transactions_array < <(egrep -h -v '^DATE,TYPE,ASSET,ASSET_AMOUNT,CURRENCY,CURRENCY_AMOUNT,EXCHANGE|^#|^$|^ +$' TRANSACTIONS-*.csv | egrep ",${f_asset},.+,${f_exchange}" | sort) + for f_transaction in ${f_transactions_array[@]} + do + mapfile -d, -t f_transaction_array < <(echo $f_transaction) + f_date=${f_transaction_array[0]} + f_type=${f_transaction_array[1]} + f_asset_amount=${f_transaction_array[3]} + f_currency=${f_transaction_array[4]} + f_currency_amount=${f_transaction_array[5]} + f_sell_result=0 + f_taxable=0 + f_tax_type="" + f_one_year_ago="" + f_staking_rewards_asset=0 + f_giveaway_asset=0 + f_instant_trade_bonus_asset=0 + f_leverage_result_asset=0 + f_asset_amount_tax_able=0 + f_asset_amount_tax_free=0 + + # Ignore USDT + [[ $f_asset =~ USDT ]] && continue + + # If moveng to stake -> irrelevant/ignore + [[ $f_type =~ stake ]] && continue + + # what did I spent on asset in currency + if [[ $f_type =~ buy|leverage-buy ]] + then + g_calc "$f_currency_spent+$f_currency_amount" + f_currency_spent=$g_calc_result + fi + + # what did I spent on asset + if [[ $f_type =~ buy|leverage-buy|reward-staking|instant_trade_bonus|giveaway ]] + then + g_calc "$f_asset_quantity+$f_asset_amount" + f_asset_quantity=$g_calc_result + fi + + + # rise result if reward-staking|instant_trade_bonus|giveaway + if [[ $f_type =~ reward-staking|instant_trade_bonus|giveaway ]] + then + g_calc "$f_result+$f_currency_amount" + f_result=$g_calc_result + + if [[ $f_type =~ reward-staking ]] + then + f_staking_rewards_asset=$f_currency_amount + f_taxable=$f_currency_amount + f_tax_type="Staking Reward (Einkommenssteuersatz)" + fi + if [[ $f_type =~ instant_trade_bonus ]] + then + f_instant_trade_bonus_asset=$f_currency_amount + f_taxable=$f_currency_amount + f_tax_type="Instand Trade Bonus (Einkommenssteuersatz)" + fi + if [[ $f_type =~ giveaway ]] + then + f_giveaway_asset=$f_currency_amount + f_taxable=$f_currency_amount + f_tax_type="Giveaway (Einkommenssteuersatz)" + fi + + fi + + # calculate result and tax (if taxable) on sale + if [[ $f_type =~ sell|leverage-sell ]] + then + + # how much in percentage is sold? + g_calc "100/$f_asset_quantity*$f_asset_amount" + printf -v f_sold_percentage %.2f $g_calc_result + #echo "Sold percentage: $f_sold_percentage" + + # what is the currency amount with result (profit/loss)? + g_calc "$f_currency_amount/$f_sold_percentage*100" + f_curency_amount_with_result=$g_calc_result + #echo "f_curency_amount_with_result: $f_curency_amount_with_result" + + # what is the result of this sell in currency (profit/loss)? + g_calc "$f_curency_amount_with_result-$f_currency_spent" + f_sell_result=$g_calc_result + #echo "Result: $f_sell_result" + + # at leverage always full taxable + if [[ $f_type == leverage-sell ]] + then + f_taxable=$f_sell_result + f_tax_type="Verkauf gehebelter Position (Kapitalertragssteuer)" + # at no leverage-sell + else + ## taxable - one year? + f_seconds_sell_ago=$(date -d "$f_date 1 year ago" +%s) + + oldIFS=$IFS + IFS=$'\n' + # add all but sell to f_asset_amount_tax_able or f_asset_amount_tax_free + mapfile -t f_assetlines_array < <(egrep ",$f_exchange,[a-z]+,$f_asset," ALL_TRANSACTIONS_OVERVIEW.csv.tmp | grep -v ',sell,') + for f_assetline in ${f_assetlines_array[@]} + do + mapfile -d, -t f_assetline_array < <(echo $f_assetline) + f_assetline_date=${f_assetline_array[0]} + f_assetline_type=${f_assetline_array[2]} + f_assetline_amount=${f_assetline_array[4]} + f_seconds_transfer_ago=$(date -d "$f_assetline_date" +%s) + if [ $f_seconds_sell_ago -gt $f_seconds_transfer_ago ] + then + # buy from sell one year ago + g_calc "$f_asset_amount_tax_free+$f_assetline_amount" + f_asset_amount_tax_free=$g_calc_result + else + # buy from sell in one year + g_calc "$f_asset_amount_tax_able+$f_assetline_amount" + f_asset_amount_tax_able=$g_calc_result + fi + done + + # add sell to f_asset_amount_tax_able or f_asset_amount_tax_free + mapfile -t f_assetlines_sell_array < <(egrep ",$f_exchange,sell,$f_asset," ALL_TRANSACTIONS_OVERVIEW.csv.tmp) + for f_assetlinesell in ${f_assetlines_sell_array[@]} + do + mapfile -d, -t f_assetline_sell_array < <(echo $f_assetlinesell) + f_assetline_amount=${f_assetline_sell_array[4]} + g_calc "$f_asset_amount_tax_free-$f_assetline_amount" + f_assetline_amount_left=$g_calc_result + if g_num_is_lower_equal $f_assetline_amount_left 0 + then + g_calc "$f_asset_amount_tax_able+$f_assetline_amount_left" + f_asset_amount_tax_able=$g_calc_result + else + f_asset_amount_tax_free=$f_assetline_amount_left + fi + done + IFS=$oldIFS + + if g_num_is_higher_equal $f_asset_amount_tax_free $f_asset_amount + then + ## completely tax free if over 1 year + f_taxable=0 + f_one_year_ago="yes" + # reduce tax free volume + g_calc "$f_asset_amount_tax_free-$f_asset_amount" + f_asset_amount_tax_free=$g_calc_result + elif g_num_is_lower_equal $f_asset_amount_tax_free 0 + then + ## complete taxable if under 1 year + f_one_year_ago="no" + f_taxable=$f_sell_result + f_tax_type="Verkauf (Einkommenssteuersatz)" + g_calc "$f_asset_amount_tax_able-$f_asset_amount" + f_asset_amount_tax_able=$g_calc_result + else + ## partially taxable + f_one_year_ago="partially" + # calculate taxable num of e.g. ETH + g_calc "$f_asset_amount-$f_asset_amount_tax_free" + f_taxable_asset=$g_calc_result + + # calculate in percentage from sell sum + g_calc "100/$f_asset_amount*$f_taxable_asset" + f_percentage_taxable=$g_calc_result + + # calculate part of sell_result from percentage from sell sum + g_calc "$f_sell_result/100*$f_percentage_taxable" + f_tax_able=$g_calc_result + + f_tax_type="Verkauf (Einkommenssteuersatz)" + g_calc "$f_asset_amount_tax_able-$f_taxable_asset" + + fi + fi + + # refesh amount on sell + g_calc "$f_asset_quantity-$f_asset_amount" + f_asset_quantity=$g_calc_result + g_calc "$f_currency_spent-($f_currency_spent/100*$f_sold_percentage)" + f_currency_spent=$g_calc_result + + #g_calc "$f_trade_result_asset+$f_sell_result" + f_trade_result_asset=$g_calc_result + + fi + + echo "$f_date,$f_exchange,$f_type,$f_asset,$f_asset_amount,$f_currency,$f_currency_amount,$f_one_year_ago,$f_currency_spent,$f_asset_quantity,$f_result,$f_sell_result,$f_tax_type,$f_taxable" >>ALL_TRANSACTIONS_OVERVIEW.csv.tmp + echo "$f_date,$f_exchange,$f_type,$f_asset,$f_asset_amount,$f_currency,$f_currency_amount,$f_one_year_ago,$f_currency_spent,$f_asset_quantity,$f_result,$f_sell_result,$f_tax_type,$f_taxable" + + done + + # calculate totals by exchange/asset + f_staking_rewards_asset=$(cat ALL_TRANSACTIONS_OVERVIEW.csv.tmp | egrep ",$f_exchange,reward-staking,$f_asset," | cut -d, -f7 | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM) }") + f_giveaway_asset=$(cat ALL_TRANSACTIONS_OVERVIEW.csv.tmp | egrep ",$f_exchange,giveaway,$f_asset," | cut -d, -f7 | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM) }") + f_instant_trade_bonus_asset=$(cat ALL_TRANSACTIONS_OVERVIEW.csv.tmp | egrep ",$f_exchange,instant_trade_bonus,$f_asset," | cut -d, -f7 | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM) }") + + echo "Straking Rewards: $f_staking_rewards_asset" + echo "Giveaways: $f_giveaway_asset" + echo "Instand Trade Bonus: $f_instant_trade_bonus_asset" + echo "" + + #echo "$f_asset $f_exchange" | grep "ETH JustTrade" && exit 0 + + done + + mv ALL_TRANSACTIONS_OVERVIEW.csv.tmp ALL_TRANSACTIONS_OVERVIEW.csv + + # calculate totals from csv + f_instant_trade_bonus=$(cat ALL_TRANSACTIONS_OVERVIEW.csv | grep ',instant_trade_bonus,' | cut -d, -f7 | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM) }") + f_staking_rewards=$(cat ALL_TRANSACTIONS_OVERVIEW.csv | grep ',reward-staking,' | cut -d, -f7 | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM) }") + f_giveaway=$(cat ALL_TRANSACTIONS_OVERVIEW.csv | grep ',giveaway,' | cut -d, -f7 | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM) }") + + + echo "========== Total Results ===========" + echo "Trade Result: $f_trade_result" + echo "Staking Result: $f_staking_rewards" + echo "Giveaway Result: $f_giveaway" + echo "Instand Trade Bonus: $f_instant_trade_bonus" + echo "" + + # generate and go through list of years and Exchange/Tax-Types + cat ALL_TRANSACTIONS_OVERVIEW.csv | cut -d- -f1 | sort -u | while read f_tax_year + do + echo "========== Tax year $f_tax_year (German Tax Law) ==========" + cat ALL_TRANSACTIONS_OVERVIEW.csv | grep "^$f_tax_year-" | egrep -v ',,0' | cut -d, -f 2,13 | sort -u | while read f_exchange_tax_type + do + f_tax=$(cat ALL_TRANSACTIONS_OVERVIEW.csv | grep "^$f_tax_year-" | egrep -v ',,0' | cut -d, -f 2,13,14 | grep "$f_exchange_tax_type" | cut -d, -f3 | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM) }") + echo "$f_exchange_tax_type: $f_tax" + done + echo "" + done + +}