🚀 AI One-Click Generation of quantconnect Strategy Code

How to Set a Custom Benchmark in QuantConnect for Performance Comparison

quantconnect | Published on: 2025/12/9
The following content is generated by EasyQuant .

Problem Description

How do I compare my algorithm's performance against the SPY index?

Solution

To compare your algorithm's performance against the SPY index in QuantConnect, you should use the set_benchmark method within your initialize function.

Setting the benchmark accomplishes two things:

  1. Visual Comparison: It overlays the benchmark's performance on your strategy's equity curve in the backtest results.
  2. Statistical Comparison: It calculates key performance metrics (such as Alpha, Beta, and Information Ratio) relative to the specified benchmark.

Implementation Details

By default, QuantConnect often uses SPY as the benchmark, but it is best practice to define it explicitly to ensure your statistics are accurate for your specific strategy timeframe and asset class.

You can set the benchmark using the ticker string directly or a Symbol object.

Code Example

Below is a complete, runnable algorithm demonstrating how to explicitly set SPY as the benchmark.

# region imports
from AlgorithmImports import *
# endregion

class PerformanceBenchmarkAlgorithm(QCAlgorithm):

    def initialize(self):
        # 1. Set the dates and cash
        self.set_start_date(2020, 1, 1)
        self.set_end_date(2023, 1, 1)
        self.set_cash(100000)

        # 2. Add the assets you want to trade
        # (For this example, we trade AAPL, but compare against SPY)
        self.aapl = self.add_equity("AAPL", Resolution.DAILY).symbol

        # 3. Set the Benchmark to SPY
        # This ensures Alpha/Beta stats and the equity chart comparison use SPY.
        self.set_benchmark("SPY")

        # Simple strategy logic to demonstrate trading
        self.set_holdings(self.aapl, 0.5)

    def on_data(self, data: Slice):
        # Strategy logic would go here
        pass

Alternative: Plotting Manually

If you want to visualize the price of SPY on a separate custom chart (rather than the main equity curve) without affecting the algorithm's Alpha/Beta statistics, you can plot it manually:

    def on_data(self, data: Slice):
        if "SPY" in data.bars:
            self.plot("Benchmark Comparison", "SPY Price", data["SPY"].close)

Q&A

What happens if I don't set a benchmark?

If you do not explicitly call set_benchmark, QuantConnect defaults to using SPY. However, explicitly setting it is recommended to avoid ambiguity, especially if you migrate code between projects or if defaults change in future versions of LEAN.

Can I use a different asset as a benchmark?

Yes. You can use any ticker available in the data library (e.g., self.set_benchmark("BTCUSD") for crypto strategies or self.set_benchmark("AGG") for bond strategies). This is crucial for calculating accurate Alpha and Beta, as a crypto strategy should logically be compared to Bitcoin, not the S&P 500.

Does set_benchmark trade the asset?

No. set_benchmark only requests the data required to calculate statistics and draw the comparison chart. It does not place trades, consume buying power, or add the asset to your self.portfolio.

Can I use a custom calculated value as a benchmark?

Yes. You can define a custom benchmark by passing a function to set_benchmark. This is useful if you want to track a fixed return (e.g., risk-free rate) or a composite index of your own creation.

# Example: Set benchmark to the algorithm's initial cash (0% return benchmark)
self.set_benchmark(lambda x: self.initial_cash)