# tanbeng123/is4226_dual_trend_portfolio_strat architecture

> Architecture diagram of the GitHub repository tanbeng123/is4226_dual_trend_portfolio_strat, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tanbeng123/is4226_dual_trend_portfolio_strat
- Repository: https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat

## Overview

#### Purpose and Entry Points
This is a quantitative-finance backtest for a long-only EMA-smoothed On-Balance Volume (OBV) strategy across major technology stocks, compared with SPY from 2010–2019. The repository is extremely compact: the primary runnable artifact is likely `ema_obv_portfolio`; `run_backtest.py` is the script entry point. `README.md` documents the intended notebook-oriented workflow.

#### Strategy Signal Layer
The core strategy generates directional signals by combining:
- cumulative volume-adjusted price moves (raw OBV),
- EMA smoothing of OBV using λ=0.99,
- price and smoothed-OBV directional agreement,
- 50-day and 200-day moving-average trend confirmation.

This layer transforms per-ticker daily OHLCV-style market inputs into trade eligibility or position-direction signals.
Paths: `ema_obv_portfolio`, `README.md`

#### Market Data Boundary
Historical daily price and volume data are expected to come from Yahoo Finance via `yfinance`, for ten equities plus the SPY benchmark. Data acquisition is an external-service boundary; downstream calculations rely on aligned trading-date indices, adjusted price series, and volume fields.
Paths: `run_backtest.py`, `README.md`

#### Portfolio Construction and Risk Control
Signals are aggregated across the stock universe into a diversified, no-leverage portfolio. The stated design includes volatility targeting at a 20% annual target using a 30-day volatility window, which makes sizing/risk scaling a distinct stage after raw signal generation and before return realization.
Paths: `ema_obv_portfolio`, `README.md`

#### Expanding-Window Backtest Engine
The backtester should process daily observations chronologically, avoid future-data leakage through expanding-window validation, maintain positions/equity over time, and calculate portfolio returns against initial capital of $500,000. The documented academic assumptions are 1x long-only exposure and zero transaction costs.
Paths: `run_backtest.py`, `ema_obv_portfolio`

#### Benchmarking and Analytics
Portfolio equity is evaluated against SPY using cumulative return, CAGR, volatility, Sharpe ratio, alpha/beta, information ratio, drawdown, annual returns, and trade statistics. Attribution isolates ticker-level contribution, while benchmark comparison establishes whether strategy returns exceed market exposure.
Paths: `ema_obv_portfolio`, `README.md`

#### Reporting Outputs and Runtime
The intended runtime is Python with pandas/numpy for time-series processing, yfinance for data retrieval, and matplotlib/seaborn plus Jupyter for charts and dashboards. Expected outputs include equity-versus-SPY, drawdown, annual returns, a metrics dashboard, per-ticker attribution, and trade analysis.
Paths: `run_backtest.py`, `ema_obv_portfolio`, `README.md`

Main entry points: [`run_backtest.py`](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/run_backtest.py) (Aligned OHLCV data), [`ema_obv_portfolio`](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio) (Long-only signals), [`README.md`](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/README.md) (Stock universe + SPY)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Market Data"]
  node_yahoo{{"Yahoo Finance<br/>external market-data service"}}
  node_market_data[("Aligned OHLCV data<br/>time-series dataset<br/>[run_backtest.py]")]
  node_universe["Stock universe + SPY<br/>asset universe<br/>[README.md]"]
end

subgraph group_strategy["Strategy &amp; Portfolio"]
  node_core["ema_obv_portfolio<br/>strategy package"]
  node_obv["Raw OBV<br/>indicator calculation"]
  node_ema_obv["EMA-smoothed OBV<br/>indicator calculation"]
  node_trend["Trend confirmation<br/>moving-average filter"]
  node_signals["Long-only signals<br/>signal model"]
  node_risk["Volatility targeting<br/>portfolio sizing"]
  node_portfolio["Diversified positions<br/>portfolio construction"]
end

subgraph group_backtest["Backtest Engine"]
  node_entry["run_backtest.py<br/>script entry point<br/>[run_backtest.py]"]
  node_engine["Expanding-window engine<br/>chronological simulator"]
  node_equity[("Portfolio returns &amp; equity<br/>backtest results<br/>[run_backtest.py]")]
end

subgraph group_analytics["Benchmarking &amp; Reporting"]
  node_benchmark["SPY benchmark returns<br/>benchmark series"]
  node_analytics["Performance analytics<br/>metrics and attribution"]
  node_reports["Charts &amp; dashboards<br/>reporting output<br/>[README.md]"]
end

node_readme["README.md<br/>workflow documentation<br/>[README.md]"]

