Skip to main content

ML-based portfolio selection from historical price patterns (Murray, Xia, Xiao 2024)

Project description

Charting by Machines

Tests Coverage Python License PyPI

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:

  1. Input Features: Uses 12 cumulative monthly returns as input
  2. Neural Network: CNN-LSTM architecture with MSE loss
  3. Weighting: Equal-weighted per month (EWPM)
  4. Target Variable: Normalized excess returns (RetNorm)
  5. 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

charting_by_machines-0.1.0.tar.gz (43.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

charting_by_machines-0.1.0-py3-none-any.whl (57.5 kB view details)

Uploaded Python 3

File details

Details for the file charting_by_machines-0.1.0.tar.gz.

File metadata

  • Download URL: charting_by_machines-0.1.0.tar.gz
  • Upload date:
  • Size: 43.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for charting_by_machines-0.1.0.tar.gz
Algorithm Hash digest
SHA256 33efc826e8b2a95c67fd8d8af0d2c937baa54c5a60b9a6995941930e426e0731
MD5 5c0c24b46b66d358f22529d006665361
BLAKE2b-256 c99622ff34cbb4752c408db92cfbba2c219684faef64ba62fc9d0277dfb39800

See more details on using hashes here.

File details

Details for the file charting_by_machines-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for charting_by_machines-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f631b3448d89caa94f0c501430aca07a31aa09d61fb6d00880c631d645dceb7b
MD5 5b2e8d6d75469bba54899198085c720c
BLAKE2b-256 55286c98f7420b7236e4d63550111bc78b353e19e1c0e0f32c5f4c7c938ac90f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page