eprbell / dali-rp2

DaLI (Data Loader Interface) is a data loader and input generator for RP2 (https://pypi.org/project/rp2), the privacy-focused, free, open-source cryptocurrency tax calculator: DaLI removes the need to manually prepare RP2 input files. Just like RP2, DaLI is also free, open-source and it prioritizes user privacy.
https://pypi.org/project/dali-rp2/
Apache License 2.0
66 stars 42 forks source link

Transaction Type - BORROW and REPAY LOAN #99

Open topcoderasdf opened 2 years ago

topcoderasdf commented 2 years ago

I was also wondering whether we would also need transaction type - BORROW and REPAY LOAN for margin trading. (related issue https://github.com/eprbell/rp2/issues/46)

scenario 1) Borrow 1 BTC at $20000, Pay back 1 BTC at $15000 without any trading. This results in zero profit (for simplicity, there is zero margin loan interest)

scenario 2) Borrow 1 BTC at $20000 and sell it on spot at $20000. Buy back 1 BTC at $15000 and pay back. This results in $5000 profit.

I tried to map margin trading and I think it requires keeping track of current balance vs loan amount to actually compute profit/loss of margin trading.

if current balance (including loan) >= loan amount, margin trading cannot contribute any profit/loss. However, when current balance (including loan) < loan amount, that is when margin trading starts to affect your profit / loss

for scenario 1) current balance : 1 BTC >= loan amount: 1 BTC during the whole process so it does not affect profit/loss for scenario 2) once you sell the borrowed BTC, current balance: 0 BTC < loan amount 1 BTC and it starts to affect profit / loss.

Also, you can notice that your borrow price and pay back price don't really matter. What matters are the trade prices when current balance < loan amount

ex ) Borrow 1 BTC at $20000. Sell 1 BTC at $25000. Buy 1 BTC at $15000. Pay back 1 BTC at $22000. This results in $10000 ($25000 - $15000).

Even for scenarios where you hold assets along with borrowed assets, current balance vs loan amount is all it matters.

scenario 3) Buy 2 BTC at $ 20000. Borrow 1 BTC at $ 20000. Sell 2 BTC at $ 15000. Pay back 1 BTC at $40000. Initially, current balance: 2 BTC >= 0 BTC loan amount. Then it becomes 3 BTC >= 1 BTC. Next 1 BTC >= 1 BTC (after selling 2 BTC).

In this case margin trading doesn't contribute any profit/loss. And you lose ($20000 - $15000) x 2 from trading your wholly owned BTC assets.

So I think with BORROW and REPAY LOAN transaction types, we can keep track of loan amount. Also, I think rp2 would need additional calculations that incorporate the above logic to compute margin trading profit/loss

eprbell commented 2 years ago

Thanks for starting the discussion on this (I haven't spent much time thinking about margin).

Initially I thought there may be a simpler way of representing margin in RP2:

However I then realized that borrowed and repaid coins affect accounting in a way that's not clear to me tax-wise.

For example, let's say we're using FIFO with the above example:

The question I have is: does the repaid coin come out of the FIFO queue or is there special / separate accounting for borrowed coins?

If the answer is FIFO queue:

If the answer is separate accounting for borrowed coins:

I don't know which way is correct according to tax law.

topcoderasdf commented 2 years ago

I don't know about tax law, but my guess would be to follow the second case (separate accounting). if we do FIFO, there are cases where things become problematic.

ex)

Let's say we use FIFO to compute the above case. Then our first sell is $ 5000 profit. Then we REPAY and as REPAY is non-taxable there is no profit / loss *. So my overall profit / loss is + $ 5000 gain. However, in reality, I have invested $ 20000, but left with only $ 15000 and my overall profit / loss is - $ 5000 loss.


*BORROW and REPAY should be non-taxable events as you have mentioned and my

This scenario also validates that BORROW and REPAY should be considered as non-taxable events. If we start considering BORROW and REPAY as taxable events, then this scenario becomes $ 5000 profit / loss depending on how you perceive it, but clearly, no profit / loss has been made.


My conclusion is that borrow and repay prices don't contribute to cost basis. Only trade prices should be considered for tax computation, which is validated by one of the examples from my original post

ex ) Borrow 1 BTC at $20000. Sell 1 BTC at $25000. Buy 1 BTC at $15000. Pay back 1 BTC at $22000. This results in $10000 ($25000 - $15000).

We can change the borrow and repay prices to any numbers and the overall profit / loss wouldn't change. It will still be ($25000 - $15000) gain.

So if borrow and repay prices don't contribute to cost basis, they shouldn't be queued with other FIFO transactions like buy or sell. So having a separate accounting seems most appropriate.

topcoderasdf commented 2 years ago

Also, maybe this belongs to another issue, but transaction type - LEND might be also necessary for lending crypto.

