# fifo-capital-gains-js

0.1.1 • Public • Published

# FIFO Capital Gains

Calculate your FIFO capital gains for tax-purposes

## ✨ Features

• Generate FIFO capital gains per sale
• Ability to aggregate capital gains per civil year
• Supports differentation of securities by using a symbol (e.g., stock ticker)

## 🎬 Getting started

Let's demonstrate a simple usage by obtaining the FIFO capital gains per civil year:

Now, let's say just you want the FIFO capital gains per operation, so you can execute your custom logic:

Note: When you have multiple securities, you can use the `symbol` property in operations to distinguish between them.

In case you want to aggregate the capital gains by civil year, regardless of the symbols that produced them, you can use the `aggregateByYear` function:

## 📜 API

Operations are represented using the `Operation` object. Its composition is the following:

Capital gains are represented using the `CapitalGains` object, which is defined as:

### `calculateFIFOCapitalGains(operationHistory: Operation[]): CapitalGains[]`

This method calculates the FIFO capital gains for each sell operation in the given operation history. It separates capital gains of securities using the symbols given in each operation.

`operationHistory` contains the history of operations (buy and sales) to calculate the capital gains for.

This method will throw if the amount of securities of all sell operations of a given symbol exceeds the amount of securities of all buy operations for the same symbol. This indicates that there is an error in the input, since it is not possible to sell more securities than the ones bought.

Example:

### `aggregateByYear(saleCapitalGains: CapitalGains[]): YearlyCapitalGains`

This method aggregates the capital gains generated by a sequence of sales in a yearly basis. It returns an object with the civil year as its key and the capital gains generated in the given year as its value.

`saleCapitalGains` is an array of capital gains generated by sell operations. It can be generated by calling `calculateFIFOCapitalGains` with the operation history.

Example:

### `calculateSalesForNetWithdrawal(history: Operation[], options: NetSalesOptions): Operation[]`

This method calculates the sales required to obtain a withdrawal amount set, given the history of purchases and sales and some settings.

MIT as always

## Package Sidebar

### Install

`npm i fifo-capital-gains-js`

76

0.1.1

MIT

93.6 kB

48