# How to Use AI Berkshire for Financial Analysis: A Complete Guide with Code Examples

> Explore AI Berkshire, an open-source platform for automated financial analysis. Learn to combine Python, markdown, and valuation tools with code examples for end-to-end reporting.

- Repository: [Xbt Lin/ai-berkshire](https://github.com/xbtlin/ai-berkshire)
- Tags: how-to-guide
- Published: 2026-06-28

---

**AI Berkshire is a modular, open-source research platform that combines Python data tools, markdown-based skills, and exact-arithmetic valuation utilities to automate end-to-end financial analysis, from data collection to structured report generation.**

The `xbtlin/ai-berkshire` repository provides a comprehensive toolkit for automating investment research. Built as a modular system of Python utilities and markdown-based "skills," it enables analysts to fetch market data, run rigorous valuations, and generate structured reports without requiring proprietary APIs.

## Core Architecture of AI Berkshire for Financial Analysis

The platform organizes functionality into three distinct layers that work together to create reproducible research workflows.

### Data Collection Layer

The **data-collection layer** handles external data ingestion through modules like [`tools/xueqiu_scraper.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/xueqiu_scraper.py) and [`tools/morningstar_fair_value.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/morningstar_fair_value.py). These scripts pull fundamentals, market prices, and ESG data from public sources such as Xueqiu and Morningstar, enabling cross-source verification without API keys.

### Financial Rigor Layer

The **financial-rigor layer** performs exact-arithmetic calculations and validation via [`tools/financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/financial_rigor.py) and [`tools/ashare_data.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/ashare_data.py). This layer ensures that discounted cash flow (DCF) models and multiple-based valuations avoid floating-point errors by using exact arithmetic methods.

### Research Skills Layer

The **research-skills layer** orchestrates these tools through markdown-based command bundles defined in files like [`skills/financial-data.md`](https://github.com/xbtlin/ai-berkshire/blob/main/skills/financial-data.md) and [`skills/investment-research.md`](https://github.com/xbtlin/ai-berkshire/blob/main/skills/investment-research.md). These skills automate the assembly of data, calculations, and narrative into polished reports such as `reports/阿里巴巴/阿里巴巴-业务前景展望-20260410.md`.

## End-to-End Financial Analysis Workflow

When executing a skill such as `financial-data`, AI Berkshire follows a five-step pipeline:

1. **Data Fetch** – The skill calls [`tools/ashare_data.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/ashare_data.py) to retrieve the latest price, balance-sheet, and cash-flow figures.
2. **Valuation** – [`tools/financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/financial_rigor.py) runs exact-arithmetic DCF models and cross-checks results with secondary sources like Morningstar.
3. **Screening** – Optional quality filters from [`tools/stock_screener.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/stock_screener.py) exclude companies failing debt-to-equity or free cash flow thresholds.
4. **Report Assembly** – The skill concatenates computed tables, narrative sections, and source citations into a markdown file.
5. **Output** – The final document is archived under `reports/` for direct viewing or export to PDF/HTML.

## Practical Code Examples for AI Berkshire Financial Analysis

### Executing a Full Valuation from the Command Line

Before running analysis, synchronize the latest skills using the maintenance script. Then execute a complete valuation for a specific ticker:

```bash

# Sync the latest codex-generated skills

python3 scripts/sync-codex-skills.py

# Run the financial-data skill for Kweichow Moutai (600519)

python3 -m codex_skills.financial_data --ticker 600519 --output reports/moutai_valuation.md

```

This command invokes the `financial-data` skill, which internally coordinates data retrieval from [`tools/ashare_data.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/ashare_data.py) and valuation logic from [`tools/financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/financial_rigor.py) before writing the markdown report.

### Programmatic Valuation with Python

For custom pipelines or back-testing, import the core utilities directly without the markdown skill layer:

```python
from tools.financial_rigor import ValuationEngine
from tools.ashare_data import AShareDataFetcher

# Fetch raw financials for a stock

fetcher = AShareDataFetcher()
balance_sheet, cash_flow, income_statement = fetcher.fetch('600519')

# Run exact-arithmetic DCF model

engine = ValuationEngine()
dcf_price = engine.discounted_cash_flow(
    cash_flow,
    discount_rate=0.07,
    terminal_growth=0.02,
    periods=5
)

print(f"Intrinsic DCF price for 600519: {dcf_price:.2f} CNY")

```

This approach is ideal for integrating AI Berkshire into existing analytics workflows or running batch valuations across multiple securities.

### Batch Screening and Momentum Back-testing

Screen multiple companies against quality criteria using the screener module:

```bash

# Screen A-shares by market cap and P/E ratio

python3 -m tools.stock_screener \
    --min_market_cap 50000000000 \
    --max_pe_ratio 20 \
    --exclude_sectors "金融,房地产" \
    --output reports/quality_screen.md

```

Run momentum strategy back-tests using the updated engine:

```bash
python3 -m tools.momentum_backtest_v2 \
    --lookback 90 \
    --holding-period 30 \
    --output reports/momentum_backtest.md

```

The [`momentum_backtest_v2.py`](https://github.com/xbtlin/ai-berkshire/blob/main/momentum_backtest_v2.py) module calculates annualized returns, Sharpe ratios, and maximum drawdown using the same data fetchers as the valuation tools, ensuring consistency across your financial analysis workflow.

## Summary

- AI Berkshire combines **data collection**, **exact-arithmetic valuation**, and **markdown-based skills** in a three-layer architecture maintained in the `xbtlin/ai-berkshire` repository.
- Key modules include [`tools/financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/financial_rigor.py) for valuations and [`tools/ashare_data.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/ashare_data.py) for A-share fundamentals.
- The workflow progresses from data fetching through valuation, screening, and structured report generation.
- All operations can be executed via command-line skills or direct Python imports for custom analysis pipelines.
- Generated reports are stored as version-controlled markdown files in the `reports/` directory.

## Frequently Asked Questions

### What data sources does AI Berkshire support?

The platform pulls data from public sources including Xueqiu and Morningstar through dedicated scrapers in [`tools/xueqiu_scraper.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/xueqiu_scraper.py) and [`tools/morningstar_fair_value.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/morningstar_fair_value.py). This design enables cross-source verification without requiring proprietary API keys or paid subscriptions.

### How does AI Berkshire ensure calculation accuracy?

The [`tools/financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/financial_rigor.py) module implements **exact-arithmetic valuation** methods, using precise decimal arithmetic rather than floating-point math to eliminate rounding errors in critical financial calculations like discounted cash flow and earnings multiples.

### Can I use AI Berkshire without the markdown skills?

Yes. While skills like `financial-data` provide convenient orchestration, you can import classes such as `ValuationEngine` and `AShareDataFetcher` directly from the `tools/` directory to build custom analysis pipelines or integrate with existing Python data workflows.

### Where are generated reports stored?

Completed analyses are written as markdown files to the `reports/` directory, following the structure demonstrated in `reports/阿里巴巴/阿里巴巴-业务前景展望-20260410.md`. These files can be rendered to PDF or HTML using standard markdown processors, or viewed directly in any text editor.