backtrader.analyzers.sharpe module

Sharpe Ratio Analyzer Module - Sharpe ratio calculation.

This module provides the SharpeRatio analyzer for calculating the Sharpe ratio of a strategy using a risk-free asset.

Classes:

SharpeRatio: Analyzer that calculates Sharpe ratio. SharpeRatio_Annual: Annualized Sharpe ratio analyzer.

示例

>>> cerebro = bt.Cerebro()
>>> cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='sharpe')
>>> results = cerebro.run()
>>> print(results[0].analyzers.sharpe.get_analysis())
class backtrader.analyzers.sharpe.SharpeRatio[源代码]

基类:Analyzer

This analyzer calculates the SharpeRatio of a strategy using a risk-free asset, which is simply an interest rate

Params:

  • timeframe: (default: TimeFrame.Years) # Trading period

  • compression (default: 1) # Specific trading period

    Only used for sub-day timeframes to, for example, work on an hourly timeframe by specifying "TimeFrame.Minutes" and 60 as compression

  • riskfreerate (default: 0.01 -> 1%) # Risk-free rate used for Sharpe ratio calculation

    Expressed in annual terms (see convertrate below)

  • convertrate (default: True) # Whether to convert risk-free rate from annual to monthly, weekly, daily, no intraday support

    Convert the riskfreerate from annual to monthly, weekly or daily rate. Sub-day conversions are not supported

  • factor (default: None) # If not specified, will convert according to specified date, 1 year = 12 months = 52 weeks = 252 trading days

    If None, the conversion factor for the risk-free rate from annual to the chosen timeframe will be chosen from a predefined table

    Days: 252, Weeks: 52, Months: 12, Years: 1

    Else the specified value will be used

  • annualize (default: False) # If set to True, will convert to annualized return

    If convertrate is True, the SharpeRatio will be delivered in the timeframe of choice.

    On most occasions, the SharpeRatio is delivered in annualized form. Convert the riskfreerate from annual to monthly, weekly or daily rate. Sub-day conversions are not supported

  • stddev_sample (default: False) # Whether to subtract 1 when calculating standard deviation

    If this is set to True the standard deviation will be calculated decreasing the denominator in the mean by 1. This is used when calculating the standard deviation if it's considered that not all samples are used for the calculation. This is known as the Bessels' correction

  • daysfactor (default: None) # Legacy code

    Old naming for factor. If set to anything else than None and the timeframe is TimeFrame.Days it will be assumed this is old code and the value will be used

  • legacyannual (default: False) # Only applies to years, use annualized return analyzer

    Use the AnnualReturn return analyzer, which as the name implies only works for years

  • fund (default: None) # Net asset mode or fund mode, by default will auto-detect

    If None, the actual mode of the broker (fundmode - True/False) will be autodetected to decide if the returns are based on the total net asset value or on the fund value. See set_fundmode in the broker documentation

    Set it to True or False for a specific behavior

- Get_analysis

Returns a dictionary with key "sharperatio" holding the ratio

params = (('timeframe', 8), ('compression', 1), ('riskfreerate', 0.01), ('factor', None), ('convertrate', True), ('annualize', False), ('stddev_sample', False), ('daysfactor', None), ('legacyannual', False), ('fund', None))
RATEFACTORS = {5: 252, 6: 52, 7: 12, 8: 1}
__init__(*args, **kwargs)[源代码]

Initialize the SharpeRatio analyzer.

Sets up the return analyzer based on the legacyannual parameter.

参数:
  • *args -- Positional arguments.

  • **kwargs -- Keyword arguments for analyzer parameters.

stop()[源代码]

Calculate and store the Sharpe ratio when analysis ends.

Performs the following calculations: 1. Retrieves returns from the sub-analyzer 2. Converts risk-free rate if needed 3. Calculates excess returns 4. Computes the Sharpe ratio 5. Optionally annualizes the result

class backtrader.analyzers.sharpe.SharpeRatioA[源代码]

基类:SharpeRatio

Extension of the SharpeRatio which returns the Sharpe Ratio directly in annualized form

The following param has been changed from SharpeRatio

  • annualize (default: True)

params = (('annualize', True),)