backtest
Test trading strategies on historical data with Monte Carlo simulation
What this skill does
# Backtest - Complete API Reference
Validate trading strategies using historical data, walk-forward analysis, and Monte Carlo simulation.
---
## Chat Commands
### Run Backtest
```
/backtest momentum --from 2024-01-01 --to 2024-12-31
/backtest mean-reversion --market "Trump 2028" --days 90
/backtest my-strategy --capital 10000
```
### Quick Stats
```
/backtest stats momentum Show strategy metrics
/backtest compare momentum arb Compare two strategies
/backtest monte-carlo momentum Run Monte Carlo simulation
```
### Results
```
/backtest results Show recent results
/backtest stats Alias for results
/backtest results <id> --detailed Detailed breakdown
/backtest export Export last results as CSV
```
---
## TypeScript API Reference
### Create Backtest Engine
```typescript
import { createBacktestEngine } from 'clodds/backtest';
const backtest = createBacktestEngine({
// Data source
dataSource: 'polymarket', // or custom data provider
// Capital
initialCapital: 10000,
// Fees (Polymarket: 0% on most markets; Kalshi: ~1.2% avg)
fees: {
maker: 0, // 0% maker fee (Polymarket most markets)
taker: 0, // 0% taker fee (Polymarket most markets)
// For 15-min crypto markets or Kalshi, use: taker: 0.012
},
// Slippage model
slippageModel: 'realistic', // 'none' | 'fixed' | 'realistic'
slippageBps: 10,
});
```
### Run Basic Backtest
```typescript
const result = await backtest.run({
strategy: 'momentum',
startDate: '2024-01-01',
endDate: '2024-12-31',
parameters: {
lookbackPeriod: 14,
entryThreshold: 0.02,
exitThreshold: 0.01,
},
});
console.log(`Total Return: ${result.totalReturn}%`);
console.log(`Sharpe Ratio: ${result.sharpeRatio}`);
console.log(`Max Drawdown: ${result.maxDrawdown}%`);
console.log(`Win Rate: ${result.winRate}%`);
console.log(`Profit Factor: ${result.profitFactor}`);
```
### Walk-Forward Analysis
```typescript
// Out-of-sample validation
const wf = await backtest.walkForward({
strategy: 'momentum',
startDate: '2023-01-01',
endDate: '2024-12-31',
// Train/test split
trainPeriod: '6M',
testPeriod: '1M',
step: '1M',
// Optimization
optimize: ['lookbackPeriod', 'entryThreshold'],
optimizationMetric: 'sharpe',
});
console.log(`In-Sample Sharpe: ${wf.inSampleSharpe}`);
console.log(`Out-of-Sample Sharpe: ${wf.outOfSampleSharpe}`);
console.log(`Overfitting Ratio: ${wf.overfitRatio}`);
```
### Monte Carlo Simulation
```typescript
// Stress test with randomization
const mc = await backtest.monteCarlo({
strategy: 'momentum',
trades: historicalTrades,
// Simulation settings
simulations: 10000,
confidenceLevel: 0.95,
// Randomization
shuffleTrades: true,
randomizeReturns: true,
});
console.log(`Expected Return: ${mc.expectedReturn}%`);
console.log(`95% VaR: ${mc.valueAtRisk}%`);
console.log(`Worst Case: ${mc.worstCase}%`);
console.log(`Best Case: ${mc.bestCase}%`);
console.log(`Probability of Profit: ${mc.probProfit}%`);
```
### Performance Metrics
```typescript
const metrics = await backtest.getMetrics(result);
console.log('=== Performance ===');
console.log(`Total Return: ${metrics.totalReturn}%`);
console.log(`CAGR: ${metrics.cagr}%`);
console.log(`Volatility: ${metrics.volatility}%`);
console.log('=== Risk ===');
console.log(`Sharpe Ratio: ${metrics.sharpeRatio}`);
console.log(`Sortino Ratio: ${metrics.sortinoRatio}`);
console.log(`Max Drawdown: ${metrics.maxDrawdown}%`);
console.log(`Max Drawdown Duration: ${metrics.maxDrawdownDuration} days`);
console.log('=== Trading ===');
console.log(`Total Trades: ${metrics.totalTrades}`);
console.log(`Win Rate: ${metrics.winRate}%`);
console.log(`Profit Factor: ${metrics.profitFactor}`);
console.log(`Avg Win: ${metrics.avgWin}%`);
console.log(`Avg Loss: ${metrics.avgLoss}%`);
console.log(`Expectancy: ${metrics.expectancy}%`);
```
### Custom Strategy
```typescript
// Define custom strategy
const myStrategy = {
name: 'my-strategy',
onData: async (data, context) => {
const price = data.price;
const sma = data.indicators.sma(20);
if (price < sma * 0.95 && !context.hasPosition) {
return { action: 'buy', size: context.availableCapital * 0.1 };
}
if (price > sma * 1.05 && context.hasPosition) {
return { action: 'sell', size: 'all' };
}
return { action: 'hold' };
},
};
const result = await backtest.run({
strategy: myStrategy,
startDate: '2024-01-01',
endDate: '2024-12-31',
});
```
---
## Built-in Strategies
| Strategy | Description |
|----------|-------------|
| `momentum` | Follow price trends |
| `mean-reversion` | Buy dips, sell rallies |
| `arbitrage` | Cross-platform price differences |
| `breakout` | Enter on range breakouts |
| `pairs` | Correlated market pairs |
---
## Metrics Explained
| Metric | Good Value | Description |
|--------|------------|-------------|
| **Sharpe Ratio** | > 1.0 | Risk-adjusted return |
| **Sortino Ratio** | > 1.5 | Downside-adjusted return |
| **Max Drawdown** | < 20% | Worst peak-to-trough |
| **Win Rate** | > 50% | Winning trades % |
| **Profit Factor** | > 1.5 | Gross profit / gross loss |
| **Expectancy** | > 0 | Expected $ per trade |
---
## Best Practices
1. **Use walk-forward** — Avoid overfitting
2. **Include fees** — Realistic cost modeling
3. **Test multiple periods** — Don't cherry-pick dates
4. **Monte Carlo** — Understand variance
5. **Out-of-sample** — Always validate on unseen data
Related in Code Review
gstack
IncludedFast headless browser for QA testing and site dogfooding. Navigate pages, interact with elements, verify state, diff before/after, take annotated screenshots, test responsive layouts, forms, uploads, dialogs, and capture bug evidence. Use when asked to open or test a site, verify a deployment, dogfood a user flow, or file a bug with screenshots. (gstack)
startup-due-diligence
IncludedLegal due diligence review for seed-stage and Series A startups (US, Delaware C-Corp focus). Supports both investor and founder perspectives. Capabilities include: (1) Interactive document review and issue spotting; (2) Document request list generation; (3) Cap table and SAFE/convertible note analysis; (4) Red flag identification with severity ratings; (5) Diligence report generation. TRIGGERS: due diligence, DD, startup investment, cap table review, Series A, seed round, investor diligence, legal review startup, SAFE analysis, convertible note, 409A, founder vesting.
interview-master
IncludedThis skill should be used when the user asks to "generate interview questions", "prepare for interview", "optimize resume", "conduct mock interview", "analyze git commits for resume", "generate resume from code", "review my resume", or mentions interview preparation, career assistance, or extracting project experience from git history. Provides comprehensive interview and career development guidance for both job seekers and interviewers.
fix-issue
IncludedFixes GitHub issues using parallel analysis agents for root cause investigation, code exploration, and regression detection. Reads issue context from gh CLI, searches codebase and memory for related patterns, generates a fix with tests, and links the resolution back to the issue via PR. Includes prevention analysis to avoid recurrence. Use when debugging errors, resolving regressions, fixing bugs, or triaging issues.
sf-apex
IncludedGenerates and reviews Salesforce Apex code with 150-point scoring. TRIGGER when: user writes, reviews, or fixes Apex classes, triggers, test classes, batch/queueable/schedulable jobs, or touches .cls/.trigger files. DO NOT TRIGGER when: LWC JavaScript (use sf-lwc), Flow XML (use sf-flow), SOQL-only queries (use sf-soql), or non-Salesforce code.
swift-development
IncludedComprehensive Swift development for building, testing, and deploying iOS/macOS applications. Use when Claude needs to: (1) Build Swift packages or Xcode projects from command line, (2) Run tests with XCTest or Swift Testing framework, (3) Manage iOS simulators with simctl, (4) Handle code signing, provisioning profiles, and app distribution, (5) Format or lint Swift code with SwiftFormat/SwiftLint, (6) Work with Swift Package Manager (SPM), (7) Implement Swift 6 concurrency patterns (async/await, actors, Sendable), (8) Create SwiftUI views with MVVM architecture, (9) Set up Core Data or SwiftData persistence, or any other Swift/iOS/macOS development tasks.