All skills
gracefullight avatar

/backtesting-trading-strategies

@833b232

Backtest crypto and traditional trading strategies against historical data. Calculates performance metrics (Sharpe, Sortino, max drawdown), generates equity curves, and optimizes strategy parameters. Use when user wants to test a trading strategy, validate signals, or compare approaches. Trigger with phrases like "backtest strategy", "test trading strategy", "historical performance", "simulate trades", "optimize parameters", or "validate signals".

Use this Skill: https://skilld.dev/gh/gracefullight/stock-checker/backtesting-trading-strategies

This session only. Nothing lands on disk.

referencesimplementation.md

≈1.1k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Implementation Guide

Overview

This guide covers implementing and extending the backtesting system.

Architecture

┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐
│   fetch_data    │────▶│    backtest     │────▶│    metrics      │
│   (data layer)  │     │   (engine)      │     │   (analysis)    │
└─────────────────┘     └─────────────────┘     └─────────────────┘
                               │
                               ▼
                        ┌─────────────────┐
                        │   strategies    │
                        │  (signal gen)   │
                        └─────────────────┘

Step 1: Install Dependencies

pip install pandas numpy yfinance matplotlib

# Optional for advanced features:
pip install ta-lib scipy scikit-learn

Step 2: Fetch Historical Data

# Fetch 2 years of daily BTC data
python scripts/fetch_data.py --symbol BTC-USD --period 2y --interval 1d

# Fetch specific date range
python scripts/fetch_data.py --symbol ETH-USD --start 2022-01-01 --end 2024-01-01

# Use CoinGecko for crypto (no Yahoo Finance ticker needed)
python scripts/fetch_data.py --symbol BTC --period 1y --source coingecko

Data is cached in data/{symbol}_{interval}.csv.

Step 3: Run Backtest

# Basic backtest
python scripts/backtest.py --strategy sma_crossover --symbol BTC-USD --period 1y

# With custom parameters
python scripts/backtest.py \
  --strategy rsi_reversal \
  --symbol ETH-USD \
  --period 6m \
  --capital 25000 \
  --params '{"period": 14, "overbought": 75, "oversold": 25}'

# Custom commission and slippage
python scripts/backtest.py \
  --strategy macd \
  --symbol SOL-USD \
  --period 1y \
  --commission 0.002 \
  --slippage 0.001

Step 4: Optimize Parameters

# Grid search for SMA crossover
python scripts/optimize.py \
  --strategy sma_crossover \
  --symbol BTC-USD \
  --period 1y \
  --param-grid '{"fast_period": [10, 20, 30, 50], "slow_period": [50, 100, 150, 200]}'

# Optimize RSI parameters
python scripts/optimize.py \
  --strategy rsi_reversal \
  --symbol ETH-USD \
  --param-grid '{"period": [7, 14, 21], "overbought": [70, 75, 80], "oversold": [20, 25, 30]}'

Step 5: Analyze Results

Results are saved to reports/ directory:

  • *_summary.txt - Performance metrics table
  • *_trades.csv - Trade log with entry/exit details
  • *_equity.csv - Equity curve data
  • *_chart.png - Visual equity curve and drawdown

Adding Custom Strategies

Create a new strategy by extending the base class:

# In scripts/strategies.py

class MyCustomStrategy(Strategy):
    """My custom trading strategy."""

    name = "my_strategy"
    lookback = 50  # Minimum bars needed

    def generate_signals(self, data: pd.DataFrame, params: Dict[str, Any]) -> Signal:
        # Your signal logic here
        threshold = params.get("threshold", 0.02)

        close = data["close"]
        returns = close.pct_change()

        # Example: buy after big drop, sell after big gain
        if returns.iloc[-1] < -threshold:
            return Signal(entry=True, direction="long")
        elif returns.iloc[-1] > threshold:
            return Signal(exit=True)

        return Signal()

# Register in STRATEGIES dict
STRATEGIES["my_strategy"] = MyCustomStrategy()

Configuration Options

Create config/settings.yaml:

data:
  provider: yfinance
  cache_dir: ./data
  default_interval: 1d

backtest:
  default_capital: 10000
  commission: 0.001      # 0.1% per trade
  slippage: 0.0005       # 0.05% slippage

risk:
  max_position_size: 0.95  # 95% of capital
  stop_loss: null          # Optional fixed stop loss
  take_profit: null        # Optional fixed take profit

reporting:
  output_dir: ./reports
  save_trades: true
  save_equity: true
  save_chart: true

Performance Tips

  1. Cache data: Fetch once, reuse for multiple backtests
  2. Use appropriate intervals: Daily for swing trading, hourly for day trading
  3. Test on out-of-sample data: Split data into train/test periods
  4. Watch for overfitting: Simpler strategies often generalize better
  5. Account for costs: Commission + slippage can erode profits significantly

Source: SKILL.md on GitHub

1 warning17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    This skill provides a comprehensive backtesting framework for cryptocurrency and traditional trading strategies. It allows users to fetch historical data from reliable sources like Yahoo Finance and CoinGecko, run simulations, and analyze performance metrics. No malicious behavior or security vulnerabilities were detected.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer7mo

    10/10 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 833b232. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 7 hours ago.

Activeupdated 6 months ago
What it can do
Reads files Edits files Runs commands
version
2.0.0
author
Jeremy Longshore <jeremy@intentsolutions.io>
All 6 allowed tools
ReadWriteEditGrepGlobBash(python:*)

README badge

README badge for gracefullight/stock-checker/backtesting-trading-strategies