So along with https://github.com/eprbell/dali-rp2/issues/96, currently 4 transaction types

would need to added.

eprbell commented 2 years ago

I agree with your criticism of option 1 (single queue), but there is also a problem with going with option 2 (separate accounting). Let me use your example to explain what I mean:

You suggested this should result in $10000 ($25000 - $15000), which is reasonable, but to obtain this result you would have to sell a lot you don't have (the buy is after the sale). See this discussion for more details on this: https://ttlc.intuit.com/community/investments-and-rental-properties/discussion/using-lifo-method-for-cryptocurrency-or-even-stock-cost-basis/00/1433542

topcoderasdf commented 2 years ago

Forgot to mention we would also need a transaction type that is the opposite of LEND, which would be named something like RECOVER. (couldn't think of a better terminology)

Now, I was thinking about transactions that would involve LEND and RECOVER.

ex)

I am guessing that for LEND and RECOVER, we do need to follow the FIFO principle.

This led me to think about whether we also need to add transaction type - COLLATERAL(IZE) when doing margin trading.

ex)

If we do a cost basis analysis of the above example,

I think the numbers all add up right.

So to summarize, I think we need the follow transaction types

topcoderasdf commented 2 years ago

You suggested this should result in $10000 ($25000 - $15000), which is reasonable, but to obtain this result you would have to sell a lot you don't have (the buy is after the sale)

It is just my personal opinion, but for margin trading accounting, things are opposite. For usual trades, cost basis is recorded when you buy. However, for margin trading, cost basis is recorded when you sell. So it makes sense for sell to come before buy. This is illustrated from the example that I have stated right above. I'll post a quote from my above cost basis analysis

  • the first sell of 1 BTC (loan) utilizes separate accounting method. So no profit / loss for the initial sell, but cost basis of $ 20000 is recorded for 1 BTC for margin trading.

For margin trading, your buy back transaction is the conclusive action. For usual trading, you selling transaction is the conclusive action. So when you sell your loaned asset and never buy them back and pay back the loan, you don't pay any taxes other than getting hit with loan interests. This is just like for regular non-margin trading, when you buy and don't sell, you don't pay taxes. Moreover, selling initiates margin trading, while buying initiates regular trading.

Also, I hope my example from my previous post makes things clearer as it captures the essence of margin trading

ex)

  • buy 20 ETH at $ 1400 (spot price)
  • buy 20 ETH at $ 1500
  • collaterize 20 ETH and borrow 1 BTC at $ 19000
  • sell 1 BTC at $ 20000 and buy 10 ETH at $ 2000
  • sell 20 ETH at $ 2400
  • sell 10 ETH at $ 2400
  • buy 1 BTC at $ 22000
  • repay 1 BTC at $ 24000
  • uncollaterize 20 ETH

Basically, the above is an example of margin trading that longs ETH against BTC. As ETH price appreciates faster (% 20 price appreciation) than BTC (% 10 price appreciation), margin trading will generate 10% overall profit (gain from ETH - loss from BTC) using the borrowed money.

eprbell commented 2 years ago

I understand your point (thanks for the examples) however I have a few thoughts:

topcoderasdf commented 2 years ago

Im no tax law expert, but I will post quotes from a few articles from tax services.

It’s expected that the profits and losses generated from margin trading are reportable as capital gains and losses, similar to other cryptocurrency disposals.

link: https://coinledger.io/blog/crypto-margin-trades-taxes

Thus, margin trading in and of itself is not taxable because borrowing crypto is not a taxable event. However, if you earn proceeds from margin trading or lending, it is most likely taxable.

link: https://tokentax.co/blog/taxes-on-crypto-margin-trading

These all seem to match our conclusions made from examples and cost basis analysis.


if your interpretation is correct and we allow future cost bases, this will have major ramifications on the RP2 code base (especially the accounting engine): currently future cost basis is flagged as an error in RP2.

While you could think from that perspective, another way to think of this is to use the basically the same RP2 code with the following modifications.

So basically reverse process everything. These will still require some major changes, but the logic still stays pretty much the same, so these are relatively low stress changes rather than high stress changes. I will come back to these point with more concise examples when defining the formal spec requirements.

buy 2 BTC sell 1 BTC borrow 1 BTC sell 1 BTC Does the last sell come out of the bought lot or the borrowed lot and why?

I think you were meaning something like the following instead

I would say the sell comes out from the bought lot and not the borrowed lot even though buying comes later than borrowing. I will give my reasonings using the below example

ex)

let's assume the sell comes out from the borrowed instead (which I consider to be wrong).

One could argue that we could use the buy to clear the tax lot and the numbers come out right too.

Indeed we have gained $ 1000 from the above example.

However, the logic order doesn't make sense. I have shown from various examples that for margin trading

the above example has selling coming after buying, not before, and this basically contradicts the principles of margin trading.

