finance-calculator-pro 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +334 -0
- package/dist/index.d.mts +529 -0
- package/dist/index.d.ts +529 -0
- package/dist/index.js +493 -0
- package/dist/index.mjs +426 -0
- package/package.json +25 -0
package/README.md
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
# finance-calculator-pro
|
|
2
|
+
|
|
3
|
+
A highly modular, fully independent fundamental financial calculation engine. Whether you're building a stock screener or tracking your portfolio, this library calculates the metrics tracking a company's valuation, profitability, liquidity, efficiency, and risk flawlessly.
|
|
4
|
+
|
|
5
|
+
It accepts raw numeric dataβno dependency on heavy financial APIs. It pairs amazingly well with normalized data structures (like those from `yahoo-finance2`), or simply your own math.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install finance-calculator-pro
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Complete List of Available Metrics
|
|
16
|
+
|
|
17
|
+
The engine can calculate the following metrics natively from basic fundamental variables:
|
|
18
|
+
|
|
19
|
+
### π¦ Valuation
|
|
20
|
+
- **Price-to-Earnings (P/E)**: `price / eps`
|
|
21
|
+
- **Price-to-Book (P/B)**: `price / bookValuePerShare`
|
|
22
|
+
- **Price-to-Sales (P/S)**: `price / revenuePerShare`
|
|
23
|
+
- **Price/Earnings-to-Growth (PEG)**: `pe / expectedEarningsGrowthRate`
|
|
24
|
+
- **Enterprise Multiple (EV/EBITDA)**: `enterpriseValue / ebitda`
|
|
25
|
+
- **Dividend Yield**: `annualDividendPerShare / price`
|
|
26
|
+
- **Discounted Cash Flow (DCF)**: Calculates Enterprise Present Value from terminal rates and sequential FCFs.
|
|
27
|
+
- **Graham Number**: `sqrt(22.5 * eps * bookValuePerShare)`
|
|
28
|
+
|
|
29
|
+
### π Profitability
|
|
30
|
+
- **Return on Assets (ROA)**: `netIncome / totalAssets`
|
|
31
|
+
- **Return on Equity (ROE)**: `netIncome / totalEquity`
|
|
32
|
+
- **Return on Invested Capital (ROIC)**: `nopat / investedCapital`
|
|
33
|
+
- **Gross Margin**: `grossProfit / revenue`
|
|
34
|
+
- **Operating Margin**: `operatingIncome / revenue`
|
|
35
|
+
- **Net Profit Margin**: `netIncome / revenue`
|
|
36
|
+
- **Free Cash Flow Margin**: `fcf / revenue`
|
|
37
|
+
|
|
38
|
+
### π§ Liquidity & Solvency
|
|
39
|
+
- **Current Ratio**: `totalAssets / totalLiabilities` (Using broader snapshot)
|
|
40
|
+
- **Quick Ratio**: `(totalAssets - inventory) / totalLiabilities`
|
|
41
|
+
- **Debt-to-Equity**: `totalDebt / totalEquity`
|
|
42
|
+
- **Interest Coverage**: `ebit / interestExpense`
|
|
43
|
+
|
|
44
|
+
### βοΈ Efficiency
|
|
45
|
+
- **Asset Turnover**: `revenue / totalAssets`
|
|
46
|
+
- **Inventory Turnover**: `costOfRevenue / inventory`
|
|
47
|
+
|
|
48
|
+
### β οΈ Risk & Insights
|
|
49
|
+
- **Altman Z-Score**: Fundamental bankruptcy risk predictor combining 5 variables.
|
|
50
|
+
- **Sharpe Ratio**: ROI vs Risk-free rate adjusted for standard deviation.
|
|
51
|
+
- **Target Upside**: `%` potential from current `price` to `analystTargetPrice`.
|
|
52
|
+
|
|
53
|
+
### π Growth (Timeseries)
|
|
54
|
+
- **YoY / QoQ Growth Rates**: Automatically tracks sequential growth of Revenue, Net Income, EPS, and Cash Flows.
|
|
55
|
+
- **CAGR**: Compound Annual Growth Rate over multi-period arrays.
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## π Data Inputs: What the API Accepts
|
|
60
|
+
|
|
61
|
+
To use the massive aggregator functions (like `analyzeCompany` or `analyzeValuation`), you just pass an object of shape `CompanySnapshotInput`.
|
|
62
|
+
|
|
63
|
+
**EVERY FIELD IS OPTIONAL**. If you omit a field, the engine simply skips the metric that calculates it and safely returns `null` for that metric!
|
|
64
|
+
|
|
65
|
+
### `CompanySnapshotInput` (Used for Snapshot Analyzers)
|
|
66
|
+
```typescript
|
|
67
|
+
interface CompanySnapshotInput {
|
|
68
|
+
price?: number;
|
|
69
|
+
marketCap?: number;
|
|
70
|
+
totalRevenue?: number;
|
|
71
|
+
grossProfit?: number;
|
|
72
|
+
operatingIncome?: number;
|
|
73
|
+
netIncome?: number;
|
|
74
|
+
freeCashFlow?: number;
|
|
75
|
+
eps?: number;
|
|
76
|
+
bookValuePerShare?: number;
|
|
77
|
+
revenuePerShare?: number;
|
|
78
|
+
totalAssets?: number;
|
|
79
|
+
totalLiabilities?: number;
|
|
80
|
+
totalEquity?: number;
|
|
81
|
+
totalDebt?: number;
|
|
82
|
+
cashAndEquivalents?: number;
|
|
83
|
+
inventory?: number;
|
|
84
|
+
interestExpense?: number;
|
|
85
|
+
costOfRevenue?: number;
|
|
86
|
+
annualDividendPerShare?: number;
|
|
87
|
+
expectedEarningsGrowthRate?: number;
|
|
88
|
+
ebitda?: number;
|
|
89
|
+
workingCapital?: number;
|
|
90
|
+
retainedEarnings?: number;
|
|
91
|
+
ebit?: number;
|
|
92
|
+
taxRate?: number;
|
|
93
|
+
returns?: number; // for sharpe
|
|
94
|
+
riskFree?: number; // for sharpe
|
|
95
|
+
stdDev?: number; // for sharpe
|
|
96
|
+
analystTargetPrice?: number;
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### `FundamentalTimeseriesInput` (Used for Trend Analyzers)
|
|
101
|
+
These metrics look at arrays chronologically from **oldest** to **newest**.
|
|
102
|
+
```typescript
|
|
103
|
+
interface FundamentalTimeseriesInput {
|
|
104
|
+
revenue: number[];
|
|
105
|
+
netIncome: number[];
|
|
106
|
+
costOfRevenue?: number[];
|
|
107
|
+
operatingIncome?: number[];
|
|
108
|
+
freeCashFlow?: number[];
|
|
109
|
+
eps?: number[];
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## π Complete Usage Guide
|
|
116
|
+
|
|
117
|
+
### 1. The Super Analyzer: `analyzeCompany`
|
|
118
|
+
Pass your raw snapshot of a company, and let the engine derive everything at once. Setting `withInsights = true` will automatically translate numbers into human-readable recommendations ("Good", "Bad", "Neutral").
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import { analyzeCompany } from 'finance-calculator-pro';
|
|
122
|
+
|
|
123
|
+
// You ONLY need to provide the fields you care about!
|
|
124
|
+
const rawData = {
|
|
125
|
+
price: 150,
|
|
126
|
+
eps: 5,
|
|
127
|
+
bookValuePerShare: 20,
|
|
128
|
+
marketCap: 150000,
|
|
129
|
+
totalDebt: 20000,
|
|
130
|
+
cashAndEquivalents: 5000,
|
|
131
|
+
netIncome: 5000,
|
|
132
|
+
totalAssets: 100000,
|
|
133
|
+
totalLiabilities: 60000,
|
|
134
|
+
totalEquity: 40000,
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// With Insights (Best for Beginners & UI)
|
|
138
|
+
const insightAnalysis = analyzeCompany(rawData, true);
|
|
139
|
+
|
|
140
|
+
console.log(JSON.stringify(insightAnalysis.valuation, null, 2));
|
|
141
|
+
/*
|
|
142
|
+
{
|
|
143
|
+
"pe": {
|
|
144
|
+
"value": 30,
|
|
145
|
+
"status": "Bad",
|
|
146
|
+
"insight": "Expensive. High growth is priced in."
|
|
147
|
+
},
|
|
148
|
+
"pb": {
|
|
149
|
+
"value": 7.5,
|
|
150
|
+
"status": "Bad",
|
|
151
|
+
"insight": "Trading at a high premium to book value."
|
|
152
|
+
},
|
|
153
|
+
"ps": {
|
|
154
|
+
"value": null,
|
|
155
|
+
"status": "N/A",
|
|
156
|
+
"insight": "Sales data unavailable."
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
*/
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### 2. Categorical Evaluation
|
|
163
|
+
If you only want to process a specific category instead of all metrics at once, you can explicitly call categorical analyzers! They accept the exact same `CompanySnapshotInput`.
|
|
164
|
+
|
|
165
|
+
Available Categorical Analyzers:
|
|
166
|
+
- `analyzeValuation(data, withInsights?)`
|
|
167
|
+
- `analyzeProfitability(data, withInsights?)`
|
|
168
|
+
- `analyzeLiquidity(data, withInsights?)`
|
|
169
|
+
- `analyzeEfficiency(data, withInsights?)`
|
|
170
|
+
- `analyzeRisk(data, withInsights?)`
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
import { analyzeProfitability } from 'finance-calculator-pro';
|
|
174
|
+
|
|
175
|
+
const profitabilityMetrics = analyzeProfitability(rawData, true);
|
|
176
|
+
console.log(profitabilityMetrics.roe.insight); // "Strong return on shareholder equity."
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### 3. Individual Metric Insights
|
|
180
|
+
If you only need to calculate and evaluate a **single metric** (like P/E), you can combine the pure math modules directly with the `evaluate` engine to get insights without building a full company snapshot!
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
import { pe, evaluate } from 'finance-calculator-pro';
|
|
184
|
+
|
|
185
|
+
// 1. Calculate the raw numerical value
|
|
186
|
+
const ratio = pe(150 /* price */, 5 /* eps */); // -> 30
|
|
187
|
+
|
|
188
|
+
// 2. Pass it natively to the Evaluator for automated context
|
|
189
|
+
const insight = evaluate.pe(ratio);
|
|
190
|
+
|
|
191
|
+
console.log(JSON.stringify(insight, null, 2));
|
|
192
|
+
/*
|
|
193
|
+
{
|
|
194
|
+
"value": 30,
|
|
195
|
+
"status": "Bad",
|
|
196
|
+
"insight": "Expensive. High growth is priced in."
|
|
197
|
+
}
|
|
198
|
+
*/
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### 4. Batch Analysis (Screening)
|
|
202
|
+
Evaluate hundreds of companies seamlessly in a single line.
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
import { analyzeBatch } from 'finance-calculator-pro';
|
|
206
|
+
|
|
207
|
+
const multipleCompanies = [ company1, company2, company3 ];
|
|
208
|
+
const batchResults = analyzeBatch(multipleCompanies, true);
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### 5. Timeseries / Trend Analysis
|
|
212
|
+
Pass arrays of data (oldest to newest) to get automated sequential growth rates and historical margins over time.
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
import { analyzeFundamentalTrends } from 'finance-calculator-pro';
|
|
216
|
+
|
|
217
|
+
const timeseriesData = {
|
|
218
|
+
revenue: [40000, 45000, 50000],
|
|
219
|
+
netIncome: [4000, 5000, 6000]
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
// Pass "annual" or "quarterly" depending on data density
|
|
223
|
+
const trends = analyzeFundamentalTrends(timeseriesData, "annual");
|
|
224
|
+
|
|
225
|
+
console.log(JSON.stringify(trends, null, 2));
|
|
226
|
+
/*
|
|
227
|
+
{
|
|
228
|
+
"periodType": "annual",
|
|
229
|
+
"growth": {
|
|
230
|
+
"revenueGrowth": [0.125, 0.111111],
|
|
231
|
+
"netIncomeGrowth": [0.25, 0.20],
|
|
232
|
+
"revenueCagr": 0.118,
|
|
233
|
+
"netIncomeCagr": 0.224
|
|
234
|
+
},
|
|
235
|
+
"margins": {
|
|
236
|
+
"netMargins": [0.1, 0.111, 0.12]
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
*/
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### 6. Individual Mathematical Metrics (API Reference)
|
|
243
|
+
Because this is fundamentally a generic node package, if you only need a single lightweight calculation without any object-mapping overhead, you can import and execute the mathematical functions directly.
|
|
244
|
+
|
|
245
|
+
Every function returns a `number | null` (returning `null` implicitly if any division goes to zero or infinity, guaranteeing runtime safety).
|
|
246
|
+
|
|
247
|
+
#### Valuation
|
|
248
|
+
```typescript
|
|
249
|
+
import { pe, pb, ps, peg, evEbitda, calculateEnterpriseValue, dividendYield, calculateDCF } from 'finance-calculator-pro';
|
|
250
|
+
|
|
251
|
+
pe(150 /* price */, 5 /* eps */); // -> 30
|
|
252
|
+
pb(150 /* price */, 20 /* bookValuePerShare */); // -> 7.5
|
|
253
|
+
ps(150 /* price */, 50 /* revenuePerShare */); // -> 3
|
|
254
|
+
peg(30 /* peRatio */, 0.15 /* earningsGrowthRate */); // -> 200
|
|
255
|
+
calculateEnterpriseValue(150000 /* marketCap */, 20000 /* debt */, 5000 /* cash */); // -> 165000
|
|
256
|
+
evEbitda(165000 /* enterpriseValue */, 10000 /* ebitda */); // -> 16.5
|
|
257
|
+
dividendYield(1.5 /* annualDividendPerShare */, 150 /* price */); // -> 0.01 (1%)
|
|
258
|
+
|
|
259
|
+
// DCF Example
|
|
260
|
+
calculateDCF(
|
|
261
|
+
[3000, 3500, 4000, 4500, 5000] /* projectedFCF */,
|
|
262
|
+
0.10 /* discountRateWACC */,
|
|
263
|
+
0.025 /* terminalGrowthRate */
|
|
264
|
+
);
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
#### Profitability
|
|
268
|
+
```typescript
|
|
269
|
+
import { roa, roe, roic, grossMargin, operatingMargin, netProfitMargin, fcfMargin } from 'finance-calculator-pro';
|
|
270
|
+
|
|
271
|
+
roa(5000 /* netIncome */, 100000 /* totalAssets */); // -> 0.05
|
|
272
|
+
roe(5000 /* netIncome */, 40000 /* totalEquity */); // -> 0.125
|
|
273
|
+
roic(7000 /* operatingIncome */, 0.2 /* taxRate */, 20000 /* debt */, 40000 /* equity */, 5000 /* cash */); // -> 0.1018
|
|
274
|
+
|
|
275
|
+
// Margins
|
|
276
|
+
grossMargin(50000 /* revenue */, 20000 /* costOfRevenue */); // -> 0.60
|
|
277
|
+
operatingMargin(7000 /* operatingIncome */, 50000 /* revenue */); // -> 0.14
|
|
278
|
+
netProfitMargin(5000 /* netIncome */, 50000 /* revenue */); // -> 0.10
|
|
279
|
+
fcfMargin(3000 /* freeCashFlow */, 50000 /* revenue */); // -> 0.06
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
#### Liquidity
|
|
283
|
+
```typescript
|
|
284
|
+
import { currentRatio, quickRatio, debtToEquity, interestCoverage } from 'finance-calculator-pro';
|
|
285
|
+
|
|
286
|
+
currentRatio(100000 /* currentAssets */, 60000 /* currentLiabilities */); // -> 1.66
|
|
287
|
+
quickRatio(100000 /* currentAssets */, 10000 /* inventory */, 60000 /* currentLiabilities */); // -> 1.5
|
|
288
|
+
debtToEquity(20000 /* totalDebt */, 40000 /* totalEquity */); // -> 0.5
|
|
289
|
+
interestCoverage(7500 /* ebit */, 500 /* interestExpense */); // -> 15
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
#### Efficiency
|
|
293
|
+
```typescript
|
|
294
|
+
import { assetTurnover, inventoryTurnover } from 'finance-calculator-pro';
|
|
295
|
+
|
|
296
|
+
assetTurnover(50000 /* revenue */, 100000 /* averageTotalAssets */); // -> 0.5
|
|
297
|
+
inventoryTurnover(20000 /* costOfRevenue */, 10000 /* averageInventory */); // -> 2.0
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
#### Risk & Insights
|
|
301
|
+
```typescript
|
|
302
|
+
import { altmanZScore, sharpe, targetUpside, grahamNumber } from 'finance-calculator-pro';
|
|
303
|
+
|
|
304
|
+
// Altman Z-Score
|
|
305
|
+
altmanZScore(
|
|
306
|
+
15000 /* workingCapital */,
|
|
307
|
+
20000 /* retainedEarnings */,
|
|
308
|
+
7500 /* ebit */,
|
|
309
|
+
150000 /* marketValueEquity */,
|
|
310
|
+
50000 /* sales */,
|
|
311
|
+
100000 /* totalAssets */,
|
|
312
|
+
60000 /* totalLiabilities */
|
|
313
|
+
); // -> 2.707
|
|
314
|
+
|
|
315
|
+
sharpe(0.12 /* return */, 0.04 /* riskFree */, 0.15 /* stdDev */); // -> 0.533
|
|
316
|
+
targetUpside(150 /* currentPrice */, 180 /* targetPrice */); // -> 0.20 (20% upside)
|
|
317
|
+
grahamNumber(5 /* eps */, 20 /* bookValuePerShare */); // -> 47.43
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
#### Timeseries / Growth Trends
|
|
321
|
+
The growth modules return chronological arrays representing growth from the previous period. For $N$ inputs, they return an array of length $N-1$.
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
import { calculateGrowthRate, yoyGrowth, qoqGrowth, cagr } from 'finance-calculator-pro';
|
|
325
|
+
|
|
326
|
+
// Base YoY historical array mapping
|
|
327
|
+
yoyGrowth([40000, 45000, 50000] /* chronologicalDataPoints */); // -> [0.125, 0.1111]
|
|
328
|
+
|
|
329
|
+
// Base QoQ trailing historical array mapping
|
|
330
|
+
qoqGrowth([10000, 10500, 12000, 11000] /* chronologicalDataPoints */); // -> [0.05, 0.1428, -0.0833]
|
|
331
|
+
|
|
332
|
+
// CAGR (Beginning Value, Ending Value, Periods)
|
|
333
|
+
cagr(40000 /* beginningValue */, 50000 /* endingValue */, 2 /* periods */); // -> 0.118 (11.8%)
|
|
334
|
+
```
|