This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Pyfolio is a Python library for portfolio performance analysis and risk analytics. This is a modified fork of Quantopian's original pyfolio, enhanced by CloudQuant (云金杞) with additional features including a Flask web interface and Chinese documentation support.
# Windows
install_win.bat
# Unix/Linux/macOS
./install_unix.sh# Run all tests with 4 parallel processes
pytest tests/ -n 4
# Run specific test file
pytest tests/test_timeseries.py
# Run with coverage
pytest tests/ --cov=pyfolio# Build the package
python setup.py build
# Create distribution
python setup.py sdist bdist_wheel# Install in development mode
pip install -e .The codebase follows a modular structure with specialized components:
-
Core Analysis Modules:
timeseries.py: Statistical time series analysis (VaR, Sharpe ratio, etc.)plotting.py: All visualization functions using matplotlib/seaborntears.py: Main interface that orchestrates creating comprehensive reports ("tear sheets")pos.py: Position-level analysis and metricstxn.py: Transaction-level analysisrisk.py: Risk metrics and factor analysisround_trips.py: Round trip trade analysisperf_attrib.py: Performance attribution analysis
-
Web Interface:
flask_app.py: Flask application for web-based result display- Templates in
templates/: HTML templates for web UI - Designed to work in IDEs (Spyder, PyCharm, VSCode) and browsers
-
Data Flow:
- Input: Returns series (pandas Series), positions (DataFrame), transactions (DataFrame)
- Processing: Uses empyrical for performance calculations
- Output: Matplotlib figures or Flask web pages
-
Dependencies: Requires empyrical 0.5.6 from https://gitee.com/yunjinqi/empyrical (not PyPI version)
-
Matplotlib Backend: The code sets matplotlib backend to 'Agg' in tears.py - be aware when modifying plotting code
-
Chinese Data Support: Sample data files use Chinese column names. The library handles both English and Chinese data formats.
-
Testing Data: Test data is provided in
tests/test_data/directory with pickle files for various test scenarios -
Flask Integration: The
tears.create_returns_tear_sheet()and similar functions can output to Flask whenoutput_format='flask'
- Use numpy-style docstrings for all public functions
- Follow existing import patterns (imports at top, grouped by standard/third-party/local)
- Maintain compatibility with pandas DataFrames and Series
- Use empyrical for standard performance calculations rather than reimplementing
- Preserve bilingual support (Chinese documentation, English code/comments)
When modifying analysis functions:
- Check if empyrical already provides the calculation
- Add corresponding plotting function if creating new metrics
- Update relevant tear sheet functions to include new analysis
- Add tests with sample data
When working with the Flask interface:
- Templates are in
templates/directory - Static files go in
static/directory - Use the existing
show_in_flask()pattern for new visualizations