Theil–Sen Estimator

The Theil-Sen estimator is a nonparametric statistics method for robustly fitting a regression line to sample points (1,2).

As stated in the Wikipedia article (3), the method is "the most popular nonparametric technique for estimating a linear trend" in the applied sciences due to its robustness to outliers and limited assumptions regarding measurement errors.

Relation with other Methods
The Theil-Sen estimator can be significantly more accurate than simple linear regression (least squares) for skewed and heteroskedastic data.

Method Description
The script computes all the slopes between pairs of points and takes the median as the estimate of the regression slope, m. Subsequently, the intercept, b, is determined from the sample points as the median of y(i) − mx(i) values. The regression line in the slope–intercept form, y = mx + b, is then plotted along with the calculated prediction interval (estimated by means of the root-mean-square error).

I have added two options for how to handle pairs of points:
  • Method == "All" to use the slopes of all pairs of points;
  • Method == "Random" to use the slopes of randomly generated pairs of points.

The random choice of the pairs of points is based on the Wichmann–Hill is a pseudorandom number generator.

The reason for introducing the "Random" method is that the calculation of the median involves sorting the array of slopes (the size of N*(N-1)/2, where N is the number of sample points). This is a computationally demanding procedure, which runs into the limit on the cycle computation time (200 ms ) set in TradingView. Therefore, the "All" method works only with Length < 50.

Also note that the number of lookback points is limited by by the maximum array size allowed in TradingView.

1. Sen, P. K. (1968) "Estimates of the regression coefficient based on Kendall's tau." JASA, 1379-1389.
2. Theil, H. (1950) "A rank-invariant method of linear and polynomial regression analysis." Reprinted in 1992 in Henri Theil’s contributions to economics and econometrics, Springer, 345-381.
3. https://en.wikipedia.org/wiki/Theil%E2%80%93Sen_estimator
Phát hành các Ghi chú: Fixed an issue causing instability when using the "Random" method on high-resolution real-time charts (thanks @labtab for spotting it)
Phát hành các Ghi chú: added the following features:
  • the plot changes color (to orange by default) when the price is outside the prediction interval channel
  • alert conditions for the channel break are now available
  • the with of the prediction interval can be tuned by an input parameter (Prediction Interval Multiplier); its value should be interpreted as the quantile of the normal distribution with 2 (default) roughly corresponding to the 95% prediction interval
Phát hành các Ghi chú: The built-in procedure is now used to evaluate the median.
Mã nguồn mở

Với tinh thần của TradingView, tác giả đã xuất bản tập lệnh theo mã nguồn mở, vì thế trader có thể dễ dàng hiểu và tùy chỉnh được. Bạn có thể sử dụng miễn phí, hoặc tùy chỉnh lại mã đã được cấp phép bởi Quy tắc Chung. Bạn có thể sử dụng nó trên biểu đồ.

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

Bình luận

tbiktag, I think it is brilliant.
Your code is a wonderful assembly of statistics and coding craftsmanship.
Thank you for bringing such fresh ideas from the past.
I encourage you to bring more ideas from the the world of statistics.
Thank you for your time and generosity. Truly appreciated.
+3 Phản hồi
tbiktag labtab
@labtab, Thanks a lot, I'm happy to hear that!
Phản hồi
Thanks mate - love it.
Would it be possible to have a version which allows it to look back to the start of the day instead of x bars?
+1 Phản hồi
tbiktag Prize_Fighter
@Prize_Fighter, thanks for the comment! That's a good idea, I will think about implementing it.
Phản hồi
Thank you for the wonderful script.
+1 Phản hồi
tbiktag RajeevNaik
@RajeevNaik, You are welcome!
Phản hồi
Superb, thank you so much for sharing this!
+1 Phản hồi
tbiktag allanster
@allanster, You are welcome! And thanks for the feedback!
Phản hồi
great script and explanation
+1 Phản hồi
Thank you! 🔥🔥
Phản hồi
Trang chủ Bộ lọc cổ phiếu Bộ lọc Forex Bộ lọc Tiền điện tử Lịch kinh tế Giới thiệu Tính năng Biểu đồ Trả phí Giới thiệu bạn Quy tắc Áp dụng Trung tâm Trợ giúp Giải pháp cho Website & Nhà môi giới Widget Giải pháp biểu đồ Thư viện Biểu đồ Lightweight Blog & Tin tức Twitter
Hồ sơ Tùy chỉnh Hồ sơ Tài khoản và Thanh toán Giới thiệu bạn Xu Ticket Hỗ trợ của tôi Trung tâm Trợ giúp Tin nhắn riêng Trò chuyện Đăng xuất