Skip to content

medhansh29/Investment_Agent

Repository files navigation

AI Investment Agent

An automated, AI-powered portfolio manager designed to optimize your investments, monitor the market, and execute trades via Alpaca. Combining mathematical precision (Modern Portfolio Theory) with LLM-based reasoning (Gemini), it allows for a "Human-in-the-Loop" automated investing experience.

🚀 Key Features

🧠 Thesis-Driven RAG (New!)

The agent doesn't just look at prices; it reads the news.

  • Market Intelligence: Fetches live headlines from BBC World and Yahoo Finance.
  • Context Awareness: Uses Gemini AI to calculate real-time risk scores (0-10) for:
    • Global Conflict (Protects Defense stocks like LMT, RTX)
    • Inflation Trend (Protects Energy/Real Assets like XOM, CVX)
    • Economic Instability (Protects Recession Shields like KO, PG, JNJ)
  • Thesis Protection: If the world is dangerous, the agent refuses to sell your insurance assets, even if they are up.

🛡️ "Safe Fortress" Logic

  • Anti-Casino Constraint: In Conservative mode, the agent is mathematically blocked from buying high-volatility assets (>35% Ann. Vol) like Crypto or Meme stocks.
  • Core Preservation: Strict constraints prevent "dumping" your long-term winners. It forces the optimizer to build around your core positions.

🎯 Smart Limit Orders (New!)

Instead of executing blindly at market prices, Aegis computes 10-day forward volatility and drift forecasts.

  • Dynamic Limits: Calculates the statistically probable "local low" for Buys, and "local high" for Sells individually for each stock based on its historical variance.
  • Good-Till-Cancelled (GTC): Orders sit patiently on the books waiting for the perfect technical entry or exit within your 2-week execution block.

🤖 Intelligent Rebalancing

  • Bi-Weekly Checkups: Runs optimization to keep your portfolio aligned with your risk profile.
  • Executive Summaries: All rebalances and investment reports are generated into executive, highly scannable HTML emails featuring bolded mathematical rationales, tables grouping holds by sector, and detailed news summaries.
  • Tax-Awareness: The AI Advisor explains why a trade is happening, warning you if a sale might trigger a tax event but is necessary for risk reduction.

🔍 Daily Watchdog & Volatility Auto-Rebalance (New!)

  • Auto-Monitoring: Checks your portfolio every morning.
  • Aegis Masterclass (V3): Generates a highly personalized, daily educational curriculum. The agent maintains a persistent topic memory to build a progressive syllabus over time, natively formats math using LaTeX blocks, and curates reading lists.
  • Dynamic Persona: Analysis is tailored explicitly to your demographic and risk profile.
  • Smart Alerts & Autonomous Action: Sends an email ONLY if a stock moves >10% intraday. It then automatically pivots into an emergency rebalance to address the high volatility, generating a specialized 🚨 Volatility Rebalance Report directly to your inbox detailing whether it bought the dip or legally held the asset as insurance.

📈 Portfolio Performance Analyzer (New!)

Developers and users can monitor the agent's performance in terms of raw trade outputs and portfolio metrics:

  • FIFO Trade Matching: Pairs down historical FILL activities into individual closed trades to compute exact Win Rates and Profit Factors.
  • Money-Weighted Returns (IRR): Computes annualized Internal Rate of Return (IRR) to correctly evaluate performance while adjusting for deposits/withdrawals.
  • Risk Metrics: Calculates Annualized Sharpe Ratio, Annualized Sortino Ratio, Daily Volatility, and Maximum Drawdown.
  • Reporting & Visualization: Generates terminal dashboards, Markdown reports (data/performance_report.md), and matplotlib equity curve comparison charts with drawdowns (data/performance_chart.png) along with individual trade returns and symbol PnL contributions (data/trade_analysis_chart.png).

☁️ GitHub Actions Automation

The agent runs autonomously in the cloud via GitHub Actions schedulers:

  • Daily Watchdog: Checks your positions every day at 4:30 PM. Alerts you only if a stock moves >10%.
  • Bi-Weekly Tactical Rebalance: Reminds you every two weeks to realign your portfolio.
  • Monthly Investment Day: Reminds you on the 1st of the month to deploy new capital.

