Charting by Machines
A Python package reproducing the ML-based portfolio selection methodology from "Charting by Machines" by Murray, Xia, and Xiao (2024, Journal of Financial Economics).
Overview
This package implements a machine learning approach to test the efficient market hypothesis by forecasting stock returns from historical price patterns. The methodology uses CNN-LSTM neural networks to generate return forecasts that strongly predict the cross-section of future stock returns.
Key Features
- Multiple ML Architectures: FNN, CNN, LSTM, CNN-LSTM
- Flexible Data Sources: Yahoo Finance, WRDS (CRSP), local files
- Portfolio Construction: Univariate, bivariate, and trivariate quintile sorting
- Risk Analysis: Factor models (CAPM, FF3, FF5, Carhart), Sharpe ratios
- Experiment Tracking: MLflow integration for reproducibility
- Multiple Interfaces: Python API, CLI, Jupyter notebooks
Installation
# Using pip
pip install charting-by-machines
# Using poetry (recommended for development)
git clone https://github.com/yourusername/charting-by-machines.git
cd charting-by-machines
poetry install
Quick Start
from cbm import PortfolioEngine
# Initialize the engine
engine = PortfolioEngine()
# Load data (using Yahoo Finance)
engine.load_data(
tickers=["AAPL", "MSFT", "GOOGL", ...], # or use universe="sp500"
start_date="2010-01-01",
end_date="2023-12-31"
)
# Train the CNN-LSTM model
model_id = engine.train_model(
architecture="cnn_lstm",
loss_function="mse",
weighting="ewpm",
optimization_period=("2010-01", "2018-12")
)
# Generate forecasts
forecasts = engine.forecast(model_id=model_id)
# Construct portfolios sorted by ML forecasts
portfolios = engine.construct_portfolios(
forecasts=forecasts,
n_portfolios=10,
weighting="value"
)
# Analyze performance
performance = engine.analyze_performance(portfolios)
print(performance.summary())
CLI Usage
# Train a model
cbm train --config config/default.yaml
# Generate forecasts
cbm forecast --model-id <model_id> --output forecasts.parquet
# Run backtest
cbm backtest --config config/backtest.yaml
Methodology
Based on Murray, Xia, and Xiao (2024), the package:
- Input Features: Uses 12 cumulative monthly returns as input
- Neural Network: CNN-LSTM architecture with MSE loss
- Weighting: Equal-weighted per month (EWPM)
- Target Variable: Normalized excess returns (RetNorm)
- Ensemble: Averages 30 model fits for robust forecasts
Project Structure
charting-by-machines/
├── src/cbm/
│ ├── core/ # Configuration, types, main engine
│ ├── data/ # Data adapters, feature engineering
│ ├── ml/ # Neural network models, training
│ ├── portfolio/ # Portfolio construction, analysis
│ ├── api/ # CLI, Python API
│ └── utils/ # Logging, metrics, helpers
├── tests/ # Unit and integration tests
├── config/ # Hydra configuration files
├── examples/ # Jupyter notebooks
└── docs/ # Documentation
Citation
If you use this package in your research, please cite:
@article{murray2024charting,
title={Charting by Machines: Machine Learning-Based Portfolio Selection from Historical Price Patterns},
author={Murray, Scott and Xia, Yusen and Xiao, Houping},
journal={Journal of Financial Economics},
year={2024}
}
License
MIT License - see LICENSE for details.
Metadata
Release files for charting-by-machines 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| charting_by_machines-0.2.0.tar.gz | 44.6 kB | Without provenance | Details for charting_by_machines-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| charting_by_machines-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for charting_by_machines-0.2.0-py3-none-any.whl |
Total release size: 103.4 kB
Release files / charting_by_machines-0.2.0.tar.gz
| Download URL | charting_by_machines-0.2.0.tar.gz |
|---|---|
| Size | 44.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0bf462272d3ab5c831662eea000bef91826265e9661c2fbdb7910b8a00e0597b
|
|
BLAKE2b-256 checksum How to use checksums |
2a57aa01b62c663838b216eea8fdcec5f1b71373d32553754e626161d9bbe7ea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.11
|
Release files / charting_by_machines-0.2.0-py3-none-any.whl
| Download URL | charting_by_machines-0.2.0-py3-none-any.whl |
|---|---|
| Size | 58.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ba293b9a542a8f24a1ba185c073d285d66dedf72ff00b95268e34559742c1c0f
|
|
BLAKE2b-256 checksum How to use checksums |
975a3cc4ad2b537804098f0dd11405e321ea1b8b31e40b4c58c27b48dece1bdb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.11
|
Security for charting-by-machines 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.