Zettt

Sharpe Ratio v4

I'm publishing this indicator freely, because I'd like to get it reviewed by other people. This indicator was written whilst reading the book Systematic Trading by Robert Carver. In this book Carver describes trading rules that use a "dynamic" position size based on something like an evolving Sharpe Ratio. There are only a few other Sharpe indicators on TradingView, but they are either undocumented or use closed source code. You can use the following code as you wish for your own projects.

I'd like to let other people see this work, and let me know where they think this script is wrong, so that I can improve it.

Here's a basic rundown of Sharpe Ratio and its calculation.

SR is defined as: (excess) return minus the risk free rate divided by standard deviation of those returns. (This is where we're uncertain. Is the standard deviation of the returns, or just the closes?) But anyway the calculation itself is pretty simple:

SR = (r – b) ÷ s

Where r is the return of the asset over a certain period.
b is the interest rate of the risk-free asset.
s is the standard deviation of the returns over the same period.

For this indicator to "work" correctly, we're assuming the risk-free rate is 0. In fact, I did not include b at all in the indicator because it would make things too complicated, and go beyond the aim of this work.

To calculate the returns over a certain period, I'm using Rate of Change. Then calculating the standard deviation of those returns is pretty easy because we can use the same lookback period we used for ROC for the StDev calculation, thus:

averageReturn = ta.roc(close, lookbackLength)
stdev = ta.stdev(averageReturn, lookbackLength)
sharpe = (averageReturn / stdev)


Please leave a comment below if you believe this is incorrect. The chart shows a normal ROC indicator for comparison. I've also created a "bands" version of this indicator, which I'm planning to also release. The Keltner channel is just for comparing it with the StDev bands.

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this script on a chart?