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.

Mã nguồn mở

Với tinh thần TradingView, tác giả của tập lệnh này đã xuất bản nó dưới dạng mã nguồn mở, vì vậy các nhà giao dịch có thể hiểu và xác minh nó. Chúc mừng tác giả! Bạn có thể sử dụng mã này miễn phí, nhưng việc sử dụng lại mã này trong một ấn phẩm chịu sự điều chỉnh của Nội quy nội bộ. Bạn có thể yêu thích nó để sử dụng nó trên biểu đồ.

Thông báo miễn trừ trách nhiệm

Thông tin và ấn phẩm không có nghĩa là và không cấu thành, tài chính, đầu tư, kinh doanh, hoặc các loại lời khuyên hoặc khuyến nghị khác được cung cấp hoặc xác nhận bởi TradingView. Đọc thêm trong Điều khoản sử dụng.

Bạn muốn sử dụng tập lệnh này trên biểu đồ?