Sanity check on sharpe ratio calculation please

Discussion in 'Strategy Development' started by nooby_mcnoob, Nov 15, 2018.

  1. Hi, I've got a sparse array of $ pnls (sparse since the algorithm does not trade every day) and I'm assuming risk-free rate is 0. Is the below an appropriate calculation for the annualized Sharpe ratio? Should I be including days where I wouldn't trade?

    By the way, I tried to use pyfolio and it just keeps crashing on every input so recommendations for other libraries that I could use in quantitative analysis would be great.

    >>> returns = pnls.cumsum()
    >>> r = returns.diff()
    >>> sr = r.mean()/r.std()
    >>> sr * np.sqrt(252)
  2. tommcginnis


    Yeah, that "252" has to be matched throughout -- it's assuming the "asset" (your algo) is exposed to market return and market risk every trading day. FWIW, if you set a running average right after your pnls.cumsum, the result would be a running PnL mean that would diminish with each 0-day. (If I'm reading your set-up correctly.) That would save you having to poke 0s into your data.
  3. I'm not sure I understand what you mean by "exposed to market return and market risk." The algorithm is designed to (maybe) trade once a day and go home flat. On days it doesn't want to trade, it does nothing. What it sounds like you're saying is I need to include the 0s?
  4. Ah I think I get the logic. Sharpe ratio includes the risk-free return so a true comparison would include the risk free return on a daily basis. Interesting!
  5. After adjusting the SR to account for only days where the algo trades, the SR drops to 4.37. Does this equity curve look like it matches the SR?

  6. tommcginnis


    Uh-ohhhhh! Now you're gonna come upon Sorbino, and get into a Sharpe v Sotino debate, and you're brain will ex-plode. And horror and ugliness will follow. I am so sorry. But here... :D
    nooby_mcnoob likes this.
  7. Rolling sharpe ratio:


    Do you think it's worth trying to figure out what was going on between 2012-2015?
    tommcginnis likes this.
  8. Do you think *any* of this is statistically significant?

    Craig66 likes this.
  9. I don't know. How should I find out?
  10. tommcginnis


    Well, first -- "0" and below would suck (mid-2013 to mid-2014), but the rest is well above that.
    As for what 'wasn't working' so well? Impossible to say without knowing
    1) the type of trading -- bullish, bearish, direction-blind/trend-exploiting, iron condors??
    2) knowing the periodicity of it/them.

    But as a general statement, the periodicity of the model should be a hair short of the periodicity of the market -- if the market moves in 14 day cycles, you want to be in 12-day cycles. (Or less! Subject to check, YMMV, FYI, and all that...) If the market cycles are at-or-below your models lookback parameters, you're never going to have it react fast enough to take you out as you expected, or to have put you in as you'd hoped.
    #10     Nov 15, 2018