node_readme -.->|"documents workflow"| node_entry
node_entry -->|"requests data"| node_yahoo
node_universe -->|"selects symbols"| node_yahoo
node_yahoo -->|"daily prices and volume"| node_market_data
node_market_data -->|"price and volume"| node_obv
node_obv -->|"smooths"| node_ema_obv
node_market_data -->|"price history"| node_trend
node_ema_obv -->|"OBV direction"| node_signals
node_trend -->|"trend filter"| node_signals
node_signals -->|"eligible exposures"| node_risk
node_market_data -->|"30-day volatility"| node_risk
node_risk -->|"scaled weights"| node_portfolio
node_portfolio -->|"daily positions"| node_engine
node_market_data -->|"realized market returns"| node_engine
node_engine -->|"simulated results"| node_equity
node_market_data -->|"SPY prices"| node_benchmark
node_equity -->|"strategy performance"| node_analytics
node_benchmark -->|"comparison series"| node_analytics
node_analytics -->|"visualizations and tables"| node_reports
node_core -.->|"implements"| node_signals
node_core -.->|"implements"| node_engine

click node_entry "https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/run_backtest.py"
click node_core "https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio"
click node_readme "https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/README.md"
click node_market_data "https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/run_backtest.py"
click node_universe "https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/README.md"
click node_obv "https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio"
click node_ema_obv "https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio"
click node_trend "https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio"
click node_signals "https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio"
click node_risk "https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio"
click node_portfolio "https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio"
click node_engine "https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio"
click node_equity "https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/run_backtest.py"
click node_benchmark "https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio"
click node_analytics "https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio"
click node_reports "https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/README.md"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_yahoo,node_market_data,node_universe toneBlue
class node_core,node_obv,node_ema_obv,node_trend,node_signals,node_risk,node_portfolio toneAmber
class node_entry,node_engine,node_equity toneMint
class node_benchmark,node_analytics,node_reports toneRose
class node_readme toneNeutral
```

## Components

### Market Data

- **Yahoo Finance**: external market-data service · Historical daily equity and SPY data via yfinance.
- [Aligned OHLCV data](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/run_backtest.py): time-series dataset · Adjusted prices and volumes aligned by trading date. · `run_backtest.py`
- [Stock universe + SPY](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/README.md): asset universe · Ten technology equities and the SPY benchmark. · `README.md`

### Strategy & Portfolio

- [ema_obv_portfolio](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio): strategy package · Primary strategy and portfolio implementation. · `ema_obv_portfolio`
- [Raw OBV](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio): indicator calculation · Cumulative volume-adjusted price moves per ticker. · `ema_obv_portfolio`
- [EMA-smoothed OBV](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio): indicator calculation · OBV smoothing with λ=0.99. · `ema_obv_portfolio`
- [Trend confirmation](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio): moving-average filter · 50-day and 200-day price trend checks. · `ema_obv_portfolio`
- [Long-only signals](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio): signal model · Requires price and smoothed-OBV directional agreement with trend confirmation. · `ema_obv_portfolio`
- [Volatility targeting](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio): portfolio sizing · 30-day volatility scaling to a 20% annual target. · `ema_obv_portfolio`
- [Diversified positions](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio): portfolio construction · No-leverage, 1x long-only stock exposure. · `ema_obv_portfolio`

### Backtest Engine

- [run_backtest.py](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/run_backtest.py): script entry point · Runs the backtest workflow. · `run_backtest.py`
- [Expanding-window engine](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio): chronological simulator · Maintains positions, equity, and leakage-safe daily evaluation. · `ema_obv_portfolio`
- [Portfolio returns & equity](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/run_backtest.py): backtest results · Return series and equity curve from $500,000 initial capital. · `run_backtest.py`

### Benchmarking & Reporting

- [SPY benchmark returns](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio): benchmark series · `ema_obv_portfolio`
- [Performance analytics](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/ema_obv_portfolio): metrics and attribution · Calculates returns, risk, alpha/beta, drawdown, annual results, and trade statistics. · `ema_obv_portfolio`
- [Charts & dashboards](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/README.md): reporting output · Equity comparison, drawdown, annual returns, metrics, attribution, and trade analysis. · `README.md`

### Other components

- [README.md](https://github.com/tanbeng123/is4226_dual_trend_portfolio_strat/blob/main/README.md): workflow documentation · Documents the notebook-oriented workflow and assumptions. · `README.md`

## Connections

- README.md → run_backtest.py: documents workflow
- run_backtest.py → Yahoo Finance: requests data
- Stock universe + SPY → Yahoo Finance: selects symbols
- Yahoo Finance → Aligned OHLCV data: daily prices and volume
- Aligned OHLCV data → Raw OBV: price and volume
- Raw OBV → EMA-smoothed OBV: smooths
- Aligned OHLCV data → Trend confirmation: price history
- EMA-smoothed OBV → Long-only signals: OBV direction
- Trend confirmation → Long-only signals: trend filter
- Long-only signals → Volatility targeting: eligible exposures
- Aligned OHLCV data → Volatility targeting: 30-day volatility
- Volatility targeting → Diversified positions: scaled weights
- Diversified positions → Expanding-window engine: daily positions
- Aligned OHLCV data → Expanding-window engine: realized market returns
- Expanding-window engine → Portfolio returns & equity: simulated results
- Aligned OHLCV data → SPY benchmark returns: SPY prices
- Portfolio returns & equity → Performance analytics: strategy performance
- SPY benchmark returns → Performance analytics: comparison series
- Performance analytics → Charts & dashboards: visualizations and tables
- ema_obv_portfolio → Long-only signals: implements
- ema_obv_portfolio → Expanding-window engine: implements
