A quantitative portfolio constraint engine that enforces market cap and sector diversification rules on investment strategies using structured financial datasets.
This project applies constraints to investment portfolios based on historical market capitalization (MCap) and sector data. It processes raw portfolio selections and outputs constrained portfolios ensuring exposure limits and MCap thresholds are respected.
- MCap Filtering: Removes bottom 20% of companies by Market Capitalization for each corresponding year. Also drops stocks with missing data.
- Sector Limits: Caps the weight of any single sector in the portfolio to a maximum of 25% (or 1 stock, whichever is larger). This mechanism drops the lowest MCap companies from the sector until the threshold is met.
A quantitative portfolio constraint engine that enforces market cap and sector diversification rules on investment strategies using structured financial datasets.
This project applies constraints to investment portfolios based on historical market capitalization (MCap) and sector data. It processes raw portfolio selections and outputs constrained portfolios ensuring exposure limits and MCap thresholds are respected.
- MCap Filtering: Removes bottom 20% of companies by Market Capitalization for each corresponding year. Also drops stocks with missing data.
- Sector Limits: Caps the weight of any single sector in the portfolio to a maximum of 25% (or 1 stock, whichever is larger). This mechanism drops the lowest MCap companies from the sector until the threshold is met.
data/- Put your input CSV files here:assignment_investment_rules.csv: Historical portfolio holdingsassignment_mcap.csv: Market capitalization data over timeassignment_sector_data.csv: Sector classification for companies
src/portfolio_constraint.py: Main python script to process the data
output/- Intended for the resulting constrained strategies (output.csv).
- Python 3.8+
- Pandas
Install required dependencies via:
pip install pandasDownload or copy the 3 required input files (assignment_investment_rules.csv, assignment_mcap.csv, assignment_sector_data.csv) into the directory where you run the script.
Note: Depending on how you run the code, you might want to place the CSVs in the src/ folder alongside the script or adjust the pandas read_csv paths in the source files so they point to the data/ folder.
- Open a terminal.
- Navigate to the folder containing the
.pyscript and the.csvfiles. - Run the script:
python portfolio_constraint.py
- A new
output.csvfile will be generated in the same folder.
- Input Data Format: The
assignment_investment_rules.csvfile contains strategies as strings evaluated to Python lists on execution usingast.literal_eval. - Handling missing MCap values: Stocks that explicitly have no MCap values for a specific year are filtered out with warnings printed into standard output.
- Sector Tie-breaking: When limiting sectors, the stocks representing the lowest MCap within that specific sector are the ones eliminated first.