Skip to main content

Trade and analyze stocks with portfolio optimization

Project description

Trademan

PyPI version Python 3.8+ License: MIT Documentation

A Python library and CLI tool for gathering market data and generating optimal portfolios using modern portfolio theory.

Features

  • 📊 Market Data Collection: Automated downloading and caching of S&P 500 and ETF data via yfinance
  • 🎯 Portfolio Optimization: Multiple optimization strategies (Sharpe ratio, minimum volatility, etc.)
  • 📈 Visualization: Generate beautiful portfolio allocation charts
  • 🚀 CLI Interface: Easy-to-use command-line tools for quick analysis
  • 🐍 Python API: Full programmatic access for custom workflows
  • 💾 Smart Caching: Intelligent data caching to minimize API calls

Installation

Install from PyPI:

pip install trademan

Install from source:

pip install git+https://github.com/ottermatics/trademan.git

Quick Start

CLI Usage

  1. Download Market Data:
market_dl  # Downloads S&P 500 and ETF data
  1. Generate a Portfolio:
# Create a Sharpe-optimized portfolio with $10,000 allocation
trademan -cls stocks -alloc 10000 -opt sharpe

# Minimum volatility ETF portfolio  
trademan -cls etfs -opt min_volatility -alloc 100000 -in QQQ,SPY,VTI

Python API

import trademan

# Get stock data for specific tickers
data = trademan.get_tickers(['AAPL', 'MSFT', 'GOOGL'])

# Create optimized portfolio
weights = trademan.make_portfolio(
    data, 
    opt='sharpe',           # Optimization method
    risk='ledoit_wolf',     # Risk model  
    allocate_amount=10000   # Dollar amount
)

# Visualize the portfolio
fig, ax = trademan.plot_portfolio(weights)

CLI Options

Option Description Default
-cls Asset class: etfs, stocks, all all
-opt Optimization: sharpe, min_volatility, eff_return, eff_risk sharpe
-risk Risk model: covariance, ledoit_wolf ledoit_wolf
-alloc Amount to allocate (shows share counts) None
-gamma Weight regularization (higher = more diversified) 2
-in Include specific tickers (comma-separated) None
-ex Exclude specific tickers (comma-separated) None
-min-wght Minimum weight threshold for assets 0.01
-max-wght Maximum weight limit per asset None

Configuration

Set environment variables to customize data storage:

export TRADEMAN_DATA_DIR="/path/to/data"      # Market data cache
export TRADEMAN_MEDIA_DIR="/path/to/charts"   # Generated charts

Examples

1. Best Performing S&P 500 Stocks

Create a portfolio favoring established companies with cycle penalties:

trademan -cls stocks -gamma 1 -alloc 10000 -cycl-err 10 -max-wght 0.2

Stocks Portfolio

2. Low Volatility ETF Portfolio

Generate a conservative ETF allocation:

trademan -cls etfs -gamma 0.1 -alloc 100000 \
  -in QQQ,SCHG,VGT,SLV,VIG,SPY,VOO,VUG,IAU,PAVE \
  -opt min_volatility

ETF Portfolio

How It Works

  1. Data Collection: Downloads historical price data using yfinance
  2. Risk Modeling: Calculates covariance matrices with Ledoit-Wolf shrinkage
  3. Return Estimation: Uses mean historical returns with optional adjustments
  4. Optimization: Applies modern portfolio theory via PyPortfolioOpt
  5. Allocation: Converts weights to discrete share quantities
  6. Visualization: Creates publication-ready portfolio charts

Dependencies

  • PyPortfolioOpt: Portfolio optimization algorithms
  • yfinance: Market data source
  • pandas/numpy: Data manipulation
  • matplotlib: Visualization
  • diskcache: Data caching
  • scikit-learn: Additional analytics

Development

Install development dependencies:

pip install -e .[dev]

Run tests:

pytest

License

MIT License - see LICENSE for details.

Contributing

Contributions welcome! Please read CONTRIBUTING.rst for guidelines.

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

trademan-0.1.0.tar.gz (139.3 kB view details)

Uploaded Source

Built Distribution

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

trademan-0.1.0-py3-none-any.whl (135.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: trademan-0.1.0.tar.gz
  • Upload date:
  • Size: 139.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for trademan-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f773e3fe2c3ed6df6cd6d53b3163dcc50038d82264d2c8eb946900b532f94bef
MD5 6cfb96a36ef9fd0fe74aa6d86cd0ec48
BLAKE2b-256 65364ebed777c8852baf8678e43498fd66ddeaa37a46866475c4b9b6b9b8a551

See more details on using hashes here.

Provenance

The following attestation bundles were made for trademan-0.1.0.tar.gz:

Publisher: publish.yml on Ottermatics/trademan

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: trademan-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 135.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for trademan-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f077c62280e55b19c99e0b5aeae02ddff5c1c44786922ed1a0703a0dc05b99e4
MD5 b949d3599d381104db6a2dbda2bdfcfa
BLAKE2b-256 fe9b66e1213b8d3767ab4f413327bf0affb4d9be6a21005de5ae4a407bddf80d

See more details on using hashes here.

Provenance

The following attestation bundles were made for trademan-0.1.0-py3-none-any.whl:

Publisher: publish.yml on Ottermatics/trademan

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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