Automated portfolio tracking and rebalancing system for IG Trading (Irish online trading platform). Logs in, captures portfolio state, and automatically rebalances when needed.
-
Install dependencies:
pip install -r requirements.txt
-
Initialize the database:
python init_db.py
This creates the SQLite database for storing portfolio snapshots and rebalancing actions.
-
Set up credentials:
- For GitHub Actions: Add repository secrets:
IG_LOGIN_USERNAME- Your IG Trading usernameIG_LOGIN_PASSWORD- Your IG Trading password
- For local development: Export environment variables:
On Windows:
export IG_LOGIN_USERNAME="your_username" export IG_LOGIN_PASSWORD="your_password"
set IG_LOGIN_USERNAME=your_username set IG_LOGIN_PASSWORD=your_password
- For GitHub Actions: Add repository secrets:
Run the rebalancing script (recommended):
python ig_rebalance.pyThis script will:
- Log into IG Trading
- Open the trading platform
- Extract current portfolio data
- Save a snapshot to the database
- Calculate rebalancing actions (if needed)
- Execute rebalancing trades (if needed)
- Log all actions to the database
For testing login only:
python ig_login.pyTo view stored portfolio snapshots and rebalancing actions:
python view_db.pyThe system uses SQLite to store:
- Portfolio Snapshots: Historical records of portfolio state (positions, values, cash balance)
- Rebalancing Actions: Log of all rebalancing decisions and trades executed
The database file (ig_trading.db) persists between script runs and is excluded from git.
- Credentials are loaded from environment variables - never hardcode them in the script
- GitHub secrets are automatically available as environment variables in GitHub Actions
- Keep your credentials secure and never commit them to the repository
If login fails:
- Check the generated screenshot files (
.png) for debugging - Verify your credentials are correct
- IG Trading may have updated their login page structure - you may need to update the selectors in the script
- Some sites may require additional verification (2FA, CAPTCHA, etc.)
ig_rebalance.py: Main script for portfolio tracking and rebalancingig_login.py: Login and platform opening functionalitydatabase.py: Database helper functions for storing snapshots and actionsinit_db.py: Initialize the database schemaview_db.py: View database contents for monitoring
The framework is set up with:
- ✅ Login and platform access
- ✅ Database schema for snapshots and actions
- ✅ Rebalancing workflow structure
⚠️ Portfolio data extraction (needs IG Trading platform selectors)⚠️ Rebalancing logic (needs your strategy implementation)⚠️ Trade execution (needs IG Trading API/interface integration)
- Add portfolio data extraction: Update
extract_portfolio_data()inig_rebalance.pywith actual selectors from the IG Trading platform - Implement rebalancing logic: Update
calculate_rebalancing_actions()with your rebalancing strategy - Add trade execution: Update
execute_rebalancing_actions()to place actual trades on the platform
- Uses Selenium WebDriver for browser automation
- SQLite database for persistent storage
- Includes anti-detection measures to avoid being flagged as a bot
- Uses full XPath selectors for reliable element location
- Runs in non-headless mode so you can see the browser automation in action