Rolling QuartilesThis script will continuously draw a boxplot to represent quartiles associated with data points in the current rolling window.
Description :
A quartile is a statistical term that refers to the division of a dataset based on percentiles.
Q1 : Quartile 1 - 25th percentile
Q2 : Quartile 2 - 50th percentile, as known as the median
Q3 : Quartile 3 - 75th percentile
Other points to note:
Q0: the minimum
Q4: the maximum
Other properties :
- Q1 to Q3: a range is known as the interquartile range ( IQR ). It describes where 50% of data approximately lie.
- Line segments connecting IQR to min and max (Q0→Q1, and Q3→Q4) are known as whiskers . Data lying outside the whiskers are considered as outliers. However, such extreme values will not be found in a rolling window because whenever new datapoints are introduced to the dataset, the oldest values will get dropped out, leaving Q0 and Q4 to always point to the observable min and max values.
Applications :
This script has a feature that allows moving percentiles (moving values of Q1, Q2, and Q3) to be shown. This can be applied for trading in ways such as:
- Q2: as alternative to a SMA that uses the same lookback period. We know that the Mean (SMA) is highly sensitive to extreme values. On the other hand, Median (Q2) is less affected by skewness. Putting it together, if the SMA is significantly lower than Q2, then price is regarded as negatively skewed; prices of a few candles are likely exceptionally lower. Vice versa when price is positively skewed.
- Q1 and Q3: as lower and upper bands. As mentioned above, the IQR covers approximately 50% of data within the rolling window. If price is normally distributed, then Q1 and Q3 bands will overlap a bollinger band configured with +/- 0.67x standard deviations (modifying default: 2) above and below the mean.
- The boxplot, combined with TradingView's builtin bar replay feature, makes a great tool for studies purposes. This helps visualization of price at a chosen instance of time. Speaking of which, it can also be used in conjunction with a fixed volume profile to compare and contrast the effects (in terms of price range) with and without consideration of weights by volume.
Parameters :
- Lookback: The size of the rolling window.
- Offset: Location of boxplot, right hand side relative to recent bar.
- Source data: Data points for observation, default is closing price
- Other options such as color, and whether to show/hide various lines.
Quartile
Rolling summaryStatistical methods based on mean cannot be effective all the time when attributed to financial data since it doesn't usually follow normal distribution, the data can be skewed or/and have extreme values which can be described as outliers.
In order to deal with this problem it is appropriate to use median-based techniques.
The most common one is called five-number summary/box plot, which plots median of the dataset, 25th (Q1) & 75th (Q3) percentiles (the medians of lower & upper parts of the original dataset divided by the original median), and whiskers calculated by taking range between Q1 and Q3, multiplying it by 1.5 and adding it to Q3 and subtracting it from Q1. The values which are outside the whiskers are considered outliers. Default settings of the script correspond to the classic box plot.
Seven-number summary can be also plotted by this script, by turning on 4 additional percentiles/Bowley’s seven-figure summary by turning on first 2 additional percentiles and changing their values to 10 and 90 respectively.
P.S.: Mean can be also turned in just to check the difference.
[RESEARCH] Custom Median vs Built-in MedianI compared custom median (using the sorting solution by @apozdnyakov) with built-in median (using percentile_nearest_rank function)
If you see the zero line this means the solutions give the same results.
The best results was achieved on the odd lengths.
Conclusions:
1) Do not try to reinvent the wheel.
2) Always check the Pine Script docs to find already implemented solutions.
Interquartile Range BandsInterquartile Range Bands script.
This indicator was originally developed by Alex Orekhov at his home.
The idea based on the interquartile range en.wikipedia.org
If price breaks out from the bands then it is `outlier` price.
After breakouts price always returns to its median.
Watch squeeze/expansion periods.
Anyway use it as a supplement to the other indicators.
I will glad to get your feedback.
Recursive Median FilterRecursive Median Filter indicator script.
This indicator was originally developed by John F. Ehlers (Stocks & Commodities V. 36:03 (8–11): Recursive Median Filters).