pandasMFE 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pandasmfe-0.1.0/LICENSE +21 -0
- pandasmfe-0.1.0/PKG-INFO +169 -0
- pandasmfe-0.1.0/README.md +139 -0
- pandasmfe-0.1.0/pyproject.toml +89 -0
- pandasmfe-0.1.0/setup.cfg +4 -0
- pandasmfe-0.1.0/src/pandasMFE.egg-info/PKG-INFO +169 -0
- pandasmfe-0.1.0/src/pandasMFE.egg-info/SOURCES.txt +20 -0
- pandasmfe-0.1.0/src/pandasMFE.egg-info/dependency_links.txt +1 -0
- pandasmfe-0.1.0/src/pandasMFE.egg-info/entry_points.txt +2 -0
- pandasmfe-0.1.0/src/pandasMFE.egg-info/requires.txt +14 -0
- pandasmfe-0.1.0/src/pandasMFE.egg-info/top_level.txt +1 -0
- pandasmfe-0.1.0/src/pandasmfe/__init__.py +55 -0
- pandasmfe-0.1.0/src/pandasmfe/__main__.py +7 -0
- pandasmfe-0.1.0/src/pandasmfe/accessor.py +119 -0
- pandasmfe-0.1.0/src/pandasmfe/cli.py +167 -0
- pandasmfe-0.1.0/src/pandasmfe/metrics.py +168 -0
- pandasmfe-0.1.0/src/pandasmfe/py.typed +0 -0
- pandasmfe-0.1.0/src/pandasmfe/sql.py +160 -0
- pandasmfe-0.1.0/tests/test_accessor.py +68 -0
- pandasmfe-0.1.0/tests/test_cli.py +53 -0
- pandasmfe-0.1.0/tests/test_metrics.py +105 -0
- pandasmfe-0.1.0/tests/test_sql.py +122 -0
pandasmfe-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The pandasMFE authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pandasmfe-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pandasMFE
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Risk and performance metrics (Sharpe, drawdown, VaR, CVaR) for pandas, with a .mfe accessor
|
|
5
|
+
Author: Gianmarco Scafarto
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: pandas,finance,risk,var,cvar,sharpe,drawdown
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
14
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
15
|
+
Requires-Python: >=3.11
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: pandas>=2.2
|
|
19
|
+
Requires-Dist: numpy>=1.26
|
|
20
|
+
Provides-Extra: excel
|
|
21
|
+
Requires-Dist: openpyxl>=3.1; extra == "excel"
|
|
22
|
+
Provides-Extra: data
|
|
23
|
+
Requires-Dist: yfinance; extra == "data"
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
26
|
+
Requires-Dist: build; extra == "dev"
|
|
27
|
+
Requires-Dist: twine; extra == "dev"
|
|
28
|
+
Requires-Dist: pyinstaller; extra == "dev"
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
|
|
31
|
+
# pandasMFE
|
|
32
|
+
|
|
33
|
+
Risk and performance metrics for **pandas**: annualized return (CAGR), volatility,
|
|
34
|
+
Sharpe ratio, maximum drawdown, historical and parametric VaR, CVaR (Expected Shortfall).
|
|
35
|
+
|
|
36
|
+
pandasMFE **extends pandas without modifying it**: after `import pandasmfe`, every
|
|
37
|
+
DataFrame and Series gains an `.mfe` namespace, using pandas' official
|
|
38
|
+
[accessor API](https://pandas.pydata.org/docs/development/extending.html).
|
|
39
|
+
Prices can be read from a CSV file or from a **SQLite database** with a SQL query.
|
|
40
|
+
|
|
41
|
+
> pandasMFE is an independent project and is not affiliated with the pandas project.
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
Requires Python 3.11+.
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pip install pandasMFE # library + `pandasmfe` command
|
|
49
|
+
pip install "pandasMFE[excel]" # + .xlsx export
|
|
50
|
+
pip install "pandasMFE[data]" # + yfinance, to download real prices
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Usage: the `.mfe` accessor
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
import pandas as pd
|
|
57
|
+
import pandasmfe # registers .mfe
|
|
58
|
+
|
|
59
|
+
prices = pd.read_csv("prices.csv", index_col=0, parse_dates=True)
|
|
60
|
+
|
|
61
|
+
# methods on PRICES
|
|
62
|
+
prices.mfe.risk_report(risk_free=0.02, alpha=0.95)
|
|
63
|
+
prices.mfe.max_drawdown()
|
|
64
|
+
prices.mfe.drawdown() # full drawdown series
|
|
65
|
+
returns = prices.mfe.returns()
|
|
66
|
+
|
|
67
|
+
# methods on RETURNS
|
|
68
|
+
returns.mfe.sharpe(risk_free=0.02)
|
|
69
|
+
returns.mfe.ann_volatility()
|
|
70
|
+
returns.mfe.var(alpha=0.99) # historical
|
|
71
|
+
returns.mfe.var(alpha=0.99, method="parametric") # Gaussian
|
|
72
|
+
returns.mfe.cvar(alpha=0.99)
|
|
73
|
+
|
|
74
|
+
# works on a single Series too, and chains
|
|
75
|
+
prices["STOCK_A"].mfe.returns().mfe.sharpe()
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Usage: plain functions
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
import pandasmfe as pm
|
|
82
|
+
|
|
83
|
+
pm.risk_report(prices, periods_per_year=252, risk_free=0.02, alpha=0.95)
|
|
84
|
+
pm.var_historical(pm.simple_returns(prices), alpha=0.99)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Usage: real prices from a SQL database
|
|
88
|
+
|
|
89
|
+
Prices are stored in a SQLite database (one file, built into Python: no extra
|
|
90
|
+
dependency) and read back with a SQL query.
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
pip install "pandasMFE[data]"
|
|
94
|
+
python examples/download_prices.py # SPY, AAPL, MSFT, TLT, GLD since 2015 -> examples/prices.db
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
import pandasmfe as pm
|
|
99
|
+
|
|
100
|
+
prices = pm.read_prices_sql("examples/prices.db", tickers=["SPY", "TLT"], start="2020-01-01")
|
|
101
|
+
prices.mfe.risk_report(risk_free=0.02)
|
|
102
|
+
|
|
103
|
+
pm.list_tickers("examples/prices.db") # what is in the database
|
|
104
|
+
pm.write_prices_sql(my_prices, "my.db") # save any wide DataFrame of prices
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Table schema: `prices(date TEXT, ticker TEXT, close REAL)`, one row per date and ticker.
|
|
108
|
+
More queries in `examples/queries.sql`.
|
|
109
|
+
|
|
110
|
+
## Usage: command line
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
pandasmfe prices.csv
|
|
114
|
+
pandasmfe prices.csv --rf 0.02 --alpha 0.99 -o report.csv
|
|
115
|
+
pandasmfe excel_export.csv --eu -o report.xlsx # ';' separator, decimal comma, dd/mm/yyyy dates
|
|
116
|
+
pandasmfe examples/prices.db --tickers SPY TLT --start 2020-01-01
|
|
117
|
+
python -m pandasmfe prices.csv # equivalent
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
`--tickers`, `--start` and `--end` work with both CSV files and databases.
|
|
121
|
+
Expected CSV format: first column = dates, then one price column per asset.
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
Date,STOCK_A,STOCK_B
|
|
125
|
+
2024-01-02,185.6,370.9
|
|
126
|
+
2024-01-03,184.2,370.6
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`examples/sample_prices.csv` contains **synthetic** prices (geometric Brownian motion),
|
|
130
|
+
useful to try the package without downloading anything.
|
|
131
|
+
|
|
132
|
+
## Standalone executable
|
|
133
|
+
|
|
134
|
+
The same command can be packaged with PyInstaller into a folder that runs on a
|
|
135
|
+
computer without Python:
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
pip install pyinstaller
|
|
139
|
+
pyinstaller --onedir --name pandasmfe --clean --noconfirm standalone/pandasmfe_app.py
|
|
140
|
+
dist/pandasmfe/pandasmfe examples/sample_prices.csv
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
PyInstaller builds for the operating system it runs on (a Windows `.exe` must be built on Windows).
|
|
144
|
+
|
|
145
|
+
## Conventions
|
|
146
|
+
|
|
147
|
+
| Metric | Formula | Sign |
|
|
148
|
+
|---|---|---|
|
|
149
|
+
| `ann_return` | (∏(1+r))^(N/n) − 1 | ± |
|
|
150
|
+
| `ann_volatility` | σ · √N (ddof=1) | + |
|
|
151
|
+
| `sharpe` | mean(r − rf) / σ · √N | ± |
|
|
152
|
+
| `max_drawdown` | min(P / cummax(P) − 1) | ≤ 0 |
|
|
153
|
+
| `var_hist_95` | −quantile₅%(r) | positive loss |
|
|
154
|
+
| `var_param_95` | −(μ + z₅% σ) | positive loss |
|
|
155
|
+
| `cvar_hist_95` | −mean(r ∣ r ≤ quantile₅%) | positive loss |
|
|
156
|
+
|
|
157
|
+
N = periods per year (252 for daily data), n = number of observations.
|
|
158
|
+
Missing prices are never filled: a gap gives NaN returns, which are skipped.
|
|
159
|
+
|
|
160
|
+
## Tests
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
pip install -e ".[dev]"
|
|
164
|
+
pytest
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## License
|
|
168
|
+
|
|
169
|
+
MIT
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# pandasMFE
|
|
2
|
+
|
|
3
|
+
Risk and performance metrics for **pandas**: annualized return (CAGR), volatility,
|
|
4
|
+
Sharpe ratio, maximum drawdown, historical and parametric VaR, CVaR (Expected Shortfall).
|
|
5
|
+
|
|
6
|
+
pandasMFE **extends pandas without modifying it**: after `import pandasmfe`, every
|
|
7
|
+
DataFrame and Series gains an `.mfe` namespace, using pandas' official
|
|
8
|
+
[accessor API](https://pandas.pydata.org/docs/development/extending.html).
|
|
9
|
+
Prices can be read from a CSV file or from a **SQLite database** with a SQL query.
|
|
10
|
+
|
|
11
|
+
> pandasMFE is an independent project and is not affiliated with the pandas project.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Requires Python 3.11+.
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install pandasMFE # library + `pandasmfe` command
|
|
19
|
+
pip install "pandasMFE[excel]" # + .xlsx export
|
|
20
|
+
pip install "pandasMFE[data]" # + yfinance, to download real prices
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage: the `.mfe` accessor
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
import pandas as pd
|
|
27
|
+
import pandasmfe # registers .mfe
|
|
28
|
+
|
|
29
|
+
prices = pd.read_csv("prices.csv", index_col=0, parse_dates=True)
|
|
30
|
+
|
|
31
|
+
# methods on PRICES
|
|
32
|
+
prices.mfe.risk_report(risk_free=0.02, alpha=0.95)
|
|
33
|
+
prices.mfe.max_drawdown()
|
|
34
|
+
prices.mfe.drawdown() # full drawdown series
|
|
35
|
+
returns = prices.mfe.returns()
|
|
36
|
+
|
|
37
|
+
# methods on RETURNS
|
|
38
|
+
returns.mfe.sharpe(risk_free=0.02)
|
|
39
|
+
returns.mfe.ann_volatility()
|
|
40
|
+
returns.mfe.var(alpha=0.99) # historical
|
|
41
|
+
returns.mfe.var(alpha=0.99, method="parametric") # Gaussian
|
|
42
|
+
returns.mfe.cvar(alpha=0.99)
|
|
43
|
+
|
|
44
|
+
# works on a single Series too, and chains
|
|
45
|
+
prices["STOCK_A"].mfe.returns().mfe.sharpe()
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Usage: plain functions
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
import pandasmfe as pm
|
|
52
|
+
|
|
53
|
+
pm.risk_report(prices, periods_per_year=252, risk_free=0.02, alpha=0.95)
|
|
54
|
+
pm.var_historical(pm.simple_returns(prices), alpha=0.99)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Usage: real prices from a SQL database
|
|
58
|
+
|
|
59
|
+
Prices are stored in a SQLite database (one file, built into Python: no extra
|
|
60
|
+
dependency) and read back with a SQL query.
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
pip install "pandasMFE[data]"
|
|
64
|
+
python examples/download_prices.py # SPY, AAPL, MSFT, TLT, GLD since 2015 -> examples/prices.db
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
import pandasmfe as pm
|
|
69
|
+
|
|
70
|
+
prices = pm.read_prices_sql("examples/prices.db", tickers=["SPY", "TLT"], start="2020-01-01")
|
|
71
|
+
prices.mfe.risk_report(risk_free=0.02)
|
|
72
|
+
|
|
73
|
+
pm.list_tickers("examples/prices.db") # what is in the database
|
|
74
|
+
pm.write_prices_sql(my_prices, "my.db") # save any wide DataFrame of prices
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Table schema: `prices(date TEXT, ticker TEXT, close REAL)`, one row per date and ticker.
|
|
78
|
+
More queries in `examples/queries.sql`.
|
|
79
|
+
|
|
80
|
+
## Usage: command line
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
pandasmfe prices.csv
|
|
84
|
+
pandasmfe prices.csv --rf 0.02 --alpha 0.99 -o report.csv
|
|
85
|
+
pandasmfe excel_export.csv --eu -o report.xlsx # ';' separator, decimal comma, dd/mm/yyyy dates
|
|
86
|
+
pandasmfe examples/prices.db --tickers SPY TLT --start 2020-01-01
|
|
87
|
+
python -m pandasmfe prices.csv # equivalent
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
`--tickers`, `--start` and `--end` work with both CSV files and databases.
|
|
91
|
+
Expected CSV format: first column = dates, then one price column per asset.
|
|
92
|
+
|
|
93
|
+
```
|
|
94
|
+
Date,STOCK_A,STOCK_B
|
|
95
|
+
2024-01-02,185.6,370.9
|
|
96
|
+
2024-01-03,184.2,370.6
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
`examples/sample_prices.csv` contains **synthetic** prices (geometric Brownian motion),
|
|
100
|
+
useful to try the package without downloading anything.
|
|
101
|
+
|
|
102
|
+
## Standalone executable
|
|
103
|
+
|
|
104
|
+
The same command can be packaged with PyInstaller into a folder that runs on a
|
|
105
|
+
computer without Python:
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
pip install pyinstaller
|
|
109
|
+
pyinstaller --onedir --name pandasmfe --clean --noconfirm standalone/pandasmfe_app.py
|
|
110
|
+
dist/pandasmfe/pandasmfe examples/sample_prices.csv
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
PyInstaller builds for the operating system it runs on (a Windows `.exe` must be built on Windows).
|
|
114
|
+
|
|
115
|
+
## Conventions
|
|
116
|
+
|
|
117
|
+
| Metric | Formula | Sign |
|
|
118
|
+
|---|---|---|
|
|
119
|
+
| `ann_return` | (∏(1+r))^(N/n) − 1 | ± |
|
|
120
|
+
| `ann_volatility` | σ · √N (ddof=1) | + |
|
|
121
|
+
| `sharpe` | mean(r − rf) / σ · √N | ± |
|
|
122
|
+
| `max_drawdown` | min(P / cummax(P) − 1) | ≤ 0 |
|
|
123
|
+
| `var_hist_95` | −quantile₅%(r) | positive loss |
|
|
124
|
+
| `var_param_95` | −(μ + z₅% σ) | positive loss |
|
|
125
|
+
| `cvar_hist_95` | −mean(r ∣ r ≤ quantile₅%) | positive loss |
|
|
126
|
+
|
|
127
|
+
N = periods per year (252 for daily data), n = number of observations.
|
|
128
|
+
Missing prices are never filled: a gap gives NaN returns, which are skipped.
|
|
129
|
+
|
|
130
|
+
## Tests
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
pip install -e ".[dev]"
|
|
134
|
+
pytest
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## License
|
|
138
|
+
|
|
139
|
+
MIT
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# pyproject.toml — the package's configuration file.
|
|
2
|
+
# pip and `python -m build` read this file to know how to build the package,
|
|
3
|
+
# what it is called, which version it is and what it depends on.
|
|
4
|
+
|
|
5
|
+
# ---------------------------------------------------------------------------
|
|
6
|
+
# Which tool turns the source code into an installable package.
|
|
7
|
+
# ---------------------------------------------------------------------------
|
|
8
|
+
[build-system]
|
|
9
|
+
# setuptools does the building; version 77+ supports `license = "MIT"` (PEP 639).
|
|
10
|
+
requires = ["setuptools>=77"]
|
|
11
|
+
# The function inside setuptools that pip calls to build the package.
|
|
12
|
+
build-backend = "setuptools.build_meta"
|
|
13
|
+
|
|
14
|
+
# ---------------------------------------------------------------------------
|
|
15
|
+
# Package metadata: everything shown on PyPI and used by pip.
|
|
16
|
+
# ---------------------------------------------------------------------------
|
|
17
|
+
[project]
|
|
18
|
+
# Name on PyPI, used in `pip install pandasMFE` (must be unique on PyPI).
|
|
19
|
+
# The import name is different (`import pandasmfe`): it comes from the folder src/pandasmfe.
|
|
20
|
+
name = "pandasMFE"
|
|
21
|
+
# The version is not written here: setuptools reads it from src/pandasmfe/__init__.py
|
|
22
|
+
# (see [tool.setuptools.dynamic] below), so it lives in one single place.
|
|
23
|
+
dynamic = ["version"]
|
|
24
|
+
# One-line summary shown in PyPI search results.
|
|
25
|
+
description = "Risk and performance metrics (Sharpe, drawdown, VaR, CVaR) for pandas, with a .mfe accessor"
|
|
26
|
+
# This file becomes the project page on PyPI.
|
|
27
|
+
readme = "README.md"
|
|
28
|
+
# Older Pythons get a clear error from pip; 3.9 and 3.10 are end-of-life
|
|
29
|
+
# and pandas 3 requires Python 3.11+.
|
|
30
|
+
requires-python = ">=3.11"
|
|
31
|
+
# MIT licence: anyone may use, modify and share the code, keeping the copyright notice.
|
|
32
|
+
license = "MIT"
|
|
33
|
+
# The licence text is shipped inside the package.
|
|
34
|
+
license-files = ["LICENSE"]
|
|
35
|
+
# Authors shown on PyPI (add the other group members here if it is a group project).
|
|
36
|
+
authors = [{ name = "Gianmarco Scafarto" }]
|
|
37
|
+
# Free-text tags that help people find the package on PyPI.
|
|
38
|
+
keywords = ["pandas", "finance", "risk", "var", "cvar", "sharpe", "drawdown"]
|
|
39
|
+
# Standard labels from PyPI's official list (a misspelt classifier is rejected on upload).
|
|
40
|
+
classifiers = [
|
|
41
|
+
"Programming Language :: Python :: 3",
|
|
42
|
+
"Programming Language :: Python :: 3.11",
|
|
43
|
+
"Programming Language :: Python :: 3.12",
|
|
44
|
+
"Programming Language :: Python :: 3.13",
|
|
45
|
+
"Operating System :: OS Independent",
|
|
46
|
+
|
|
47
|
+
"Intended Audience :: Financial and Insurance Industry",
|
|
48
|
+
"Topic :: Office/Business :: Financial :: Investment",
|
|
49
|
+
]
|
|
50
|
+
# Packages that pip installs automatically together with pandasMFE.
|
|
51
|
+
# Minimum versions (>=), never exact pins (==): a library must be installable
|
|
52
|
+
# next to other libraries. 2.2 and 1.26 are the oldest versions the tests were run on.
|
|
53
|
+
dependencies = [
|
|
54
|
+
"pandas>=2.2",
|
|
55
|
+
"numpy>=1.26",
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
# Optional extras, installed only on request, e.g. pip install "pandasMFE[excel]".
|
|
59
|
+
[project.optional-dependencies]
|
|
60
|
+
# Needed only to save the report as an .xlsx file.
|
|
61
|
+
excel = ["openpyxl>=3.1"]
|
|
62
|
+
# Needed only by examples/download_prices.py to download real prices.
|
|
63
|
+
data = ["yfinance"]
|
|
64
|
+
# Tools for development: tests, building, uploading and the standalone executable.
|
|
65
|
+
dev = ["pytest>=7", "build", "twine", "pyinstaller"]
|
|
66
|
+
|
|
67
|
+
# Command-line programs created by pip on installation.
|
|
68
|
+
[project.scripts]
|
|
69
|
+
# Creates the `pandasmfe` command, which calls the function main() in pandasmfe/cli.py.
|
|
70
|
+
pandasmfe = "pandasmfe.cli:main"
|
|
71
|
+
|
|
72
|
+
# ---------------------------------------------------------------------------
|
|
73
|
+
# Settings for specific tools (each tool reads only its own section).
|
|
74
|
+
# ---------------------------------------------------------------------------
|
|
75
|
+
# Single source of the version: the variable __version__ in pandasmfe/__init__.py.
|
|
76
|
+
[tool.setuptools.dynamic]
|
|
77
|
+
version = { attr = "pandasmfe.__version__" }
|
|
78
|
+
|
|
79
|
+
# The package code lives in src/ ("src layout"): tests and examples stay out of the package.
|
|
80
|
+
[tool.setuptools.packages.find]
|
|
81
|
+
where = ["src"]
|
|
82
|
+
|
|
83
|
+
# Non-.py files to include in the package: the PEP 561 marker for type hints.
|
|
84
|
+
[tool.setuptools.package-data]
|
|
85
|
+
pandasmfe = ["py.typed"]
|
|
86
|
+
|
|
87
|
+
# pytest looks for tests in the tests/ folder.
|
|
88
|
+
[tool.pytest.ini_options]
|
|
89
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pandasMFE
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Risk and performance metrics (Sharpe, drawdown, VaR, CVaR) for pandas, with a .mfe accessor
|
|
5
|
+
Author: Gianmarco Scafarto
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: pandas,finance,risk,var,cvar,sharpe,drawdown
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
14
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
15
|
+
Requires-Python: >=3.11
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: pandas>=2.2
|
|
19
|
+
Requires-Dist: numpy>=1.26
|
|
20
|
+
Provides-Extra: excel
|
|
21
|
+
Requires-Dist: openpyxl>=3.1; extra == "excel"
|
|
22
|
+
Provides-Extra: data
|
|
23
|
+
Requires-Dist: yfinance; extra == "data"
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
26
|
+
Requires-Dist: build; extra == "dev"
|
|
27
|
+
Requires-Dist: twine; extra == "dev"
|
|
28
|
+
Requires-Dist: pyinstaller; extra == "dev"
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
|
|
31
|
+
# pandasMFE
|
|
32
|
+
|
|
33
|
+
Risk and performance metrics for **pandas**: annualized return (CAGR), volatility,
|
|
34
|
+
Sharpe ratio, maximum drawdown, historical and parametric VaR, CVaR (Expected Shortfall).
|
|
35
|
+
|
|
36
|
+
pandasMFE **extends pandas without modifying it**: after `import pandasmfe`, every
|
|
37
|
+
DataFrame and Series gains an `.mfe` namespace, using pandas' official
|
|
38
|
+
[accessor API](https://pandas.pydata.org/docs/development/extending.html).
|
|
39
|
+
Prices can be read from a CSV file or from a **SQLite database** with a SQL query.
|
|
40
|
+
|
|
41
|
+
> pandasMFE is an independent project and is not affiliated with the pandas project.
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
Requires Python 3.11+.
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pip install pandasMFE # library + `pandasmfe` command
|
|
49
|
+
pip install "pandasMFE[excel]" # + .xlsx export
|
|
50
|
+
pip install "pandasMFE[data]" # + yfinance, to download real prices
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Usage: the `.mfe` accessor
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
import pandas as pd
|
|
57
|
+
import pandasmfe # registers .mfe
|
|
58
|
+
|
|
59
|
+
prices = pd.read_csv("prices.csv", index_col=0, parse_dates=True)
|
|
60
|
+
|
|
61
|
+
# methods on PRICES
|
|
62
|
+
prices.mfe.risk_report(risk_free=0.02, alpha=0.95)
|
|
63
|
+
prices.mfe.max_drawdown()
|
|
64
|
+
prices.mfe.drawdown() # full drawdown series
|
|
65
|
+
returns = prices.mfe.returns()
|
|
66
|
+
|
|
67
|
+
# methods on RETURNS
|
|
68
|
+
returns.mfe.sharpe(risk_free=0.02)
|
|
69
|
+
returns.mfe.ann_volatility()
|
|
70
|
+
returns.mfe.var(alpha=0.99) # historical
|
|
71
|
+
returns.mfe.var(alpha=0.99, method="parametric") # Gaussian
|
|
72
|
+
returns.mfe.cvar(alpha=0.99)
|
|
73
|
+
|
|
74
|
+
# works on a single Series too, and chains
|
|
75
|
+
prices["STOCK_A"].mfe.returns().mfe.sharpe()
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Usage: plain functions
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
import pandasmfe as pm
|
|
82
|
+
|
|
83
|
+
pm.risk_report(prices, periods_per_year=252, risk_free=0.02, alpha=0.95)
|
|
84
|
+
pm.var_historical(pm.simple_returns(prices), alpha=0.99)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Usage: real prices from a SQL database
|
|
88
|
+
|
|
89
|
+
Prices are stored in a SQLite database (one file, built into Python: no extra
|
|
90
|
+
dependency) and read back with a SQL query.
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
pip install "pandasMFE[data]"
|
|
94
|
+
python examples/download_prices.py # SPY, AAPL, MSFT, TLT, GLD since 2015 -> examples/prices.db
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
import pandasmfe as pm
|
|
99
|
+
|
|
100
|
+
prices = pm.read_prices_sql("examples/prices.db", tickers=["SPY", "TLT"], start="2020-01-01")
|
|
101
|
+
prices.mfe.risk_report(risk_free=0.02)
|
|
102
|
+
|
|
103
|
+
pm.list_tickers("examples/prices.db") # what is in the database
|
|
104
|
+
pm.write_prices_sql(my_prices, "my.db") # save any wide DataFrame of prices
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Table schema: `prices(date TEXT, ticker TEXT, close REAL)`, one row per date and ticker.
|
|
108
|
+
More queries in `examples/queries.sql`.
|
|
109
|
+
|
|
110
|
+
## Usage: command line
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
pandasmfe prices.csv
|
|
114
|
+
pandasmfe prices.csv --rf 0.02 --alpha 0.99 -o report.csv
|
|
115
|
+
pandasmfe excel_export.csv --eu -o report.xlsx # ';' separator, decimal comma, dd/mm/yyyy dates
|
|
116
|
+
pandasmfe examples/prices.db --tickers SPY TLT --start 2020-01-01
|
|
117
|
+
python -m pandasmfe prices.csv # equivalent
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
`--tickers`, `--start` and `--end` work with both CSV files and databases.
|
|
121
|
+
Expected CSV format: first column = dates, then one price column per asset.
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
Date,STOCK_A,STOCK_B
|
|
125
|
+
2024-01-02,185.6,370.9
|
|
126
|
+
2024-01-03,184.2,370.6
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`examples/sample_prices.csv` contains **synthetic** prices (geometric Brownian motion),
|
|
130
|
+
useful to try the package without downloading anything.
|
|
131
|
+
|
|
132
|
+
## Standalone executable
|
|
133
|
+
|
|
134
|
+
The same command can be packaged with PyInstaller into a folder that runs on a
|
|
135
|
+
computer without Python:
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
pip install pyinstaller
|
|
139
|
+
pyinstaller --onedir --name pandasmfe --clean --noconfirm standalone/pandasmfe_app.py
|
|
140
|
+
dist/pandasmfe/pandasmfe examples/sample_prices.csv
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
PyInstaller builds for the operating system it runs on (a Windows `.exe` must be built on Windows).
|
|
144
|
+
|
|
145
|
+
## Conventions
|
|
146
|
+
|
|
147
|
+
| Metric | Formula | Sign |
|
|
148
|
+
|---|---|---|
|
|
149
|
+
| `ann_return` | (∏(1+r))^(N/n) − 1 | ± |
|
|
150
|
+
| `ann_volatility` | σ · √N (ddof=1) | + |
|
|
151
|
+
| `sharpe` | mean(r − rf) / σ · √N | ± |
|
|
152
|
+
| `max_drawdown` | min(P / cummax(P) − 1) | ≤ 0 |
|
|
153
|
+
| `var_hist_95` | −quantile₅%(r) | positive loss |
|
|
154
|
+
| `var_param_95` | −(μ + z₅% σ) | positive loss |
|
|
155
|
+
| `cvar_hist_95` | −mean(r ∣ r ≤ quantile₅%) | positive loss |
|
|
156
|
+
|
|
157
|
+
N = periods per year (252 for daily data), n = number of observations.
|
|
158
|
+
Missing prices are never filled: a gap gives NaN returns, which are skipped.
|
|
159
|
+
|
|
160
|
+
## Tests
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
pip install -e ".[dev]"
|
|
164
|
+
pytest
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## License
|
|
168
|
+
|
|
169
|
+
MIT
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/pandasMFE.egg-info/PKG-INFO
|
|
5
|
+
src/pandasMFE.egg-info/SOURCES.txt
|
|
6
|
+
src/pandasMFE.egg-info/dependency_links.txt
|
|
7
|
+
src/pandasMFE.egg-info/entry_points.txt
|
|
8
|
+
src/pandasMFE.egg-info/requires.txt
|
|
9
|
+
src/pandasMFE.egg-info/top_level.txt
|
|
10
|
+
src/pandasmfe/__init__.py
|
|
11
|
+
src/pandasmfe/__main__.py
|
|
12
|
+
src/pandasmfe/accessor.py
|
|
13
|
+
src/pandasmfe/cli.py
|
|
14
|
+
src/pandasmfe/metrics.py
|
|
15
|
+
src/pandasmfe/py.typed
|
|
16
|
+
src/pandasmfe/sql.py
|
|
17
|
+
tests/test_accessor.py
|
|
18
|
+
tests/test_cli.py
|
|
19
|
+
tests/test_metrics.py
|
|
20
|
+
tests/test_sql.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pandasmfe
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""pandasMFE — risk and performance metrics for pandas, with a ``.mfe`` accessor.
|
|
2
|
+
|
|
3
|
+
Two ways to use it::
|
|
4
|
+
|
|
5
|
+
import pandasmfe as pm
|
|
6
|
+
pm.risk_report(prices) # plain functions
|
|
7
|
+
|
|
8
|
+
prices.mfe.risk_report() # accessor, available after `import pandasmfe`
|
|
9
|
+
|
|
10
|
+
Prices can come from a CSV or from a SQLite database (``pm.read_prices_sql``).
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
# Importing accessor.py runs its decorators, which register `.mfe` on every
|
|
14
|
+
# DataFrame and Series. The import is needed for this side effect only, so the
|
|
15
|
+
# name is never used ("noqa: F401" tells linters the unused import is intentional).
|
|
16
|
+
from pandasmfe import accessor as _accessor # noqa: F401
|
|
17
|
+
|
|
18
|
+
# Re-export the public functions, so users write pm.risk_report(...)
|
|
19
|
+
# instead of pm.metrics.risk_report(...). Same objects, not copies.
|
|
20
|
+
from pandasmfe.metrics import (
|
|
21
|
+
annualized_return,
|
|
22
|
+
annualized_volatility,
|
|
23
|
+
cvar_historical,
|
|
24
|
+
drawdown_series,
|
|
25
|
+
max_drawdown,
|
|
26
|
+
risk_report,
|
|
27
|
+
sharpe_ratio,
|
|
28
|
+
simple_returns,
|
|
29
|
+
var_historical,
|
|
30
|
+
var_parametric,
|
|
31
|
+
)
|
|
32
|
+
# Re-export the database functions in the same way.
|
|
33
|
+
from pandasmfe.sql import list_tickers, read_prices_sql, write_prices_sql
|
|
34
|
+
|
|
35
|
+
# The package version, written in this single place; pyproject.toml reads it from here.
|
|
36
|
+
__version__ = "0.1.0"
|
|
37
|
+
|
|
38
|
+
# The official list of public names: what `from pandasmfe import *` imports,
|
|
39
|
+
# and what users can rely on. Internal modules and helpers are left out.
|
|
40
|
+
__all__ = [
|
|
41
|
+
"simple_returns",
|
|
42
|
+
"annualized_return",
|
|
43
|
+
"annualized_volatility",
|
|
44
|
+
"sharpe_ratio",
|
|
45
|
+
"drawdown_series",
|
|
46
|
+
"max_drawdown",
|
|
47
|
+
"var_historical",
|
|
48
|
+
"var_parametric",
|
|
49
|
+
"cvar_historical",
|
|
50
|
+
"risk_report",
|
|
51
|
+
"read_prices_sql",
|
|
52
|
+
"write_prices_sql",
|
|
53
|
+
"list_tickers",
|
|
54
|
+
"__version__",
|
|
55
|
+
]
|