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!


  • administrators

    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 using TimeReturn and specify the actual target timeframe.


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.