Metadata-Version: 2.4
Name: fynance
Version: 1.6.0
Summary: Python and Cython scripts of machine learning, econometrics and statistical features for financial analysis
Author-email: Arthur Bernard <arthur.bernard.92@gmail.com>
License: MIT
Project-URL: Homepage, https://github.com/ArthurBernard/Fynance
Project-URL: Documentation, https://fynance.readthedocs.io/
Project-URL: Source Code, https://github.com/ArthurBernard/Fynance/
Project-URL: Download, https://pypi.org/project/fynance/
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Financial and Insurance Industry
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python
Classifier: Programming Language :: Cython
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Office/Business :: Financial
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE.txt
Requires-Dist: matplotlib>=3.7
Requires-Dist: numba>=0.59
Requires-Dist: numpy>=2.0
Requires-Dist: polars>=1.0
Requires-Dist: scipy>=1.11
Requires-Dist: seaborn>=0.12
Requires-Dist: torch>=2.0
Provides-Extra: dev
Requires-Dist: Cython>=3.0; extra == "dev"
Requires-Dist: pytest>=7.4; extra == "dev"
Requires-Dist: pytest-benchmark>=4.0; extra == "dev"
Requires-Dist: interrogate>=1.5; extra == "dev"
Requires-Dist: pytest-cov>=4.1; extra == "dev"
Requires-Dist: ruff>=0.4; extra == "dev"
Requires-Dist: mypy>=1.8; extra == "dev"
Requires-Dist: numpydoc; extra == "dev"
Provides-Extra: doc
Requires-Dist: sphinx>=7.0; extra == "doc"
Requires-Dist: furo; extra == "doc"
Requires-Dist: numpydoc; extra == "doc"
Requires-Dist: nbsphinx; extra == "doc"
Requires-Dist: sphinx-design; extra == "doc"
Requires-Dist: sphinx-copybutton; extra == "doc"
Dynamic: license-file

<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/ArthurBernard/Fynance/develop/doc/source/_static/logo-dark-transparent.svg">
  <img alt="Fynance logo" src="https://raw.githubusercontent.com/ArthurBernard/Fynance/develop/doc/source/_static/logo-light-transparent.svg" height="180px" align="left">
</picture>

# **Fynance**

[![Python versions](https://img.shields.io/pypi/pyversions/fynance)](https://pypi.org/project/fynance/)
[![PyPI](https://img.shields.io/pypi/v/fynance.svg)](https://pypi.org/project/fynance/)
[![PyPI status](https://img.shields.io/pypi/status/fynance.svg?colorB=blue)](https://pypi.org/project/fynance/)
[![CI](https://github.com/ArthurBernard/Fynance/actions/workflows/ci.yml/badge.svg)](https://github.com/ArthurBernard/Fynance/actions/workflows/ci.yml)
[![License](https://img.shields.io/github/license/ArthurBernard/fynance.svg)](https://github.com/ArthurBernard/Fynance/blob/master/LICENSE.txt)<br>
[![Documentation](https://readthedocs.org/projects/fynance/badge/?version=latest)](https://fynance.readthedocs.io/en/latest/)
[![Coverage](https://codecov.io/gh/ArthurBernard/Fynance/branch/develop/graph/badge.svg)](https://codecov.io/gh/ArthurBernard/Fynance)
[![Docstring coverage](https://raw.githubusercontent.com/ArthurBernard/Fynance/develop/badges/interrogate_badge.svg)](https://github.com/ArthurBernard/Fynance)
[![Downloads](https://pepy.tech/badge/fynance)](https://pepy.tech/project/fynance)

___

Python and Cython package providing **machine learning**, **econometric** and **statistical** tools
for **financial analysis** and **backtesting of trading strategies**.

## Installation

```bash
pip install fynance
```

From source:

```bash
git clone https://github.com/ArthurBernard/Fynance.git
cd Fynance
pip install -e ".[dev]"
python setup.py build_ext --inplace
```

## Subpackages

**Algorithms** `fynance.algorithms`  
Portfolio allocation methods (ERC, HRP, IVP, MDP, MVP), walk-forward wrappers, and position sizing (fractional Kelly, volatility targeting, transaction costs).

**Backtest** `fynance.backtest`  
Profit-and-loss plotting and performance measurement.

**Estimator** `fynance.estimator`  
Cython ARMA / GARCH parameter estimation.

**Features** `fynance.features`  
Kalman filter, technical indicators (Bollinger, RSI, MACD, ROC, realized volatility, rolling skew/kurtosis/autocorr, …),
statistical momentums (SMA, EMA, WMA, …), metrics (Sharpe, Sortino, Calmar, drawdown, tail ratio, …), scaling
(incl. rolling rank), feature-engineering tools (multi-resolution, Granger causality), and market-regime detection.

**Models** `fynance.models`  
Econometric models (MA, ARMA, ARMA-GARCH), neural networks with PyTorch (MLP, RNN, GRU, LSTM,
MultiHeadAttention, **TCN**, **Transformer**), a **direction+magnitude stacking ensemble**,
differentiable loss functions (Sharpe, Sortino, Calmar, Omega, directional, hybrid),
robust-training utilities (purged CV, early stopping, sample weighting), and walk-forward rolling evaluation.

## Quick start

```python
import numpy as np
import fynance as fy

# Sharpe ratio
returns = np.random.randn(252) * 0.01
print(fy.sharpe(returns))

# ERC portfolio allocation
cov = np.cov(np.random.randn(5, 252))
weights = fy.ERC(cov)
print(weights)
```

Rolling walk-forward training with a neural network:

```python
import torch
import torch.nn as nn
from fynance.models.rolling import RollMultiLayerPerceptron

model = RollMultiLayerPerceptron(X, y, layers=[64, 32])
model.set_optimizer(nn.MSELoss, torch.optim.Adam, lr=1e-3)
model(train_period=252, test_period=21, roll_period=21)  # walk-forward windows
for eval_set, test_set in model:   # each step trains on the past, tests the next
    model._training()
```

See [`Notebooks/pytorch_examples.ipynb`](Notebooks/pytorch_examples.ipynb) for a
runnable tour (metrics, allocation, MLP/TCN/Transformer with custom losses,
walk-forward CV).

## Links

- PyPI: https://pypi.org/project/fynance/
- Documentation: https://fynance.readthedocs.io/en/latest/
- Source: https://github.com/ArthurBernard/Fynance
- Changelog: https://github.com/ArthurBernard/Fynance/blob/master/CHANGELOG.md
