Gain Loss Report from Binance Trade History Export

Don Victor
2 min readNov 15, 2021

--

While you can always use Koinly (affiliate link) to pull your transactions from Binance and generate tax reports, nothing beats the satisfaction (or frustration!) of doing it on your own.

The following post shows how you can generate a gains losses report from a Binance trade history export using nodejs. See the caveats section at the end if you’re wondering what’s covered and what’s not.

Disclaimer: I am not an accountant. This post is not tax advice and should not be construed as such.

The Input

If you haven’t already done so, use this earlier post to export your trade history. You’ll need to export your entire trade history from start to end.

This export looks something like this. Let’s change the name of the file to tradehistory.csv

Date(UTC),Pair,Side,Price,Executed,Amount,Fee
2021–06–30 20:02:37,ETHAUD,SELL,”3,000.0000000000",0.2876600000ETH,862.98000000AUD,0.8629800000AUD
2021–06–25 10:29:18,ETHAUD,BUY,”2,500.0000000000",0.2876600000ETH,719.15000000AUD,0.0002876600ETH
...

The Output

The console shows you all the sell trades with the Cost basis and profit for each.

...
{
'Date(UTC)': '2021-05-31 15:36:50',
Pair: 'ETHAUD',
Side: 'SELL',
Price: 3400,
...
CostBasis: 717.6602027027027,
Profit: -145.80862670270275
},
...

And a summary of the total gain and loss.

Net Gain Loss: 342.3086808194797

If you need to filter to a specific tax year, just change the line where netGainLoss is calculated to filter in on Date(UTC).

The Program

Note: this was a use-once-and-throw-away code, so I didn’t bother to handle any errors

Pseudo Code

  • Extract lines from the csv
  • Parse each line
  • Split into buys and sells
  • Match units in each sell against units in buys
  • Group sells with close by trade times together

Caveats

  • The code uses the first in first out method to calculate gains and losses. There are other ways to calculate gains and losses.
  • The code does not handle crypto currency to crypto currency trades. However, to workaround this, you can change the trade history report to replace a crypto-1-to-crypto-2 trade with a crypto-1-to-fiat trade + fiat-to-crypto-2 trade.

Summary

Tax calculation is hard and fun.

If you decide to use Koinly, using the affiliate link (at the beginning of this post) to sign up to a paid plan will send some money my way. Don’t use the affiliate link if you have a discount code though (or you won’t get the discount!)

--

--

Responses (1)