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[源代码]¶
基类:
AnalyzerThis analyzer calculates the SharpeRatio of a strategy using a risk-free asset, which is simply an interest rate
Params:
timeframe: (default:TimeFrame.Years) # Trading periodcompression(default:1) # Specific trading periodOnly 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 calculationExpressed in annual terms (see
convertratebelow)convertrate(default:True) # Whether to convert risk-free rate from annual to monthly, weekly, daily, no intraday supportConvert the
riskfreeratefrom annual to monthly, weekly or daily rate. Sub-day conversions are not supportedfactor(default:None) # If not specified, will convert according to specified date, 1 year = 12 months = 52 weeks = 252 trading daysIf
None, the conversion factor for the risk-free rate from annual to the chosen timeframe will be chosen from a predefined tableDays: 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 returnIf
convertrateis True, the SharpeRatio will be delivered in thetimeframeof choice.On most occasions, the SharpeRatio is delivered in annualized form. Convert the
riskfreeratefrom annual to monthly, weekly or daily rate. Sub-day conversions are not supportedstddev_sample(default:False) # Whether to subtract 1 when calculating standard deviationIf this is set to
Truethe standard deviation will be calculated decreasing the denominator in the mean by1. 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' correctiondaysfactor(default:None) # Legacy codeOld naming for
factor. If set to anything else thanNoneand thetimeframeisTimeFrame.Daysit will be assumed this is old code and the value will be usedlegacyannual(default:False) # Only applies to years, use annualized return analyzerUse the
AnnualReturnreturn analyzer, which as the name implies only works for yearsfund(default:None) # Net asset mode or fund mode, by default will auto-detectIf 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_fundmodein the broker documentationSet it to
TrueorFalsefor 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}¶
- class backtrader.analyzers.sharpe.SharpeRatioA[源代码]¶
基类:
SharpeRatioExtension 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),)¶