Skip to content

iamkhalid2/kwealth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“Š Munger-Buffett Analyst Swarm

A multi-agent stock analysis system that synthesizes opposing investment perspectives into balanced recommendations. The system channels Charlie Munger's skeptical "inversion thinking" and Warren Buffett's value-focused optimism through separate AI agents, then merges their insights into actionable investment theses.

Python 3.11+ License: MIT


🎯 What It Does

Given a stock ticker (e.g., AAPL, MSTR, TSLA), the system:

  1. Fetches real-time financial metrics and news via yfinance
  2. Generates a Bear Memo using Munger-style skepticism (focuses on risks, red flags, overvaluation)
  3. Generates a Bull Memo using Buffett-style value analysis (focuses on moats, pricing power, durability)
  4. Synthesizes both perspectives into a final recommendation with confidence scoring and caveats

Output: A structured report with:

  • Final recommendation: STRONG_BUY | BUY | HOLD | SELL | STRONG_SELL
  • Confidence score (0.0–1.0)
  • Balanced synthesis summary
  • Key caveats for independent verification
  • Full bear and bull analyst memos

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   User      β”‚
β”‚  (Ticker)   β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Streamlit UI (app.py)                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚               Scout Agent (scout.py)                  β”‚
β”‚  β€’ Fetches financial metrics from yfinance           β”‚
β”‚  β€’ Pulls recent news headlines                       β”‚
β”‚  β†’ Outputs: ScoutReport                              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β–Ό                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Bear Analyst    β”‚    β”‚  Bull Analyst    β”‚
β”‚  (Munger AI)     β”‚    β”‚  (Buffett AI)    β”‚
β”‚                  β”‚    β”‚                  β”‚
β”‚  Gemini LLM      β”‚    β”‚  Gemini LLM      β”‚
β”‚  + System Prompt β”‚    β”‚  + System Prompt β”‚
β”‚                  β”‚    β”‚                  β”‚
β”‚  β†’ AnalystMemo   β”‚    β”‚  β†’ AnalystMemo   β”‚
β”‚    (Bear)        β”‚    β”‚    (Bull)        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                       β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β–Ό
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚  Synthesizer Agent     β”‚
         β”‚  (synthesizer.py)      β”‚
         β”‚                        β”‚
         β”‚  Gemini LLM            β”‚
         β”‚  Weighs both theses    β”‚
         β”‚                        β”‚
         β”‚  β†’ FinalRecommendation β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
                     β–Ό
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚  Rich UI Report        β”‚
         β”‚  β€’ Colored badge       β”‚
         β”‚  β€’ Confidence score    β”‚
         β”‚  β€’ Synthesis summary   β”‚
         β”‚  β€’ Caveats list        β”‚
         β”‚  β€’ Expandable memos    β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Setup

Prerequisites

Installation

  1. Clone the repository

    git clone https://github.com/iamkhalid2/kwealth.git
    cd kwealth
  2. Create and activate virtual environment

    On Windows:

    python -m venv venv
    venv\Scripts\activate

    On macOS/Linux:

    python3 -m venv venv
    source venv/bin/activate
  3. Install dependencies

    pip install -r requirements.txt
  4. Configure API key

    Create a .env file (use .env.example as template):

    GEMINI_API_KEY=your_actual_api_key_here

πŸ’» Usage

Run the Streamlit UI

streamlit run app.py
# or
python -m streamlit run app.py

Then open your browser to http://localhost:8501

Note: Make sure your virtual environment is activated before running the app.

Example Workflow

  1. Enter a ticker: AAPL
  2. Click πŸ” Analyze
  3. Watch the multi-step progress:
    • βœ… Fetching data...
    • βœ… Generating Bear memo (Munger)...
    • βœ… Generating Bull memo (Buffett)...
    • βœ… Synthesizing recommendations...
  4. Review the final report with metrics, memos, and caveats

Programmatic Usage

from agents.scout import fetch_scout_report
from agents.analyst import generate_bear_memo, generate_bull_memo
from agents.synthesizer import synthesize

# Fetch data
report = fetch_scout_report("AAPL")

# Generate opposing perspectives
bear = generate_bear_memo(report)
bull = generate_bull_memo(report)

# Synthesize final recommendation
final = synthesize(bull, bear, report.metrics)

print(f"Recommendation: {final.recommendation}")
print(f"Confidence: {final.confidence_score}")
print(f"Summary: {final.synthesis_summary}")

πŸ”§ Configuration

Edit config.py to customize:

