This script processes a CSV file to calculate the FIFO (First In, First Out) cost basis and gain/loss for buy and sell transactions. The input file must follow the structure below:
Column Name | Description | Example |
---|---|---|
Date |
The transaction date in YYYY-MM-DD format. |
2025-01-01 |
Asset |
The name of the asset being transacted. | Asset A |
Type |
The transaction type: either Buy or Sell . |
Buy or Sell |
Quantity |
The number of units bought or sold. | 10 or -10 |
Price |
The price per unit for the transaction. | 100 (price per unit) |
Total |
The total value of the transaction (Quantity * Price ). |
1000 |
- The Date column must be sorted in chronological order + Type (because date resolution and BUY + SELL same day) within each asset group.
- If the input CSV is unsorted, the script will automatically sort it by Asset and Date.
- For Type, use only
Buy
for purchases andSell
for sales. - The Quantity must be positive for both buys and sells.
- Ensure all values are numeric (except
Date
,Asset
, andType
). - Each Asset should have consistent naming (e.g., "Bitcoin" vs. "BTC").
Date,Asset,Type,Quantity,Price,Total
2025-01-01,Asset A,Buy,10,100,1000
2025-01-02,Asset B,Buy,20,50,1000
2025-01-05,Asset A,Sell,5,120,600
2025-01-06,Asset B,Sell,10,60,600
see prepare_trade_republic_transactions
. this is assuming a account_transactions.csv
made via the pyTR package. the problem with date resolution is open there as issue.
π‘ Processing asset 10/12: Solana
π BUY added: {'Shares': 9.607163, 'Rate': -10.408900109220589}
π BUY added: {'Shares': 4.55106, 'Rate': -21.972903016000668}
π BUY added: {'Shares': 4.362183, 'Rate': -22.92430189196556}
π BUY added: {'Shares': 4.457579, 'Rate': -22.433702240610877}
π BUY added: {'Shares': 4.776301, 'Rate': -20.936703947259605}
π BUY added: {'Shares': 5.390051, 'Rate': -18.552700150703583}
π BUY added: {'Shares': 5.217355, 'Rate': -19.166800035650247}
π BUY added: {'Shares': 4.312277, 'Rate': -23.18960493493345}
π BUY added: {'Shares': 4.938149, 'Rate': -20.250502769357507}
π BUY added: {'Shares': 5.127547, 'Rate': -19.50250285370373}
π BUY added: {'Shares': 5.033624, 'Rate': -19.866402417025984}
π SELL of 57.635075 shares at 14.299348096623454 per unit.
FIFO queue before processing: [{'Shares': 9.607163, 'Rate': -10.408900109220589}, {'Shares': 4.55106, 'Rate': -21.972903016000668}, {'Shares': 4.362183, 'Rate': -22.92430189196556}, {'Shares': 4.457579, 'Rate': -22.433702240610877}, {'Shares': 4.776301, 'Rate': -20.936703947259605}, {'Shares': 5.390051, 'Rate': -18.552700150703583}, {'Shares': 5.217355, 'Rate': -19.166800035650247}, {'Shares': 4.312277, 'Rate': -23.18960493493345}, {'Shares': 4.938149, 'Rate': -20.250502769357507}, {'Shares': 5.127547, 'Rate': -19.50250285370373}, {'Shares': 5.033624, 'Rate': -19.866402417025984}]
Total amount before processing: 57.773289
β
Fully consumed BUY: {'Shares': 9.607163, 'Rate': -10.408900109220589}
β
Fully consumed BUY: {'Shares': 4.55106, 'Rate': -21.972903016000668}
β
Fully consumed BUY: {'Shares': 4.362183, 'Rate': -22.92430189196556}
β
Fully consumed BUY: {'Shares': 4.457579, 'Rate': -22.433702240610877}
β
Fully consumed BUY: {'Shares': 4.776301, 'Rate': -20.936703947259605}
β
Fully consumed BUY: {'Shares': 5.390051, 'Rate': -18.552700150703583}
β
Fully consumed BUY: {'Shares': 5.217355, 'Rate': -19.166800035650247}
β
Fully consumed BUY: {'Shares': 4.312277, 'Rate': -23.18960493493345}
β
Fully consumed BUY: {'Shares': 4.938149, 'Rate': -20.250502769357507}
β
Fully consumed BUY: {'Shares': 5.127547, 'Rate': -19.50250285370373}
π Partially consumed BUY: {'Shares': 0.138214, 'Rate': -19.866402417025984}
Processed SELL. Cost basis: -1097.254185056333, Gain/Loss: -273.11
FIFO queue after processing: [{'Shares': 0.138214, 'Rate': -19.866402417025984}]
Total bought: 57.773289000000005, Total sold: 57.635075
π SELL of 0.138214 shares at 62.8735149840103 per unit.
FIFO queue before processing: [{'Shares': 0.138214, 'Rate': -19.866402417025984}]
Total amount before processing: 0.138214
β
Fully consumed BUY: {'Shares': 0.138214, 'Rate': -19.866402417025984}
Processed SELL. Cost basis: -2.7458149436668293, Gain/Loss: 5.94
FIFO queue after processing: []
Total bought: 57.773289000000005, Total sold: 57.773289
and a sample result xlsx
Date Asset Type Shares Sale Price FIFO Cost Basis Gain/Loss
2023-06-11 00:00:00 Orchid SELL 1350.236914 0.05 -100.08 -36.08
2023-06-11 00:00:00 Polygon SELL 2271.364974 0.59 -2198.28 -850.42
2023-12-09 00:00:00 Polygon SELL 2.029882 0.36 -1.72 -0.99
2023-06-11 00:00:00 Solana SELL 57.635075 14.3 -1097.25 -273.11
2023-12-09 00:00:00 Solana SELL 0.138214 62.87 -2.75 5.94
2023-06-11 00:00:00 The Sandbox SELL 184.56729 0.34 -100.37 -37.37
2023-06-11 00:00:00 Uniswap SELL 198.518015 3.76 -1098.29 -350.99
2023-12-09 00:00:00 Uniswap SELL 0.360853 3.46 -1.71 -0.46