---
title: "Algorithm Statistics"
description: "The StatisticsResults object tracks all the statistics of your algorithm. You can access them during execution."
canonical: https://www.quantconnect.com/docs/v2/writing-algorithms/statistics/algorithm-statistics
markdown: https://www.quantconnect.com/docs/v2/writing-algorithms/statistics/algorithm-statistics.md
language: python
source: https://github.com/QuantConnect/Documentation/tree/master/03%20Writing%20Algorithms/39%20Statistics/01%20Algorithm%20Statistics
modified: 2026-10-29
index: https://www.quantconnect.com/llms.txt
---

# Algorithm Statistics

[Documentation](https://www.quantconnect.com/docs/v2.md) > [Writing Algorithms](https://www.quantconnect.com/docs/v2/writing-algorithms.md) > [Statistics](https://www.quantconnect.com/docs/v2/writing-algorithms/statistics.md) > Algorithm Statistics

> Code examples are shown in Python. Append `?lang=csharp` to this URL for the C# version of this page.

## Introduction

The `StatisticsResults` object tracks all the statistics of your algorithm. You can access the statistics anywhere in your algorithm where you have a reference to the algorithm class. Some of these statistics are a function of the [risk free interest rate](https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/risk-free-interest-rate/key-concepts.md) and the [number of trading days per year](https://www.quantconnect.com/docs/v2/writing-algorithms/initialization.md#10-Set-Algorithm-Settings).

## Properties

The `StatisticsResults` object has the following properties:

*Members of `QuantConnect.Statistics.StatisticsResults` (JSON): https://www.quantconnect.com/services/inspector?type=T:QuantConnect.Statistics.StatisticsResults&language=python*

## Add Statistics

To add custom statistics to the `StatisticsResults` object, call the `set_summary_statistic` method with the statistic name and value.

```python
self.set_summary_statistic("name", value)
```

## Get Values

To get the `StatisticsResults`, use the `statistics` member of the algorithm class.

```python
sharpe = self.statistics.total_performance.portfolio_statistics.sharpe_ratio
```

To get the value of a custom statistic while your algorithm runs, index the `summary` member with the statistic name. The values in the `summary` dictionary are strings, so you may need to cast the value that returns to a different data type.

```python
value = self.statistics.summary["name"]
```

The following table describe other ways to get the statistics after your backtest completes:

| Deployment Target | Access Tools |
| --- | --- |
| QuantConnect Cloud | [Backtest results page](https://www.quantconnect.com/docs/v2/cloud-platform/backtesting/results.md) in the Algorithm Lab [Backtest results page](https://www.quantconnect.com/docs/v2/local-platform/backtesting/results.md) on Local Platform [ReadBacktest](https://www.quantconnect.com/docs/v2/research-environment/meta-analysis/backtest-analysis.md) API method [/backtests/read](https://www.quantconnect.com/docs/v2/cloud-platform/api-reference/backtest-management/read-backtest/backtest-statistics.md) endpoint |
| Local | [Backtest results page](https://www.quantconnect.com/docs/v2/local-platform/backtesting/results.md) on Local Platform `<organizationWorkspace> / <projectName> / backtests / <unixTimestamp> / <algorithmId>.json` |

## Examples

The following examples demonstrate common practices for getting algorithm statistics.

### Example 1: Portfolio Statistics

To evaluate the algorithm's best domain or regime, we can log the portfolio statistics daily along the backtest to analyze. To do so, we log the Sharpe Ratio, Beta, and 95% Value-at-Risk at the end of each day, storing them in the object store for assessment later.

```python
class AlgorithmStatisticsAlgorithm(QCAlgorithm):
    _statistics = ["Time,Sharpe,Beta,95VaR"]

    def initialize(self) -> None:
        self.set_start_date(2024, 9, 12)
        self.set_end_date(2024, 10, 1)
        self.set_cash(1000000)

        # Request SPY data to trade.
        self.spy = self.add_equity("SPY").symbol
        # Create an EMA indicator to generate trade signals.
        self._ema = self.ema(self.spy, 20, Resolution.DAILY)
        # Warm-up indicator for immediate readiness.
        self.warm_up_indicator(self.spy, self._ema, Resolution.DAILY)

    def on_data(self, slice: Slice) -> None:
        bar = slice.bars.get(self.spy)
        if bar:
            # Trend-following strategy using price above or below EMA.
            # If the price is above EMA, SPY is in an uptrend, and we buy it.
            if bar.close > self._ema.current.value and not self.portfolio[self.spy].is_long:
                self.set_holdings(self.spy, 1)
            elif bar.close < self._ema.current.value and not self.portfolio[self.spy].is_short:
                self.set_holdings(self.spy, -1)

    def on_end_of_day(self, symbol: Symbol) -> None:
        # Obtain the algorithm statistics interested.
        sharpe = self.statistics.total_performance.portfolio_statistics.sharpe_ratio
        b = self.statistics.total_performance.portfolio_statistics.beta
        var = self.statistics.total_performance.portfolio_statistics.value_at_risk_95

        # Plot the statistics.
        self.plot("Statistics", "Sharpe", sharpe)
        self.plot("Statistics", "Beta", b)
        self.plot("Statistics", "Value-at-Risk", var)

        # Write to save the statistics.
        self._statistics.append(f'{self.time.strftime("%Y%m%d")},{sharpe},{b},{var}')

    def on_end_of_algorithm(self) -> None:
        # Save the logged statistics for later access in the object store.
        self.object_store.save(f'{self.project_id}/algorithm-statistics', '\n'.join(self._statistics))
```

### Other Examples

For more examples, see the following algorithms:

**Demonstration Algorithms**

- [StatisticsResultsAlgorithm.py](https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/StatisticsResultsAlgorithm.py) (Python)
