Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Why do I get 'None' as value for SharpeRatio?

    General Code/Help
    4
    5
    2107
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      CptanPanic last edited by

      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())
      
      '''
      Richard O'Regan 1 Reply Last reply Reply Quote 2
      • Richard O'Regan
        Richard O'Regan @CptanPanic last edited by

        @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!

        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by

          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.

          1 Reply Last reply Reply Quote 4
          • D
            duality_ last edited by

            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 change riskfreerate to 0.0, I get a ratio of 0.024310682470976966.

            1 Reply Last reply Reply Quote 2
            • B
              backtrader administrators last edited by

              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

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
              $(document).ready(function () { app.coldLoad(); }); }