KMI30 Tracker is a Python-based tool that scrapes KMI30 index data from PSX website, calculates portfolio allocations with a custom multiplier, and tracks your investments over time.
- Web scraping of live KMI30 data from PSX
- Portfolio calculation with customizable investment amount and multiplier (default 1.22)
- SQLite database for historical tracking
- Automated daily data collection
- Portfolio rebalancing calculations
- Detailed reporting and analysis
- macOS (tested on macOS 10.15+)
- Python 3.8 or higher
- Internet connection for data scraping
python3 --versionIf Python 3.8+ is not installed, install it via Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install python3cd /Users/mbp/Desktop/kmi30trackerpython3 -m venv venv
source venv/bin/activatepip install -r requirements.txtpython3 kmi30_tracker.py --investment 100000 --multiplier 1.22python3 kmi30_tracker.py statuspython3 kmi30_tracker.py --scrape-onlypython3 kmi30_tracker.py --calculate-only --investment 200000| Option | Description | Default |
|---|---|---|
--investment, -i |
Total investment amount in PKR | 100,000 |
--multiplier, -m |
Index weight multiplier | 1.22 |
--scrape-only |
Only scrape data, don't calculate | False |
--calculate-only |
Only calculate from existing data | False |
--date |
Specific date (YYYY-MM-DD) | Today |
--top-n |
Show top N allocations | 15 |
--log-level |
Logging level (DEBUG/INFO/WARNING/ERROR) | INFO |
python3 kmi30_tracker.py --investment 500000 --multiplier 1.22 --top-n 20python3 kmi30_tracker.py --calculate-only --date 2025-08-30 --investment 300000python3 kmi30_tracker.py --log-level DEBUG --scrape-onlykmi30tracker/
├── kmi30_tracker.py # Main script
├── scraper.py # Web scraper module
├── database.py # Database operations
├── calculator.py # Portfolio calculations
├── requirements.txt # Python dependencies
├── setup_automation.sh # Automation script
├── README.md # This file
├── kmi30_tracker.db # SQLite database (created automatically)
├── kmi30_tracker.log # Log file (created automatically)
└── portfolio_summary_*.txt # Daily summary files
- daily_snapshots - Raw KMI30 data for each day
- portfolio_allocations - Calculated allocations per day
- portfolio_settings - Investment settings history
- portfolio_performance - Performance tracking (future)
chmod +x setup_automation.sh./setup_automation.shThis will:
- Set up daily cron job to run at 6 PM (after market close)
- Configure proper paths and environment
- Set up log rotation
crontab -eAdd this line for daily execution at 6 PM:
0 18 * * 1-5 cd /Users/mbp/Desktop/kmi30tracker && /usr/bin/python3 kmi30_tracker.py --investment 100000 >> cron.log 2>&1
- Fetch KMI30 Data: Current prices and index weightings
- Apply Multiplier: Multiply each IDX WTG by 1.22 (configurable)
- Calculate Percentages: Normalize to get portfolio percentages
- Determine Shares: Calculate whole shares based on current prices
- Track Cash: Monitor uninvested cash due to fractional shares
chmod +x kmi30_tracker.py
chmod +x setup_automation.shsource venv/bin/activate # Activate virtual environment
pip install -r requirements.txt # Reinstall dependencies- Check internet connection
- PSX website might be down
- Website structure may have changed
- Check firewall/proxy settings
rm kmi30_tracker.db # Delete and recreate database
python3 database.py # Test database creation- Check
kmi30_tracker.logfor detailed error messages - Use
--log-level DEBUGfor verbose output
python3 -c "
from database import KMI30Database
db = KMI30Database()
data = db.get_latest_data()
print(f'Latest data: {len(data)} stocks')
for stock in data[:5]:
print(f'{stock[\"symbol\"]}: {stock[\"current\"]} PKR')
"ls -lh kmi30_tracker.dbls -lh kmi30_tracker.log- Respectful Scraping: Built-in delays and proper headers
- Local Storage: All data stored locally in SQLite
- No Credentials: No API keys or passwords required
- Rate Limiting: Automatic throttling to avoid being blocked
For issues or questions:
- Check the log files first
- Review this README
- Test individual modules:
python3 scraper.py # Test scraping python3 database.py # Test database python3 calculator.py # Test calculations
This tool is for educational and personal use only. Please ensure compliance with:
- PSX website terms of service
- Pakistani securities regulations
- Your broker's trading policies
The authors are not responsible for any financial decisions made using this tool.