For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

sharpe ratio wrong?

  • Hello!

    I've been using the result from the sharpe ratio analyzer for quite some time simply as follows:
    cerebro.addanalyzer(btanalyzers.SharpeRatio, _name='mysharpe')
    My data is hourly... but i don't think i need to take that into consideration in the sharpe ratio parameters, right?
    A week ago someone who is more advanced in trading than me saw my equity curves and said there's no way the sharpe ratio is correct. So I implemented it by myself:

    def get_sharpe(thestrat, f_result, total_days):
        daily_ret_1 = thestrat.analyzers.dailyret.get_analysis().values()
        len_list_daily_rets = len(daily_ret_1)
        #to only get relevant data without max period etc
        daily_ret = list(daily_ret_1)[(len_list_daily_rets-total_days-1):]
        mean_daily_ret = mean(daily_ret)
        daily_ret_array = np.array(daily_ret)
        daily_volatility = np.sqrt(sum((daily_ret_array - mean_daily_ret)**2)/(len_list_daily_rets-1))
        annualized_volatility = np.sqrt(365)*daily_volatility
        cummulative_return = list(thestrat.analyzers.timeret.get_analysis().values())[0]
        annual_return = (1 + cummulative_return) ** (365. / total_days) - 1
        sharperatio = annual_return / annualized_volatility
        f_result.write("Sharpe Ratio calc: {:.2f} \n".format(sharperatio))

    I got completely different results..
    Has anyone an idea why that is? Did I miss a parameter when adding the analyzer?

    Thank you!

  • administrators

    Question number 1:

    • Have you read the documentation? (Let me answer that one: No) (At least the part about the SharpeRatio analyzer)

    Question number 2:

    • What and in which form are you trying to calculate?

    Question number 3 (set of questions)

    • Don't you think that if you consider some results right and some wrong, you could provide those results for comparison?
    • A working snippet? Sample data?

    Question number 4:

    • Why 365 in your code? That's not what people use.

  • Question number 1:

    I have read it several times... probably i didn't understand it. Or I didn't find an answer to my problem.

    Question number 2:

    I am trying to calculate an annual sharpe ratio for a bitcoin strategy using hourly charts.

    Question number 3:

    Yes you're right. Equity curve of my strategy:
    Strategy long startdate: 2015-01-01 00:00:00 enddate: 2018-06-01 00:00:00
    Sharpe Ratio calc: 3.84
    Return: 4461.33%
    Annualized Return: 205.93%
    Sharpe Ratio: 0.65
    Max Drawdown: 29.26%
    MAR: 7.04
    Number Trades: 135; Number Longs: 135; Number Shorts: 0
    Win/Loss Ratio 0.88; Long Win/Loss Ratio: 0.88; Short Win/Loss Ratio: nan
    Mean Return: 3.33%; Mean Ret per Long: 3.33%; Mean Ret per Short: nan%
    AverageHoldingBars 124.61

    You see 2 different sharpe ratios: "Sharpe Ratio: 0.65" is what I receive from the analyzer. "Sharpe Ratio calc: 3.84" is what I calculated with my own implementation.

    Question number 4:

    I use 365, because it's Bitcoin... so traded every day of the year.


  • So my guess now is that it's exactly this part of the parameters which causes the issue:

    • list itemfactor (default: None)

    If None, the conversion factor for the riskfree rate from annual to the chosen timeframe will be chosen from a predefined table

    Days: 252, Weeks: 52, Months: 12, Years: 1

    Else the specified value will be used

    I should probably specify that it should use 365 days?

  • administrators

    @alain said in sharpe ratio wrong?:

    I am trying to calculate an annual sharpe ratio for a bitcoin strategy using hourly charts.

    And where and what is your risk free rate? (In your calculations)

  • I didn't include any riskfree rate. Or say I set it to zero.
    So you're right, there are for sure some differences in my calculations compared to the analyzer. You think if I change these two things (# days/rate) it would give me the "right" sharpe ratio ?

  • administrators

    Let me be frank:

    • You are calculating something ... which doesn't match the definition of SharpeRatio

    @alain said in sharpe ratio wrong?:

    it would give me the "right" sharpe ratio ?

    And you expect something which is the right SharpeRatio. There isn't such a thing. You use the literature, try to be as close as possible to it, use the parameters which fit your situation and you get a result.

    The most surprising thing is that your first conclusion is: the SharpeRatio in backtrader is wrong.

    Furthermore you use the SharpeRatio (without considering the validity of the calculation) with daily returns and you probably expect that to match the SharpeRatio when you use monthly or yearly returns. Well, it won't even if you annualize things.

    Please read this:

  • If I had concluded something I wouldn't be asking here...
    Ok, gonna do some reading.

  • administrators

    Sorry but you have, even if your intent was other.

    There is a huge difference between a thread title like this:

    • Question with regards to the SharpeRatio calculation

    and this:

    • sharpe ratio wrong?

    By writing your own calculation to compare results you had already concluded that the results provided by backtrader weren't right, but not that you were not using the analyzer properly.

    It's not that I believe your post is ill-intentioned, but you had already concluded.