OPEN-SOURCE SCRIPT
Updated

N-Degree Moment-Based Adaptive Detection

1 055
🙏🏻 N-Degree Moment-Based Adaptive Detection (NDMBAD) method is a generalization of MBAD since the horizontal line fit passing through the data's mean can be simply treated as zero-degree polynomial regression. We can extend the MBAD logic to higher-degree polynomial regression.

I don't think I need to talk a lot about the thing there; the logic is really the same as in MBAD, just hit the link above and read if you want. The only difference is now we can gather cumulants not only from the horizontal mean fit (degree = 0) but also from higher-order polynomial regression fit, including linear regression (degree = 1).

Why?
Simply because residuals from the 0-degree model don't contain trend information, and while in some cases that's exactly what you need, in other cases, you want to model your trend explicitly. Imagine your underlying process trends in a steady manner, and you want to control the extreme deviations from the process's core. If you're going to use 0-degree, you'll be treating this beautiful steady trend as a residual itself, which "constantly deviates from the process mean." It doesn't make much sense.

How?
First, if you set the length to 0, you will end up with the function incrementally applied to all your data starting from bar_index 0. This can be called the expanding window mode. That's the functionality I include in all my scripts lately (where it makes sense). As I said in the MBAD description, choosing length is a matter of doing business & applied use of my work, but I think I'm open to talk about it.

I don't see much sense in using degree > 1 though (still in research on it). If you have dem curves, you can use Fourier transform -> spectral filtering / harmonic regression (regression with Fourier terms). The job of a degree > 0 is to model the direction in data, and degree 1 gets it done. In mean reversion strategies, it means that you don't wanna put 0-degree polynomial regression (i.e., the mean) on non-stationary trending data in moving window mode because, this way, your residuals will be contaminated with the trend component.

By the way, you can send thanks to aaron294c , he said like mane MBAD is dope, and it's gonna really complement his work, so I decided to drop NDMBAD now, gonna be more useful since it covers more types of data.


I wanned to call it N-Order Moment Adaptive Detection because it abbreviates to NOMAD, which sounds cool and suits me well, because when I perform as a fire dancer, nomad style is one of my outfits. Burning Man stuff vibe, you know. But the problem is degree and order really mean two different things in the polynomial context, so gotta stay right & precise—that's the priority.

Release Notes
Sup, new stuff for dem users:
- Non-matrix calculation method is added for 0th and 1st degree: it's faster like that, and anyways 2+ degrees is ish for 95% cases;
- Script's default settings were adjusted a bit so it's gonna be a bit easier to use it in study-on-study manner out of the box, also default rounding by mintick is out now for the same reason;

For the Wanderers, Creators, Magicians, Operators etc:
- regression fucntions were generalized to accept any kind of arrays, so you can jst copypaste it and use for any purpose ok
- linreg() has commented lines for TLS regression (u gotta use this type of regresion when both dep and indep variables are Not monotonic, e.g if u wanna calculate hedge ratio between 2 vehicles)
- Line 150 is a hidden gem, that's moment based assymtry index, if u please, made it lately, can be quite interesting
- Removed offset parameter, it's not relevant to MBAD application. Text me if u still want it
Release Notes
add bug fix

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.