A simple Python package for financial calculations
Project description
PyFi Finance
A lightweight Python package for financial calculations.
Installation
pip install pyfi-finance
Features
- Core financial functions (compound interest, present value, NPV, IRR)
- Loan calculations (monthly payments, amortization schedules)
- Portfolio analysis (returns, volatility, Sharpe ratio, optimization)
- Stock technical indicators (moving averages, RSI, Bollinger Bands, MACD)
- Option pricing (Black-Scholes model)
- Command-line tools for quick financial calculations
Usage Examples
Basic Financial Calculations
from pyfi_finance.core import compound_interest, present_value
# Calculate compound interest
final_amount = compound_interest(principal=1000, rate=0.05, time=5, periods_per_year=1)
print(f"Final amount after 5 years: ${final_amount:.2f}")
# Calculate present value
pv = present_value(future_value=1000, rate=0.05, time=5)
print(f"Present value: ${pv:.2f}")
Portfolio Analysis
import numpy as np
from pyfi_finance.portfolio import portfolio_return, portfolio_volatility, sharpe_ratio
# Define portfolio parameters
returns = [0.05, 0.1, 0.15] # Expected returns
weights = [0.4, 0.3, 0.3] # Portfolio weights
cov_matrix = np.array([
[0.04, 0.02, 0.01],
[0.02, 0.09, 0.03],
[0.01, 0.03, 0.16]
]) # Covariance matrix
# Calculate portfolio metrics
expected_return = portfolio_return(returns, weights)
volatility = portfolio_volatility(returns, weights, cov_matrix)
sr = sharpe_ratio(expected_return, volatility, risk_free_rate=0.01)
print(f"Expected Return: {expected_return:.2%}")
print(f"Volatility: {volatility:.2%}")
print(f"Sharpe Ratio: {sr:.2f}")
Option Pricing
from pyfi_finance.options import black_scholes, option_delta
# Calculate option price using Black-Scholes
price = black_scholes(
stock_price=100,
strike_price=95,
time_to_expiry=0.5,
risk_free_rate=0.02,
volatility=0.25,
option_type='call'
)
print(f"Call option price: ${price:.2f}")
# Calculate option delta
delta = option_delta(
stock_price=100,
strike_price=95,
time_to_expiry=0.5,
risk_free_rate=0.02,
volatility=0.25,
option_type='call'
)
print(f"Call option delta: {delta:.4f}")
Command-Line Tools
PyFi Finance provides the following command-line tools:
Compound Interest Calculator
compound-interest --principal 1000 --rate 0.05 --time 5 --periods 1
Option Price Calculator
option-price --stock-price 100 --strike-price 95 --time 0.5 --rate 0.02 --volatility 0.25 --type call --show-delta
Portfolio Optimizer
portfolio-optimize --returns 0.05 0.1 0.15 --cov-matrix 0.04 0.02 0.01 0.02 0.09 0.03 0.01 0.03 0.16 --risk-free-rate 0.01
Troubleshooting Command-Line Tools
If you encounter a "'command' is not recognized" error when trying to use the command-line tools:
-
Make sure the package is installed correctly:
pip install pyfi-finance
-
Verify Python's Scripts directory is in your PATH:
- On Windows, this is typically:
C:\Users\<username>\AppData\Local\Programs\Python\Python3xx\Scripts\ - On Unix/Linux/Mac:
/usr/local/bin/or~/.local/bin/
- On Windows, this is typically:
-
Open a new command prompt/terminal after installation to refresh the PATH
-
Use the full path to the command if necessary:
# Windows C:\Users\<username>\AppData\Local\Programs\Python\Python3xx\Scripts\compound-interest.exe --principal 1000 --rate 0.05 --time 5 # Unix/Linux/Mac /usr/local/bin/compound-interest --principal 1000 --rate 0.05 --time 5
-
Verify the scripts were installed by checking the Scripts directory:
# Windows dir C:\Users\<username>\AppData\Local\Programs\Python\Python3xx\Scripts\compound-interest* # Unix/Linux/Mac ls -la /usr/local/bin/compound-interest*
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyfi_finance-0.1.1.tar.gz.
File metadata
- Download URL: pyfi_finance-0.1.1.tar.gz
- Upload date:
- Size: 13.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2be5d5b8e5d74ea6c746acdd6276b4fbcb364918d851da77935fa1984b3a040a
|
|
| MD5 |
5551d242fc201bc2737c1b36bba68d74
|
|
| BLAKE2b-256 |
992adeb1a464fb758cdf1e7e36f6b0965b21af59311c01f9664f0fb1ada847d3
|
File details
Details for the file pyfi_finance-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pyfi_finance-0.1.1-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4eb70ceb9be57b1002c95f9c9b0d058e96785705af90df7aff36d5305f5c325
|
|
| MD5 |
709548b60e6fa692f8c31ff683f101cf
|
|
| BLAKE2b-256 |
1d4428fddfea0c7b7cf801e367a14a633b5edbc06ef217dee65feded88426fed
|