Development of Cost Basis Calculation System (FIFO, LIFO, HIFO)
Cost basis method directly impacts tax obligation. FIFO in rising market gives higher tax (sell cheapest, accumulated early). HIFO minimizes current tax. Average cost simplest, required in Germany and Netherlands.
Cost Basis Calculation
class CostBasisEngine {
// FIFO: first bought = first sold
async calculateFIFO(userId: string, asset: string, amount: number, proceeds: number) {
const lots = await this.db.getLots(userId, asset, { orderBy: "acquired_at ASC" });
return this.consumeLots(lots, amount, proceeds);
}
// LIFO: last bought = first sold
async calculateLIFO(userId: string, asset: string, amount: number, proceeds: number) {
const lots = await this.db.getLots(userId, asset, { orderBy: "acquired_at DESC" });
return this.consumeLots(lots, amount, proceeds);
}
// HIFO: highest cost = first sold (minimize tax)
async calculateHIFO(userId: string, asset: string, amount: number, proceeds: number) {
const lots = await this.db.getLots(userId, asset, { orderBy: "cost_per_unit DESC" });
return this.consumeLots(lots, amount, proceeds);
}
}
Cost basis system supporting FIFO, LIFO, HIFO, Average Cost and UK Section 104 — 2-3 weeks development.







