Outlier Detector with N-Sigma Confidence Intervals

A detrended series that oscilates around zero is obtained after first differencing a time series (i.e. subtracting the closing price for a candle from the one immediately before, for example). Hypothetically, assuming that every detrended closing price is independent of each other (what might not be true!), these values will follow a normal distribution with mean zero and unknown variance sigma squared (assuming equal variance, what is also probably not true as volatility changes over time for different pairs). After studentizing, they follow a Student's t-distribution, but as the sample size increases (back periods > 30, at least), they follow a standard normal distribution.

This script was developed for personal use and the idea is spotting candles that are at least 99% bigger than average (using N = 3) as they will cross the upper and lower confidence interval limits. N = 2 would roughly provide a 95% confidence interval.
Sürüm Notları: Added extra confidence bands for 95%.
I recommend the standard values N = 2 and N = 3 that provide, respectively, approximately 95% and 99% confidence intervals.
Note: I suggest using smaller sample sizes (between the 30 and 100 last candles) for sigma estimation as they tend to represent better the recent volatility. I also suggest to use sample size=400 for long-term average volatility.
Sürüm Notları: Corrected bias in sigma estimation.

Remark: the original interpretation is a bit misleading. When the series crosses over the interval limits, one can say that the current candle length is 95% or 99% as extreme as the expected length.
Also in hypothesis testing, one could say that the hypothesis of the candle length being within the expected range is rejected at 5% or 1% significance level.

As closing prices can be seen as a random walk on chart, this series is basically modelling its error.
Sürüm Notları: Updated graphic layout for better visualization.

An analogous approach for candle length is just thinking of it as changes in the closing price ( I would rename ir as Price Change Outlier Detector if TV allowed it!).
A green column means a positive change in price and a red column means a negative change in price. These changes are always relative to the last price.

If a column crosses from from the inner band to the outer band, the change in price is considered to be approximately 95% as extreme as expected.
If it crosses both bands, the change in price it considered to be approximately 99% as extreme as expected.
Sürüm Notları: Minor bug fix.
Sürüm Notları: Fixed sigma estimator so it doesn't depend on the present value.
Sürüm Notları: Fixing last fix.
Sürüm Notları: I've been thinking about this publication for a while and thought it needed a well deserved rework.


  • Changed bars to positive values and kept colors relative to price movement;
  • Changed the standard deviation to be estimated using the last 30 bars;
  • Made it visually cleaner.

These changes were made to make it simpler and easier to interpret.

INTERPRETATION: as a bar crosses 2 or 3, the relative price change is 95% or 99% as extreme as expected, respectively.
Açık kaynak kodlu komut dosyası

Gerçek TradingView ruhuyla, bu betiğin yazarı, yatırımcının anlayabilmesi ve doğrulayabilmesi için onu açık kaynak olarak yayınladı. Yazarın eline sağlık! Bunu ücretsiz olarak kullanabilirsiniz, ancak bu kodun bir yayında yeniden kullanımı Kullanım Koşulları ile yönetilir. Bir grafikte kullanmak için favorilere ekleyebilirsiniz.


Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, işlem veya diğer türden tavsiye veya tavsiyeler anlamına gelmez ve teşkil etmez. Kullanım Şartları'nda daha fazlasını okuyun.

Bu komut dosyasını bir grafikte kullanmak ister misiniz?