fifo-capital-gains-js
Advanced tools
Comparing version 0.1.0 to 0.1.1
/** | ||
* Aggregates the capital gains generated by a sequence of sales by year | ||
* | ||
* @param saleCapitalGains Array of capital gains generated by sell operations | ||
* | ||
* @returns A map of year to capital gains generated in the given year | ||
*/ | ||
function aggregateByYear(saleCapitalGains) { | ||
return saleCapitalGains.reduce((capitalGainsPerYear, { capitalGains, sale: { date } }) => ({ | ||
...capitalGainsPerYear, | ||
[date.getFullYear()]: (capitalGainsPerYear[date.getFullYear()] || 0) + capitalGains, | ||
}), {}); | ||
} | ||
/** | ||
* Calculates the FIFO capital gains for the given operation history. | ||
@@ -24,15 +38,2 @@ * It separates capital gains of securities using the symbols given | ||
} | ||
/** | ||
* Aggregates the capital gains generated by a sequence of sales by year | ||
* | ||
* @param saleCapitalGains Array of capital gains generated by sell operations | ||
* | ||
* @returns A map of year to capital gains generated in the given year | ||
*/ | ||
function aggregateByYear(saleCapitalGains) { | ||
return saleCapitalGains.reduce((capitalGainsPerYear, { capitalGains, sale: { date } }) => ({ | ||
...capitalGainsPerYear, | ||
[date.getFullYear()]: (capitalGainsPerYear[date.getFullYear()] || 0) + capitalGains, | ||
}), {}); | ||
} | ||
function calculateCapitalGainsForSale(operationHistory, sale) { | ||
@@ -58,3 +59,79 @@ let capitalGains = 0; | ||
export { calculateFIFOCapitalGains, aggregateByYear }; | ||
/** | ||
* Consolidates the history by deleting purchase and sale that match (i.e., same symbol), | ||
* so that the history contains purchases without a matching sale and sales without | ||
* a matching purchase. | ||
* @param history History of operation | ||
*/ | ||
function consolidateHistory(history) { | ||
const _history = [...history]; | ||
_history.forEach(({ type, symbol }, index) => { | ||
if (type === 'BUY') { | ||
return; | ||
} | ||
for (let i = 0; i < index; i++) { | ||
const operation = _history[i]; | ||
const amount = _history[index].amount; | ||
if (operation.type === 'BUY' && operation.symbol === symbol) { | ||
_history[i] = { | ||
...operation, | ||
amount: Math.max(0, operation.amount - amount), | ||
}; | ||
_history[index] = { | ||
..._history[index], | ||
amount: amount - Math.min(operation.amount, amount), | ||
}; | ||
} | ||
} | ||
}); | ||
return _history.filter(({ amount }) => amount > 0); | ||
} | ||
/** | ||
* Calculates sales needed to obtain a net withdrawal amount. | ||
* It may return more than one operation, if multiple symbols must be sold. | ||
* Symbols are sold in the order of their purchase operations, rather than date. | ||
* If you want to prioritize the sale of certain securities, | ||
* make sure their purchase operations appear first in the history array. | ||
* | ||
* Returns an array of operations containing the new sale(s). | ||
* @param history Operation history | ||
* @param options Options | ||
*/ | ||
function calculateSalesForNetWithdrawal(history, options) { | ||
const { netWithdrawal, capitalGainsTax, date, prices } = options; | ||
const operations = {}; | ||
const consolidatedHistory = consolidateHistory(history); | ||
let currentWithdrawal = 0; | ||
let i = 0; | ||
while (currentWithdrawal < netWithdrawal && i < consolidatedHistory.length) { | ||
const op = consolidatedHistory[i]; | ||
if (op.type === 'BUY' && op.date < date) { | ||
const price = prices[op.symbol]; | ||
const capitalGainsPerShare = price - op.price < 0 | ||
? price - op.price | ||
: (price - op.price) * (1 - capitalGainsTax); | ||
const netGainsPerShare = capitalGainsPerShare + op.price; | ||
const amount = Math.min((netWithdrawal - currentWithdrawal) / netGainsPerShare, op.amount); | ||
currentWithdrawal += amount * netGainsPerShare; | ||
const key = getHashKey({ symbol: op.symbol, date, type: 'SELL' }); | ||
operations[key] = { | ||
type: 'SELL', | ||
amount: amount + (operations[key] ? operations[key].amount : 0), | ||
price, | ||
date, | ||
symbol: op.symbol, | ||
}; | ||
} | ||
i++; | ||
} | ||
return Object.keys(operations) | ||
.sort() | ||
.map((key) => operations[key]); | ||
} | ||
function getHashKey(operation) { | ||
return `${operation.date.toUTCString()}#${operation.type}#${operation.symbol}`; | ||
} | ||
export { calculateFIFOCapitalGains, aggregateByYear, calculateSalesForNetWithdrawal }; | ||
//# sourceMappingURL=index.esm.js.map |
@@ -1,2 +0,2 @@ | ||
function calculateFIFOCapitalGains(a){const t=a.map(a=>({...a}));return t.filter(({type:a})=>"SELL"===a).reduce((a,e)=>[...a,calculateCapitalGainsForSale(t,e)],[])}function aggregateByYear(a){return a.reduce((a,{capitalGains:t,sale:{date:e}})=>({...a,[e.getFullYear()]:(a[e.getFullYear()]||0)+t}),{})}function calculateCapitalGainsForSale(a,t){let e=0;const l={...t};if(a.filter(({type:a,symbol:e,date:l})=>"BUY"===a&&e===t.symbol&&l<t.date).forEach(a=>{const l=Math.min(t.amount,a.amount);a.amount-=l,t.amount-=l,e+=l*(t.price-a.price)}),t.amount>0)throw Error(`Amount of sales for symbol ${t.symbol} exceeds the amount of buys.`);return{capitalGains:e,sale:l}}export{calculateFIFOCapitalGains,aggregateByYear}; | ||
function aggregateByYear(t){return t.reduce((t,{capitalGains:a,sale:{date:e}})=>({...t,[e.getFullYear()]:(t[e.getFullYear()]||0)+a}),{})}function calculateFIFOCapitalGains(t){const a=t.map(t=>({...t}));return a.filter(({type:t})=>"SELL"===t).reduce((t,e)=>[...t,calculateCapitalGainsForSale(a,e)],[])}function calculateCapitalGainsForSale(t,a){let e=0;const o={...a};if(t.filter(({type:t,symbol:e,date:o})=>"BUY"===t&&e===a.symbol&&o<a.date).forEach(t=>{const o=Math.min(a.amount,t.amount);t.amount-=o,a.amount-=o,e+=o*(a.price-t.price)}),a.amount>0)throw Error(`Amount of sales for symbol ${a.symbol} exceeds the amount of buys.`);return{capitalGains:e,sale:o}}function consolidateHistory(t){const a=[...t];return a.forEach(({type:t,symbol:e},o)=>{if("BUY"!==t)for(let t=0;t<o;t++){const n=a[t],l=a[o].amount;"BUY"===n.type&&n.symbol===e&&(a[t]={...n,amount:Math.max(0,n.amount-l)},a[o]={...a[o],amount:l-Math.min(n.amount,l)})}}),a.filter(({amount:t})=>t>0)}function calculateSalesForNetWithdrawal(t,a){const{netWithdrawal:e,capitalGainsTax:o,date:n,prices:l}=a,r={},c=consolidateHistory(t);let i=0,s=0;for(;i<e&&s<c.length;){const t=c[s];if("BUY"===t.type&&t.date<n){const a=l[t.symbol],c=(a-t.price<0?a-t.price:(a-t.price)*(1-o))+t.price,s=Math.min((e-i)/c,t.amount);i+=s*c;const u=getHashKey({symbol:t.symbol,date:n,type:"SELL"});r[u]={type:"SELL",amount:s+(r[u]?r[u].amount:0),price:a,date:n,symbol:t.symbol}}s++}return Object.keys(r).sort().map(t=>r[t])}function getHashKey(t){return`${t.date.toUTCString()}#${t.type}#${t.symbol}`}export{calculateFIFOCapitalGains,aggregateByYear,calculateSalesForNetWithdrawal}; | ||
//# sourceMappingURL=index.esm.min.js.map |
@@ -8,2 +8,17 @@ (function (global, factory) { | ||
/** | ||
* Aggregates the capital gains generated by a sequence of sales by year | ||
* | ||
* @param saleCapitalGains Array of capital gains generated by sell operations | ||
* | ||
* @returns A map of year to capital gains generated in the given year | ||
*/ | ||
function aggregateByYear(saleCapitalGains) { | ||
return saleCapitalGains.reduce(function (capitalGainsPerYear, _a) { | ||
var _b; | ||
var capitalGains = _a.capitalGains, date = _a.sale.date; | ||
return (tslib.__assign(tslib.__assign({}, capitalGainsPerYear), (_b = {}, _b[date.getFullYear()] = (capitalGainsPerYear[date.getFullYear()] || 0) + capitalGains, _b))); | ||
}, {}); | ||
} | ||
/** | ||
* Calculates the FIFO capital gains for the given operation history. | ||
@@ -33,16 +48,2 @@ * It separates capital gains of securities using the symbols given | ||
} | ||
/** | ||
* Aggregates the capital gains generated by a sequence of sales by year | ||
* | ||
* @param saleCapitalGains Array of capital gains generated by sell operations | ||
* | ||
* @returns A map of year to capital gains generated in the given year | ||
*/ | ||
function aggregateByYear(saleCapitalGains) { | ||
return saleCapitalGains.reduce(function (capitalGainsPerYear, _a) { | ||
var _b; | ||
var capitalGains = _a.capitalGains, date = _a.sale.date; | ||
return (tslib.__assign(tslib.__assign({}, capitalGainsPerYear), (_b = {}, _b[date.getFullYear()] = (capitalGainsPerYear[date.getFullYear()] || 0) + capitalGains, _b))); | ||
}, {}); | ||
} | ||
function calculateCapitalGainsForSale(operationHistory, sale) { | ||
@@ -71,4 +72,79 @@ var capitalGains = 0; | ||
/** | ||
* Consolidates the history by deleting purchase and sale that match (i.e., same symbol), | ||
* so that the history contains purchases without a matching sale and sales without | ||
* a matching purchase. | ||
* @param history History of operation | ||
*/ | ||
function consolidateHistory(history) { | ||
var _history = tslib.__spreadArrays(history); | ||
_history.forEach(function (_a, index) { | ||
var type = _a.type, symbol = _a.symbol; | ||
if (type === 'BUY') { | ||
return; | ||
} | ||
for (var i = 0; i < index; i++) { | ||
var operation = _history[i]; | ||
var amount = _history[index].amount; | ||
if (operation.type === 'BUY' && operation.symbol === symbol) { | ||
_history[i] = tslib.__assign(tslib.__assign({}, operation), { amount: Math.max(0, operation.amount - amount) }); | ||
_history[index] = tslib.__assign(tslib.__assign({}, _history[index]), { amount: amount - Math.min(operation.amount, amount) }); | ||
} | ||
} | ||
}); | ||
return _history.filter(function (_a) { | ||
var amount = _a.amount; | ||
return amount > 0; | ||
}); | ||
} | ||
/** | ||
* Calculates sales needed to obtain a net withdrawal amount. | ||
* It may return more than one operation, if multiple symbols must be sold. | ||
* Symbols are sold in the order of their purchase operations, rather than date. | ||
* If you want to prioritize the sale of certain securities, | ||
* make sure their purchase operations appear first in the history array. | ||
* | ||
* Returns an array of operations containing the new sale(s). | ||
* @param history Operation history | ||
* @param options Options | ||
*/ | ||
function calculateSalesForNetWithdrawal(history, options) { | ||
var netWithdrawal = options.netWithdrawal, capitalGainsTax = options.capitalGainsTax, date = options.date, prices = options.prices; | ||
var operations = {}; | ||
var consolidatedHistory = consolidateHistory(history); | ||
var currentWithdrawal = 0; | ||
var i = 0; | ||
while (currentWithdrawal < netWithdrawal && i < consolidatedHistory.length) { | ||
var op = consolidatedHistory[i]; | ||
if (op.type === 'BUY' && op.date < date) { | ||
var price = prices[op.symbol]; | ||
var capitalGainsPerShare = price - op.price < 0 | ||
? price - op.price | ||
: (price - op.price) * (1 - capitalGainsTax); | ||
var netGainsPerShare = capitalGainsPerShare + op.price; | ||
var amount = Math.min((netWithdrawal - currentWithdrawal) / netGainsPerShare, op.amount); | ||
currentWithdrawal += amount * netGainsPerShare; | ||
var key = getHashKey({ symbol: op.symbol, date: date, type: 'SELL' }); | ||
operations[key] = { | ||
type: 'SELL', | ||
amount: amount + (operations[key] ? operations[key].amount : 0), | ||
price: price, | ||
date: date, | ||
symbol: op.symbol, | ||
}; | ||
} | ||
i++; | ||
} | ||
return Object.keys(operations) | ||
.sort() | ||
.map(function (key) { return operations[key]; }); | ||
} | ||
function getHashKey(operation) { | ||
return operation.date.toUTCString() + "#" + operation.type + "#" + operation.symbol; | ||
} | ||
exports.calculateFIFOCapitalGains = calculateFIFOCapitalGains; | ||
exports.aggregateByYear = aggregateByYear; | ||
exports.calculateSalesForNetWithdrawal = calculateSalesForNetWithdrawal; | ||
@@ -75,0 +151,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("tslib")):"function"==typeof define&&define.amd?define(["exports","tslib"],t):t((e=e||self).FifoCapitalGainsJs={},e.tslib)}(this,function(e,i){"use strict";e.calculateFIFOCapitalGains=function(e){var n=e.map(function(e){return i.__assign({},e)});return n.filter(function(e){return"SELL"===e.type}).reduce(function(e,t){return i.__spreadArrays(e,[function(e,a){var n=0,t=i.__assign({},a);if(e.filter(function(e){var t=e.type,n=e.symbol,r=e.date;return"BUY"===t&&n===a.symbol&&r<a.date}).forEach(function(e){var t=Math.min(a.amount,e.amount);e.amount-=t,a.amount-=t,n+=t*(a.price-e.price)}),0<a.amount)throw Error("Amount of sales for symbol "+a.symbol+" exceeds the amount of buys.");return{capitalGains:n,sale:t}}(n,t)])},[])},e.aggregateByYear=function(e){return e.reduce(function(e,t){var n,r=t.capitalGains,a=t.sale.date;return i.__assign(i.__assign({},e),((n={})[a.getFullYear()]=(e[a.getFullYear()]||0)+r,n))},{})},Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("tslib")):"function"==typeof define&&define.amd?define(["exports","tslib"],e):e((t=t||self).FifoCapitalGainsJs={},t.tslib)}(this,function(t,g){"use strict";t.calculateFIFOCapitalGains=function(t){var a=t.map(function(t){return g.__assign({},t)});return a.filter(function(t){return"SELL"===t.type}).reduce(function(t,e){return g.__spreadArrays(t,[function(t,r){var a=0,e=g.__assign({},r);if(t.filter(function(t){var e=t.type,a=t.symbol,n=t.date;return"BUY"===e&&a===r.symbol&&n<r.date}).forEach(function(t){var e=Math.min(r.amount,t.amount);t.amount-=e,r.amount-=e,a+=e*(r.price-t.price)}),0<r.amount)throw Error("Amount of sales for symbol "+r.symbol+" exceeds the amount of buys.");return{capitalGains:a,sale:e}}(a,e)])},[])},t.aggregateByYear=function(t){return t.reduce(function(t,e){var a,n=e.capitalGains,r=e.sale.date;return g.__assign(g.__assign({},t),((a={})[r.getFullYear()]=(t[r.getFullYear()]||0)+n,a))},{})},t.calculateSalesForNetWithdrawal=function(t,e){for(var a,s,n,r=e.netWithdrawal,o=e.capitalGainsTax,i=e.date,u=e.prices,l={},c=(a=t,(s=g.__spreadArrays(a)).forEach(function(t,e){var a=t.type,n=t.symbol;if("BUY"!==a)for(var r=0;r<e;r++){var o=s[r],i=s[e].amount;"BUY"===o.type&&o.symbol===n&&(s[r]=g.__assign(g.__assign({},o),{amount:Math.max(0,o.amount-i)}),s[e]=g.__assign(g.__assign({},s[e]),{amount:i-Math.min(o.amount,i)}))}}),s.filter(function(t){return 0<t.amount})),f=0,m=0;f<r&&m<c.length;){var p,y,d,_,b=c[m];"BUY"===b.type&&b.date<i&&(y=((p=u[b.symbol])-b.price<0?p-b.price:(p-b.price)*(1-o))+b.price,f+=(d=Math.min((r-f)/y,b.amount))*y,_=(n={symbol:b.symbol,date:i,type:"SELL"}).date.toUTCString()+"#"+n.type+"#"+n.symbol,l[_]={type:"SELL",amount:d+(l[_]?l[_].amount:0),price:p,date:i,symbol:b.symbol}),m++}return Object.keys(l).sort().map(function(t){return l[t]})},Object.defineProperty(t,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=index.umd.min.js.map |
@@ -1,5 +0,11 @@ | ||
# Change Log | ||
# Changelog | ||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. | ||
### [0.1.1](https://github.com/bernardobelchior/fifo-capital-gains-js/compare/v0.1.0...v0.1.1) (2020-05-26) | ||
### Features | ||
- add calculateSalesForNetWithdrawal ([4f84706](https://github.com/bernardobelchior/fifo-capital-gains-js/commit/4f847065bc8675a6cc68b9a648d26f9d813fb400)) | ||
<a name="0.1.0"></a> | ||
@@ -6,0 +12,0 @@ |
@@ -1,56 +0,5 @@ | ||
/** | ||
* Calculates the FIFO capital gains for the given operation history. | ||
* It separates capital gains of securities using the symbols given | ||
* in each operation. | ||
* | ||
* @param operationHistory History of operations (buy and sales) to | ||
* calculate the capital gains for. | ||
* | ||
* @throws 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. | ||
* | ||
* @returns The FIFO capital gains for each sell operation | ||
*/ | ||
export function calculateFIFOCapitalGains(operationHistory) { | ||
const history = operationHistory.map((obj) => ({ ...obj })); | ||
const sales = history.filter(({ type }) => type === 'SELL'); | ||
return sales.reduce((capitalGains, sale) => [ | ||
...capitalGains, | ||
calculateCapitalGainsForSale(history, sale), | ||
], []); | ||
} | ||
/** | ||
* Aggregates the capital gains generated by a sequence of sales by year | ||
* | ||
* @param saleCapitalGains Array of capital gains generated by sell operations | ||
* | ||
* @returns A map of year to capital gains generated in the given year | ||
*/ | ||
export function aggregateByYear(saleCapitalGains) { | ||
return saleCapitalGains.reduce((capitalGainsPerYear, { capitalGains, sale: { date } }) => ({ | ||
...capitalGainsPerYear, | ||
[date.getFullYear()]: (capitalGainsPerYear[date.getFullYear()] || 0) + capitalGains, | ||
}), {}); | ||
} | ||
function calculateCapitalGainsForSale(operationHistory, sale) { | ||
let capitalGains = 0; | ||
const saleCopy = { ...sale }; | ||
operationHistory | ||
.filter(({ type, symbol, date }) => type === 'BUY' && symbol === sale.symbol && date < sale.date) | ||
.forEach((buy) => { | ||
const amountSold = Math.min(sale.amount, buy.amount); | ||
buy.amount -= amountSold; | ||
sale.amount -= amountSold; | ||
capitalGains += amountSold * (sale.price - buy.price); | ||
}); | ||
if (sale.amount > 0) { | ||
throw Error(`Amount of sales for symbol ${sale.symbol} exceeds the amount of buys.`); | ||
} | ||
return { | ||
capitalGains, | ||
sale: saleCopy, | ||
}; | ||
} | ||
import { aggregateByYear } from './aggregations'; | ||
import { calculateFIFOCapitalGains } from './capital-gains'; | ||
import { calculateSalesForNetWithdrawal } from './net-sales'; | ||
export { calculateFIFOCapitalGains, aggregateByYear, calculateSalesForNetWithdrawal, }; | ||
//# sourceMappingURL=index.js.map |
@@ -1,63 +0,5 @@ | ||
import { __assign, __spreadArrays } from "tslib"; | ||
/** | ||
* Calculates the FIFO capital gains for the given operation history. | ||
* It separates capital gains of securities using the symbols given | ||
* in each operation. | ||
* | ||
* @param operationHistory History of operations (buy and sales) to | ||
* calculate the capital gains for. | ||
* | ||
* @throws 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. | ||
* | ||
* @returns The FIFO capital gains for each sell operation | ||
*/ | ||
export function calculateFIFOCapitalGains(operationHistory) { | ||
var history = operationHistory.map(function (obj) { return (__assign({}, obj)); }); | ||
var sales = history.filter(function (_a) { | ||
var type = _a.type; | ||
return type === 'SELL'; | ||
}); | ||
return sales.reduce(function (capitalGains, sale) { return __spreadArrays(capitalGains, [ | ||
calculateCapitalGainsForSale(history, sale), | ||
]); }, []); | ||
} | ||
/** | ||
* Aggregates the capital gains generated by a sequence of sales by year | ||
* | ||
* @param saleCapitalGains Array of capital gains generated by sell operations | ||
* | ||
* @returns A map of year to capital gains generated in the given year | ||
*/ | ||
export function aggregateByYear(saleCapitalGains) { | ||
return saleCapitalGains.reduce(function (capitalGainsPerYear, _a) { | ||
var _b; | ||
var capitalGains = _a.capitalGains, date = _a.sale.date; | ||
return (__assign(__assign({}, capitalGainsPerYear), (_b = {}, _b[date.getFullYear()] = (capitalGainsPerYear[date.getFullYear()] || 0) + capitalGains, _b))); | ||
}, {}); | ||
} | ||
function calculateCapitalGainsForSale(operationHistory, sale) { | ||
var capitalGains = 0; | ||
var saleCopy = __assign({}, sale); | ||
operationHistory | ||
.filter(function (_a) { | ||
var type = _a.type, symbol = _a.symbol, date = _a.date; | ||
return type === 'BUY' && symbol === sale.symbol && date < sale.date; | ||
}) | ||
.forEach(function (buy) { | ||
var amountSold = Math.min(sale.amount, buy.amount); | ||
buy.amount -= amountSold; | ||
sale.amount -= amountSold; | ||
capitalGains += amountSold * (sale.price - buy.price); | ||
}); | ||
if (sale.amount > 0) { | ||
throw Error("Amount of sales for symbol " + sale.symbol + " exceeds the amount of buys."); | ||
} | ||
return { | ||
capitalGains: capitalGains, | ||
sale: saleCopy, | ||
}; | ||
} | ||
import { aggregateByYear } from './aggregations'; | ||
import { calculateFIFOCapitalGains } from './capital-gains'; | ||
import { calculateSalesForNetWithdrawal } from './net-sales'; | ||
export { calculateFIFOCapitalGains, aggregateByYear, calculateSalesForNetWithdrawal, }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "fifo-capital-gains-js", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Calculate your FIFO capital gains for tax-purposes", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -318,4 +318,59 @@ # FIFO Capital Gains | ||
### `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. | ||
```ts | ||
const history: Operation[] = [ | ||
{ | ||
amount: 10, | ||
date: new Date('2020-01-01'), | ||
price: 100, | ||
symbol: 'STK1', | ||
type: 'BUY', | ||
}, | ||
{ | ||
amount: 10, | ||
date: new Date('2021-01-01'), | ||
price: 200, | ||
symbol: 'STK2', | ||
type: 'BUY', | ||
}, | ||
{ | ||
amount: 10, | ||
date: new Date('2021-01-01'), | ||
price: 200, | ||
symbol: 'STK1', | ||
type: 'BUY', | ||
}, | ||
] | ||
const options: NetSalesOptions = { | ||
netWithdrawal: 4000, | ||
capitalGainsTax: 0.5, | ||
date: new Date('2022-01-01'), | ||
prices: { STK1: 300, STK2: 600 }, | ||
} | ||
const sales = calculateSalesForNetWithdrawal(history, options)) | ||
// [ | ||
// { | ||
// amount: 10, | ||
// date: new Date('2022-01-01'), | ||
// price: 300, | ||
// symbol: 'STK1', | ||
// type: 'SELL', | ||
// }, | ||
// { | ||
// amount: 5, | ||
// date: new Date('2022-01-01'), | ||
// price: 600, | ||
// symbol: 'STK2', | ||
// type: 'SELL', | ||
// }, | ||
// ]) | ||
``` | ||
## 🥂 License | ||
[MIT](./LICENSE.md) as always |
@@ -1,2 +0,2 @@ | ||
// Type definitions for fifo-capital-gains-js 0.1.0 | ||
// Type definitions for fifo-capital-gains-js 0.1.1 | ||
// Project: https://github.com/bernardobelchior/fifo-capital-gains-js | ||
@@ -7,68 +7,7 @@ // Definitions by: Bernardo Belchior <bernardo.belchior1@gmail.com> <https://github.com/bernardobelchior> | ||
export interface Operation { | ||
/** | ||
* Symbol that identifies the underlying security. It is used only for differentation between | ||
* operations of different securities. Can be the stock ticker or any other identifier that | ||
* is different from other securities'. | ||
*/ | ||
symbol: string; | ||
/** | ||
* Date when the operation took place | ||
*/ | ||
date: Date; | ||
/** | ||
* Price of the security when the operation took place. | ||
* If it is a buy operation, this is the buying price; if a sell operation, | ||
* then this is the selling price. | ||
*/ | ||
price: number; | ||
/** | ||
* Number of units transacted. | ||
*/ | ||
amount: number; | ||
/** | ||
* Type of the operation | ||
*/ | ||
type: 'BUY' | 'SELL'; | ||
} | ||
export interface CapitalGains { | ||
/** | ||
* Sale that triggered the capital gains | ||
*/ | ||
sale: Operation; | ||
/** | ||
* Capital gains triggered from the sale | ||
*/ | ||
capitalGains: number; | ||
} | ||
export declare type Year = number; | ||
/** | ||
* Aggregation of the capital gains by year. | ||
* Maps from year to the capital gains realized in that year. | ||
*/ | ||
export declare type YearlyCapitalGains = Record<Year, number>; | ||
/** | ||
* Calculates the FIFO capital gains for the given operation history. | ||
* It separates capital gains of securities using the symbols given | ||
* in each operation. | ||
* | ||
* @param operationHistory History of operations (buy and sales) to | ||
* calculate the capital gains for. | ||
* | ||
* @throws 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. | ||
* | ||
* @returns The FIFO capital gains for each sell operation | ||
*/ | ||
export declare function calculateFIFOCapitalGains(operationHistory: Operation[]): CapitalGains[]; | ||
/** | ||
* Aggregates the capital gains generated by a sequence of sales by year | ||
* | ||
* @param saleCapitalGains Array of capital gains generated by sell operations | ||
* | ||
* @returns A map of year to capital gains generated in the given year | ||
*/ | ||
export declare function aggregateByYear(saleCapitalGains: CapitalGains[]): YearlyCapitalGains; | ||
import { aggregateByYear, Year, YearlyCapitalGains } from './aggregations'; | ||
import { calculateFIFOCapitalGains, CapitalGains } from './capital-gains'; | ||
import { calculateSalesForNetWithdrawal, NetSalesOptions } from './net-sales'; | ||
import { Operation } from './types'; | ||
export { calculateFIFOCapitalGains, aggregateByYear, calculateSalesForNetWithdrawal, CapitalGains, Year, YearlyCapitalGains, NetSalesOptions, Operation, }; | ||
//# sourceMappingURL=index.d.ts.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
93639
48
676
376
1