Relative slopeRelative slope metric
Description:
I was in need to create a simple, naive and elegant metric that was able to tell how strong is the trend in a given rolling window. While abstaining from using more complicated and arguably more precise approaches, I’ve decided to use Linearly Weighted Linear Regression slope for this goal. Outright values are useful, but the problem was that I wasn’t able to use it in comparative analysis, i.e between different assets & different resolutions & different window sizes, because obviously the outputs are scale-variant.
Here is the asset-agnostic, resolution-agnostic and window size agnostic version of the metric.
I made it asset agnostic & resolution agnostic by including spread information to the formula. In our case it's weighted stdev over differenced data (otherwise we contaminate the spread with the trend info). And I made it window size agnostic by adding a non-linear relation of length to the output, so finally it will be aprox in (-1, 1) interval, by taking square root of length, nothing fancy. All these / 2 and * 2 in unexpected places all around the formula help us to return the data to it’s natural scale while keeping the transformations in place.
Peace TV
Differencing
Nth Order Differencing Oscillator Perform higher order differencing through convolution, the result is equivalent to cascading N momentum oscillators of periods P :
mom(mom(mom(mom(x,P)...,P)
Settings
length - Period of the oscillator, indicate the lag to use (equivalent to the period in a momentum oscillator)
order - Differencing order, indicate how many times differencing is performed (number of times a momentum oscillator is cascaded)
src - Input of the indicator
Usage
Differencing consists in subtracting an input to a previous input, this is what the momentum oscillator performs. This is often done in order to remove longer-term variations in the price. Differencing also induces a 90-degree phase shift for all sinusoids in a signal, this is why oscillators can have this leading effect, as such higher differencing can sometimes help have a faster and more visible lead.
In red the indicator with period 50 and differencing order 2, below a momentum oscillator of the same period.
It is important to note that differencing is an operation that increases noise, in fact, you might have seen some oscillators use the median price hl2 instead of the closing price, this is because the median price contains less noise than the closing price, as such more differencing require a smoother input.
Here both the sma and the oscillator period are equal to 20 with a differencing order of 5.
In time series analysis the order of differencing is chosen depending on the order of integration, more simply put we should choose a differencing order that responds to the question: "How many time should I differentiate my time series so that the result is stationary?", for stocks prices this differencing order should be 1.
Technically speaking differencing orders higher than 3 might be overkill, as higher orders return noisier outputs that might no longer be representative of the original input.
here a period of 14 with differencing order of 20 is used, we can see more periodic results but they are not really representative of the closing prices.
Details
Simple differencing is actually achieved thought convolution, if we take a first-order difference x - x(1) , we can see that this is equivalent to 1*x + -1*x(1) , the coefficients are 1 and -1, for the momentum oscillator the difference is that the coefficients include 0 values. So we only need a function generating the coefficients of our Nth order difference oscillator, in order to get them lets analyze the impulse response of a cascaded change function.
Here 5 change function are cascaded, the coefficients are: (1,-5,10,-10,5,-1)
If you look at these coefficients and the ones of higher/lower order differences we can deduce various things
The impulse response is symmetric
The first coefficient is always 1 and the last always -1
The number of coefficients increase with higher differencing orders
The sign of the current coefficient is different from the sign of the previous one
From the shape of the impulse response, we can deduce that the coefficients of an Nth order differencing operator is a windowed series of 1,-1,1...,-1 , and that's actually the case, for an Nth order differencing operator the values of this window are given by the Nth row of the Pascal triangle.
There are various ways to get the values in the row of the Pascal triangle, one involving using the combination formula, however, we can do it way faster by using the recursive formula used in line number 13. Now that we have our coefficients we only need to separate them with 0 values and that's all.
Conclusion
We can see that oscillators are noisier than the original input signal, this is can be a desired effect in order to make lagging indicators more reactive, but it can also be overlooked due to the results appearing leading the price or just looking more predictable, however, we should note that higher-order differencing does not provide a consistent nor reliable solution toward minimizing lag, nor does classical oscillators.
The indicator is not useful, but if for some reason you require a lot of differencing operations to be done and don't want to use consecutive change or mom functions, then this script might results useful to you.