finance_functions¶
shortfx.fxNumeric.finance_functions
¶
Financial Functions Module.
Contains functions for common financial calculations: future value (FV), present value (PV), periodic payment (PMT), number of periods (NPER), interest rate (RATE), internal rate of return (IRR), net present value (NPV), and depreciation (SLN, DB).
Key Features
- TVM (Time Value of Money) functions
- Iterative calculations for IRR and RATE
- Depreciation methods
- Compatible with ordinary and due annuities
Example
future_value(rate=0.05, nper=5, pmt=0, pv=-1000) 1276.2815625000003
Functions¶
accounts_payable_turnover(total_purchases: Union[int, float], average_payables: Union[int, float]) -> float
¶
Calculate accounts payable turnover ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_purchases
|
Union[int, float]
|
Total supplier purchases for the period. |
required |
average_payables
|
Union[int, float]
|
Average accounts payable. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Payable turnover ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If average_payables is zero. |
Example
accounts_payable_turnover(600_000, 100_000) 6.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
accrint(issue: str, first_interest: str, settlement: str, rate: float, par: float, frequency: int, basis: int = 0) -> float
¶
Accrued interest for a security that pays periodic interest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
issue
|
str
|
Issue date (YYYY-MM-DD). |
required |
first_interest
|
str
|
First interest date (YYYY-MM-DD). |
required |
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
rate
|
float
|
Annual coupon rate. |
required |
par
|
float
|
Par value. |
required |
frequency
|
int
|
Payments per year (1, 2, or 4). |
required |
basis
|
int
|
Day-count basis (0-4). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Accrued interest. |
Example
round(accrint("2008-03-01", "2008-08-31", "2008-05-01", 0.10, 1000, 2), 2) 16.67
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
accrintm(issue: str, settlement: str, rate: float, par: float, basis: int = 0) -> float
¶
Accrued interest for a security that pays interest at maturity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
issue
|
str
|
Issue date (YYYY-MM-DD). |
required |
settlement
|
str
|
Settlement (maturity) date (YYYY-MM-DD). |
required |
rate
|
float
|
Annual coupon rate. |
required |
par
|
float
|
Par value. |
required |
basis
|
int
|
Day-count basis (0-4). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Accrued interest. |
Example
round(accrintm("2008-04-01", "2008-06-15", 0.10, 1000, 3), 2) 20.55
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
acid_test_ratio(cash: Union[int, float], marketable_securities: Union[int, float], receivables: Union[int, float], current_liabilities: Union[int, float]) -> float
¶
Calculate the acid-test (quick) ratio.
acid_test = (cash + marketable_securities + receivables) / current_liabilities
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cash
|
Union[int, float]
|
Cash and equivalents. |
required |
marketable_securities
|
Union[int, float]
|
Short-term investments. |
required |
receivables
|
Union[int, float]
|
Accounts receivable. |
required |
current_liabilities
|
Union[int, float]
|
Current liabilities. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Acid-test ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If current_liabilities is zero. |
Example
acid_test_ratio(50_000, 30_000, 40_000, 80_000) 1.5
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
altman_z_score(working_capital: float, retained_earnings: float, ebit: float, market_cap: float, total_liabilities: float, total_assets: float, sales: float) -> float
¶
Calculates the Altman Z-Score for bankruptcy prediction.
Z = 1.2·X1 + 1.4·X2 + 3.3·X3 + 0.6·X4 + 1.0·X5
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
working_capital
|
float
|
Current assets minus current liabilities. |
required |
retained_earnings
|
float
|
Total retained earnings. |
required |
ebit
|
float
|
Earnings before interest and taxes. |
required |
market_cap
|
float
|
Market capitalisation of equity. |
required |
total_liabilities
|
float
|
Total liabilities. |
required |
total_assets
|
float
|
Total assets (must be positive). |
required |
sales
|
float
|
Total sales / revenue. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Altman Z-Score (> 2.99 safe, 1.81–2.99 grey zone, < 1.81 distress). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any input is not numeric. |
ValueError
|
If total_assets or total_liabilities is not positive. |
Example
round(altman_z_score(50, 30, 40, 200, 100, 300, 250), 2) 2.82
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 | |
amorlinc(cost: float, date_purchased: str, first_period: str, salvage: float, period: int, rate: float, basis: int = 0) -> float
¶
Linear depreciation for each accounting period (French system).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cost
|
float
|
Asset cost. |
required |
date_purchased
|
str
|
Purchase date (YYYY-MM-DD). |
required |
first_period
|
str
|
End of first period (YYYY-MM-DD). |
required |
salvage
|
float
|
Salvage value. |
required |
period
|
int
|
Period number (0-based). |
required |
rate
|
float
|
Depreciation rate. |
required |
basis
|
int
|
Day-count basis (0-4). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Depreciation for the period. |
Example
round(amorlinc(2400, "2008-01-01", "2008-12-31", 300, 1, 0.15), 2) 360.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
annuity_due_certain(payment: float, interest_rate: float, periods: int) -> float
¶
Present value of an annuity-due (payments at beginning of each period).
PV = payment × (1 − v^n) / d where d = i/(1+i).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payment
|
float
|
Payment per period (> 0). |
required |
interest_rate
|
float
|
Interest rate per period (> 0). |
required |
periods
|
int
|
Number of periods (positive integer). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Present value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument has the wrong type. |
ValueError
|
If numeric constraints are violated. |
Usage Example
round(annuity_due_certain(1000, 0.05, 10), 2) 8107.82
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
annuity_due_fv(payment: float, rate: float, periods: int) -> float
¶
Future value of an annuity due (payments at the beginning of each period).
FV = payment × [((1+r)^n − 1) / r] × (1+r).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payment
|
float
|
Periodic payment amount. |
required |
rate
|
float
|
Interest rate per period (e.g. 0.05 for 5%). |
required |
periods
|
int
|
Number of periods. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Future value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs have wrong types. |
ValueError
|
If rate < 0 or periods < 1. |
Example
round(annuity_due_fv(1000, 0.05, 10), 2) 13206.79
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
annuity_due_pv(payment: float, rate: float, periods: int) -> float
¶
Present value of an annuity due (payments at the beginning of each period).
PV = payment × [(1 − (1+r)^−n) / r] × (1+r).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payment
|
float
|
Periodic payment amount. |
required |
rate
|
float
|
Interest rate per period. |
required |
periods
|
int
|
Number of periods. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Present value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs have wrong types. |
ValueError
|
If rate < 0 or periods < 1. |
Example
round(annuity_due_pv(1000, 0.05, 10), 2) 8107.82
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
annuity_immediate_certain(payment: float, interest_rate: float, periods: int) -> float
¶
Present value of an annuity-immediate (payments at end of each period).
PV = payment × (1 − v^n) / i where v = 1/(1+i).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payment
|
float
|
Payment per period (> 0). |
required |
interest_rate
|
float
|
Interest rate per period (> 0). |
required |
periods
|
int
|
Number of periods (positive integer). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Present value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument has the wrong type. |
ValueError
|
If numeric constraints are violated. |
Usage Example
round(annuity_immediate_certain(1000, 0.05, 10), 2) 7721.73
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
asset_coverage_ratio(total_assets: Union[int, float], intangible_assets: Union[int, float], current_liabilities: Union[int, float], total_debt: Union[int, float]) -> float
¶
Return the asset coverage ratio.
ACR = (Total Assets − Intangible Assets − Current Liabilities) / Total Debt. Measures the ability of tangible assets to cover debt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_assets
|
Union[int, float]
|
Total assets. |
required |
intangible_assets
|
Union[int, float]
|
Intangible assets (goodwill, patents, …). |
required |
current_liabilities
|
Union[int, float]
|
Current liabilities. |
required |
total_debt
|
Union[int, float]
|
Total outstanding debt (must be > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Asset coverage ratio as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If total_debt is zero or negative. |
Example
asset_coverage_ratio(5_000_000, 500_000, 1_000_000, 2_000_000) 1.75
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
asset_turnover(net_sales: Union[int, float], total_assets: Union[int, float]) -> float
¶
Calculate the asset turnover ratio.
Measures how efficiently a company uses its assets to generate revenue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net_sales
|
Union[int, float]
|
Total net sales/revenue. |
required |
total_assets
|
Union[int, float]
|
Average total assets. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Asset turnover ratio as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If total_assets is zero or negative. |
Example
asset_turnover(500000, 250000) 2.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
binomial_option_price(s: float, k: float, t: float, r: float, sigma: float, steps: int, option_type: str = 'call') -> float
¶
European option price via the Cox-Ross-Rubinstein binomial tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
float
|
Current spot price (> 0). |
required |
k
|
float
|
Strike price (> 0). |
required |
t
|
float
|
Time to expiration in years (> 0). |
required |
r
|
float
|
Risk-free interest rate. |
required |
sigma
|
float
|
Annualised volatility (> 0). |
required |
steps
|
int
|
Number of tree steps (positive integer). |
required |
option_type
|
str
|
|
'call'
|
Returns:
| Type | Description |
|---|---|
float
|
Option price as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument has the wrong type. |
ValueError
|
If constraints are violated. |
Usage Example
round(binomial_option_price(100, 100, 1.0, 0.05, 0.2, 100), 2) 10.43
Complexity: O(n²) where n = steps
Source code in shortfx/fxNumeric/finance_functions.py
8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 | |
black_scholes_call(s: float, k: float, t: float, r: float, sigma: float) -> float
¶
Calculates the price of a European call option (Black-Scholes model).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
float
|
Current price of the underlying asset. |
required |
k
|
float
|
Strike price. |
required |
t
|
float
|
Time to expiration in years. |
required |
r
|
float
|
Risk-free interest rate (annualised, e.g. 0.05). |
required |
sigma
|
float
|
Volatility of the underlying (annualised, e.g. 0.2). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Theoretical call option price. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any input is not numeric. |
ValueError
|
If s, k, t, or sigma are not positive. |
Example
round(black_scholes_call(100, 100, 1, 0.05, 0.2), 2) 10.45
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
black_scholes_put(s: float, k: float, t: float, r: float, sigma: float) -> float
¶
Calculates the price of a European put option (Black-Scholes model).
Uses put-call parity: P = C - S + K·e^(-rT).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
float
|
Current price of the underlying asset. |
required |
k
|
float
|
Strike price. |
required |
t
|
float
|
Time to expiration in years. |
required |
r
|
float
|
Risk-free interest rate (annualised). |
required |
sigma
|
float
|
Volatility of the underlying (annualised). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Theoretical put option price. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any input is not numeric. |
ValueError
|
If s, k, t, or sigma are not positive. |
Example
round(black_scholes_put(100, 100, 1, 0.05, 0.2), 2) 5.57
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
bond_convexity(coupon_rate: float, ytm: float, periods: int, face: float = 100.0) -> float
¶
Calculates the convexity of a bond.
Convexity measures the curvature of the price-yield relationship, providing a second-order correction to duration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coupon_rate
|
float
|
Annual coupon rate (e.g. 0.05). |
required |
ytm
|
float
|
Yield to maturity per period. |
required |
periods
|
int
|
Number of remaining coupon periods. |
required |
face
|
float
|
Face (par) value of the bond. |
100.0
|
Returns:
| Type | Description |
|---|---|
float
|
Bond convexity. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs have wrong types. |
ValueError
|
If periods or face is not positive. |
Example
round(bond_convexity(0.05, 0.05, 10, 100), 2) 60.98
Complexity: O(n) where n = periods
Source code in shortfx/fxNumeric/finance_functions.py
bond_current_yield(annual_coupon: float, market_price: float) -> float
¶
Calculate the current yield of a bond.
Description
Current Yield = Annual Coupon Payment / Market Price × 100. A simple measure of investment income relative to price.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annual_coupon
|
float
|
Annual coupon payment. |
required |
market_price
|
float
|
Current market price of the bond. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Current yield as a percentage. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If market_price is zero. |
Usage Example
bond_current_yield(50, 980) 5.102040816326531
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
bond_equivalent_yield(face_value: float, purchase_price: float, days_to_maturity: int) -> float
¶
Bond equivalent yield (BEY) for a discount instrument.
BEY = (F − P) / P × (365 / days).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
face_value
|
float
|
Face value (> 0). |
required |
purchase_price
|
float
|
Purchase price (> 0, < face_value). |
required |
days_to_maturity
|
int
|
Days to maturity (positive integer). |
required |
Returns:
| Type | Description |
|---|---|
float
|
BEY as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument has the wrong type. |
ValueError
|
If constraints are violated. |
Usage Example
round(bond_equivalent_yield(1000, 980, 90), 6) 0.082766
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
bond_yield_to_worst(face: float, coupon_rate: float, price: float, years_to_maturity: int, call_years: list[int] | None = None) -> float
¶
Estimate the yield-to-worst for a callable bond.
Computes the yield-to-maturity and each yield-to-call, then returns the minimum. Uses a simple Newton iteration for each scenario.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
face
|
float
|
Face / par value. |
required |
coupon_rate
|
float
|
Annual coupon rate. |
required |
price
|
float
|
Current market price. |
required |
years_to_maturity
|
int
|
Years to final maturity. |
required |
call_years
|
list[int] | None
|
Optional list of years at which the bond is callable. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Yield-to-worst as a decimal. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs have wrong types. |
ValueError
|
If price <= 0 or years_to_maturity < 1. |
Example
round(bond_yield_to_worst(1000, 0.06, 950, 10, [5]), 4) 0.0708
Complexity: O(k × i), k = number of call dates, i = iterations.
Source code in shortfx/fxNumeric/finance_functions.py
6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 | |
break_even_units(fixed_costs: float, price_per_unit: float, variable_cost_per_unit: float) -> float
¶
Calculates the break-even point in units.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fixed_costs
|
float
|
Total fixed costs. |
required |
price_per_unit
|
float
|
Selling price per unit. |
required |
variable_cost_per_unit
|
float
|
Variable cost per unit. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Number of units needed to break even. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If price equals variable cost (no margin). |
Example
break_even_units(10000, 50, 30) 500.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
breakeven_units(fixed_costs: Union[int, float], price_per_unit: Union[int, float], variable_cost_per_unit: Union[int, float]) -> float
¶
Calculate the break-even point in units.
Units = Fixed Costs / (Price - Variable Cost).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fixed_costs
|
Union[int, float]
|
Total fixed costs. |
required |
price_per_unit
|
Union[int, float]
|
Selling price per unit. |
required |
variable_cost_per_unit
|
Union[int, float]
|
Variable cost per unit. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Number of units to break even. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If price <= variable cost or fixed_costs < 0. |
Example
breakeven_units(10000, 50, 30) 500.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
burn_rate(starting_cash: Union[int, float], ending_cash: Union[int, float], months: int) -> float
¶
Calculate the monthly cash burn rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
starting_cash
|
Union[int, float]
|
Cash at the beginning of the period. |
required |
ending_cash
|
Union[int, float]
|
Cash at the end of the period. |
required |
months
|
int
|
Number of months in the period. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Average monthly cash burn (positive = spending cash). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not of correct types. |
ValueError
|
If months is less than 1. |
Example
burn_rate(1000000, 700000, 3) 100000.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
cagr(beginning: float, ending: float, years: float) -> float
¶
Calculates the Compound Annual Growth Rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
beginning
|
float
|
Starting value. |
required |
ending
|
float
|
Ending value. |
required |
years
|
float
|
Number of years. |
required |
Returns:
| Type | Description |
|---|---|
float
|
CAGR as a decimal (e.g. 0.10 for 10%). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If beginning <= 0 or years <= 0. |
Example
round(cagr(1000, 2000, 5), 4) 0.1487
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
calmar_ratio(annualized_return: Union[int, float], max_drawdown: Union[int, float]) -> float
¶
Calculate the Calmar ratio: annualized return / max drawdown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annualized_return
|
Union[int, float]
|
Annualized return (as decimal). |
required |
max_drawdown
|
Union[int, float]
|
Maximum drawdown as a positive decimal (e.g. 0.20 for 20 %). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Calmar ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If max_drawdown is not positive. |
Example
calmar_ratio(0.15, 0.10) 1.5
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
capital_intensity_ratio(total_assets: Union[int, float], total_revenue: Union[int, float]) -> float
¶
Return the capital intensity ratio.
Measures how much asset investment is needed to generate one unit of revenue. CIR = Total Assets / Total Revenue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_assets
|
Union[int, float]
|
Total assets (must be ≥ 0). |
required |
total_revenue
|
Union[int, float]
|
Total revenue (must be > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Capital intensity ratio as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If total_revenue is zero or negative. |
Example
capital_intensity_ratio(2_000_000, 1_000_000) 2.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
capm(risk_free_rate: float, beta: float, market_return: float) -> float
¶
Computes the expected return using the Capital Asset Pricing Model.
E(R) = Rf + β × (Rm − Rf).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
risk_free_rate
|
float
|
Risk-free rate as decimal. |
required |
beta
|
float
|
Asset beta (systematic risk). |
required |
market_return
|
float
|
Expected market return as decimal. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Expected return as a decimal. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
Example
round(capm(0.03, 1.2, 0.10), 4) 0.114
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
cash_conversion_cycle(days_inventory: Union[int, float], days_receivable: Union[int, float], days_payable: Union[int, float]) -> float
¶
Calculate the Cash Conversion Cycle: DIO + DSO - DPO.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
days_inventory
|
Union[int, float]
|
Days inventory outstanding (DIO). |
required |
days_receivable
|
Union[int, float]
|
Days sales outstanding (DSO). |
required |
days_payable
|
Union[int, float]
|
Days payable outstanding (DPO). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cash conversion cycle in days. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
Example
cash_conversion_cycle(45, 30, 35) 40.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
cash_conversion_efficiency(operating_cash_flow: Union[int, float], net_income: Union[int, float]) -> float
¶
Return the cash conversion efficiency ratio.
Measures how efficiently net income is converted into operating cash flow. CCE = OCF / Net Income.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operating_cash_flow
|
Union[int, float]
|
Operating cash flow amount. |
required |
net_income
|
Union[int, float]
|
Net income (must be ≠ 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cash conversion efficiency as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If net_income is zero. |
Example
cash_conversion_efficiency(90_000, 100_000) 0.9
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
cash_flow_coverage(operating_cash_flow: Union[int, float], total_debt: Union[int, float]) -> float
¶
Return the cash flow coverage ratio.
Measures a company's ability to pay off its total debt using operating cash flow. CFR = OCF / Total Debt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operating_cash_flow
|
Union[int, float]
|
Operating cash flow amount. |
required |
total_debt
|
Union[int, float]
|
Total outstanding debt (must be ≠ 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cash flow coverage ratio as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If total_debt is zero. |
Example
cash_flow_coverage(500_000, 1_000_000) 0.5
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
cash_ratio(cash: Union[int, float], current_liabilities: Union[int, float]) -> float
¶
Calculate the cash ratio.
The most conservative liquidity ratio, using only cash and cash equivalents to cover current liabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cash
|
Union[int, float]
|
Cash and cash equivalents. |
required |
current_liabilities
|
Union[int, float]
|
Total current liabilities. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cash ratio as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If current_liabilities is zero or negative. |
Example
cash_ratio(150000, 300000) 0.5
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
cash_return_on_assets(operating_cash_flow: Union[int, float], total_assets: Union[int, float]) -> float
¶
Return the cash return on assets ratio.
CROA = Operating Cash Flow / Total Assets. Measures the cash-generating efficiency of the asset base.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operating_cash_flow
|
Union[int, float]
|
Operating cash flow. |
required |
total_assets
|
Union[int, float]
|
Total assets (must be > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cash return on assets as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If total_assets is zero or negative. |
Example
cash_return_on_assets(250_000, 2_000_000) 0.125
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
compound_interest(principal: float, rate: float, n: int, t: float) -> float
¶
Calculates compound interest (total amount minus principal).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
principal
|
float
|
The initial amount. |
required |
rate
|
float
|
Annual interest rate. |
required |
n
|
int
|
Number of compounding periods per year. |
required |
t
|
float
|
Number of years. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The interest earned (total - principal). |
Example
compound_interest(1000, 0.05, 12, 1) 51.16
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
conditional_var(returns: list[float], confidence: float = 0.95) -> float
¶
Conditional Value at Risk (CVaR / Expected Shortfall).
Average of losses exceeding the VaR threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
list[float]
|
List of periodic returns. |
required |
confidence
|
float
|
Confidence level in (0, 1), default 0.95. |
0.95
|
Returns:
| Type | Description |
|---|---|
float
|
CVaR as a positive float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If returns is empty or confidence is out of range. |
Usage Example
returns = [-0.02, -0.01, 0.0, 0.01, 0.02, -0.03, 0.015, 0.005, -0.005, 0.01] round(conditional_var(returns, 0.9), 4) 0.03
Complexity: O(n log n)
Source code in shortfx/fxNumeric/finance_functions.py
continuous_compounding(principal: float, rate: float, time: float) -> float
¶
Future value with continuous compounding.
Description
FV = P × e^{r·t}.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
principal
|
float
|
Initial investment amount (> 0). |
required |
rate
|
float
|
Annual interest rate (as decimal). |
required |
time
|
float
|
Time in years (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Future value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If principal or time are not positive. |
Usage Example
round(continuous_compounding(1000, 0.05, 10), 2) 1648.72
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
contribution_margin(revenue: Union[int, float], variable_costs: Union[int, float]) -> float
¶
Calculate the contribution margin: Revenue - Variable Costs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
revenue
|
Union[int, float]
|
Total revenue. |
required |
variable_costs
|
Union[int, float]
|
Total variable costs. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Contribution margin. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
Example
contribution_margin(100_000, 60_000) 40000.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
convexity_adjustment(convexity: float, delta_yield: float) -> float
¶
Calculate the convexity adjustment for bond price change.
Description
Adjustment = 0.5 × Convexity × (Δy)². This is the second-order correction added to the duration-based price change estimate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
convexity
|
float
|
Bond convexity measure. |
required |
delta_yield
|
float
|
Yield change (as decimal, e.g. 0.01 for 1%). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Convexity adjustment as a float (percentage price change). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
Usage Example
convexity_adjustment(150.0, 0.01) 0.0075
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
cost_of_debt_after_tax(interest_rate: float, tax_rate: float) -> float
¶
Calculate the after-tax cost of debt.
Description
After-tax cost = Interest Rate × (1 − Tax Rate). Used in WACC calculations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interest_rate
|
float
|
Pre-tax cost of debt (as decimal, e.g. 0.06). |
required |
tax_rate
|
float
|
Corporate tax rate (as decimal, e.g. 0.30). |
required |
Returns:
| Type | Description |
|---|---|
float
|
After-tax cost of debt (as decimal). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
Usage Example
cost_of_debt_after_tax(0.06, 0.30) 0.042
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
cost_of_equity_capm(risk_free_rate: Union[int, float], beta: Union[int, float], market_return: Union[int, float]) -> float
¶
Calculate cost of equity using CAPM: Ke = Rf + β * (Rm - Rf).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
risk_free_rate
|
Union[int, float]
|
Risk-free rate (decimal). |
required |
beta
|
Union[int, float]
|
Equity beta. |
required |
market_return
|
Union[int, float]
|
Expected market return (decimal). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cost of equity (decimal). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
Example
cost_of_equity_capm(0.03, 1.2, 0.10) 0.114
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
coupdaybs(settlement: str, maturity: str, frequency: int, basis: int = 0) -> float
¶
Days from beginning of coupon period to settlement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
frequency
|
int
|
Coupon payments per year (1, 2, or 4). |
required |
basis
|
int
|
Day-count basis (0-4). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Number of days. |
Example
coupdaybs("2011-01-25", "2011-11-15", 2, 1) 71
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
coupdays(settlement: str, maturity: str, frequency: int, basis: int = 0) -> float
¶
Number of days in the coupon period containing settlement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
frequency
|
int
|
Coupon payments per year (1, 2, or 4). |
required |
basis
|
int
|
Day-count basis (0-4). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Days in the coupon period. |
Example
coupdays("2011-01-25", "2011-11-15", 2, 1) 181
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
coupdaysnc(settlement: str, maturity: str, frequency: int, basis: int = 0) -> float
¶
Days from settlement to next coupon date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
frequency
|
int
|
Coupon payments per year (1, 2, or 4). |
required |
basis
|
int
|
Day-count basis (0-4). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Number of days. |
Example
coupdaysnc("2011-01-25", "2011-11-15", 2, 1) 110
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
coupncd(settlement: str, maturity: str, frequency: int) -> date
¶
Next coupon date after settlement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
frequency
|
int
|
Coupon payments per year (1, 2, or 4). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
date |
date
|
Next coupon date. |
Example
coupncd("2011-01-25", "2011-11-15", 2) datetime.date(2011, 5, 15)
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
coupnum(settlement: str, maturity: str, frequency: int) -> int
¶
Number of coupons between settlement and maturity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
frequency
|
int
|
Coupon payments per year (1, 2, or 4). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of coupon payments. |
Example
coupnum("2011-01-25", "2013-11-15", 2) 6
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
couppcd(settlement: str, maturity: str, frequency: int) -> date
¶
Previous coupon date before settlement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
frequency
|
int
|
Coupon payments per year (1, 2, or 4). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
date |
date
|
Previous coupon date. |
Example
couppcd("2011-01-25", "2011-11-15", 2) datetime.date(2010, 11, 15)
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
credit_spread(corporate_yield: float, risk_free_yield: float) -> float
¶
Credit spread between a corporate bond and risk-free rate.
Spread = y_corporate − y_risk_free.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corporate_yield
|
float
|
Yield of the corporate bond. |
required |
risk_free_yield
|
float
|
Yield of the risk-free benchmark. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Credit spread as a float (in the same units as the yields). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
Usage Example
credit_spread(0.065, 0.04) 0.025
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
cumipmt(rate: float, nper: int, pv: float, start_period: int, end_period: int, type: int = 0) -> float
¶
Calculates cumulative interest paid between two periods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
Interest rate per period. |
required |
nper
|
int
|
Total number of periods. |
required |
pv
|
float
|
Present value (loan amount). |
required |
start_period
|
int
|
First period (1-based). |
required |
end_period
|
int
|
Last period (1-based). |
required |
type
|
int
|
0 = end of period, 1 = beginning. Default 0. |
0
|
Returns:
| Type | Description |
|---|---|
float
|
Cumulative interest paid. |
Example
round(cumipmt(0.05/12, 360, 100000, 1, 12), 2) -4948.04
Complexity: O(n) where n = end_period - start_period
Source code in shortfx/fxNumeric/finance_functions.py
cumprinc(rate: float, nper: int, pv: float, start_period: int, end_period: int, type: int = 0) -> float
¶
Calculates cumulative principal paid between two periods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
Interest rate per period. |
required |
nper
|
int
|
Total number of periods. |
required |
pv
|
float
|
Present value (loan amount). |
required |
start_period
|
int
|
First period (1-based). |
required |
end_period
|
int
|
Last period (1-based). |
required |
type
|
int
|
0 = end of period, 1 = beginning. Default 0. |
0
|
Returns:
| Type | Description |
|---|---|
float
|
Cumulative principal paid. |
Example
round(cumprinc(0.05/12, 360, 100000, 1, 12), 2) -1493.72
Complexity: O(n) where n = end_period - start_period
Source code in shortfx/fxNumeric/finance_functions.py
current_ratio(current_assets: Union[int, float], current_liabilities: Union[int, float]) -> float
¶
Computes the current (liquidity) ratio.
current_ratio = current_assets / current_liabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_assets
|
Union[int, float]
|
Total current assets. |
required |
current_liabilities
|
Union[int, float]
|
Total current liabilities. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The current ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If current_liabilities is zero or negative. |
Example
current_ratio(150000, 100000) 1.5
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
days_payable_outstanding(accounts_payable: Union[int, float], cost_of_goods_sold: Union[int, float], days: Union[int, float] = 365) -> float
¶
Calculate days payable outstanding (DPO).
DPO = (accounts_payable / COGS) × days
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
accounts_payable
|
Union[int, float]
|
Average accounts payable. |
required |
cost_of_goods_sold
|
Union[int, float]
|
Cost of goods sold for the period. |
required |
days
|
Union[int, float]
|
Number of days in the period (default 365). |
365
|
Returns:
| Type | Description |
|---|---|
float
|
DPO in days. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If cost_of_goods_sold is zero. |
Example
days_payable_outstanding(50_000, 500_000) 36.5
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
db(cost: float, salvage: float, life: Union[int, float], period: Union[int, float], month: int = 12) -> float
¶
Calculates the depreciation of an asset for a specified period using the fixed-declining balance method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cost
|
float
|
The initial cost of the asset. |
required |
salvage
|
float
|
The salvage value at the end of the useful life of the asset. |
required |
life
|
Union[int, float]
|
The number of periods over which the asset is being depreciated (e.g., years). |
required |
period
|
Union[int, float]
|
The period for which you want to calculate the depreciation. Must be between 1 and 'life' (inclusive). |
required |
month
|
int
|
The number of months in the first year. Defaults to 12. If month is 12, then the initial depreciation is applied over 12 months. If month is different, it adjusts the first period's depreciation. |
12
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The depreciation amount for the specified period. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric or 'month' is not an integer. |
ValueError
|
If 'cost' or 'salvage' are negative, 'life' or 'period' are non-positive, 'period' is greater than 'life', or 'month' is not between 1 and 12. |
Example
Asset: Cost $10,000, Salvage $1,000, Life 6 years. Depreciation for year 1.¶
db(cost=10000, salvage=1000, life=6, period=1) 3333.333333333333
Depreciation for year 2¶
db(cost=10000, salvage=1000, life=6, period=2) 2222.222222222222
For an asset acquired mid-year (e.g., 9 months in first year)¶
db(cost=10000, salvage=1000, life=6, period=1, month=9) 2500.0
Cost: O(n), where n is the number of periods (life). Iterates up to the requested period.
Source code in shortfx/fxNumeric/finance_functions.py
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 | |
ddb(cost: float, salvage: float, life: Union[int, float], period: Union[int, float], factor: float = 2.0) -> float
¶
Calculates depreciation using the double-declining-balance method.
In each period the depreciation is min(rate * book_value, book_value - salvage).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cost
|
float
|
Initial cost of the asset. |
required |
salvage
|
float
|
Salvage value at end of life. |
required |
life
|
Union[int, float]
|
Useful life in periods. |
required |
period
|
Union[int, float]
|
Period for which to compute depreciation (1-based). |
required |
factor
|
float
|
Declining-balance factor (default 2 for double). |
2.0
|
Returns:
| Type | Description |
|---|---|
float
|
Depreciation for the given period. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If any value is negative, life is zero, or period > life. |
Example
round(ddb(10000, 1000, 5, 1), 2) 4000.0 round(ddb(10000, 1000, 5, 2), 2) 2400.0
Complexity: O(period)
Source code in shortfx/fxNumeric/finance_functions.py
debt_service_coverage(net_operating_income: Union[int, float], total_debt_service: Union[int, float]) -> float
¶
Calculate the Debt Service Coverage Ratio (DSCR).
Measures ability to service debt obligations with operating income.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net_operating_income
|
Union[int, float]
|
Net operating income. |
required |
total_debt_service
|
Union[int, float]
|
Total debt service (principal + interest). |
required |
Returns:
| Type | Description |
|---|---|
float
|
DSCR as a float. Values > 1 indicate sufficient income. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If total_debt_service is zero. |
Example
debt_service_coverage(500000, 400000) 1.25
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
debt_service_ratio(net_operating_income: Union[int, float], total_debt_service: Union[int, float]) -> float
¶
Calculate debt service coverage ratio (DSCR).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net_operating_income
|
Union[int, float]
|
Net operating income. |
required |
total_debt_service
|
Union[int, float]
|
Total debt service (principal + interest). |
required |
Returns:
| Type | Description |
|---|---|
float
|
DSCR ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If total_debt_service is zero. |
Example
debt_service_ratio(300_000, 200_000) 1.5
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
debt_to_capital(total_debt: Union[int, float], total_equity: Union[int, float]) -> float
¶
Return the debt-to-capital ratio.
D/(D+E). Measures the proportion of debt in the capital structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_debt
|
Union[int, float]
|
Total debt (must be ≥ 0). |
required |
total_equity
|
Union[int, float]
|
Total equity (must be ≥ 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Debt-to-capital ratio as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If total_debt + total_equity is zero. |
Example
debt_to_capital(400_000, 600_000) 0.4
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
debt_to_equity(total_debt: float, total_equity: float) -> float
¶
Computes the debt-to-equity ratio.
D/E = Total Debt / Total Equity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_debt
|
float
|
Total liabilities (non-negative). |
required |
total_equity
|
float
|
Total shareholders' equity (positive). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The D/E ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If debt < 0 or equity <= 0. |
Example
debt_to_equity(500000, 250000) 2.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
debt_to_income(monthly_debt: float, monthly_income: float) -> float
¶
Calculate Debt-to-Income (DTI) ratio.
DTI = monthly_debt / monthly_income * 100.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
monthly_debt
|
float
|
Total monthly debt payments. |
required |
monthly_income
|
float
|
Gross monthly income. |
required |
Returns:
| Type | Description |
|---|---|
float
|
DTI ratio as a percentage. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If monthly_income is not positive. |
Example
debt_to_income(1500, 5000) 30.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
decreasing_annuity_pv(interest_rate: float, periods: int) -> float
¶
Present value of a decreasing annuity-immediate paying (n−k+1) at time k.
(Dä)_n = (n − a_n) / i.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interest_rate
|
float
|
Interest rate per period (> 0). |
required |
periods
|
int
|
Number of periods (positive integer). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Present value per unit as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument has the wrong type. |
ValueError
|
If numeric constraints are violated. |
Usage Example
round(decreasing_annuity_pv(0.05, 10), 4) 45.5653
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
defensive_interval(liquid_assets: Union[int, float], daily_operating_expenses: Union[int, float]) -> float
¶
Return the defensive interval ratio (in days).
DIR = Liquid Assets / Daily Operating Expenses. Measures how many days a company can operate from liquid assets alone without additional revenue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
liquid_assets
|
Union[int, float]
|
Cash + marketable securities + receivables. |
required |
daily_operating_expenses
|
Union[int, float]
|
Average daily operating expenses (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Defensive interval in days. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If daily_operating_expenses is zero or negative. |
Example
defensive_interval(500_000, 10_000) 50.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
deferred_annuity_pv(payment: float, interest_rate: float, deferral: int, periods: int) -> float
¶
Present value of a deferred annuity-immediate.
PV = v^deferral × payment × a_{periods} where a_{periods} is the annuity-immediate PV factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payment
|
float
|
Payment per period (> 0). |
required |
interest_rate
|
float
|
Interest rate per period (> 0). |
required |
deferral
|
int
|
Number of deferral periods (≥ 0). |
required |
periods
|
int
|
Number of payment periods (positive integer). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Present value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument has the wrong type. |
ValueError
|
If numeric constraints are violated. |
Usage Example
round(deferred_annuity_pv(1000, 0.05, 5, 10), 2) 6050.18
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
degree_of_financial_leverage(ebit: Union[int, float], interest_expense: Union[int, float]) -> float
¶
Calculate the degree of financial leverage (DFL).
DFL = EBIT / (EBIT − interest)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ebit
|
Union[int, float]
|
Earnings before interest and taxes. |
required |
interest_expense
|
Union[int, float]
|
Interest expense. |
required |
Returns:
| Type | Description |
|---|---|
float
|
DFL ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If EBIT equals interest (division by zero). |
Example
degree_of_financial_leverage(500_000, 100_000) 1.25
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
disc(settlement: str, maturity: str, pr: float, redemption: float, basis: int = 0) -> float
¶
Discount rate for a security.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
pr
|
float
|
Security price per $100 face. |
required |
redemption
|
float
|
Redemption value per $100 face. |
required |
basis
|
int
|
Day-count basis (0-4). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Discount rate. |
Example
round(disc("2018-02-07", "2018-07-15", 97.975, 100, 0), 6) 0.046131
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
discount_factor(rate: float, periods: Union[int, float]) -> float
¶
Discount factor for a given rate and number of periods.
Description
DF = 1 / (1 + r)^n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
Discount rate per period (> 0). |
required |
periods
|
Union[int, float]
|
Number of periods (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Discount factor as a float in (0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If rate or periods are not positive. |
Usage Example
round(discount_factor(0.05, 10), 6) 0.613913
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
dividend_discount_price(dividend: Union[int, float], discount_rate: Union[int, float], periods: int, growth_rate: Union[int, float] = 0.0) -> float
¶
Calculate stock price via a finite-period dividend discount model.
P = Σ D₀·(1+g)^t / (1+r)^t for t = 1 … periods
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dividend
|
Union[int, float]
|
Current dividend (D₀). |
required |
discount_rate
|
Union[int, float]
|
Required rate of return (decimal, > 0). |
required |
periods
|
int
|
Number of future periods. |
required |
growth_rate
|
Union[int, float]
|
Dividend growth rate per period (decimal, default 0). |
0.0
|
Returns:
| Type | Description |
|---|---|
float
|
Present value of future dividends. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric / periods not int. |
ValueError
|
If discount_rate not positive, periods < 1, or dividend negative. |
Example
round(dividend_discount_price(2.0, 0.10, 5), 4) 7.5816
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
dividend_payout_ratio(dividends_paid: Union[int, float], net_income: Union[int, float]) -> float
¶
Calculate the dividend payout ratio as a percentage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dividends_paid
|
Union[int, float]
|
Total dividends paid. |
required |
net_income
|
Union[int, float]
|
Net income of the company. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Payout ratio as a percentage. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If net_income is zero. |
Example
dividend_payout_ratio(40000, 100000) 40.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
dividend_yield(annual_dividend: Union[int, float], market_price: Union[int, float]) -> float
¶
Calculate the dividend yield as a percentage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annual_dividend
|
Union[int, float]
|
Total annual dividend per share. |
required |
market_price
|
Union[int, float]
|
Current market price per share. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Dividend yield as a percentage (e.g. 3.5 means 3.5%). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If market_price is zero or negative. |
Example
dividend_yield(2, 50) 4.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
dollarde(fractional_dollar: float, fraction: int) -> float
¶
Convert fractional dollar price to decimal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fractional_dollar
|
float
|
Dollar price expressed as a fraction. |
required |
fraction
|
int
|
Denominator of the fraction. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Decimal dollar price. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fraction ≤ 0. |
Example
dollarde(1.02, 16) 1.125
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
dollarfr(decimal_dollar: float, fraction: int) -> float
¶
Convert decimal dollar price to fractional notation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
decimal_dollar
|
float
|
Decimal dollar price. |
required |
fraction
|
int
|
Denominator for the fractional part. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Fractional dollar price. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fraction ≤ 0. |
Example
dollarfr(1.125, 16) 1.02
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
dupont_roe(net_margin: Union[int, float], asset_turnover_ratio: Union[int, float], equity_multiplier: Union[int, float]) -> float
¶
Calculate Return on Equity using the DuPont decomposition.
ROE = Net Profit Margin × Asset Turnover × Equity Multiplier. All inputs should be ratios (not percentages).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net_margin
|
Union[int, float]
|
Net profit margin ratio (e.g. 0.10 for 10%). |
required |
asset_turnover_ratio
|
Union[int, float]
|
Asset turnover ratio. |
required |
equity_multiplier
|
Union[int, float]
|
Total assets / shareholders' equity. |
required |
Returns:
| Type | Description |
|---|---|
float
|
ROE as a decimal ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
Example
dupont_roe(0.10, 1.5, 2.0) 0.3
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
duration(settlement: str, maturity: str, coupon: float, yld: float, frequency: int, basis: int = 0) -> float
¶
Macaulay duration for a bond with par value $100.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
coupon
|
float
|
Annual coupon rate. |
required |
yld
|
float
|
Annual yield. |
required |
frequency
|
int
|
Coupon payments per year (1, 2, or 4). |
required |
basis
|
int
|
Day-count basis (0-4). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Macaulay duration in years. |
Example
round(duration("2008-01-01", "2016-01-01", 0.08, 0.09, 2, 1), 4) 5.9937
Complexity: O(n) where n = number of coupons
Source code in shortfx/fxNumeric/finance_functions.py
duration_gap(asset_duration: float, liability_duration: float, leverage_ratio: float) -> float
¶
Duration gap for an institution's balance sheet.
Gap = D_A − (L/A) × D_L.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
asset_duration
|
float
|
Duration of assets (≥ 0). |
required |
liability_duration
|
float
|
Duration of liabilities (≥ 0). |
required |
leverage_ratio
|
float
|
Liabilities / Assets ratio (≥ 0, typically < 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Duration gap as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If constraints are violated. |
Usage Example
round(duration_gap(5.0, 3.0, 0.9), 2) 2.3
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
earnings_per_share(net_income: Union[int, float], shares_outstanding: Union[int, float]) -> float
¶
Computes earnings per share (EPS).
EPS = net_income / shares_outstanding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net_income
|
Union[int, float]
|
Net income. |
required |
shares_outstanding
|
Union[int, float]
|
Number of outstanding shares. |
required |
Returns:
| Type | Description |
|---|---|
float
|
EPS value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If shares_outstanding is zero or negative. |
Example
earnings_per_share(1000000, 500000) 2.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
earnings_yield(earnings_per_share: Union[int, float], price_per_share: Union[int, float]) -> float
¶
Calculate the earnings yield: EPS / Price (inverse of P/E ratio).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
earnings_per_share
|
Union[int, float]
|
Earnings per share. |
required |
price_per_share
|
Union[int, float]
|
Market price per share. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Earnings yield as a decimal. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If price_per_share is not positive. |
Example
earnings_yield(5, 100) 0.05
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
economic_order_quantity(annual_demand: float, order_cost: float, holding_cost: float) -> float
¶
Economic Order Quantity (EOQ) — Wilson's formula.
EOQ = √(2 × D × S / H)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annual_demand
|
float
|
Annual demand in units. |
required |
order_cost
|
float
|
Fixed cost per order. |
required |
holding_cost
|
float
|
Holding cost per unit per year. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Optimal order quantity. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If any input is not positive. |
Example
round(economic_order_quantity(10000, 50, 2), 2) 707.11
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
economic_value_added(nopat: Union[int, float], capital_employed: Union[int, float], wacc: Union[int, float]) -> float
¶
Return the Economic Value Added (EVA).
EVA = NOPAT - (Capital Employed × WACC). Measures value created above the required return on invested capital.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nopat
|
Union[int, float]
|
Net Operating Profit After Taxes. |
required |
capital_employed
|
Union[int, float]
|
Total capital invested in the business. |
required |
wacc
|
Union[int, float]
|
Weighted Average Cost of Capital (as a decimal). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Economic value added as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
Example
economic_value_added(150_000, 1_000_000, 0.10) 50000.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
effect(nominal_rate: float, npery: int) -> float
¶
Calculates the effective annual interest rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nominal_rate
|
float
|
Nominal annual interest rate. |
required |
npery
|
int
|
Number of compounding periods per year. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The effective annual rate. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If nominal_rate <= 0 or npery < 1. |
Example
round(effect(0.10, 4), 6) 0.103813
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
effective_annual_rate(nominal_rate: Union[int, float], periods_per_year: int) -> float
¶
Convert a nominal interest rate to the effective annual rate.
EAR = (1 + r/n)^n - 1 where r is the nominal rate and n is the number of compounding periods per year.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nominal_rate
|
Union[int, float]
|
Nominal annual interest rate (e.g. 0.12 for 12%). |
required |
periods_per_year
|
int
|
Number of compounding periods per year. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Effective annual rate as a decimal. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not of the correct type. |
ValueError
|
If periods_per_year is less than 1. |
Example
round(effective_annual_rate(0.12, 12), 6) 0.126825
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
effective_duration(price_down: float, price_up: float, initial_price: float, delta_yield: float) -> float
¶
Calculate the effective duration of a bond.
Description
Effective duration = (P₋ - P₊) / (2 × P₀ × Δy), where P₋ is the price when yields decrease, P₊ when yields increase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
price_down
|
float
|
Bond price when yield decreases by delta_yield. |
required |
price_up
|
float
|
Bond price when yield increases by delta_yield. |
required |
initial_price
|
float
|
Current bond price. |
required |
delta_yield
|
float
|
The yield shift (as decimal, e.g. 0.01 for 1%). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Effective duration as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If initial_price or delta_yield is zero. |
Usage Example
effective_duration(102.0, 98.0, 100.0, 0.01) 2.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
endowment_insurance_pv(benefit: float, interest_rate: float, mortality_rates: list[float]) -> float
¶
Present value of an endowment insurance.
Combines term life insurance with a pure endowment: pays benefit on death during the term or on survival at the end of the term.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
benefit
|
float
|
Amount payable (> 0). |
required |
interest_rate
|
float
|
Annual effective interest rate (> 0). |
required |
mortality_rates
|
list[float]
|
One-year mortality probabilities for the term. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Actuarial present value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument has the wrong type. |
ValueError
|
If numeric constraints are violated. |
Usage Example
round(endowment_insurance_pv(100000, 0.05, [0.01, 0.02, 0.03]), 2) 86557.82
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
enterprise_value_simple(market_cap_value: Union[int, float], total_debt: Union[int, float], cash: Union[int, float]) -> float
¶
Calculate simplified enterprise value: EV = Market Cap + Debt - Cash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
market_cap_value
|
Union[int, float]
|
Market capitalisation. |
required |
total_debt
|
Union[int, float]
|
Total debt. |
required |
cash
|
Union[int, float]
|
Cash and cash equivalents. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Enterprise value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
Example
enterprise_value_simple(1_000_000, 200_000, 50_000) 1150000.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
equity_multiplier(total_assets: Union[int, float], total_equity: Union[int, float]) -> float
¶
Calculate the equity multiplier (total assets / shareholders' equity).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_assets
|
Union[int, float]
|
Total assets value. |
required |
total_equity
|
Union[int, float]
|
Shareholders' equity value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Equity multiplier ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If total_equity is zero. |
Example
equity_multiplier(500_000, 200_000) 2.5
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
ev_to_ebitda(enterprise_value: Union[int, float], ebitda: Union[int, float]) -> float
¶
Calculate the Enterprise Value to EBITDA ratio.
A valuation measure comparing the value of a company to its earnings before interest, taxes, depreciation, and amortization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enterprise_value
|
Union[int, float]
|
Enterprise value of the company. |
required |
ebitda
|
Union[int, float]
|
Earnings before interest, taxes, depreciation, and amortization. |
required |
Returns:
| Type | Description |
|---|---|
float
|
EV/EBITDA ratio as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If ebitda is zero. |
Example
ev_to_ebitda(1000000, 200000) 5.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
expected_shortfall(portfolio_value: float, mean_return: float, std_return: float, confidence: float = 0.95) -> float
¶
Calculates parametric Expected Shortfall (CVaR) assuming normal returns.
ES = Portfolio × (μ − σ × φ(z_α) / (1 − α))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio_value
|
float
|
Total portfolio value. |
required |
mean_return
|
float
|
Expected return. |
required |
std_return
|
float
|
Standard deviation of returns. |
required |
confidence
|
float
|
Confidence level (e.g. 0.95). |
0.95
|
Returns:
| Type | Description |
|---|---|
float
|
Expected Shortfall as a positive loss amount. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If confidence is not in (0, 1). |
Example
round(expected_shortfall(1_000_000, 0.001, 0.02, 0.95), 2) 40134.66
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
fixed_asset_turnover(net_sales: Union[int, float], net_fixed_assets: Union[int, float]) -> float
¶
Calculate fixed asset turnover ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net_sales
|
Union[int, float]
|
Net sales for the period. |
required |
net_fixed_assets
|
Union[int, float]
|
Average net fixed assets. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Fixed asset turnover ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If net_fixed_assets is zero. |
Example
fixed_asset_turnover(1_000_000, 250_000) 4.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
fixed_charge_coverage(ebit: Union[int, float], fixed_charges: Union[int, float]) -> float
¶
Calculate the Fixed Charge Coverage Ratio.
Measures the ability to cover fixed charges (interest, leases, etc.) with earnings before interest and taxes plus those fixed charges.
FCCR = (EBIT + fixed_charges) / fixed_charges
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ebit
|
Union[int, float]
|
Earnings before interest and taxes. |
required |
fixed_charges
|
Union[int, float]
|
Total fixed charges (interest, lease payments, etc.). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Fixed charge coverage ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If fixed_charges is zero. |
Example
fixed_charge_coverage(300000, 100000) 4.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
force_of_interest(annual_rate: float) -> float
¶
Convert annual effective interest rate to force of interest (continuous rate).
δ = ln(1 + i).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annual_rate
|
float
|
Annual effective interest rate (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Force of interest as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If annual_rate is not numeric. |
ValueError
|
If annual_rate is not positive. |
Usage Example
round(force_of_interest(0.05), 6) 0.048790
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
force_of_mortality(mortality_rate: float) -> float
¶
Convert a one-year mortality probability to force of mortality.
μ = −ln(1 − q).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mortality_rate
|
float
|
One-year mortality probability in (0, 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Force of mortality as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If mortality_rate is not numeric. |
ValueError
|
If mortality_rate is not in (0, 1). |
Usage Example
round(force_of_mortality(0.02), 6) 0.020203
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
free_cash_flow_yield(free_cash_flow: Union[int, float], market_cap: Union[int, float]) -> float
¶
Return the free cash flow yield.
FCF Yield = Free Cash Flow / Market Capitalization. Indicates how much free cash flow a company generates relative to its market value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
free_cash_flow
|
Union[int, float]
|
Free cash flow amount. |
required |
market_cap
|
Union[int, float]
|
Market capitalization (must be > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Free cash flow yield as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If market_cap is zero or negative. |
Example
free_cash_flow_yield(50_000, 1_000_000) 0.05
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
future_value(rate: float, nper: Union[int, float], pmt: float, pv: float, type: int = 0) -> float
¶
Calculates the Future Value (FV) of an investment based on a series of periodic constant payments and a constant interest rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
The interest rate per period (e.g., 0.05 for 5% annual rate). |
required |
nper
|
Union[int, float]
|
The total number of payment periods in an investment. |
required |
pmt
|
float
|
The payment made each period. Payments are typically negative (cash outflow). |
required |
pv
|
float
|
The Present Value (PV), or the lump-sum amount that a series of future payments is worth right now. Typically negative (cash outflow). |
required |
type
|
int
|
When payments are due. 0 = at the end of the period (default). 1 = at the beginning of the period. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The Future Value of the investment. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any input is not a number or if 'type' is not 0 or 1. |
ValueError
|
If 'nper' is negative. |
Example
FV of $1000 invested for 5 years at 5% annual interest, compounded annually.¶
future_value(rate=0.05, nper=5, pmt=0, pv=-1000) 1276.2815625000003
FV of $100 payments made at end of each year for 5 years at 5% annual interest.¶
future_value(rate=0.05, nper=5, pmt=-100, pv=0) 552.5631250000001
Cost: O(1), direct future value formula calculation.
Source code in shortfx/fxNumeric/finance_functions.py
fvschedule(principal: float, schedule: List[float]) -> float
¶
Calculates future value with a variable schedule of interest rates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
principal
|
float
|
The initial investment amount. |
required |
schedule
|
List[float]
|
List of interest rates for each period. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The future value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are invalid. |
ValueError
|
If schedule is empty. |
Example
fvschedule(1000, [0.05, 0.06, 0.07]) 1190.91
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
garman_klass_volatility(ohlc: list[tuple[float, float, float, float]]) -> float
¶
Garman-Klass volatility estimator from OHLC bars.
σ²_GK = (1/n) Σ [0.5·ln(H/L)² − (2ln2−1)·ln(C/O)²].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ohlc
|
list[tuple[float, float, float, float]]
|
List of (open, high, low, close) tuples. Each price must be > 0, high ≥ low, high ≥ open, high ≥ close, low ≤ open, low ≤ close. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Annualised volatility estimate as a float (assuming 252 trading days). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If ohlc is not a list or elements are not tuples. |
ValueError
|
If ohlc is empty or prices violate constraints. |
Usage Example
bars = [(100, 105, 98, 103), (103, 107, 101, 106), (106, 110, 104, 108)] round(garman_klass_volatility(bars), 4) > 0 True
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
gordon_growth_price(dividend: Union[int, float], discount_rate: Union[int, float], growth_rate: Union[int, float]) -> float
¶
Estimate the intrinsic price of a stock using the Gordon Growth Model.
P = D₁ / (r - g)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dividend
|
Union[int, float]
|
Expected dividend next period. |
required |
discount_rate
|
Union[int, float]
|
Required rate of return (decimal). |
required |
growth_rate
|
Union[int, float]
|
Constant dividend growth rate (decimal, must be < discount_rate). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Intrinsic stock price. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If growth_rate >= discount_rate or dividend is negative. |
Example
gordon_growth_price(2.0, 0.10, 0.05) 40.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
gross_margin(revenue: Union[int, float], cogs: Union[int, float]) -> float
¶
Computes the gross margin ratio.
gross_margin = (revenue − COGS) / revenue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
revenue
|
Union[int, float]
|
Total revenue. |
required |
cogs
|
Union[int, float]
|
Cost of goods sold. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Gross margin as a decimal. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If revenue is zero. |
Example
gross_margin(100000, 60000) 0.4
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
gross_profit_margin(revenue: Union[int, float], cost_of_goods_sold: Union[int, float]) -> float
¶
Calculate gross profit margin as a percentage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
revenue
|
Union[int, float]
|
Total revenue. |
required |
cost_of_goods_sold
|
Union[int, float]
|
Cost of goods sold. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Gross profit margin percentage. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If revenue is zero. |
Example
gross_profit_margin(200_000, 120_000) 40.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
growing_annuity_pv(payment: float, rate: float, growth: float, periods: int) -> float
¶
Present value of a growing annuity.
Description
PV = PMT / (r − g) × [1 − ((1+g)/(1+r))^n].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payment
|
float
|
First periodic payment (> 0). |
required |
rate
|
float
|
Discount rate per period (> 0). |
required |
growth
|
float
|
Growth rate per period (>= 0, must be != rate). |
required |
periods
|
int
|
Number of periods (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Present value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not of the expected type. |
ValueError
|
If constraints are violated. |
Usage Example
round(growing_annuity_pv(100, 0.10, 0.03, 20), 2) 1045.05
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
growing_perpetuity_pv(payment: float, rate: float, growth: float) -> float
¶
Present value of a growing perpetuity.
Description
PV = PMT / (r − g), where g < r.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payment
|
float
|
First periodic payment (> 0). |
required |
rate
|
float
|
Discount rate per period (> 0). |
required |
growth
|
float
|
Growth rate per period (>= 0, must be < rate). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Present value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If payment or rate are not positive, or growth >= rate. |
Usage Example
growing_perpetuity_pv(100, 0.10, 0.03) 1428.5714285714287
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
historical_var(returns: list[float], confidence: float = 0.95) -> float
¶
Historical Value at Risk (VaR) at a given confidence level.
Returns the loss threshold such that losses exceed this value with probability (1 − confidence).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
list[float]
|
List of periodic returns (e.g. daily log-returns). |
required |
confidence
|
float
|
Confidence level in (0, 1), default 0.95. |
0.95
|
Returns:
| Type | Description |
|---|---|
float
|
VaR as a positive float (loss amount). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If returns is empty or confidence is out of range. |
Usage Example
returns = [-0.02, -0.01, 0.0, 0.01, 0.02, -0.03, 0.015, 0.005, -0.005, 0.01] round(historical_var(returns, 0.9), 4) 0.03
Complexity: O(n log n)
Source code in shortfx/fxNumeric/finance_functions.py
holding_period_return(initial_value: float, final_value: float, income: float = 0.0) -> float
¶
Holding period return (HPR).
HPR = (final − initial + income) / initial.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_value
|
float
|
Initial investment value (> 0). |
required |
final_value
|
float
|
Final investment value (≥ 0). |
required |
income
|
float
|
Total income received during the period (≥ 0). |
0.0
|
Returns:
| Type | Description |
|---|---|
float
|
HPR as a float (e.g. 0.10 = 10%). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If constraints are violated. |
Usage Example
round(holding_period_return(1000, 1100, 50), 4) 0.15
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
implied_volatility(option_price: float, s: float, k: float, t: float, r: float, option_type: str = 'call', tol: float = 1e-06, max_iter: int = 100) -> float
¶
Estimates implied volatility via bisection (standalone, no scipy).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
option_price
|
float
|
Observed market price of the option. |
required |
s
|
float
|
Current price of the underlying asset. |
required |
k
|
float
|
Strike price. |
required |
t
|
float
|
Time to expiration in years. |
required |
r
|
float
|
Risk-free interest rate (annualised). |
required |
option_type
|
str
|
|
'call'
|
tol
|
float
|
Convergence tolerance. |
1e-06
|
max_iter
|
int
|
Maximum iterations. |
100
|
Returns:
| Type | Description |
|---|---|
float
|
Implied volatility (annualised). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If numeric inputs are not numeric. |
ValueError
|
If option_type is invalid or convergence fails. |
Example
round(implied_volatility(10.45, 100, 100, 1, 0.05, "call"), 2) 0.2
Complexity: O(max_iter)
Source code in shortfx/fxNumeric/finance_functions.py
increasing_annuity_pv(interest_rate: float, periods: int) -> float
¶
Present value of an increasing annuity-immediate paying k at time k.
(Iä)_n = (ä_n − n·v^n) / i where ä_n is the annuity-immediate PV of 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interest_rate
|
float
|
Interest rate per period (> 0). |
required |
periods
|
int
|
Number of periods (positive integer). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Present value per unit of increase as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument has the wrong type. |
ValueError
|
If numeric constraints are violated. |
Usage Example
round(increasing_annuity_pv(0.05, 10), 4) 39.3738
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
inflation_adjusted_value(amount: Union[int, float], rate: Union[int, float], years: Union[int, float]) -> float
¶
Calculate the inflation-adjusted (real) value of money.
real_value = amount / (1 + rate)^years
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
Union[int, float]
|
Nominal amount. |
required |
rate
|
Union[int, float]
|
Annual inflation rate as a decimal (e.g. 0.03 for 3 %). |
required |
years
|
Union[int, float]
|
Number of years. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Present real value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If rate is -1 (division by zero). |
Example
round(inflation_adjusted_value(1000, 0.03, 10), 2) 744.09
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
information_ratio(portfolio_return: Union[int, float], benchmark_return: Union[int, float], tracking_error: Union[int, float]) -> float
¶
Calculate the Information Ratio: (Rp - Rb) / TE.
Measures excess return per unit of tracking error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio_return
|
Union[int, float]
|
Portfolio return (decimal). |
required |
benchmark_return
|
Union[int, float]
|
Benchmark return (decimal). |
required |
tracking_error
|
Union[int, float]
|
Tracking error (std dev of excess returns). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Information ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If tracking_error is not positive. |
Example
information_ratio(0.12, 0.10, 0.04) 0.5
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
interest_burden_ratio(pre_tax_income: Union[int, float], ebit: Union[int, float]) -> float
¶
Return the interest burden ratio (DuPont component).
IBR = Pre-tax Income / EBIT. Values near 1 indicate low interest expense relative to operating earnings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pre_tax_income
|
Union[int, float]
|
Earnings before taxes. |
required |
ebit
|
Union[int, float]
|
Earnings before interest and taxes (must be ≠ 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Interest burden ratio as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If ebit is zero. |
Example
interest_burden_ratio(180_000, 200_000) 0.9
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
interest_coverage_ratio(ebit: Union[int, float], interest_expense: Union[int, float]) -> float
¶
Calculate the interest coverage ratio.
Measures a company's ability to pay interest on outstanding debt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ebit
|
Union[int, float]
|
Earnings before interest and taxes. |
required |
interest_expense
|
Union[int, float]
|
Total interest expense. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Interest coverage ratio as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If interest_expense is zero. |
Example
interest_coverage_ratio(500000, 100000) 5.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
intrate(settlement: str, maturity: str, investment: float, redemption: float, basis: int = 0) -> float
¶
Interest rate for a fully invested security.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
investment
|
float
|
Amount invested. |
required |
redemption
|
float
|
Amount received at maturity. |
required |
basis
|
int
|
Day-count basis (0-4). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Interest rate. |
Example
round(intrate("2008-02-15", "2008-05-15", 1000000, 1014420, 2), 6) 0.058328
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
inventory_to_sales(inventory: Union[int, float], net_sales: Union[int, float]) -> float
¶
Return the inventory-to-sales ratio.
Measures the proportion of inventory relative to net sales. A lower ratio indicates faster inventory turnover.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inventory
|
Union[int, float]
|
Inventory value (must be ≥ 0). |
required |
net_sales
|
Union[int, float]
|
Net sales (must be > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Inventory-to-sales ratio as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If net_sales is zero or negative. |
Example
inventory_to_sales(200_000, 1_000_000) 0.2
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
inventory_turnover(cogs: Union[int, float], average_inventory: Union[int, float]) -> float
¶
Calculate the inventory turnover ratio.
Measures how many times inventory is sold and replaced over a period.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cogs
|
Union[int, float]
|
Cost of goods sold. |
required |
average_inventory
|
Union[int, float]
|
Average inventory during the period. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Inventory turnover ratio as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If average_inventory is zero or negative. |
Example
inventory_turnover(600000, 100000) 6.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
inventory_turnover_ratio(cost_of_goods_sold: Union[int, float], average_inventory: Union[int, float]) -> float
¶
Calculate inventory turnover ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cost_of_goods_sold
|
Union[int, float]
|
COGS for the period. |
required |
average_inventory
|
Union[int, float]
|
Average inventory value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Inventory turnover ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If average_inventory is zero. |
Example
inventory_turnover_ratio(500_000, 100_000) 5.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
ipmt(rate: float, per: Union[int, float], nper: Union[int, float], pv: float, fv: float = 0.0, type_: int = 0) -> float
¶
Calculates the interest portion of a payment for a given period.
Uses the standard TVM formula to compute each periodic payment, then isolates the interest component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
Interest rate per period. |
required |
per
|
Union[int, float]
|
The period for which to find interest (1-based). |
required |
nper
|
Union[int, float]
|
Total number of periods. |
required |
pv
|
float
|
Present value (principal). |
required |
fv
|
float
|
Future value (default 0). |
0.0
|
type_
|
int
|
0 = end of period (ordinary), 1 = beginning (annuity due). |
0
|
Returns:
| Type | Description |
|---|---|
float
|
Interest portion of the payment for the specified period. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If per < 1 or per > nper. |
Example
round(ipmt(0.1/12, 1, 36, 8000), 2) -66.67
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
irr(cash_flows: List[float], guess: float = 0.1) -> float
¶
Calculates the Internal Rate of Return (IRR) for a series of cash flows.
The IRR is the discount rate that makes the Net Present Value (NPV) of all cash flows from a particular project equal to zero. This function uses an iterative method to find the IRR.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cash_flows
|
List[float]
|
A list of cash flows, where the first cash flow is typically the initial investment (negative), followed by positive future cash inflows. |
required |
guess
|
float
|
An optional initial guess for the IRR. Defaults to 0.1 (10%). |
0.1
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The Internal Rate of Return. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If cash_flows is not a list or contains non-numeric values, or guess is not numeric. |
ValueError
|
If cash_flows is empty, all cash flows are positive or negative, or the function fails to converge. |
Example
Initial investment of -$100, followed by $20, $30, $40, $50, $60¶
irr([-100, 20, 30, 40, 50, 60]) 0.2809484834920677
Cost: O(k*n), where k is the number of iterations (up to 1000) and n is the length of cash_flows. Iterative method.
Source code in shortfx/fxNumeric/finance_functions.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | |
ispmt(rate: float, per: int, nper: int, pv: float) -> float
¶
Interest paid during a specific period of an investment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
Interest rate per period. |
required |
per
|
int
|
Period for which to calculate interest (1-based). |
required |
nper
|
int
|
Total number of periods. |
required |
pv
|
float
|
Present value (principal). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Interest paid in the period. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If per < 1 or per > nper. |
Example
round(ispmt(0.1/12, 1, 36, 8000000), 2) -64814.81
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
kelly_criterion(win_probability: Union[int, float], win_loss_ratio: Union[int, float]) -> float
¶
Calculate the Kelly criterion: f* = p - (1-p)/b.
Determines the optimal fraction of capital to wager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
win_probability
|
Union[int, float]
|
Probability of winning (0 < p < 1). |
required |
win_loss_ratio
|
Union[int, float]
|
Ratio of average win to average loss (b > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Optimal fraction of bankroll to bet (can be negative → don't bet). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If win_probability not in (0,1) or win_loss_ratio not positive. |
Example
kelly_criterion(0.6, 2.0) 0.4
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
leverage_ratio(total_assets: Union[int, float], total_equity: Union[int, float]) -> float
¶
Calculate the leverage ratio (equity multiplier): Assets / Equity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_assets
|
Union[int, float]
|
Total assets. |
required |
total_equity
|
Union[int, float]
|
Total equity. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Leverage ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If total_equity is not positive. |
Example
leverage_ratio(500_000, 200_000) 2.5
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
levered_beta(asset_beta: float, tax_rate: float, de_ratio: float) -> float
¶
Calculate levered (equity) beta using the inverse Hamada equation.
Description
β_L = β_u × (1 + (1 − tax_rate) × D/E). Adds the financial leverage effect to the asset beta.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
asset_beta
|
float
|
Unlevered (asset) beta. |
required |
tax_rate
|
float
|
Corporate tax rate (0–1). |
required |
de_ratio
|
float
|
Debt-to-equity ratio. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Levered beta. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
Usage Example
round(levered_beta(0.8889, 0.30, 0.5), 2) 1.2
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
life_annuity_pv(annual_payment: float, interest_rate: float, mortality_rates: list[float]) -> float
¶
Present value of a life annuity-due using discrete mortality rates.
Computes Σ_k (payment × v^k × _kp_x) where v = 1/(1+i) and _kp_x is the probability of surviving k years from the given mortality vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annual_payment
|
float
|
Payment per period (> 0). |
required |
interest_rate
|
float
|
Annual effective interest rate (> 0). |
required |
mortality_rates
|
list[float]
|
List of one-year mortality probabilities q_x, q_{x+1}, … for successive ages. Each value must be in [0, 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Present value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument has the wrong type. |
ValueError
|
If numeric constraints are violated. |
Usage Example
round(life_annuity_pv(1000, 0.05, [0.01, 0.02, 0.03]), 2) 3635.81
Complexity: O(n) where n = len(mortality_rates)
Source code in shortfx/fxNumeric/finance_functions.py
loan_amortization_table(principal: float, annual_rate: float, periods: int) -> list[dict]
¶
Generates a loan amortization table with fixed payments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
principal
|
float
|
Loan principal. |
required |
annual_rate
|
float
|
Annual interest rate (e.g. 0.06 for 6%). |
required |
periods
|
int
|
Number of monthly payment periods. |
required |
Returns:
| Type | Description |
|---|---|
list[dict]
|
A list of dicts with keys |
list[dict]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If principal <= 0, annual_rate < 0 or periods < 1. |
Example
table = loan_amortization_table(1200, 0.12, 3) table[0]["payment"] 481.2
Complexity: O(periods)
Source code in shortfx/fxNumeric/finance_functions.py
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 | |
loan_to_value(loan_amount: float, appraised_value: float) -> float
¶
Calculate the Loan-to-Value (LTV) ratio.
LTV = loan_amount / appraised_value * 100.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loan_amount
|
float
|
Outstanding loan balance. |
required |
appraised_value
|
float
|
Current appraised value of the asset. |
required |
Returns:
| Type | Description |
|---|---|
float
|
LTV ratio as a percentage. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If appraised_value is not positive. |
Example
loan_to_value(180_000, 200_000) 90.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
macaulay_duration_simple(coupon_rate: float, ytm: float, periods: int) -> float
¶
Calculates Macaulay duration from coupon rate and yield to maturity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coupon_rate
|
float
|
Annual coupon rate (e.g. 0.05 for 5%). |
required |
ytm
|
float
|
Yield to maturity per period (e.g. 0.05). |
required |
periods
|
int
|
Number of remaining coupon periods. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Macaulay duration in periods. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs have wrong types. |
ValueError
|
If periods is not positive or ytm is negative. |
Example
round(macaulay_duration_simple(0.05, 0.05, 10), 4) 7.1078
Complexity: O(n) where n = periods
Source code in shortfx/fxNumeric/finance_functions.py
margin_of_safety_pct(actual_sales: Union[int, float], breakeven_sales: Union[int, float]) -> float
¶
Calculate the margin of safety as a percentage.
MoS% = (Actual - Breakeven) / Actual × 100
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actual_sales
|
Union[int, float]
|
Actual or expected sales. |
required |
breakeven_sales
|
Union[int, float]
|
Break-even sales level. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Margin of safety percentage. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If actual_sales is not positive. |
Example
margin_of_safety_pct(200_000, 150_000) 25.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
marginal_cost(change_in_total_cost: Union[int, float], change_in_quantity: Union[int, float]) -> float
¶
Return the marginal cost of production.
Marginal Cost = ΔTotal Cost / ΔQuantity. Indicates the cost of producing one additional unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
change_in_total_cost
|
Union[int, float]
|
Change in total production cost. |
required |
change_in_quantity
|
Union[int, float]
|
Change in quantity produced (must be ≠ 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Marginal cost as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If change_in_quantity is zero. |
Example
marginal_cost(500, 100) 5.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
markdown_percentage(original_price: Union[int, float], sale_price: Union[int, float]) -> float
¶
Calculate markdown (discount) percentage.
markdown = (original_price - sale_price) / original_price × 100
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_price
|
Union[int, float]
|
Original price before discount. |
required |
sale_price
|
Union[int, float]
|
Discounted selling price. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Discount as a percentage. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If original_price is zero. |
Example
markdown_percentage(100, 75) 25.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
market_cap(share_price: Union[int, float], shares_outstanding: Union[int, float]) -> float
¶
Calculate market capitalisation: Price × Shares Outstanding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
share_price
|
Union[int, float]
|
Current share price. |
required |
shares_outstanding
|
Union[int, float]
|
Total shares outstanding. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Market capitalisation. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If either value is not positive. |
Example
market_cap(150, 1_000_000) 150000000.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
markup_percentage(cost: Union[int, float], selling_price: Union[int, float]) -> float
¶
Calculate markup percentage (margin over cost).
markup = (selling_price - cost) / cost × 100
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cost
|
Union[int, float]
|
Unit cost. |
required |
selling_price
|
Union[int, float]
|
Selling price. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Markup as a percentage. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If cost is zero. |
Example
markup_percentage(50, 80) 60.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
mduration(settlement: str, maturity: str, coupon: float, yld: float, frequency: int, basis: int = 0) -> float
¶
Modified duration for a bond with par value $100.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
coupon
|
float
|
Annual coupon rate. |
required |
yld
|
float
|
Annual yield. |
required |
frequency
|
int
|
Coupon payments per year (1, 2, or 4). |
required |
basis
|
int
|
Day-count basis (0-4). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Modified Macaulay duration. |
Example
round(mduration("2008-01-01", "2016-01-01", 0.08, 0.09, 2, 1), 4) 5.7356
Complexity: O(n) where n = number of coupons
Source code in shortfx/fxNumeric/finance_functions.py
mirr(cash_flows: List[float], finance_rate: float, reinvest_rate: float) -> float
¶
Calculates the Modified Internal Rate of Return.
MIRR resolves IRR's multiple-root issue by separating the finance rate (for negative flows) from the reinvestment rate (for positive flows).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cash_flows
|
List[float]
|
List of cash flows (first is typically negative). |
required |
finance_rate
|
float
|
Interest rate paid on borrowed money. |
required |
reinvest_rate
|
float
|
Return rate on reinvested positive flows. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The MIRR as a decimal. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If cash_flows has < 2 values or no sign change. |
Example
round(mirr([-100, 50, 60, 70], 0.10, 0.12), 4) 0.2598
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 | |
modified_dietz_return(start_value: float, end_value: float, cash_flows: list, cf_days: list, total_days: int) -> float
¶
Calculate the Modified Dietz rate of return.
Description
A money-weighted approximation that adjusts for the timing of cash flows: R = (V1 - V0 - CF) / (V0 + Σ(CFi × Wi)), where Wi = (total_days - dayi) / total_days.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_value
|
float
|
Portfolio value at start. |
required |
end_value
|
float
|
Portfolio value at end. |
required |
cash_flows
|
list
|
List of cash flow amounts (positive = inflow). |
required |
cf_days
|
list
|
Day number of each cash flow (0 = start). |
required |
total_days
|
int
|
Total days in the period (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Modified Dietz return as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If numeric arguments are not numeric or lists are wrong. |
ValueError
|
If lists differ in length or total_days <= 0. |
Usage Example
modified_dietz_return(1000, 1200, [100], [15], 30) 0.09090909090909091
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 | |
modified_duration_simple(mac_duration: float, ytm: float, frequency: int = 1) -> float
¶
Calculates modified duration from Macaulay duration.
Modified duration = Macaulay duration / (1 + ytm / frequency).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mac_duration
|
float
|
Macaulay duration in periods. |
required |
ytm
|
float
|
Yield to maturity (annualised). |
required |
frequency
|
int
|
Coupon payments per year (1, 2, 4, 12). |
1
|
Returns:
| Type | Description |
|---|---|
float
|
Modified duration. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs have wrong types. |
ValueError
|
If frequency is not positive. |
Example
round(modified_duration_simple(7.1078, 0.05, 1), 4) 6.7693
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
money_weighted_return(cashflows: list[tuple[float, float]], guess: float = 0.05, tolerance: float = 1e-09, max_iterations: int = 200) -> float
¶
Money-weighted rate of return (IRR of investor cash flows).
Solves Σ CF_i / (1+r)^{t_i} = 0 via Newton-Raphson.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cashflows
|
list[tuple[float, float]]
|
List of (time, amount) tuples. At least one positive and one negative amount required. Times in years. |
required |
guess
|
float
|
Initial rate guess (default 0.05). |
0.05
|
tolerance
|
float
|
Convergence tolerance (default 1e-9). |
1e-09
|
max_iterations
|
int
|
Maximum iterations (default 200). |
200
|
Returns:
| Type | Description |
|---|---|
float
|
Money-weighted return as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If cashflows don't have both signs or convergence fails. |
Usage Example
cfs = [(0, -1000), (0.5, 50), (1.0, 1080)] round(money_weighted_return(cfs), 4) 0.1332
Complexity: O(n × max_iterations)
Source code in shortfx/fxNumeric/finance_functions.py
8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 | |
months_of_runway(cash_available: Union[int, float], monthly_burn_rate: Union[int, float]) -> float
¶
Calculate financial runway in months.
How many months a company can operate before running out of cash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cash_available
|
Union[int, float]
|
Current available cash. |
required |
monthly_burn_rate
|
Union[int, float]
|
Average monthly cash burn (positive). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Number of months of runway. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If monthly_burn_rate is not positive. |
Example
months_of_runway(600000, 100000) 6.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
mortgage_remaining_balance(principal: float, annual_rate: float, years: int, payments_made: int) -> float
¶
Calculates the remaining balance of a fixed-rate mortgage after N payments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
principal
|
float
|
Original loan principal. |
required |
annual_rate
|
float
|
Annual interest rate. |
required |
years
|
int
|
Original loan term in years. |
required |
payments_made
|
int
|
Number of monthly payments already made. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Remaining loan balance. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs have wrong types. |
ValueError
|
If payments_made exceeds total payments or inputs are not positive. |
Example
round(mortgage_remaining_balance(200_000, 0.035, 30, 60), 2) 178779.95
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
mortgage_total_cost(principal: float, annual_rate: float, years: int) -> float
¶
Calculates the total cost of a fixed-rate mortgage (principal + interest).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
principal
|
float
|
Loan principal amount. |
required |
annual_rate
|
float
|
Annual interest rate (e.g. 0.035 for 3.5%). |
required |
years
|
int
|
Loan term in years. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Total amount paid over the life of the loan. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs have wrong types. |
ValueError
|
If principal, annual_rate, or years are not positive. |
Example
round(mortgage_total_cost(200_000, 0.035, 30), 2) 323312.18
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
net_asset_value(total_assets: float, total_liabilities: float, shares: float) -> float
¶
Calculate Net Asset Value (NAV) per share.
Description
NAV = (Total Assets − Total Liabilities) / Shares Outstanding. Common in mutual fund and ETF valuation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_assets
|
float
|
Total assets of the fund. |
required |
total_liabilities
|
float
|
Total liabilities of the fund. |
required |
shares
|
float
|
Number of shares outstanding. |
required |
Returns:
| Type | Description |
|---|---|
float
|
NAV per share. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If shares is zero. |
Usage Example
net_asset_value(1_000_000, 200_000, 10_000) 80.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
net_profit_margin(net_income: Union[int, float], revenue: Union[int, float]) -> float
¶
Calculate the net profit margin as a percentage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net_income
|
Union[int, float]
|
Net income after all expenses. |
required |
revenue
|
Union[int, float]
|
Total revenue. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Net profit margin as a percentage. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If revenue is zero. |
Example
net_profit_margin(25000, 100000) 25.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
nominal(effect_rate: float, npery: int) -> float
¶
Calculates the nominal annual interest rate from effective rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
effect_rate
|
float
|
Effective annual interest rate. |
required |
npery
|
int
|
Number of compounding periods per year. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The nominal annual rate. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If effect_rate <= 0 or npery < 1. |
Example
round(nominal(0.103813, 4), 6) 0.1
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
nominal_to_real_rate(nominal_rate: Union[int, float], inflation_rate: Union[int, float]) -> float
¶
Convert nominal interest rate to real rate using the Fisher equation.
real ≈ (1 + nominal) / (1 + inflation) - 1
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nominal_rate
|
Union[int, float]
|
Nominal interest rate as a decimal. |
required |
inflation_rate
|
Union[int, float]
|
Inflation rate as a decimal. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Real interest rate as a decimal. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If inflation_rate is -1 (division by zero). |
Example
round(nominal_to_real_rate(0.08, 0.03), 6) 0.048544
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
nopat_margin(nopat: Union[int, float], revenue: Union[int, float]) -> float
¶
Return the NOPAT margin.
NOPAT Margin = Net Operating Profit After Taxes / Revenue. Indicates operational profitability after taxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nopat
|
Union[int, float]
|
Net Operating Profit After Taxes. |
required |
revenue
|
Union[int, float]
|
Total revenue (must be > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
NOPAT margin as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If revenue is zero or negative. |
Example
nopat_margin(150_000, 1_000_000) 0.15
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
nper(rate: float, pmt: float, pv: float, fv: float = 0.0, type: int = 0) -> float
¶
Calculates the number of periods for an investment based on periodic, constant payments and a constant interest rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
The interest rate per period. |
required |
pmt
|
float
|
The payment made each period. |
required |
pv
|
float
|
The Present Value. |
required |
fv
|
float
|
The Future Value. Defaults to 0.0. |
0.0
|
type
|
int
|
When payments are due (0 for end, 1 for beginning). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The number of periods. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any input is not a number or if 'type' is not 0 or 1. |
ValueError
|
If pmt is 0 and rate is not 0, or if arguments lead to invalid log. |
Example
Number of months to pay off a $10,000 loan with $100 monthly payments at 5% annual interest.¶
Monthly rate = 0.05 / 12¶
round(nper(rate=0.05/12, pmt=-100, pv=10000), 2) 122.09
Cost: O(1), direct logarithmic calculation.
Source code in shortfx/fxNumeric/finance_functions.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
npv(rate: float, values: List[float]) -> float
¶
Calculates the Net Present Value (NPV) of an investment.
NPV is the sum of the present values of the individual cash flows. The initial investment (cash outflow) should be the first value in the 'values' list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
The discount rate per period (e.g., 0.05 for 5%). |
required |
values
|
List[float]
|
A list of cash flows, starting with the initial investment. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The Net Present Value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If 'rate' is not numeric or 'values' is not a list of numbers. |
ValueError
|
If 'values' is empty. |
Example
NPV of initial -$100 investment, followed by $20, $30, $40, $50, $60 at 10% discount rate.¶
round(npv(rate=0.10, values=[-100, 20, 30, 40, 50, 60]), 2) 60.85
Cost: O(n), iterates over all cash flows.
Source code in shortfx/fxNumeric/finance_functions.py
omega_ratio(returns: list, threshold: float = 0.0) -> float
¶
Calculate the Omega ratio of a return series.
Description
Omega = Σmax(Ri - threshold, 0) / Σmax(threshold - Ri, 0). A higher omega indicates a more favourable return distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
list
|
List of periodic returns (as decimals). |
required |
threshold
|
float
|
Minimum acceptable return (default 0.0). |
0.0
|
Returns:
| Type | Description |
|---|---|
float
|
Omega ratio (positive float). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not the correct type. |
ValueError
|
If returns is empty or all returns are below threshold. |
Usage Example
omega_ratio([0.05, 0.02, -0.01, 0.03, -0.02], 0.0) 2.0
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
operating_cash_flow_ratio(operating_cash_flow: Union[int, float], current_liabilities: Union[int, float]) -> float
¶
Calculate the Operating Cash Flow Ratio.
Measures how well current liabilities are covered by cash flow from operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operating_cash_flow
|
Union[int, float]
|
Cash flow from operations. |
required |
current_liabilities
|
Union[int, float]
|
Total current liabilities. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Operating cash flow ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If current_liabilities is zero or negative. |
Example
operating_cash_flow_ratio(250000, 200000) 1.25
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
operating_expense_ratio(operating_expenses: Union[int, float], net_sales: Union[int, float]) -> float
¶
Calculate the operating expense ratio (OER).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operating_expenses
|
Union[int, float]
|
Total operating expenses. |
required |
net_sales
|
Union[int, float]
|
Net sales revenue. |
required |
Returns:
| Type | Description |
|---|---|
float
|
OER as a decimal ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If net_sales is zero. |
Example
operating_expense_ratio(80_000, 200_000) 0.4
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
operating_leverage_degree(contribution_margin_value: Union[int, float], operating_income: Union[int, float]) -> float
¶
Calculate the Degree of Operating Leverage: DOL = CM / EBIT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contribution_margin_value
|
Union[int, float]
|
Total contribution margin. |
required |
operating_income
|
Union[int, float]
|
Operating income (EBIT). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Degree of operating leverage. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If operating_income is zero. |
Example
operating_leverage_degree(100_000, 40_000) 2.5
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
operating_margin(operating_income: Union[int, float], revenue: Union[int, float]) -> float
¶
Computes the operating margin ratio.
operating_margin = operating_income / revenue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operating_income
|
Union[int, float]
|
Operating income (EBIT). |
required |
revenue
|
Union[int, float]
|
Total revenue. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Operating margin as a decimal. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If revenue is zero. |
Example
operating_margin(25000, 100000) 0.25
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
operating_profit_margin(operating_income: Union[int, float], revenue: Union[int, float]) -> float
¶
Return the operating profit margin.
OPM = Operating Income / Revenue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operating_income
|
Union[int, float]
|
Operating income (EBIT). |
required |
revenue
|
Union[int, float]
|
Total revenue (must be > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Operating profit margin as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If revenue is zero or negative. |
Example
operating_profit_margin(200_000, 1_000_000) 0.2
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
option_charm_bs(s: float, k: float, t: float, r: float, sigma: float, option_type: str = 'call') -> float
¶
Black-Scholes charm (delta decay): rate of change of delta w.r.t. time.
Charm = −∂²C/∂S∂t. Useful for hedging adjustments between rebalances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
float
|
Current spot price (> 0). |
required |
k
|
float
|
Strike price (> 0). |
required |
t
|
float
|
Time to expiration in years (> 0). |
required |
r
|
float
|
Risk-free interest rate. |
required |
sigma
|
float
|
Annualised volatility (> 0). |
required |
option_type
|
str
|
|
'call'
|
Returns:
| Type | Description |
|---|---|
float
|
Charm value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If numeric args are not numeric or option_type not str. |
ValueError
|
If constraints are violated. |
Usage Example
round(option_charm_bs(100, 100, 1.0, 0.05, 0.2), 6) -0.065667
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
option_color_bs(s: float, k: float, t: float, r: float, sigma: float) -> float
¶
Black-Scholes color: rate of change of gamma w.r.t. time (∂Γ/∂t).
Color = −N'(d1)/(2·S·t·σ√t) × [2rt + 1 + d1×(2(r−q)t − d2σ√t)/(σ√t)] with q = 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
float
|
Current spot price (> 0). |
required |
k
|
float
|
Strike price (> 0). |
required |
t
|
float
|
Time to expiration in years (> 0). |
required |
r
|
float
|
Risk-free interest rate. |
required |
sigma
|
float
|
Annualised volatility (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Color value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If s, k, t, or sigma are not positive. |
Usage Example
abs(option_color_bs(100, 100, 1.0, 0.05, 0.2)) < 1 True
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
option_delta_bs(s: float, k: float, t: float, r: float, sigma: float, option_type: str = 'call') -> float
¶
Black-Scholes delta for a European option.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
float
|
Current underlying price. |
required |
k
|
float
|
Strike price. |
required |
t
|
float
|
Time to expiry in years. |
required |
r
|
float
|
Risk-free rate. |
required |
sigma
|
float
|
Volatility (annualised). |
required |
option_type
|
str
|
|
'call'
|
Returns:
| Type | Description |
|---|---|
float
|
Delta value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If numeric inputs are wrong type. |
ValueError
|
If s, k, t, sigma <= 0 or bad option_type. |
Example
round(option_delta_bs(100, 100, 1, 0.05, 0.2), 4) 0.6368
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
option_gamma_bs(s: float, k: float, t: float, r: float, sigma: float) -> float
¶
Black-Scholes gamma for a European option.
Gamma is the same for calls and puts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
float
|
Current underlying price. |
required |
k
|
float
|
Strike price. |
required |
t
|
float
|
Time to expiry in years. |
required |
r
|
float
|
Risk-free rate. |
required |
sigma
|
float
|
Volatility (annualised). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Gamma value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If numeric inputs are wrong type. |
ValueError
|
If s, k, t, sigma <= 0. |
Example
round(option_gamma_bs(100, 100, 1, 0.05, 0.2), 6) 0.018762
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
option_rho_bs(s: float, k: float, t: float, r: float, sigma: float, option_type: str = 'call') -> float
¶
Black-Scholes rho: sensitivity to the risk-free interest rate.
Description
Rho_call = K T e^{−rT} Φ(d₂) Rho_put = −K T e^{−rT} Φ(−d₂)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
float
|
Current underlying asset price (> 0). |
required |
k
|
float
|
Strike price (> 0). |
required |
t
|
float
|
Time to expiration in years (> 0). |
required |
r
|
float
|
Risk-free interest rate (annualised, as decimal). |
required |
sigma
|
float
|
Volatility of the underlying (annualised, > 0). |
required |
option_type
|
str
|
|
'call'
|
Returns:
| Type | Description |
|---|---|
float
|
Rho as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any numeric argument is not numeric. |
ValueError
|
If s, k, t, or sigma are not positive. |
ValueError
|
If option_type is not 'call' or 'put'. |
Usage Example
round(option_rho_bs(100, 100, 1.0, 0.05, 0.2), 4) 53.2325
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
option_speed_bs(s: float, k: float, t: float, r: float, sigma: float) -> float
¶
Black-Scholes speed: rate of change of gamma w.r.t. spot price (∂³C/∂S³).
Speed = −Gamma/S × (d1/(σ√t) + 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
float
|
Current spot price (> 0). |
required |
k
|
float
|
Strike price (> 0). |
required |
t
|
float
|
Time to expiration in years (> 0). |
required |
r
|
float
|
Risk-free interest rate. |
required |
sigma
|
float
|
Annualised volatility (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Speed value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If s, k, t, or sigma are not positive. |
Usage Example
round(option_speed_bs(100, 100, 1.0, 0.05, 0.2), 6) -0.000516
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
option_theta_bs(s: float, k: float, t: float, r: float, sigma: float, option_type: str = 'call') -> float
¶
Black-Scholes theta: time decay of an option per year.
Description
Theta_call = −S φ(d₁) σ / (2√T) − r K e^{−rT} Φ(d₂) Theta_put = −S φ(d₁) σ / (2√T) + r K e^{−rT} Φ(−d₂)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
float
|
Current underlying asset price (> 0). |
required |
k
|
float
|
Strike price (> 0). |
required |
t
|
float
|
Time to expiration in years (> 0). |
required |
r
|
float
|
Risk-free interest rate (annualised, as decimal). |
required |
sigma
|
float
|
Volatility of the underlying (annualised, > 0). |
required |
option_type
|
str
|
|
'call'
|
Returns:
| Type | Description |
|---|---|
float
|
Theta (per year) as a float. Divide by 365 for daily theta. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any numeric argument is not numeric. |
ValueError
|
If s, k, t, or sigma are not positive. |
ValueError
|
If option_type is not 'call' or 'put'. |
Usage Example
round(option_theta_bs(100, 100, 1.0, 0.05, 0.2), 4) -6.4140
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
option_vanna_bs(s: float, k: float, t: float, r: float, sigma: float) -> float
¶
Black-Scholes vanna: sensitivity of delta to volatility (∂²C/∂S∂σ).
Vanna = (−d2 / σ) × N'(d1) × e^{−qt}/S (with q=0).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
float
|
Current spot price (> 0). |
required |
k
|
float
|
Strike price (> 0). |
required |
t
|
float
|
Time to expiration in years (> 0). |
required |
r
|
float
|
Risk-free interest rate. |
required |
sigma
|
float
|
Annualised volatility (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Vanna value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If s, k, t, or sigma are not positive. |
Usage Example
round(option_vanna_bs(100, 100, 1.0, 0.05, 0.2), 6) -0.281430
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
option_vega_bs(s: float, k: float, t: float, r: float, sigma: float) -> float
¶
Black-Scholes vega: sensitivity of option price to volatility.
Description
Vega = S √T φ(d₁), where φ is the standard normal PDF. The result is the change in option price per unit change in volatility. Vega is the same for calls and puts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
float
|
Current underlying asset price (> 0). |
required |
k
|
float
|
Strike price (> 0). |
required |
t
|
float
|
Time to expiration in years (> 0). |
required |
r
|
float
|
Risk-free interest rate (annualised, as decimal). |
required |
sigma
|
float
|
Volatility of the underlying (annualised, > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Vega as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If s, k, t, or sigma are not positive. |
Usage Example
round(option_vega_bs(100, 100, 1.0, 0.05, 0.2), 4) 37.5245
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
option_zomma_bs(s: float, k: float, t: float, r: float, sigma: float) -> float
¶
Black-Scholes zomma: rate of change of gamma w.r.t. volatility.
Zomma = Gamma × (d1×d2 − 1) / σ.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
float
|
Current spot price (> 0). |
required |
k
|
float
|
Strike price (> 0). |
required |
t
|
float
|
Time to expiration in years (> 0). |
required |
r
|
float
|
Risk-free interest rate. |
required |
sigma
|
float
|
Annualised volatility (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Zomma value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If s, k, t, or sigma are not positive. |
Usage Example
round(option_zomma_bs(100, 100, 1.0, 0.05, 0.2), 6) -0.088885
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
parkinson_volatility(high_low: list[tuple[float, float]]) -> float
¶
Parkinson volatility estimator from high-low prices.
σ²_P = (1 / (4n·ln2)) Σ ln(H/L)².
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
high_low
|
list[tuple[float, float]]
|
List of (high, low) tuples. Each price must be > 0, high ≥ low. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Annualised volatility estimate (assuming 252 trading days). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If high_low is not a list or elements are wrong. |
ValueError
|
If list is empty or constraints are violated. |
Usage Example
bars = [(105, 98), (107, 101), (110, 104)] round(parkinson_volatility(bars), 4) > 0 True
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
payback_period(initial_investment: float, cash_flows: List[float]) -> float
¶
Calculates the simple payback period of an investment.
The payback period is the time required for cumulative cash flows to equal the initial investment. Returns a fractional period value using linear interpolation within the break-even period.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_investment
|
float
|
Positive upfront cost. |
required |
cash_flows
|
List[float]
|
List of periodic cash inflows. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Number of periods to recover the investment. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If the investment is never recovered. |
Example
payback_period(1000, [300, 400, 400, 200]) 2.75
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
pduration(rate: float, pv: float, fv: float) -> float
¶
Calculates the number of periods to reach a target future value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
Interest rate per period (must be > 0). |
required |
pv
|
float
|
Present value (must be > 0). |
required |
fv
|
float
|
Target future value (must be > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Number of periods needed. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If rate, pv, or fv are not positive. |
Example
round(pduration(0.05, 1000, 2000), 4) 14.2067
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
plowback_ratio(dividends_per_share: Union[int, float], earnings_per_share: Union[int, float]) -> float
¶
Calculate the plowback (retention) ratio from per-share values.
plowback = 1 − (DPS / EPS)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dividends_per_share
|
Union[int, float]
|
Dividends per share. |
required |
earnings_per_share
|
Union[int, float]
|
Earnings per share. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Plowback ratio in [0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If earnings_per_share is zero. |
Example
plowback_ratio(2, 5) 0.6
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
pmt(rate: float, nper: Union[int, float], pv: float, fv: float = 0.0, type: int = 0) -> float
¶
Calculates the payment for a loan based on constant payments and a constant interest rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
The interest rate per period. |
required |
nper
|
Union[int, float]
|
The total number of payments for the loan. |
required |
pv
|
float
|
The Present Value, or the principal amount of the loan. This is typically positive (cash inflow to you). |
required |
fv
|
float
|
The Future Value, or the cash balance you want after the last payment. Defaults to 0.0 (loan fully paid off). |
0.0
|
type
|
int
|
When payments are due. 0 = at the end of the period (default). 1 = at the beginning of the period. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The payment made each period (will be negative as cash outflow). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any input is not a number or if 'type' is not 0 or 1. |
ValueError
|
If 'nper' is not positive or if denominator for rate=0 becomes 0. |
Example
Monthly payment for a $100,000 loan at 5% annual interest for 30 years.¶
Monthly rate = 0.05 / 12, NPER = 30 * 12 = 360¶
round(pmt(rate=0.05/12, nper=360, pv=100000), 2) -536.82
Cost: O(1), direct periodic payment formula calculation.
Source code in shortfx/fxNumeric/finance_functions.py
ppmt(rate: float, per: Union[int, float], nper: Union[int, float], pv: float, fv: float = 0.0, type_: int = 0) -> float
¶
Calculates the principal portion of a payment for a given period.
The principal portion is the total payment minus the interest portion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
Interest rate per period. |
required |
per
|
Union[int, float]
|
The period for which to find principal (1-based). |
required |
nper
|
Union[int, float]
|
Total number of periods. |
required |
pv
|
float
|
Present value (principal). |
required |
fv
|
float
|
Future value (default 0). |
0.0
|
type_
|
int
|
0 = end of period (ordinary), 1 = beginning (annuity due). |
0
|
Returns:
| Type | Description |
|---|---|
float
|
Principal portion of the payment for the specified period. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If per < 1 or per > nper. |
Example
round(ppmt(0.1/12, 1, 36, 8000), 2) -191.47
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
present_value(rate: float, nper: Union[int, float], pmt: float, fv: float = 0.0, type: int = 0) -> float
¶
Calculates the Present Value (PV) of an investment based on a series of future payments and a constant interest rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
The interest rate per period (e.g., 0.05 for 5% annual rate). |
required |
nper
|
Union[int, float]
|
The total number of payment periods in an investment. |
required |
pmt
|
float
|
The payment made each period. Payments are typically negative (cash outflow). |
required |
fv
|
float
|
The Future Value, or a cash balance you want to attain after the last payment. Defaults to 0.0. |
0.0
|
type
|
int
|
When payments are due. 0 = at the end of the period (default). 1 = at the beginning of the period. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The Present Value of the investment. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any input is not a number or if 'type' is not 0 or 1. |
ValueError
|
If 'nper' is negative. |
Example
PV of $1276.28 received in 5 years at 5% annual interest.¶
present_value(rate=0.05, nper=5, pmt=0, fv=1276.28) -999.9989807559194
PV of $100 payments received at end of each year for 5 years at 5% annual interest.¶
present_value(rate=0.05, nper=5, pmt=100, fv=0) -432.94766060133177
Cost: O(1), direct present value formula calculation.
Source code in shortfx/fxNumeric/finance_functions.py
present_value_perpetuity(payment: float, rate: float) -> float
¶
Present value of a perpetuity (infinite stream of equal payments).
Description
PV = PMT / r.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payment
|
float
|
Periodic payment amount (> 0). |
required |
rate
|
float
|
Discount rate per period (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Present value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If payment or rate are not positive. |
Usage Example
present_value_perpetuity(100, 0.05) 2000.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
price(settlement: str, maturity: str, rate: float, yld: float, redemption: float, frequency: int, basis: int = 0) -> float
¶
Price per $100 face of a security paying periodic interest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
rate
|
float
|
Annual coupon rate. |
required |
yld
|
float
|
Annual yield. |
required |
redemption
|
float
|
Redemption value per $100 face. |
required |
frequency
|
int
|
Coupon payments per year (1, 2, or 4). |
required |
basis
|
int
|
Day-count basis (0-4). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Price per $100 face value. |
Example
round(price("2008-02-15", "2017-11-15", 0.0575, 0.065, 100, 2, 0), 4) 94.6344
Complexity: O(n) where n = number of coupons
Source code in shortfx/fxNumeric/finance_functions.py
price_earnings_growth(pe_ratio: Union[int, float], earnings_growth_rate: Union[int, float]) -> float
¶
Return the Price/Earnings-to-Growth (PEG) ratio.
PEG = P/E Ratio / Earnings Growth Rate. A PEG < 1 may indicate the stock is undervalued relative to its growth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pe_ratio
|
Union[int, float]
|
Price-to-earnings ratio. |
required |
earnings_growth_rate
|
Union[int, float]
|
Expected earnings growth rate as a percentage (e.g. 15 for 15 %). Must be > 0. |
required |
Returns:
| Type | Description |
|---|---|
float
|
PEG ratio as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If earnings_growth_rate is zero or negative. |
Example
price_earnings_growth(20, 10) 2.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
price_to_book(market_price: Union[int, float], book_value_per_share: Union[int, float]) -> float
¶
Calculate the Price-to-Book (P/B) ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
market_price
|
Union[int, float]
|
Current market price per share. |
required |
book_value_per_share
|
Union[int, float]
|
Book value per share. |
required |
Returns:
| Type | Description |
|---|---|
float
|
P/B ratio as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If book_value_per_share is zero or negative. |
Example
price_to_book(50, 25) 2.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
price_to_earnings(market_price: Union[int, float], earnings_per_share_value: Union[int, float]) -> float
¶
Calculate the Price-to-Earnings (P/E) ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
market_price
|
Union[int, float]
|
Current market price per share. |
required |
earnings_per_share_value
|
Union[int, float]
|
Earnings per share (EPS). |
required |
Returns:
| Type | Description |
|---|---|
float
|
P/E ratio as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If earnings_per_share_value is zero. |
Example
price_to_earnings(150, 5) 30.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
price_to_sales(market_cap: Union[int, float], total_revenue: Union[int, float]) -> float
¶
Calculate the price-to-sales (P/S) ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
market_cap
|
Union[int, float]
|
Market capitalisation. |
required |
total_revenue
|
Union[int, float]
|
Total revenue for the period. |
required |
Returns:
| Type | Description |
|---|---|
float
|
P/S ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If total_revenue is zero. |
Example
price_to_sales(5_000_000, 2_000_000) 2.5
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
pricedisc(settlement: str, maturity: str, discount: float, redemption: float, basis: int = 0) -> float
¶
Price per $100 face of a discounted security.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
discount
|
float
|
Discount rate. |
required |
redemption
|
float
|
Redemption value per $100 face. |
required |
basis
|
int
|
Day-count basis (0-4). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Price per $100 face value. |
Example
round(pricedisc("2008-02-16", "2008-03-01", 0.0525, 100, 2), 4) 99.7958
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
pricemat(settlement: str, maturity: str, issue: str, rate: float, yld: float, basis: int = 0) -> float
¶
Price per $100 face of a security that pays at maturity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
issue
|
str
|
Issue date (YYYY-MM-DD). |
required |
rate
|
float
|
Annual coupon rate. |
required |
yld
|
float
|
Annual yield. |
required |
basis
|
int
|
Day-count basis (0-4). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Price per $100 face value. |
Example
round(pricemat("2008-02-15", "2008-04-13", "2007-11-11", 0.061, 0.061, 0), 4) 99.9844
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
profitability_index(rate: float, initial_investment: float, cash_flows: List[float]) -> float
¶
Calculates the Profitability Index (PI) of a project.
PI = PV(future cash flows) / initial investment. A PI > 1 indicates a potentially profitable investment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
Discount rate per period. |
required |
initial_investment
|
float
|
Positive upfront cost. |
required |
cash_flows
|
List[float]
|
List of future cash flows. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Profitability index as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If initial_investment <= 0. |
Example
round(profitability_index(0.10, 1000, [300, 400, 500, 200]), 4) 1.1156
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
purchasing_power(amount: Union[int, float], inflation_rate: Union[int, float], years: Union[int, float]) -> float
¶
Calculate remaining purchasing power after inflation erodes value.
pp = amount * (1 - inflation_rate)^years
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
Union[int, float]
|
Initial monetary amount. |
required |
inflation_rate
|
Union[int, float]
|
Annual inflation rate as a decimal. |
required |
years
|
Union[int, float]
|
Number of years. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Reduced purchasing power. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
Example
round(purchasing_power(1000, 0.03, 10), 2) 737.42
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
put_call_parity_check(call_price: float, put_price: float, s: float, k: float, r: float, t: float, tolerance: float = 0.01) -> bool
¶
Check if put-call parity holds within a tolerance.
Parity: C − P = S − K·e^{−rT}.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
call_price
|
float
|
Observed call option price (≥ 0). |
required |
put_price
|
float
|
Observed put option price (≥ 0). |
required |
s
|
float
|
Current spot price (> 0). |
required |
k
|
float
|
Strike price (> 0). |
required |
r
|
float
|
Risk-free interest rate. |
required |
t
|
float
|
Time to expiration in years (> 0). |
required |
tolerance
|
float
|
Maximum allowable deviation (default 0.01). |
0.01
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument has the wrong type. |
ValueError
|
If constraints are violated. |
Usage Example
put_call_parity_check(10.45, 5.57, 100, 100, 0.05, 1.0, 0.5) True
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
quick_ratio(current_assets: Union[int, float], inventories: Union[int, float], current_liabilities: Union[int, float]) -> float
¶
Calculate the quick (acid-test) ratio.
Measures a company's ability to meet short-term obligations with its most liquid assets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_assets
|
Union[int, float]
|
Total current assets. |
required |
inventories
|
Union[int, float]
|
Total inventories. |
required |
current_liabilities
|
Union[int, float]
|
Total current liabilities. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Quick ratio as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If current_liabilities is zero or negative. |
Example
quick_ratio(100000, 30000, 50000) 1.4
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
rate(nper: Union[int, float], pmt: float, pv: float, fv: float = 0.0, type: int = 0, guess: float = 0.1) -> float
¶
Calculates the interest rate per period of an annuity. This function uses an iterative approach (Newton-Raphson-like) to find the rate, as there's no direct algebraic solution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nper
|
Union[int, float]
|
The total number of payment periods. |
required |
pmt
|
float
|
The payment made each period. |
required |
pv
|
float
|
The Present Value. |
required |
fv
|
float
|
The Future Value. Defaults to 0.0. |
0.0
|
type
|
int
|
When payments are due (0 for end, 1 for beginning). |
0
|
guess
|
float
|
Your guess for the rate. Defaults to 0.1 (10%). |
0.1
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The interest rate per period. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any input is not a number or if 'type' is not 0 or 1. |
ValueError
|
If 'nper' is not positive or if the function fails to converge. |
Example
If you pay $536.82 monthly for 360 months on a $100,000 loan, what's the annual rate?¶
Result will be monthly rate, multiply by 12 for annual.¶
monthly_rate = rate(nper=360, pmt=-536.82, pv=100000) round(monthly_rate * 12, 4) 0.05
If you invest $1000 and it grows to $1276.28 in 5 years with no additional payments.¶
round(rate(nper=5, pmt=0, pv=-1000, fv=1276.28), 4) 0.05
Cost: O(k), where k is the number of iterations (up to 1000). Newton-Raphson/bisection iterative method.
Source code in shortfx/fxNumeric/finance_functions.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |
real_rate_of_return(nominal_rate: Union[int, float], inflation_rate: Union[int, float]) -> float
¶
Computes the real rate of return using the Fisher equation.
real = ((1 + nominal) / (1 + inflation)) − 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nominal_rate
|
Union[int, float]
|
Nominal interest rate as a decimal. |
required |
inflation_rate
|
Union[int, float]
|
Inflation rate as a decimal. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Real rate of return as a decimal. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If inflation_rate is −1 (division by zero). |
Example
round(real_rate_of_return(0.08, 0.03), 6) 0.048544
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
receivables_turnover(net_credit_sales: Union[int, float], average_receivables: Union[int, float]) -> float
¶
Calculate accounts receivable turnover ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net_credit_sales
|
Union[int, float]
|
Net credit sales for the period. |
required |
average_receivables
|
Union[int, float]
|
Average accounts receivable. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Receivables turnover ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If average_receivables is zero. |
Example
receivables_turnover(1_000_000, 125_000) 8.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
received(settlement: str, maturity: str, investment: float, discount: float, basis: int = 0) -> float
¶
Amount received at maturity for a fully invested security.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
investment
|
float
|
Amount invested. |
required |
discount
|
float
|
Discount rate. |
required |
basis
|
int
|
Day-count basis (0-4). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Amount received at maturity. |
Example
round(received("2008-02-15", "2008-05-15", 1000000, 0.0575, 2), 2) 1014584.65
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
retention_ratio(net_income: Union[int, float], dividends_paid: Union[int, float]) -> float
¶
Calculate the retention ratio (plowback ratio).
RR = (Net Income - Dividends) / Net Income
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net_income
|
Union[int, float]
|
Net income. |
required |
dividends_paid
|
Union[int, float]
|
Dividends paid. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Retention ratio as a decimal. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If net_income is not positive. |
Example
retention_ratio(100_000, 30_000) 0.7
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
return_on_assets(net_income: Union[int, float], total_assets: Union[int, float]) -> float
¶
Computes return on assets (ROA).
ROA = net_income / total_assets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net_income
|
Union[int, float]
|
Net income. |
required |
total_assets
|
Union[int, float]
|
Total assets. |
required |
Returns:
| Type | Description |
|---|---|
float
|
ROA as a decimal. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If total_assets is zero. |
Example
return_on_assets(50000, 500000) 0.1
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
return_on_capital_employed(ebit: Union[int, float], total_assets: Union[int, float], current_liabilities: Union[int, float]) -> float
¶
Calculate return on capital employed (ROCE).
ROCE = EBIT / (total_assets − current_liabilities)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ebit
|
Union[int, float]
|
Earnings before interest and taxes. |
required |
total_assets
|
Union[int, float]
|
Total assets. |
required |
current_liabilities
|
Union[int, float]
|
Current liabilities. |
required |
Returns:
| Type | Description |
|---|---|
float
|
ROCE as a decimal ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If capital employed is zero. |
Example
return_on_capital_employed(100_000, 800_000, 200_000) 0.16666666666666666
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
return_on_equity(net_income: Union[int, float], shareholders_equity: Union[int, float]) -> float
¶
Computes return on equity (ROE).
ROE = net_income / shareholders_equity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net_income
|
Union[int, float]
|
Net income. |
required |
shareholders_equity
|
Union[int, float]
|
Total shareholders' equity. |
required |
Returns:
| Type | Description |
|---|---|
float
|
ROE as a decimal. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If shareholders_equity is zero. |
Example
return_on_equity(50000, 200000) 0.25
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
return_on_net_assets(net_income: Union[int, float], fixed_assets: Union[int, float], working_capital: Union[int, float]) -> float
¶
Calculate return on net assets (RONA).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net_income
|
Union[int, float]
|
Net income for the period. |
required |
fixed_assets
|
Union[int, float]
|
Net fixed assets. |
required |
working_capital
|
Union[int, float]
|
Net working capital. |
required |
Returns:
| Type | Description |
|---|---|
float
|
RONA as a decimal ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If net assets is zero. |
Example
return_on_net_assets(50_000, 300_000, 100_000) 0.125
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
revenue_per_employee(total_revenue: Union[int, float], number_of_employees: int) -> float
¶
Calculate revenue generated per employee.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_revenue
|
Union[int, float]
|
Total revenue of the company. |
required |
number_of_employees
|
int
|
Total number of employees. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Revenue per employee. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not of correct types. |
ValueError
|
If number_of_employees is less than 1. |
Example
revenue_per_employee(5000000, 50) 100000.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
roi(gain: float, cost: float) -> float
¶
Calculates Return on Investment as a percentage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gain
|
float
|
Total return (revenue or final value). |
required |
cost
|
float
|
Total cost (initial investment). |
required |
Returns:
| Type | Description |
|---|---|
float
|
ROI percentage. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If cost is zero. |
Example
roi(1500, 1000) 50.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
rri(nper: Union[int, float], pv: float, fv: float) -> float
¶
Equivalent interest rate for the growth of an investment.
Description
Returns the rate needed for pv to grow to fv over nper periods. Formula: (fv / pv)^(1/nper) - 1. Equivalent to Excel RRI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nper
|
Union[int, float]
|
Number of periods (> 0). |
required |
pv
|
float
|
Present value (must not be zero). |
required |
fv
|
float
|
Future value. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Equivalent per-period interest rate. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If nper ≤ 0 or pv = 0. |
Example
round(rri(96, 10000, 11000), 6) 0.000993
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
rule_of_72(rate: Union[int, float]) -> float
¶
Estimates the number of periods to double an investment.
Uses the Rule of 72: periods ≈ 72 / (rate × 100).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
Union[int, float]
|
Interest rate per period as a decimal (e.g. 0.06 for 6 %). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Approximate number of periods to double. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If rate is not numeric. |
ValueError
|
If rate is zero or negative. |
Example
rule_of_72(0.06) 12.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
sales_growth_rate(current_sales: Union[int, float], previous_sales: Union[int, float]) -> float
¶
Return the sales growth rate as a decimal.
Growth = (Current − Previous) / Previous.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_sales
|
Union[int, float]
|
Revenue in the current period. |
required |
previous_sales
|
Union[int, float]
|
Revenue in the prior period (must be > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Sales growth rate as a float (e.g. 0.25 for 25 %). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If previous_sales is zero or negative. |
Example
sales_growth_rate(1_250_000, 1_000_000) 0.25
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
sharpe_ratio(returns: List[float], risk_free_rate: float = 0.0) -> float
¶
Computes the Sharpe ratio of a return series.
SR = (mean(R) − Rf) / std(R).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
List[float]
|
List of periodic returns (as decimals). |
required |
risk_free_rate
|
float
|
Risk-free rate per period. |
0.0
|
Returns:
| Type | Description |
|---|---|
float
|
The Sharpe ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are invalid types. |
ValueError
|
If returns is empty or has zero standard deviation. |
Example
round(sharpe_ratio([0.05, 0.02, 0.08, -0.01, 0.04], 0.01), 4) 0.4728
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
simple_interest(principal: float, rate: float, time_periods: float) -> float
¶
Calculates simple interest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
principal
|
float
|
The initial amount. |
required |
rate
|
float
|
Annual interest rate (e.g. 0.05 for 5%). |
required |
time_periods
|
float
|
Number of periods (years). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The interest earned. |
Example
simple_interest(1000, 0.05, 3) 150.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
sln(cost: float, salvage: float, life: Union[int, float]) -> float
¶
Calculates the straight-line depreciation of an asset for one period.
Depreciation is allocated evenly over the useful life of the asset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cost
|
float
|
The initial cost of the asset. |
required |
salvage
|
float
|
The salvage value at the end of the useful life of the asset. |
required |
life
|
Union[int, float]
|
The number of periods over which the asset is being depreciated. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The straight-line depreciation amount per period. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If 'life' is zero or negative. |
Example
An asset costs $10,000, has a salvage value of $2,000, and a useful life of 5 years.¶
sln(cost=10000, salvage=2000, life=5) 1600.0
Cost: O(1), direct arithmetic calculation.
Source code in shortfx/fxNumeric/finance_functions.py
sortino_ratio(returns: List[float], risk_free_rate: float = 0.0) -> float
¶
Computes the Sortino ratio (uses downside deviation only).
SR = (mean(R) − Rf) / downside_dev.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
List[float]
|
List of periodic returns (as decimals). |
required |
risk_free_rate
|
float
|
Minimum acceptable return per period. |
0.0
|
Returns:
| Type | Description |
|---|---|
float
|
The Sortino ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are invalid types. |
ValueError
|
If returns is empty or has zero downside deviation. |
Example
round(sortino_ratio([0.05, 0.02, 0.08, -0.01, 0.04], 0.01), 4) 1.0328
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
spot_to_forward(spot1: float, t1: float, spot2: float, t2: float) -> float
¶
Implied forward rate between two spot rates.
f(t1,t2) = [(1+s2)^t2 / (1+s1)^t1]^{1/(t2−t1)} − 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spot1
|
float
|
Spot rate for period t1 (> 0). |
required |
t1
|
float
|
Shorter maturity in years (> 0). |
required |
spot2
|
float
|
Spot rate for period t2 (> 0). |
required |
t2
|
float
|
Longer maturity in years (> t1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Implied forward rate as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If constraints are violated. |
Usage Example
round(spot_to_forward(0.04, 1, 0.05, 2), 6) 0.060096
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
sustainable_growth_rate(roe: Union[int, float], retention: Union[int, float]) -> float
¶
Calculate the sustainable growth rate: SGR = ROE × Retention Ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
roe
|
Union[int, float]
|
Return on equity (decimal, e.g. 0.15 for 15%). |
required |
retention
|
Union[int, float]
|
Retention ratio (decimal, 0 to 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Sustainable growth rate as a decimal. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If retention not in [0, 1]. |
Example
sustainable_growth_rate(0.15, 0.7) 0.105
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
syd(cost: float, salvage: float, life: int, per: int) -> float
¶
Calculates depreciation using the sum-of-years' digits method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cost
|
float
|
Initial cost of the asset. |
required |
salvage
|
float
|
Salvage value at end of life. |
required |
life
|
int
|
Useful life in periods. |
required |
per
|
int
|
Period for which to calculate (1-based). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Depreciation for the specified period. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If parameters are out of valid ranges. |
Example
syd(10000, 1000, 5, 1) 3000.0 syd(10000, 1000, 5, 5) 600.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
tbilleq(settlement: str, maturity: str, discount: float) -> float
¶
Bond-equivalent yield for a Treasury bill.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
discount
|
float
|
Discount rate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Bond-equivalent yield. |
Example
round(tbilleq("2008-03-31", "2008-06-01", 0.0914), 6) 0.094151
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
tbillprice(settlement: str, maturity: str, discount: float) -> float
¶
Price per $100 face for a Treasury bill.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
discount
|
float
|
Discount rate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Price per $100 face. |
Example
round(tbillprice("2008-03-31", "2008-06-01", 0.09), 4) 98.45
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
tbillyield(settlement: str, maturity: str, pr: float) -> float
¶
Yield for a Treasury bill.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
pr
|
float
|
Price per $100 face. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Annual yield. |
Example
round(tbillyield("2008-03-31", "2008-06-01", 98.45), 6) 0.091417
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
term_life_insurance_pv(benefit: float, interest_rate: float, mortality_rates: list[float]) -> float
¶
Present value of a term life insurance (death benefit paid at end of year of death).
Computes Σ_k (benefit × v^{k+1} × kp_x × q) for k = 0 … n-1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
benefit
|
float
|
Death benefit amount (> 0). |
required |
interest_rate
|
float
|
Annual effective interest rate (> 0). |
required |
mortality_rates
|
list[float]
|
List of one-year mortality probabilities for successive ages. Each value must be in [0, 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Actuarial present value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument has the wrong type. |
ValueError
|
If numeric constraints are violated. |
Usage Example
round(term_life_insurance_pv(100000, 0.05, [0.01, 0.02, 0.03]), 2) 5262.59
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
time_weighted_return(period_returns: list[float]) -> float
¶
Time-weighted rate of return (geometric linking of sub-period returns).
TWR = ∏(1 + r_i) − 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
period_returns
|
list[float]
|
List of sub-period returns (e.g. [0.02, -0.01, 0.03]). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cumulative TWR as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If period_returns is not a list of numerics. |
ValueError
|
If the list is empty or any return ≤ −1. |
Usage Example
round(time_weighted_return([0.02, -0.01, 0.03]), 6) 0.040094
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
times_interest_earned(ebit: Union[int, float], interest_expense: Union[int, float]) -> float
¶
Calculate times interest earned (TIE) ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ebit
|
Union[int, float]
|
Earnings before interest and taxes. |
required |
interest_expense
|
Union[int, float]
|
Interest expense for the period. |
required |
Returns:
| Type | Description |
|---|---|
float
|
TIE ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If interest_expense is zero. |
Example
times_interest_earned(500_000, 100_000) 5.0
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
tobin_q_ratio(market_value: Union[int, float], replacement_cost: Union[int, float]) -> float
¶
Return Tobin's Q ratio.
Q = Market Value of Assets / Replacement Cost of Assets. A Q > 1 suggests the market values the firm above its asset replacement cost.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
market_value
|
Union[int, float]
|
Total market value of the firm. |
required |
replacement_cost
|
Union[int, float]
|
Replacement cost of assets (must be > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Tobin's Q as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If replacement_cost is zero or negative. |
Example
tobin_q_ratio(1_500_000, 1_000_000) 1.5
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
treynor_ratio(portfolio_return: Union[int, float], risk_free_rate: Union[int, float], beta: Union[int, float]) -> float
¶
Calculate the Treynor ratio: (Rp - Rf) / β.
Measures risk-adjusted return per unit of systematic (market) risk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio_return
|
Union[int, float]
|
Portfolio return (as decimal, e.g. 0.12 for 12 %). |
required |
risk_free_rate
|
Union[int, float]
|
Risk-free rate (as decimal). |
required |
beta
|
Union[int, float]
|
Portfolio beta (systematic risk). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Treynor ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If beta is zero. |
Example
treynor_ratio(0.12, 0.03, 1.2) 0.075
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
ulcer_index(values: list) -> float
¶
Calculate the Ulcer Index for a price or value series.
Description
Measures the depth and duration of drawdowns. UI = sqrt(mean(drawdown_pct²)), where drawdown_pct is the percentage decline from the running peak.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
list
|
List of prices or portfolio values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Ulcer Index as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If values is not a list of numerics. |
ValueError
|
If values has fewer than 2 elements. |
Usage Example
ulcer_index([100, 105, 102, 98, 103]) 3.5734484731396085
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
unlevered_beta(levered_beta: float, tax_rate: float, de_ratio: float) -> float
¶
Calculate unlevered (asset) beta using the Hamada equation.
Description
β_u = β_L / (1 + (1 − tax_rate) × D/E). Strips out the financial leverage effect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
levered_beta
|
float
|
Levered (equity) beta. |
required |
tax_rate
|
float
|
Corporate tax rate (0–1). |
required |
de_ratio
|
float
|
Debt-to-equity ratio. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Unlevered beta. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
Usage Example
round(unlevered_beta(1.2, 0.30, 0.5), 4) 0.8889
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
var_parametric(portfolio_value: float, mean_return: float, std_return: float, confidence: float = 0.95) -> float
¶
Calculates parametric Value at Risk (VaR) assuming normal returns.
VaR = Portfolio × (μ − z_α × σ), standalone using math.erfinv.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio_value
|
float
|
Total portfolio value. |
required |
mean_return
|
float
|
Expected return (e.g. 0.001 for 0.1%). |
required |
std_return
|
float
|
Standard deviation of returns. |
required |
confidence
|
float
|
Confidence level (e.g. 0.95 or 0.99). |
0.95
|
Returns:
| Type | Description |
|---|---|
float
|
Value at Risk as a positive loss amount. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If confidence is not in (0, 1) or std_return is negative. |
Example
round(var_parametric(1_000_000, 0.001, 0.02, 0.95), 2) 31901.39
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
vdb(cost: float, salvage: float, life: float, start_period: float, end_period: float, factor: float = 2.0, no_switch: bool = False) -> float
¶
Variable declining balance depreciation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cost
|
float
|
Initial cost of the asset. |
required |
salvage
|
float
|
Salvage value. |
required |
life
|
float
|
Useful life of the asset. |
required |
start_period
|
float
|
Start of the depreciation period. |
required |
end_period
|
float
|
End of the depreciation period. |
required |
factor
|
float
|
Declining balance factor (default 2 = DDB). |
2.0
|
no_switch
|
bool
|
If True, do not switch to straight-line. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Depreciation for the period. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If parameters are invalid. |
Example
round(vdb(2400, 300, 10, 0, 1), 2) 480.0
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
wacc(equity: float, debt: float, cost_of_equity: float, cost_of_debt: float, tax_rate: float) -> float
¶
Computes the Weighted Average Cost of Capital.
WACC = (E/(E+D)) × Re + (D/(E+D)) × Rd × (1 − T).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
equity
|
float
|
Market value of equity (positive). |
required |
debt
|
float
|
Market value of debt (non-negative). |
required |
cost_of_equity
|
float
|
Cost of equity as decimal (e.g. 0.10 for 10%). |
required |
cost_of_debt
|
float
|
Cost of debt as decimal. |
required |
tax_rate
|
float
|
Corporate tax rate as decimal (0 to 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The WACC as a decimal. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If equity <= 0 or debt < 0 or tax_rate not in [0, 1]. |
Example
round(wacc(600000, 400000, 0.08, 0.05, 0.30), 4) 0.062
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
wacc_two_sources(equity: Union[int, float], cost_of_equity: Union[int, float], debt: Union[int, float], cost_of_debt: Union[int, float], tax_rate: Union[int, float]) -> float
¶
Return the Weighted Average Cost of Capital (two-source model).
WACC = (E / V) · Re + (D / V) · Rd · (1 − Tc), where V = E + D.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
equity
|
Union[int, float]
|
Market value of equity (must be ≥ 0). |
required |
cost_of_equity
|
Union[int, float]
|
Cost of equity as a decimal. |
required |
debt
|
Union[int, float]
|
Market value of debt (must be ≥ 0). |
required |
cost_of_debt
|
Union[int, float]
|
Cost of debt as a decimal. |
required |
tax_rate
|
Union[int, float]
|
Corporate tax rate as a decimal (0–1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
WACC as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If equity + debt is zero. |
Example
round(wacc_two_sources(600_000, 0.10, 400_000, 0.05, 0.30), 4) 0.074
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
weighted_average_cost_of_debt(debts: list[float], rates: list[float], tax_rate: float = 0.0) -> float
¶
Compute the weighted average after-tax cost of debt.
Each debt weight is debts[i] / total_debt. The effective rate
is rates[i] * (1 - tax_rate).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
debts
|
list[float]
|
Outstanding amounts for each debt tranche. |
required |
rates
|
list[float]
|
Annual interest rate for each tranche. |
required |
tax_rate
|
float
|
Corporate tax rate in [0, 1). |
0.0
|
Returns:
| Type | Description |
|---|---|
float
|
Weighted average cost as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs have wrong types. |
ValueError
|
If lists are empty, lengths differ, or total debt is zero. |
Example
round(weighted_average_cost_of_debt([1000, 2000], [0.05, 0.07], 0.3), 4) 0.0443
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
working_capital(current_assets: Union[int, float], current_liabilities: Union[int, float]) -> float
¶
Calculate working capital.
Measures a company's short-term liquidity: the difference between current assets and current liabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_assets
|
Union[int, float]
|
Total current assets. |
required |
current_liabilities
|
Union[int, float]
|
Total current liabilities. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Working capital as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
Example
working_capital(500000, 300000) 200000
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
working_capital_ratio(current_assets: Union[int, float], current_liabilities: Union[int, float]) -> float
¶
Calculate the working capital ratio (current ratio).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_assets
|
Union[int, float]
|
Total current assets. |
required |
current_liabilities
|
Union[int, float]
|
Total current liabilities. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Working capital ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If current_liabilities is zero. |
Example
working_capital_ratio(150_000, 100_000) 1.5
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
xirr(values: List[float], dates: list, guess: float = 0.1) -> float
¶
Calculates Internal Rate of Return for irregularly spaced cash flows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
List[float]
|
Cash flow amounts. |
required |
dates
|
list
|
Corresponding dates (datetime objects). |
required |
guess
|
float
|
Initial rate guess. Default 0.1. |
0.1
|
Returns:
| Type | Description |
|---|---|
float
|
The XIRR rate. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are invalid types. |
ValueError
|
If convergence fails or inputs are invalid. |
Example
from datetime import date round(xirr([-1000, 300, 400, 500], ... [date(2020,1,1), date(2020,6,1), date(2021,1,1), date(2021,6,1)]), 4) 0.2417
Complexity: O(k*n)
Source code in shortfx/fxNumeric/finance_functions.py
xnpv(rate: float, values: List[float], dates: list) -> float
¶
Calculates Net Present Value for irregularly spaced cash flows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
Discount rate. |
required |
values
|
List[float]
|
Cash flow amounts. |
required |
dates
|
list
|
Corresponding dates (datetime objects). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Net present value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are invalid types. |
ValueError
|
If lengths differ or lists are empty. |
Example
from datetime import date round(xnpv(0.1, [-1000, 300, 400, 500], ... [date(2020,1,1), date(2020,6,1), date(2021,1,1), date(2021,6,1)]), 2) 72.46
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
yang_zhang_volatility(ohlc: list[tuple[float, float, float, float]]) -> float
¶
Yang-Zhang volatility estimator from OHLC bars.
Combines overnight, open-to-close, and Rogers-Satchell components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ohlc
|
list[tuple[float, float, float, float]]
|
List of (open, high, low, close) tuples (≥ 2 bars). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Annualised volatility estimate (assuming 252 trading days). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If ohlc is not a valid list of 4-tuples. |
ValueError
|
If fewer than 2 bars or constraints violated. |
Usage Example
bars = [(100, 105, 98, 103), (103, 107, 101, 106), (106, 110, 104, 108)] round(yang_zhang_volatility(bars), 4) > 0 True
Complexity: O(n)
Source code in shortfx/fxNumeric/finance_functions.py
yield_bond(settlement: str, maturity: str, rate: float, pr: float, redemption: float, frequency: int, basis: int = 0) -> float
¶
Yield of a security that pays periodic interest.
Description
Uses Newton-Raphson iteration to find the yield. Equivalent to Excel YIELD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
rate
|
float
|
Annual coupon rate. |
required |
pr
|
float
|
Current price per $100 face. |
required |
redemption
|
float
|
Redemption value per $100 face. |
required |
frequency
|
int
|
Coupon payments per year (1, 2, or 4). |
required |
basis
|
int
|
Day-count basis (0-4). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Annual yield. |
Example
round(yield_bond("2008-02-15", "2016-11-15", 0.0575, 95.04287, 100, 2, 0), 4) 0.065
Complexity: O(k × n) where k = iterations, n = coupons
Source code in shortfx/fxNumeric/finance_functions.py
yielddisc(settlement: str, maturity: str, pr: float, redemption: float, basis: int = 0) -> float
¶
Yield for a discounted security.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
pr
|
float
|
Price per $100 face. |
required |
redemption
|
float
|
Redemption value per $100 face. |
required |
basis
|
int
|
Day-count basis (0-4). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Annual yield. |
Example
round(yielddisc("2008-02-16", "2008-03-01", 99.795, 100, 2), 6) 0.056873
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
yieldmat(settlement: str, maturity: str, issue: str, rate: float, pr: float, basis: int = 0) -> float
¶
Yield of a security that pays interest at maturity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settlement
|
str
|
Settlement date (YYYY-MM-DD). |
required |
maturity
|
str
|
Maturity date (YYYY-MM-DD). |
required |
issue
|
str
|
Issue date (YYYY-MM-DD). |
required |
rate
|
float
|
Annual coupon rate. |
required |
pr
|
float
|
Price per $100 face. |
required |
basis
|
int
|
Day-count basis (0-4). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Annual yield. |
Example
round(yieldmat("2008-03-15", "2008-11-03", "2007-11-08", 0.0625, 100.0123, 0), 4) 0.0609
Complexity: O(1)
Source code in shortfx/fxNumeric/finance_functions.py
zero_coupon_price(face_value: float, yield_rate: float, years_to_maturity: float) -> float
¶
Price of a zero-coupon bond.
P = F / (1 + y)^T.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
face_value
|
float
|
Face (par) value of the bond (> 0). |
required |
yield_rate
|
float
|
Yield to maturity (> 0). |
required |
years_to_maturity
|
float
|
Time to maturity in years (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Bond price as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If constraints are violated. |
Usage Example
round(zero_coupon_price(1000, 0.05, 10), 2) 613.91
Complexity: O(1)