Nadaraya-Watson Smoothers [LuxAlgo]The following tool smoothes the price data using various methods derived from the Nadaraya-Watson estimator, a simple Kernel regression method. This method makes use of the Gaussian kernel as a weighting function.
Users have the option to use a non-repainting as well as a repainting method, see the USAGE section for more information.
🔶 USAGE
🔹 Non Repainting
When Repainting Smoothing is disabled the returned indicator acts similarly to a regular causal moving average. This result could be described as an "endpoint Nadaraya-Watson estimator".
Unlike a regular moving average whose degree of smoothness is commonly determined by the length of its calculation window, the degree of smoothness of the proposed indicator is determined by the bandwidth setting, with a higher value returning smoother results.
In the above chart, a bandwidth value of 50 is used. An increasing value of the smoother is indicative of an uptrend, while a decreasing value is indicative of a downtrend.
🔹 Repainting
Non-causal smoothing methods have found low support from technical analysts because they tend to repaint. Yet, they can provide powerful insights such as estimating underlying trends in the price as well as seeing how far prices deviate from them. They can also make drawing certain patterns easier and can help see underlying structures in the price more clearly.
Using higher bandwidth values allows for estimating longer-term trends in the price.
Triangular labels highlight points where the direction of the estimator change. This allows for the identification of tops and bottoms in the underlying trend which can be compared to the actual price tops and bottoms.
Note that multiple labels can appear in real time, highlighting real-time changes in the estimator's direction. The most recent label on a series of labels is the first to appear. This can eventually be useful for the real-time predictive application of the estimator. However, it is not a usage we particularly recommend.
🔶 DETAILS
The Nadaraya-Watson estimator can be described as a series of weighted averages using a specific normalized kernel as a weighting function. For each point of the estimator at time t , the peak of the kernel is located at time t , as such the highest weights are attributed to values neighboring the price located at time t .
A lower bandwidth value would contribute toward a more important weighting of the price at a precise point and would as such less smooth results. In the case where our bandwidth is so small that the resulting kernel is just an impulse, we would get the raw price back.
However, when the bandwidth is sufficiently large, prices would be weighted similarly, thus resulting in a result closer to the price mean.
It can be interesting to note that due to the nature of the estimator and its weighting procedure, real-time results would not deviate drastically for points in the estimator near the center of the calculation window.
🔶 SETTINGS
Bandwidth : controls the bandwidth of the Gaussian kernel, with higher values returning smoother results.
Src : Input source of the kernel regression.
Repainting Smoothing : Determine if the smoothing method should repaint or not. If disabled the "endpoint Nadaraya-Watson estimator" is returned.
Gaussian
Outlier Detector with N-Sigma Confidence IntervalsA 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.
Resampling Filter Pack [DW]This is an experimental study that calculates filter values at user defined sample rates.
This study is aimed to provide users with alternative functions for filtering price at custom sample rates.
First, source data is resampled using the desired rate and cycle offset. The highest possible rate is 1 bar per sample (BPS).
There are three resampling methods to choose from:
-> BPS - Resamples based on the number of bars.
-> Interval - Resamples based on time in multiples of current charting timeframe.
-> PA - Resamples based on changes in price action by a specified size. The PA algorithm in this script is derived from my Range Filter algorithm.
The range for PA method can be sized in points, pips, ticks, % of price, ATR, average change, and absolute quantity.
Then, the data is passed through one of my custom built filter functions designed to calculate filter values upon trigger conditions rather than bars.
In this study, these functions are used to calculate resampled prices based on bar rates, but they can be used and modified for a number of purposes.
The available conditional sampling filters in this study are:
-> Simple Moving Average (SMA)
-> Exponential Moving Average (EMA)
-> Zero Lag Exponential Moving Average (ZLEMA)
-> Double Exponential Moving Average (DEMA)
-> Rolling Moving Average (RMA)
-> Weighted Moving Average (WMA)
-> Hull Moving Average (HMA)
-> Exponentially Weighted Hull Moving Average (EWHMA)
-> Two Pole Butterworth Low Pass Filter (BLP)
-> Two Pole Gaussian Low Pass Filter (GLP)
-> Super Smoother Filter (SSF)
Downsampling is a powerful filtering approach that can be applied in numerous ways. However, it does suffer from a trade off, like most studies do.
Reducing the sample rate will completely eliminate certain levels of noise, at the cost of some spectral distortion. The lower your sample rate is, the more distortion you'll see.
With that being said, for analyzing trends, downsampling may prove to be one of your best friends!
Right Sided Ricker Moving Average And The Gaussian DerivativesIn general gaussian related indicators are built by using the gaussian function in one way or another, for example a gaussian filter is built by using a truncated gaussian function as filter kernel (kernel refer to the set weights) and has many great properties, note that i say truncated because the gaussian function is not supposed to be finite. In general the gaussian function is represented by a symmetrical bell shaped curve, however the gaussian function is parametric, and the user might adjust the position of the peak as well as the width of the curve, an indicator using this parametric approach is the Arnaud Legoux moving average (ALMA) who posses a length parameter controlling the filter length, a peak parameter controlling the position of the peak of the gaussian function as well as a width parameter, those parameters can increase/decrease the lag and smoothness of the moving average output.
However what about the derivatives of the gaussian function ? We don't talk much about them and thats a pity because they are extremely interesting and have many great properties as well, therefore in this post i'll present a low lag moving average based on the modification of the 2nd order derivative of the gaussian function, i believe this post will be extremely informative and i hope you will enjoy reading it, if you are not a math person you can skip the introduction on gaussian derivatives and their properties used as filter kernel.
Gaussian Derivatives And The Ricker Wavelet
The notion of derivative is continuous, so we will stick with the term discrete derivative instead, which just refer to the rate of change in the function, we have a change function in pinescript, and we will be using it to show an approximation of the gaussian function derivatives.
Earlier i used the term 2nd order derivative, here the derivative order refer to the order of differentiation, that is the number of time we apply the change function. For example the 0 (zeroth) order derivative mean no differentiation, the 1st order derivative mean we use differentiation 1 time, that is change(f) , 2nd order mean we use differentiation 2 times, that is change(change(f)) , derivates based on multiple differentiation are called "higher derivative". It will be easier to show a graphic :
Here we can see a normal gaussian function in blue, its scaled 1st order derivative in orange, and its scaled 2nd derivative in green, note that i use scaled because i used multiplication in order for you to see each curve, else it would have been less easy to observe them. The number of time a gaussian function derivative cross 0 is based on the order of differentiation, that is 2nd order = the function crossing 0 two times.
Now we can explain what is the Ricker wavelet, the Ricker wavelet is just the normalized 2nd order derivative of a gaussian function with inverted sign, and unlike the gaussian function the only thing you can change is the width parameter. The formula of the Ricker wavelet is show'n here en.wikipedia.org , where sigma is the width parameter.
The Ricker wavelet has this look :
Because she is shaped like a sombrero the Ricker wavelet is also called "mexican hat wavelet", now what would happen if we used a Ricker wavelet as filter kernel ? The response is that we would end-up with a bandpass filter, in fact the derivatives of the gaussian function would all give the kernel of a bandpass filter, with higher order derivatives making the frequency response of the filter approximate a symmetrical gaussian function, if i recall a filter using the first order derivative of a gaussian function would give a frequency response that is left skewed, this skewness is removed when using higher order derivatives.
The Indicator
I didn't wanted to make a bandpass filter, as lately i'am more interested in low-lag filters, so how can we use the Ricker wavelet to make a low-lag low-pass filter ? The response is by taking the right side of the Ricker wavelet, and since values of the wavelets are negatives near the border we know that the filter passband is non-monotonic, that is we know that the filter will have low-lag as frequencies in the passband will be amplified.
So taking the right side of the Ricker wavelet only mean that t has to be greater than 0 and linearly increasing, thats easy, however the width parameter can be tricky to use, this was already the case with ALMA, so how can we work with it ? First it can be seen that values of width needs to be adjusted based on the filter length.
In red width = 14, in green width = 5. We can see that an higher values of width would give really low weights, when the number of negative weights is too important the filter can have a negative group delay thus becoming predictive, this simply mean that the overshoots/undershoots will be crazy wild and that a great fit will be impossible.
Here two moving averages using the previous described kernels, they don't fit the price well at all ! In order to fix this we can simply define width as a function of the filter length, therefore the parameter "Percentage Width" was introduced, and simply set the width of the Ricker wavelet as p percent of the filter length. Lower values of percent width reduce the lag of the moving average, but lets see precisely how this parameter influence the filter output :
Here the filter length is equal to 100, and the percent width is equal to 60, the fit is quite great, lower values of percent width will increase overshoots, in fact the filter become predictive once the percent width is equal or lower to 50.
Here the percent width is equal to 50. Higher values of percent width reduce the overshoots, and a value of 100 return a filter with no overshoots that is suited to act as a lagging moving average.
Above percent width is set to 100. In order to make use of the predictive side of the filter, it would be great to introduce a forecast option, however this require to find the best forecast horizon period based on length and width, this is no easy task.
Finally lets estimate a least squares moving average with the proposed moving average, you know me...a percent width set to 63 will return a relatively good estimate of the LSMA.
LSMA in green and the proposed moving in red with percent width = 63 and both length = 100.
Conclusion
A new low-lag moving average using a right sided Ricker wavelet as filter kernel has been introduced, we have also seen some properties of gaussian derivatives. You can see that lately i published more moving averages where the user can adjust certain properties of the filter kernel such as curve width for example, if you like those moving averages you can check the Parametric Corrective Linear Moving Averages indicator published last month :
I don't exclude working with pure forms of gaussian derivatives in the future, as i didn't published much oscillators lately.
Thx for reading !
Roofing Filter [DW]This is an experimental study built on the concept of using roofing filters on price data proposed by John Ehlers.
Roofing filters are a type of bandpass filter conventionally used in HF radio receivers in the first IF stage to limit the frequency spectrum passed on to later stages in the receiver.
The goal in applying roofing filters to a price signal is to simultaneously attenuate high frequency noise and low frequency distortion to pass an oscillating signal with a nearly zero mean for analysis and/or further calculation.
In this study, there are three filter types to choose from:
-> Ehlers Roofing Filter, which passes data through a 2 pole high pass filter, then through a Super Smoother filter.
-> Gaussian Roofing Filter, which passes data through a 2 pole Gaussian high pass filter, then through a 2 pole Gaussian low pass filter.
-> Butterworth Roofing Filter, which passes data through a 2 pole Butterworth high pass filter, then through a 2 pole Butterworth low pass filter.
Each filter type has different amplitude and delay characteristics, so play around with each type and see which response suits your needs best.
There is an option to normalize the scale of the output as well. The normalization process in this script is computed by comparing positive and negative outputs to the filter's moving RMS value.
The resulting oscillator can be fed through numerous conventional indicators including Stochastic Oscillator, RSI, CCI, etc. to generate smoother, less distorted indicators for a clearer view of turning points.
Alternatively, it can also act as an indicator itself, as implied by the corresponding color scheme included in the script.
Although roofing filters are not conventionally used in the analysis of market data, applying such spectral analysis techniques may prove to be quite useful for the design of more efficient indicators and more reliable predictions.
Gaussian MFI BFThe signals are based on the Gaussian Channel calculated by Donovan Wall. Thanks also to Kiasaki for Rate of Change code.
Simply going long and short based on Gaussian channel was not consistent enough so I also included an MFI filter. We only go long if Money Flow Index is greater than the last candle (more money is flowing in than out). The opposite for short. Also employed Rate of Change function (white background) to avoid sideways market.
A 2% stop loss seems to be the most balanced for this strategy but can can altered in the settngs.
Also included a take-profit function but it seems this is best not implemented for this strategy, at least in the case of XBT/USD.
A derivative of The MFI is also plotted on the chart simply to compare the current candle with the last candle visually.
Background color:
Green = Long
Red = Short
White = No Trade
Gaussian distribution %sWith 4 Moving averages options, length input and source input this script will help you test so you can find the best moving average type and length according to the gaussian distribution theory.
Gaussian Distribution Theory:
68% of all data points fit within 1 Standard deviations of the mean
95% of all data points fit within 2 Standard deviations of the mean
99.7% of all data points fit within 3 Standard deviations of the mean
Generalized Average Generic Gaussian Envelope mcbw_This is a moving average with a customizable gaussian kernel. You can shape your kernel by selecting your parameters in the settings window. This is not something that is immediately ready to mess with by just applying it on the chart, it is very useful for people who are researching indicators and developing new tools. To see the shape of your kernel you can plug it into google or wolfram. This indicator and the related ones are rather technical in nature, so feel free to comment any questions you may have and to see if anyone has asked your question.
Read more here:
Happy studying and enjoy your life!
2019 will be absolutely insane!
Gaussian FilterGaussian Filter script.
This indicator was described by John F. Ehlers in his book "Rocket Science for Traders" (2001, Chapter 15: Infinite Impulse Response Filters).
Gaussian Channel [DW]This study is an experiment utilizing the Ehlers Gaussian Filter technique combined with lag reduction techniques and true range to analyze trend activity.
Gaussian filters, as Ehlers explains it, are simply exponential moving averages applied multiple times.
First, beta and alpha are calculated based on the sampling period and number of poles specified. The maximum number of poles available in this script is 9.
Next, the data being analyzed is given a truncation option for reduced lag, which can be enabled with "Reduced Lag Mode".
Then the alpha and source values are used to calculate the filter and filtered true range of the dataset.
Filtered true range with a specified multiplier is then added to and subtracted from the filter, generating a channel.
Lastly, a one pole filter with a N pole alpha is averaged with the filter to generate a faster filter, which can be enabled with "Fast Response Mode".
Custom bar colors are included.
Note: Both the sampling period and number of poles directly affect how much lag the indicator has, and how smooth the output is.
Larger inputs will result in smoother outputs with increased lag, and smaller inputs will have noisier outputs with reduced lag.
For the best results, I recommend not setting the sampling period any lower than the number of poles + 1. Going lower truncates the equation.
NG [All Moving Averages]Collection of some of the best moving averages.
I've tried to collect them all but TV became so slow, that it was completely unusable.
So i left only those that performs best on various backtest systems.
NG [Gaussian Filter Multi-Pole]When smoothing data there is always a trade-off between lag and removal of noise.
Gaussian filter has a consistently low lag and a very smooth curve.
This filter works for poles higher than the 4 (usual usage).
Mathematically maximum poles is 15 after which coefficients are too high to see any difference.
By tuning Lag and number of Poles you can achieve a very smooth MA with least lag possible.
It's just as good as 3rd gen moving averages and can be used as input feed for other indicators.
Gaussian smoothGaussian smooth of price. Colour telling the momentum bending (= second diff). Port of mq4 indicator.
Tip jar: 165PuWddQdWynFf3fmNi6tVCG6gWf4usKG. Thank you.