Setting Default Description
GEMINI_MODEL gemini-3-flash-preview Gemini model to use
REQUEST_TIMEOUT 30 API timeout in seconds
MAX_HEADLINES 5 Number of news headlines to fetch

πŸ“‚ Project Structure

kwealth/
β”œβ”€β”€ SPEC.md                 # Full technical specification
β”œβ”€β”€ requirements.txt        # Python dependencies
β”œβ”€β”€ .env.example            # API key template
β”œβ”€β”€ config.py               # Centralized configuration
β”œβ”€β”€ app.py                  # Streamlit UI (entry point)
└── agents/
    β”œβ”€β”€ __init__.py         # Package exports
    β”œβ”€β”€ schemas.py          # Pydantic data models
    β”œβ”€β”€ scout.py            # Financial data fetcher
    β”œβ”€β”€ analyst.py          # Bear/Bull memo generators
    └── synthesizer.py      # Final recommendation logic

🧠 How It Works

1. Scout Agent (Data Gathering)

  • Calls yfinance to fetch:
    • Financial metrics (P/E, P/B, D/E, FCF, market cap, etc.)
    • Recent news headlines
  • Normalizes data (e.g., converts yfinance's D/E percentage to ratio)
  • Returns structured ScoutReport

2. Analyst Agents (Perspective Generation)

Bear Agent (Munger) focuses on:

  • Hidden risks and overvaluation
  • Debt concerns and accounting red flags
  • Competitive threats and market saturation
  • Crypto/fad exposure warnings

Bull Agent (Buffett) focuses on:

  • Durable competitive moats
  • Brand power and pricing power
  • Free cash flow generation
  • Management quality and capital allocation

Both use Gemini's structured output to produce typed AnalystMemo objects with:

  • Summary (3-4 sentences)
  • Key points (3-5 bullets)
  • Risk/reward assessment
  • Confidence score

3. Synthesizer Agent (Final Judgment)

  • Receives both bear and bull memos + current metrics
  • Uses Gemini to weigh arguments intelligently (not simple averaging)
  • Produces:
    • Final recommendation enum
    • Confidence score (0.0–1.0)
    • Synthesis summary balancing both views
    • 3-5 specific caveats for user to verify independently

πŸ“Š Example Output

AAPL Analysis (January 2026)

Recommendation: SELL (Confidence: 70%)

Synthesis:

While Apple remains a premier cash-flow engine with an unrivaled ecosystem, the valuation has reached levels that ignore structural risks. A P/B ratio over 51 and 32x P/E for a maturing hardware business suggest the stock is priced for perfection. The bear argument regarding financial engineering is compelling; using debt to fund buybacks at these valuations suggests management is prioritizing EPS support over fundamental reinvestment...

Key Caveats:

  • ⚠️ Verify net cash position to determine if 152% D/E is mitigated by offshore liquidity
  • ⚠️ Monitor US/EU antitrust cases targeting App Store commission structures
  • ⚠️ Evaluate upcoming iPhone refresh cycle for genuine AI-driven super-cycle
  • ⚠️ Analyze Services growth sustainability if third-party payments are mandated
  • ⚠️ Check for shifts in capital allocation strategy at current price levels

πŸ›‘οΈ Error Handling

The system gracefully handles:

  • Invalid tickers: Shows user-friendly error message
  • API failures: Catches Gemini 503/timeout errors with clear messages
  • Missing data: Displays "N/A" for unavailable metrics (e.g., no dividend yield for growth stocks)

πŸ”’ Security Notes

  • Never commit .env with real API keys
  • API key is loaded via python-dotenv from .env
  • Use .env.example as a template for sharing

🀝 Contributing

Contributions welcome! Areas for improvement:

  • Add support for portfolio-level analysis
  • Integrate additional data sources (SEC filings, earnings transcripts)
  • Implement caching for repeated ticker lookups
  • Add charting/visualization for historical performance
  • Support non-US stocks

πŸ“„ License

MIT License - see LICENSE for details.


πŸ™ Acknowledgments

  • yfinance for financial data
  • Google Gemini for LLM-powered analysis
  • Streamlit for rapid UI prototyping
  • Inspired by the investment philosophies of Charlie Munger and Warren Buffett

⚠️ Disclaimer

This tool is for educational and research purposes only. It does not constitute financial advice. Always conduct your own due diligence and consult with a qualified financial advisor before making investment decisions. The system's recommendations are generated by AI and may contain errors or biases.

About

A multi-agent stock analysis system that synthesizes opposing investment perspectives into balanced recommendations.

Resources

License

Stars

Watchers

Forks

Contributors

Languages