✉️ Beautiful HTML Emails

  • All AI communication is sent beautifully formatted directly to your inbox.
  • Supports native LaTeX rendering via CodeCogs, macro-sector tables, conditional green/red market badging, and emojis.

🛠️ Setup & Installation

Follow these steps to deploy Aegis as your personal autonomous investment agent.

1. Clone & Install

git clone https://github.com/medhansh29/Investment_Agent.git
cd Investment_Agent
pip install -r requirements.txt

2. Obtaining API Keys

Aegis relies on three free services to operate. You must obtain the following keys:

  1. Alpaca Trading API (Broker):
    • Go to the Alpaca Dashboard.
    • Sign up and generate your Paper Trading API Key and Secret Key. Use Paper keys first to safely test the agent with play money!
  2. Google Gemini (AI Brain):
    • Medhansh will provide you with the shared GEMINI_API_KEY to use.
  3. Gmail SMTP (Email Alerts):
    • You need an App Password to allow the agent to send you Masterclass emails.
    • Go to your Google Account -> Security -> 2-Step Verification -> App Passwords.
    • Create a new App Password named "Aegis". It will be a 16-character string.

3. Initialize Templates

Copy the provided template files to create your personal local configuration.

Environment Variables:

cp .env.example .env

Open .env and paste the exact keys you generated in Step 2.

User State & Portfolio Memory:

cp data/user_state.example.json data/user_state.json

Open data/user_state.json and configure your name, email, baseline monthly_investment (e.g., $1000), and your universe of stock tickers you want the agent to track.

4. Enable Cloud Automation

To have the agent run in the background 24/7 without keeping your computer on, simply push it to a private GitHub repository!

  1. Go to your repository Settings -> Secrets and variables -> Actions.
  2. Add the following New repository secrets:
    • ALPACA_API_KEY
    • ALPACA_SECRET_KEY
    • GEMINI_API_KEY
    • SMTP_EMAIL (Your Gmail address)
    • SMTP_PASSWORD (Your 16-digit App Password)
  3. Under the Actions tab, ensure workflows are enabled. The .github/workflows/ cron jobs will now automatically run the Daily Watchdog, Bi-Weekly Rebalance, and Monthly Investment loops.

🖥️ Usage

You can interact with the agent manually using the helper script in the scripts/ folder:

Interactive Mode

The standard way to run the agent manually.

# Verify and execute trades (Bi-Weekly Rebalance)
./scripts/run_agent.sh interactive rebalance

# Deploy monthly capital (Monthly Invest)
./scripts/run_agent.sh interactive invest

# Choose mode interactively
./scripts/run_agent.sh interactive

Trigger Workflows Manually

Simulate the automated alerts (sends actionable notifications/emails):

./scripts/run_agent.sh rebalance   # Triggers Bi-Weekly Alert
./scripts/run_agent.sh invest      # Triggers Monthly Alert
./scripts/run_agent.sh daily       # Runs Daily Watchdog Check

Portfolio Performance Analysis

To evaluate the agent's performance and output reports:

# Run analysis on live Alpaca Paper/Live account
python3 scripts/analyze_performance.py

# Run analysis offline using local CSV cached data
python3 scripts/analyze_performance.py --use-csv

📂 File Structure

The project follows a modular Source Layout:

  • src/: Source Code
    • core/: The Brain (main.py, config.py, state_manager.py)
    • strategies/: The Math (portfolio_optimizer.py)
    • integrations/: The Connections (alpaca_client.py, gemini_client.py)
    • utils/: Helpers (notifications, templates, performance_analyzer.py)
  • scripts/: Automation Scripts
    • run_agent.sh: Main entry point helper.
    • analyze_performance.py: Performance analysis and reporting script.
    • install_cron.sh: Setup script for automation.
  • data/: Storage
    • user_state.json: Portfolio history and settings.
    • performance_report.md: Generated report (git ignored).
    • performance_chart.png: Generated equity curve and drawdown chart (git ignored).
    • trade_analysis_chart.png: Generated closed trade distribution and realized PnL contribution chart (git ignored).

Disclaimer: This is an automated trading tool. Use at your own risk. Paper trading is recommended for testing.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors