backtest-expert
by @finskills
Design, execute, and evaluate quantitative trading strategies using historical price data and Fama-French factor attribution via the Finskills API.
clawhub install finskills-backtest-expertπ About This Skill
name: Backtest Expert version: 1.0.2 description: "Design, execute, and evaluate quantitative trading strategies using historical price data and Fama-French factor attribution via the Finskills API." author: finskills metadata: openclaw: requires: env: - FINSKILLS_API_KEY primaryEnv: FINSKILLS_API_KEY homepage: https://github.com/finskills/backtest-expert
Backtest Expert
Design, execute, and evaluate quantitative trading strategies using historical price data from the Finskills API. Transforms a natural-language strategy hypothesis into a rigorous backtest with performance metrics, drawdown analysis, Fama-French factor attribution, and walk-forward validation.
Setup
API Key required β Register at https://finskills.net to get your free key.
Header: X-API-Key:
> Get your API key: Register at https://finskills.net β free tier available, Pro plan unlocks real-time quotes, history, and financials.
When to Activate This Skill
Activate when the user:
Strategy Hypothesis Formulation
Before fetching data, clarify the strategy with the user:
1. Universe: Which stocks? (Single stock, S&P 500, sector, custom list) 2. Signal: What is the entry trigger? (Moving average crossover, RSI level, fundamental metric, etc.) 3. Entry: Long, Short, or Long/Short? 4. Exit: Trailing stop, fixed target, time-based, signal reversal? 5. Holding Period: Daily, weekly, monthly? 6. Position Sizing: Equal weight, volatility-adjusted, Kelly? 7. Benchmark: SPY (default) or sector ETF?
State the strategy in a formal grammar:
ENTRY: Buy {SYMBOL} when {condition}
EXIT: Sell when {condition} OR stop at {-X%}
HOLD: Max {N} days / bars
SIZING: {$N fixed / X% of portfolio / equal weight}
BENCH: {SPY / QQQ / custom}
PERIOD: From {YYYY-MM-DD} to {YYYY-MM-DD}
Data Retrieval β Finskills API Calls
1. Historical OHLCV for Strategy Symbols
GET https://finskills.net/v1/stocks/history/{SYMBOL}?period=5y&interval=1d
Parameters:
period: 1y, 2y, 5y, 10y, maxinterval: 1d (daily), 1wk (weekly), 1mo (monthly)Extract: date, open, high, low, close, volume, adjustedClose
Repeat for each symbol in the strategy universe AND the benchmark (SPY or QQQ).
2. Fama-French 3-Factor Data
GET https://finskills.net/v1/free/market/fama-french
Extract: Mkt-RF (market excess return), SMB (small-minus-big), HML (high-minus-low), RF (risk-free rate)
Use for: Attribution analysis β what % of strategy return is explained by market, size, value factorsAnalysis Workflow
Step 1 β Data Preparation
1. Align all price series to the same trading calendar (drop non-overlapping dates).
2. Use adjustedClose for all calculations (accounts for splits and dividends).
3. Compute daily returns: r_t = (adjustedClose_t / adjustedClose_{t-1}) - 1
4. Handle missing data: forward-fill up to 2 days; drop if missing > 2 consecutive days.
Step 2 β Signal Generation
Based on the strategy rules, generate entry/exit signals for each day in the backtest.
Example Signal Implementations:
Simple Moving Average Crossover:
SMA_fast = rolling_mean(adjustedClose, window=fast_n)
SMA_slow = rolling_mean(adjustedClose, window=slow_n)
Signal = 1 (Long) when SMA_fast crosses above SMA_slow
Signal = 0 (Flat) when SMA_fast crosses below SMA_slow
RSI Reversion:
RSI = 100 - 100 / (1 + avg_gain_14 / avg_loss_14)
Signal = 1 (Long) when RSI < 30 (oversold)
Signal = 0 (Flat) when RSI > 70 (overbought)
Momentum:
Momentum_N = current_price / price_N_days_ago - 1
Signal = 1 when Momentum_N > threshold
Signal = 0 otherwise
Step 3 β Portfolio Returns Calculation
strategy_return_t = Signal_{t-1} Γ return_t (signal from prior day, no lookahead)
benchmark_return_t = SPY daily return
Compute cumulative returns:
cumulative_return = PRODUCT(1 + strategy_return_t) - 1
Apply transaction costs (default: 0.10% per round-trip trade, adjustable):
cost_t = 0.001 Γ |Signal_t - Signal_{t-1}| (cost only on signal changes)
net_return_t = strategy_return_t - cost_t
Step 4 β Performance Metrics
Compute the full performance scorecard:
| Metric | Formula | |--------|---------| | Total Return | PRODUCT(1 + r_t) - 1 | | Annualized Return (CAGR) | (1 + total_return)^(252/n_days) - 1 | | Annualized Volatility | STD(r_t) Γ β252 | | Sharpe Ratio | (CAGR - rf_rate) / annualized_vol | | Sortino Ratio | (CAGR - rf_rate) / downside_vol | | Max Drawdown | max(1 - V_t / max(V_s for s β€ t)) | | Calmar Ratio | CAGR / Max Drawdown | | Win Rate | % of trading days with positive return | | Avg Win / Avg Loss | mean positive return / mean negative return | | Profit Factor | sum wins / sum losses | | Alpha (vs benchmark) | CAGR_strategy - CAGR_benchmark | | Beta | COV(r_strategy, r_benchmark) / VAR(r_benchmark) | | Information Ratio | Alpha / tracking error | | Number of Trades | Count of signal transitions | | Avg Holding Period | Avg days per trade |
Step 5 β Fama-French Factor Attribution
Regress strategy excess returns against F-F factors:
r_strategy - RF = Ξ± + Ξ²β(Mkt-RF) + Ξ²β(SMB) + Ξ²β(HML) + Ξ΅
Interpret:
Step 6 β Walk-Forward Validation
To detect overfitting, split the full history:
Walk-Forward Efficiency Ratio:
WFE = OOS_Sharpe / IS_Sharpe
Step 7 β Risk Scenarios
Stress-test the strategy in 3 regimes: 1. 2020 COVID Crash: Feb 19 β Mar 23, 2020 (S&P -34% in 33 days) 2. 2022 Bear Market: Jan 1 β Dec 31, 2022 (S&P -19.4%) 3. 2008 Financial Crisis: Oct 1 β Dec 31, 2008 (S&P -38% in quarter)
For each stress scenario, report the strategy's return vs. SPY return.
Output Format
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β BACKTEST REPORT β {STRATEGY NAME} β
β Period: {start_date} to {end_date} ({N} trading days) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββπ STRATEGY SUMMARY
Universe: {SYMBOL(s) or index}
Entry Signal: {description}
Exit Signal: {description}
Position Type: {Long / Long-Short}
Benchmark: {SPY / QQQ}
Tx Cost: 0.10% per round-trip
π CUMULATIVE RETURNS
Strategy: +{X.X}% ($100K β ${value}K)
Benchmark: +{X.X}% ($100K β ${value}K)
Alpha: +{X.X}% annualized
π PERFORMANCE SCORECARD
Strategy Benchmark Ξ vs. Bench
CAGR: {X.X}% {X.X}% +{X.X}%
Volatility: {X.X}% {X.X}%
Sharpe: {X.XX} {X.XX} +{X.XX}
Sortino: {X.XX} {X.XX}
Max Drawdown: -{X.X}% -{X.X}%
Calmar: {X.XX} {X.XX}
Win Rate: {X.X}% β
Profit Factor: {X.XX} β
Avg Hold: {N} days β
N Trades: {count} β
π DRAWDOWN ANALYSIS
Worst Drawdown: -{X}% (from {date} to {date}, {N} days to recover)
2nd Worst Drawdown: -{X}%
Average Drawdown: -{X}%
𧬠FACTOR ATTRIBUTION (Fama-French)
Ξ± (Annualized): {X.XX}% [{statistically significant yes/no}]
Ξ² Market: {X.XX}
Ξ² SMB (size): {X.XX} [{small-cap / large-cap} tilt]
Ξ² HML (value): {X.XX} [{value / growth} tilt]
RΒ² (explained): {X}%
π WALK-FORWARD VALIDATION
In-Sample ({dates}): Sharpe {X.XX}, CAGR {X.X}%
Out-of-Sample ({dates}): Sharpe {X.XX}, CAGR {X.X}%
WF Efficiency Ratio: {X.XX} β {Generalizes well / Some degradation / Likely overfit}
π₯ STRESS TEST
2020 COVID Crash: Strategy {+/-X}% vs SPY -{X}%
2022 Bear Market: Strategy {+/-X}% vs SPY -{X}%
2008 Q4 Crash: Strategy {+/-X}% vs SPY -{X}%
π― VERDICT
Signal Quality: {Strong / Moderate / Weak / Overfit}
Trade Live? {Yes / Caution / No β further development needed}
Suggested Refinement: {1β2 concrete suggestions to improve the strategy}
Limitations
βοΈ Configuration
API Key required β Register at https://finskills.net to get your free key.
Header: X-API-Key:
> Get your API key: Register at https://finskills.net β free tier available, Pro plan unlocks real-time quotes, history, and financials.