So the correct way to compute cost basis analysis for the above example would be

topcoderasdf commented 2 years ago

Formal Spec Requirements for Margin Trading.

Transaction Types

Two separate accounting engines.

Required metrics

Rules for margin trading accounting engine

Requirements for triggering margin trading

topcoderasdf commented 2 years ago

Example Cases

ex 1)

  1. Borrow 1 BTC at $20000
  2. Pay back 1 BTC at $15000

Cost basis analysis

  1. Current balance: 1 BTC. Margin loan balance: 1 BTC
  2. Current balance: 0 BTC. Margin loan balance: 0 BTC

Total gain / loss: 0


ex 2)

  1. Borrow 1 BTC at $20000
  2. Sell 1 BTC at $20000
  3. Buy 1 BTC at $15000

Cost basis analysis

  1. Current balance: 1 BTC. Margin loan balance: 1 BTC
  2. Selling results in current balance < margin loan balance. Margin trading triggered. Sell assigned to margin trading tax lot with cost basis $ 20000. Current balance 0 BTC. Margin loan balance: 1 BTC.
  3. Cost basis of sell $ 20000 > Cost basis of buy $ 15000. Gain of ($ 20000 - $ 15000) = $ 5000 made. tax lot cleared. Current balance: 1 BTC Margin loan balance: 1 BTC

Total gain / loss : $ 5000


ex 3)

  1. Borrow 1 BTC at $20000
  2. Sell 1 BTC at $25000
  3. Buy 1 BTC at $15000
  4. Pay back 1 BTC at $22000

Cost basis analysis

  1. Current balance: 1 BTC. Margin loan balance: 1 BTC
  2. Selling results in current balance < margin loan balance. Margin trading triggered. Sell assigned to margin trading tax lot with cost basis $ 25000. Current balance 0 BTC. Margin loan balance: 1 BTC.
  3. Cost basis of sell $ 50000 > Cost basis of buy $ 15000. Gain of ($ 25000 - $ 15000) = $ 10000 made. tax lot cleared. Current balance: 1 BTC. Margin loan balance: 1 BTC
  4. Current balance: 0 BTC. Margin loan balance: 0 BTC

Total gain / loss: $ 10000


ex 4)

  1. Buy 20 ETH at $ 1400 (spot price)
  2. Buy 20 ETH at $ 1500
  3. Collaterize 20 ETH and borrow 1 BTC at $ 19000
  4. Sell 1 BTC at $ 20000 and buy 10 ETH at $ 2000
  5. Sell 20 ETH at $ 2400
  6. Sell 10 ETH at $ 2400
  7. Buy 1 BTC at $ 22000
  8. Repay 1 BTC at $ 24000
  9. Uncollaterize 20 ETH

Cost basis analysis

  1. Buy assigned to a regular tax lot with cost basis 20 x $ 1400. Current balance: 20 ETH. Margin loan balance: 0 ETH.
  2. Buy assigned to a regular tax lot with cost basis 20 x $ 1500. ETH: Current balance: 40 ETH. Margin loan balance: 0 ETH.
  3. 20 ETH with cost basis 20 x $ 1400 removed from the FIFO queue and reassigned to a ETH collateral lot (hash map). ETH: Current balance: 40 ETH. Margin loan balance: 0 ETH. BTC: Current balance: 1 BTC. Margin loan balance: 1 BTC.
  4. Selling results in current balance < margin loan balance. Margin trading triggered. Sell assigned to a margin tax lot with cost basis 1 x $ 20000. BTC: Current balance: 0 BTC Margin loan balance: 1 BTC. Buy assigned to regular tax lot with cost basis 10 x $ 2000. ETH: Current balance: 50 ETH Margin loan balance: 0 ETH.
  5. Sell removes 20 ETH with cost basis 20 x $ 1500 from the regular tax FIFO queue. Gain of (20 x $ 2400 - 20 x $ 1500) = $ 18000. ETH: Current balance: 30 ETH. Margin loan balance: 0 ETH.
  6. Sell removes 10 ETH with cost basis 10 x $ 2000 from the regular tax FIFO queue. Gain of (10 x $ 2400 - 10 x $ 2000) = $ 4000. ETH: Current balance: 20 ETH. Margin loan balance: 0 ETH.
  7. Buy clears margin tax lots. Loss of (cost basis of sell $ 20000 - cost basis of buy $ 22000) = - $ 2000. BTC: Current balance: 1 BTC. Margin loan balance: 1 BTC
  8. Current balance 0 BTC. Margin loan balance: 0 BTC
  9. remove 20 ETH with cost basis 20 x $ 1400 from the collateral lot and queue back into the regular FIFO queue. ETH: Current balance 20 ETH. Margin loan balance: 0 ETH

Total gain / loss: Steps 5,6,7 generated gain / loss $ 18000 + $ 4000 - $ 2000 = $ 20000