From 1975e1dbbbd211ea842f130a9812511cb2ac897f Mon Sep 17 00:00:00 2001 From: Aaron Fiore Date: Fri, 26 Jul 2024 19:55:11 -0700 Subject: [PATCH] hledger-flow: coinbase: cost-basis work-around for bitcoin.tax Issue #51 describes at least a few undocumented bitcoin.tax issues: - Bicoin.tax gives unexpected cost-basis results when `Fee` is given with `Total` (when `Total` is given in place of `Price`). The expectation is that bitcoin.tax will perform the cost-basis calculation on `Total` when `Fee` is also given. However, bitcoin.tax *will* give expected cost-basis results if `Price` is given in place of `Total` (with `Fee` also given) *or* if `Total` is given *after* local cost-basis adjustments are made (but *without* `Fee` given). The rationale for why docker-finance doesn't use `Price`: * docker-finance has all of the `Total`s; so `Price` isn't necessary. * Local price information isn't available for most trades (and shouldn't be necessary since all `Total`s are available). - Additionally, when `Fee` is non-fiat (crypto), it now must be marked as a SPEND in order to be disposed (and to produce an accurate closing report). - Finally, if `FeeCurrency` *does* not match either `Symbol` or `Currency` (e.g., BTC-ETH w/ BNB fee), it's unknown if cost-basis must be calculated locally as well (if `Total` is given). Local calculations cannot be done because `Fee` price information is (almost certainly) not available for this type of trade. Until upstream can assert that attaching the `Fee` will subsequently adjust the cost-basis of `Total` *and* dispose of the `Fee` in the process (while also allowing `Total` to be used in place of `Price`), the `Fee` (and `FeeCurrency`) column(s) must not be populated and values instead moved to SPEND (as described above). Upstream is aware of these issues (since May) and they're in the process of resolution. In the meantime, docker-finance work-arounds should suffice for all trades that have a fiat `Fee` and/or a `Fee`/`FeeCurrency` that matches one side of the trading pair. --- .../accounts/coinbase/coinbase-shared.rules | 58 ++++++++++++++++++- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/container/src/hledger-flow/accounts/coinbase/coinbase-shared.rules b/container/src/hledger-flow/accounts/coinbase/coinbase-shared.rules index 1eb8664..4f7154d 100644 --- a/container/src/hledger-flow/accounts/coinbase/coinbase-shared.rules +++ b/container/src/hledger-flow/accounts/coinbase/coinbase-shared.rules @@ -359,7 +359,17 @@ if %type ^buy$ account2 assets:coinbase:%subaccount:%buy_total_currency comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, buy_id:%buy_id, taxed_as:BUY comment2 %created_at +0000,BUY,coinbase,%amount_currency,%amount_amount,%buy_total_currency,%buy_total_amount,, -# NOTE: cost-basis = proceeds + fee = %buy_total_amount (but do *NOT* include Fee/FeeCurrency column) + +if %type ^buy$ +& %buy_fee_amount [1-9] + comment2 %created_at +0000,BUY,coinbase,%amount_currency,%amount_amount,%buy_total_currency,%buy_total_amount,%buy_fee_currency,%buy_fee_amount +# TODO: HACK: re: #51, FeeCurrency and Fee are added even though cost-basis is calculated (for non-fiat fee disposal, see lib_taxes) + +# TODO: HACK: see #51 and respective lib_taxes work-around +if %type ^buy$ +& %buy_fee_amount [1-9] +& %buy_fee_currency ^USD$ + comment2 %created_at +0000,BUY,coinbase,%amount_currency,%amount_amount,%buy_total_currency,%buy_total_amount,, if %type ^buy$ & %buy_total_currency ^USDC$ @@ -404,7 +414,17 @@ if %type ^sell$ account2 assets:coinbase:%subaccount:%sell_total_currency comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, sell_id:%sell_id, taxed_as:SELL comment2 %created_at +0000,SELL,coinbase,%amount_currency,%amount_amount,%sell_total_currency,%sell_total_amount,, -# NOTE: cost-basis = sale - fee = %sell_total_amount (but do *NOT* include Fee/FeeCurrency column) + +if %type ^sell$ +& %sell_fee_amount [1-9] + comment2 %created_at +0000,SELL,coinbase,%amount_currency,%amount_amount,%sell_total_currency,%sell_total_amount,%sell_fee_currency,%sell_fee_amount +# TODO: HACK: re: #51, FeeCurrency and Fee are added even though cost-basis is calculated (for non-fiat fee disposal, see lib_taxes) + +# TODO: HACK: see #51 and respective lib_taxes work-around +if %type ^sell$ +& %sell_fee_amount [1-9] +& %sell_fee_currency ^USD$ + comment2 %created_at +0000,SELL,coinbase,%amount_currency,%amount_amount,%sell_total_currency,%sell_total_amount,, if %type ^sell$ & %sell_total_currency ^USDC$ @@ -689,6 +709,23 @@ if %type ^advanced_trade_fill$ comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, advanced_trade_fill_fill_price:%advanced_trade_fill_fill_price, advanced_trade_fill_product_id:%advanced_trade_fill_product_id, advanced_trade_fill_order_id:%advanced_trade_fill_order_id, advanced_trade_fill_order_side:%advanced_trade_fill_order_side, advanced_trade_fill_pair_lhs:%advanced_trade_fill_pair_lhs, advanced_trade_fill_pair_rhs:%advanced_trade_fill_pair_rhs, direction:%direction, taxed_as:BUY comment2 %created_at +0000,BUY,coinbase,%advanced_trade_fill_pair_lhs,%amount_amount,%advanced_trade_fill_pair_rhs,%advanced_trade_fill_cost_basis_amount,, +if %type ^advanced_trade_fill$ +& %advanced_trade_fill_order_side ^buy$ +& %advanced_trade_fill_real_value_amount [1-9] +& %advanced_trade_fill_commission [1-9] +& %direction ^IN$ + comment2 %created_at +0000,BUY,coinbase,%advanced_trade_fill_pair_lhs,%amount_amount,%advanced_trade_fill_pair_rhs,%advanced_trade_fill_cost_basis_amount,%advanced_trade_fill_pair_rhs,%advanced_trade_fill_commission +# TODO: HACK: re: #51, FeeCurrency and Fee are added even though cost-basis is calculated (for non-fiat fee disposal, see lib_taxes) + +# TODO: HACK: see #51 and respective lib_taxes work-around +if %type ^advanced_trade_fill$ +& %advanced_trade_fill_order_side ^buy$ +& %advanced_trade_fill_real_value_amount [1-9] +& %advanced_trade_fill_commission [1-9] +& %advanced_trade_fill_pair_rhs ^USD$ +& %direction ^IN$ + comment2 %created_at +0000,BUY,coinbase,%advanced_trade_fill_pair_lhs,%amount_amount,%advanced_trade_fill_pair_rhs,%advanced_trade_fill_cost_basis_amount,, + # # SELL # @@ -708,4 +745,21 @@ if %type ^advanced_trade_fill$ comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, advanced_trade_fill_fill_price:%advanced_trade_fill_fill_price, advanced_trade_fill_product_id:%advanced_trade_fill_product_id, advanced_trade_fill_order_id:%advanced_trade_fill_order_id, advanced_trade_fill_order_side:%advanced_trade_fill_order_side, advanced_trade_fill_pair_lhs:%advanced_trade_fill_pair_lhs, advanced_trade_fill_pair_rhs:%advanced_trade_fill_pair_rhs, direction:%direction, taxed_as:SELL comment2 %created_at +0000,SELL,coinbase,%advanced_trade_fill_pair_lhs,%amount_amount,%advanced_trade_fill_pair_rhs,%advanced_trade_fill_cost_basis_amount,, +if %type ^advanced_trade_fill$ +& %advanced_trade_fill_order_side ^sell$ +& %advanced_trade_fill_real_value_amount [1-9] +& %advanced_trade_fill_commission [1-9] +& %direction ^OUT$ + comment2 %created_at +0000,SELL,coinbase,%advanced_trade_fill_pair_lhs,%amount_amount,%advanced_trade_fill_pair_rhs,%advanced_trade_fill_cost_basis_amount,%advanced_trade_fill_pair_rhs,%advanced_trade_fill_commission +# TODO: HACK: re: #51, FeeCurrency and Fee are added even though cost-basis is calculated (for non-fiat fee disposal, see lib_taxes) + +# TODO: HACK: see #51 and respective lib_taxes work-around +if %type ^advanced_trade_fill$ +& %advanced_trade_fill_order_side ^sell$ +& %advanced_trade_fill_real_value_amount [1-9] +& %advanced_trade_fill_commission [1-9] +& %advanced_trade_fill_pair_rhs ^USD$ +& %direction ^OUT$ + comment2 %created_at +0000,SELL,coinbase,%advanced_trade_fill_pair_lhs,%amount_amount,%advanced_trade_fill_pair_rhs,%advanced_trade_fill_cost_basis_amount,, + # vim: sw=2 sts=2 si ai et