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/

    Sharpe Ratio is Different when Position Size % is Different

    Indicators/Strategies/Analyzers
    2
    3
    30
    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.
    • R
      riskfreealpha last edited by

      The Sharpe ratio is supposed to be the same regardless of how much cash % is in the portfolio (ex: 90% vs 5%). However, I am getting two different sharpe ratios for the same strategy but with a different cash position. Here is the code:

      class MaCrossStrategy(bt.Strategy):
      
          def __init__(self):
              pass
              ma_fast = bt.ind.SMA(period= 50)
              ma_slow = bt.ind.SMA(period = 200)
      
              self.crossover = bt.ind.CrossOver(ma_fast, ma_slow)
      
          def next(self):
              if not self.position:
                  if self.crossover > 0:
                      self.buy()
              elif self.crossover < 0:
                  self.close()
      
      
      cerebro = bt.Cerebro()
      
      data = bt.feeds.YahooFinanceData(
              dataname = 'SPY',
              fromdate = datetime.datetime(2000, 1, 1),
              todate = datetime.datetime(2021, 4, 4)
      )
      cerebro.adddata(data)
      cerebro.addstrategy(MaCrossStrategy)
      cerebro.broker.setcash(1000000)
      cerebro.addsizer(bt.sizers.PercentSizer, percents = 10)
      cerebro.addanalyzer(btanalyzers.SharpeRatio, _name = "sharpe")
      cerebro.addanalyzer(btanalyzers.Transactions, _name = "trans")
      cerebro.addanalyzer(btanalyzers.TradeAnalyzer, _name = "trades")
      back = cerebro.run()
      print('Ending portfolio value: {:,.0f}'.format(cerebro.broker.getvalue()))
      print(back[0].analyzers.sharpe.get_analysis())
      print(back[0].analyzers.trans.get_analysis())
      profit = cerebro.broker.getvalue() - 1000000
      print('total profit: {:,.0f}'.format(profit))
      cerebro.plot()
      
      
      

      The Sharpe ratio I get for sizer = 10 (cerebro.addsizer(bt.sizers.PercentSizer, percents = 10) is -0.212 but the Sharpe ratio I get for sizer = 95 is 0.67.

      What is the reason for this and how can I solve it?

      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @riskfreealpha last edited by

        @riskfreealpha said in Sharpe Ratio is Different when Position Size % is Different:

        The Sharpe ratio is supposed to be the same regardless of how much cash % is in the portfolio (ex: 90% vs 5%).

        What do you base this assumption on?

        R 1 Reply Last reply Reply Quote 0
        • R
          riskfreealpha @run-out last edited by

          @run-out if you start out with a portfolio with no cash and then increase it to 50% cash, the expected return and standard deviation are also halved, thereby leaving the Sharpe ratio unchanged.

          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(); }); }