From 2c1b8969510349030b403653488ce4d0b607b955 Mon Sep 17 00:00:00 2001 From: Aaron Fiore Date: Fri, 26 Jul 2024 19:34:27 -0700 Subject: [PATCH] hledger-flow: gemini: exchange: 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. --- .../gemini/gemini-exchange-shared.rules | 18 ++++++++++++++--- .../accounts/gemini/gemini-shared.bash | 20 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/container/src/hledger-flow/accounts/gemini/gemini-exchange-shared.rules b/container/src/hledger-flow/accounts/gemini/gemini-exchange-shared.rules index 7645fa0..6fc31db 100644 --- a/container/src/hledger-flow/accounts/gemini/gemini-exchange-shared.rules +++ b/container/src/hledger-flow/accounts/gemini/gemini-exchange-shared.rules @@ -15,7 +15,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -fields code_,date,type,order_type,currency_one,currency_two,amount_,cost,fees,destination,txid,direction,subaccount +fields code_,date,type,order_type,currency_one,currency_two,amount_,cost,fees,cost_basis,destination,txid,direction,subaccount date-format %Y-%m-%d %H:%M:%S description %date +0000 @@ -92,12 +92,24 @@ if %type ^TRADE$ if %order_type ^Buy$ amount %amount_ %currency_one @@ %cost %currency_two comment type:%type, order_type:%order_type, order_id:%txid, tid:%code_, taxed_as:BUY - comment2 %date +0000,BUY,gemini,%currency_one,%amount_,%currency_two,%cost,%currency_two,%fees + comment2 %date +0000,BUY,gemini,%currency_one,%amount_,%currency_two,%cost_basis,%currency_two,%fees +# 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 %order_type ^Buy$ +& %currency_two ^USD$ + comment2 %date +0000,BUY,gemini,%currency_one,%amount_,%currency_two,%cost_basis,, if %order_type ^Sell$ amount -%amount_ %currency_one @@ %cost %currency_two comment type:%type, order_type:%order_type, order_id:%txid, tid:%code_, taxed_as:SELL - comment2 %date +0000,SELL,gemini,%currency_one,%amount_,%currency_two,%cost,%currency_two,%fees + comment2 %date +0000,SELL,gemini,%currency_one,%amount_,%currency_two,%cost_basis,%currency_two,%fees +# 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 %order_type ^Sell$ +& %currency_two ^USD$ + comment2 %date +0000,SELL,gemini,%currency_one,%amount_,%currency_two,%cost_basis,, # GUSD/USD workaround (see preprocess) if %type ^TRADE$ diff --git a/container/src/hledger-flow/accounts/gemini/gemini-shared.bash b/container/src/hledger-flow/accounts/gemini/gemini-shared.bash index 1340634..df3c0f5 100755 --- a/container/src/hledger-flow/accounts/gemini/gemini-shared.bash +++ b/container/src/hledger-flow/accounts/gemini/gemini-shared.bash @@ -111,6 +111,7 @@ function parse_transfers() } printf info_feeAmount OFS + printf OFS # Cost-basis (N/A) printf $8 OFS # Destination (info_destination) printf $9 OFS # TXID (info_txHash) @@ -176,6 +177,7 @@ function parse_transfers() printf OFS # Price (N/A) printf OFS # Fees (N/A) + printf OFS # Cost-basis (N/A) printf $6 OFS # Destination (info_destination) printf $7 OFS # TXID (info_txHash) @@ -250,6 +252,23 @@ function parse_trades() } printf fee_amount OFS # Fees + # TODO: HACK: see #51 and respective lib_taxes work-around + # Calculate cost-basis + switch ($5) + { + case "Buy": + cost_basis=sprintf("%.8f", cost + fee_amount) + break + case "Sell": + cost_basis=sprintf("%.8f", cost - fee_amount) + break + default: + printf "FATAL: unsupported order type: " $5 + print $0 + exit + } + printf cost_basis OFS + printf OFS # Destination (N/A) printf $10 OFS # TXID (tid) @@ -309,6 +328,7 @@ function parse_earn() printf priceAmount OFS printf OFS # Fees + printf OFS # Cost-basis printf OFS # Destination printf OFS # TXID