Trade and analyze stocks with portfolio optimization
Project description
Trademan
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
- Download Market Data:
market_dl # Downloads S&P 500 and ETF data
- 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
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
How It Works
- Data Collection: Downloads historical price data using yfinance
- Risk Modeling: Calculates covariance matrices with Ledoit-Wolf shrinkage
- Return Estimation: Uses mean historical returns with optional adjustments
- Optimization: Applies modern portfolio theory via PyPortfolioOpt
- Allocation: Converts weights to discrete share quantities
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f773e3fe2c3ed6df6cd6d53b3163dcc50038d82264d2c8eb946900b532f94bef
|
|
| MD5 |
6cfb96a36ef9fd0fe74aa6d86cd0ec48
|
|
| BLAKE2b-256 |
65364ebed777c8852baf8678e43498fd66ddeaa37a46866475c4b9b6b9b8a551
|
Provenance
The following attestation bundles were made for trademan-0.1.0.tar.gz:
Publisher:
publish.yml on Ottermatics/trademan
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
trademan-0.1.0.tar.gz -
Subject digest:
f773e3fe2c3ed6df6cd6d53b3163dcc50038d82264d2c8eb946900b532f94bef - Sigstore transparency entry: 491267996
- Sigstore integration time:
-
Permalink:
Ottermatics/trademan@e505309fe9982c743f9f9b509c76b4e01616eb3c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Ottermatics
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@e505309fe9982c743f9f9b509c76b4e01616eb3c -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f077c62280e55b19c99e0b5aeae02ddff5c1c44786922ed1a0703a0dc05b99e4
|
|
| MD5 |
b949d3599d381104db6a2dbda2bdfcfa
|
|
| BLAKE2b-256 |
fe9b66e1213b8d3767ab4f413327bf0affb4d9be6a21005de5ae4a407bddf80d
|
Provenance
The following attestation bundles were made for trademan-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on Ottermatics/trademan
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
trademan-0.1.0-py3-none-any.whl -
Subject digest:
f077c62280e55b19c99e0b5aeae02ddff5c1c44786922ed1a0703a0dc05b99e4 - Sigstore transparency entry: 491268029
- Sigstore integration time:
-
Permalink:
Ottermatics/trademan@e505309fe9982c743f9f9b509c76b4e01616eb3c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Ottermatics
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@e505309fe9982c743f9f9b509c76b4e01616eb3c -
Trigger Event:
push
-
Statement type: