# Backtest Strategy

> Runs backtests on trading strategies using historical market data. Calculates performance metrics including Sharpe ratio, maximum drawdown, win rate, total return, and generates equity curves. Trigger when the user requests backtesting, strategy simulation, or performance evaluation.

- **Type:** Skill
- **Install:** `agentstack add skill-qunyou-agent-finance-skills-backtest-strategy`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [qunyou-agent](https://agentstack.voostack.com/s/qunyou-agent)
- **Installs:** 0
- **Category:** [Data & Analytics](https://agentstack.voostack.com/c/data-and-analytics)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [qunyou-agent](https://github.com/qunyou-agent)
- **Source:** https://github.com/qunyou-agent/finance-skills/tree/main/skills/trading/backtest-strategy

## Install

```sh
agentstack add skill-qunyou-agent-finance-skills-backtest-strategy
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# Backtest Strategy

Runs backtests on trading strategies and provides performance analysis.

## Real Code Reference

- `tradinglearn/backtest/backtester.py` — `Backtester` class: `run_backtest(data, strategy_class, strategy_params)`, `get_performance()`, `plot_results()`
- `tradinglearn/strategies/macd_strategy.py` — `MACDStrategy` with `generate_signals(data)` → positions
- `tradinglearn/pytdx2/backtest.py` — `BacktestEngine` + `BacktestConfig` + `BaseStrategy`
- `tradinglearn/utils/parameter_optimizer.py` — `ParameterOptimizer.optimize_macd_parameters()`

## Architecture

```
DataLoader → Strategy signals → Portfolio tracking → Metrics calculation → Report
```

1. **DataLoader** — fetch historical K-line via `data_fetcher.fetch_stock_data(ticker, start, end)`
2. **Strategy** — generate buy/sell signals per bar (`MACDStrategy(fast, slow, signal)`)
3. **Backtester** — `run_backtest(data, MACDStrategy, params)` iterates bars, tracks positions
4. **Metrics** — `get_performance()` returns Sharpe, max drawdown, win rate, total return, CAGR
5. **Plot** — `plot_results()` shows price vs portfolio value overlay

## Usage

```python
from backtest.backtester import Backtester
from strategies.macd_strategy import MACDStrategy
from utils.data_fetcher import fetch_stock_data

data = fetch_stock_data("000001", start_date="2024-01-01", end_date="2025-01-01")
bt = Backtester(initial_capital=100000.0, transaction_cost=0.001)
bt.run_backtest(data, MACDStrategy, {"fast_period": 12, "slow_period": 26, "signal_period": 9})
bt.generate_detailed_report()
bt.plot_results()
```

## Key Checks

- No lookahead bias — signal at bar `t` uses only data up to bar `t`
- Out-of-sample validation separate from parameter optimization
- Account for transaction costs (commission + slippage)
- Handle corporate actions (splits, dividends) in price data

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [qunyou-agent](https://github.com/qunyou-agent)
- **Source:** [qunyou-agent/finance-skills](https://github.com/qunyou-agent/finance-skills)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-qunyou-agent-finance-skills-backtest-strategy
- Seller: https://agentstack.voostack.com/s/qunyou-agent
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
