Why do I get 'None' as value for SharpeRatio?
-
So I am new to backtrader, and have setup an initial strategy. I have a few analyzer added, and the other ones give a value, but the SharpeRatio one gives me 'None' Any ideas?
cerebro = bt.Cerebro() cerebro.broker.setcash(1000.0) print('STart Portfolio Value: %.2f' % cerebro.broker.getvalue()) # Add a strategy cerebro.addstrategy(TestStrategy) # Pass it to the backtrader datafeed and add it to the cerebro #data_feed = bt.feeds.PandasData(dataname=data['USDT_ETH']) # Create a Data Feed data_feed = bt.feeds.InfluxDB( dataname="USDT_ETH", username="admin", password="conStant", database="finance", host="192.168.1.82", open="open", close="close", high="high", low="low", # Do not pass values before this date fromdate=datetime.datetime(2017, 1, 1), # Do not pass values after this date todate=datetime.datetime(2017, 12, 31)) cerebro.adddata(data_feed) data_feed = bt.feeds.InfluxDB( dataname="USDT_BTC", username="admin", password="conStant", database="finance", host="192.168.1.82", open="open", close="close", high="high", low="low", # Do not pass values before this date fromdate=datetime.datetime(2017, 1, 1), # Do not pass values after this date todate=datetime.datetime(2017, 12, 31)) cerebro.adddata(data_feed) # Analyzer cerebro.addanalyzer(btanalyzers.SharpeRatio, _name='mysharpe') cerebro.addanalyzer(btanalyzers.AnnualReturn, _name='annual') thestrats = cerebro.run() thestrat = thestrats[0] cerebro.plot() print('Sharpe Ratio:', thestrat.analyzers.mysharpe.get_analysis()) print('Anual Ratio:', thestrat.analyzers.annual.get_analysis()) print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue()) '''
-
@CptanPanic Ha, I had exactly the same thing! I figured I must be doing something wrong but instead of investing time resolving, since I don't look at Sharpe much anyway, I decided to ignore for now and I'd come back to it later. Would be interested to see the solution!
-
Because the analyzer has not been able to calculate any
SharpeRatio
If you only have 1 year of data and taking into account that the default timeframe for the calculation is
years
, no calculation can take place. Sharpe needs at least 2 samples for the given timeframe, to calculate the variance.Rather than using
AnnualReturn
, consider usingTimeReturn
and specify the actual target timeframe. -
So I used the
TimeReturn
, but get a negative result.
My data candles are spaced 30 minutes apart, so I did:cerebro.addanalyzer(btanalyzers.SharpeRatio, _name='sharpe', timeframe=bt.TimeFrame.Minutes, compression=30)
But this is what I get when calling
print
on the result:SharpeRatio: - sharperatio: -0.922698416321026
What's the reason for this? One possibility I see is that the
convertrate
does not support sub-day conversions. If I changeriskfreerate
to0.0
, I get a ratio of0.024310682470976966
. -
Docs - Analyzers Reference - Sharpe Ratio
In 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
-
@backtrader Thanks bro! You are the best.