Occasionally once you search what you find something else...
I had been (and still am) looking for a good estimator of this trend. Without being a DSP ace, those played around with a few moving averages quickly realized that in the event that you want the outcome smooth you receive lag. You are able to remove the lag but you receive overshooting; that is as bad. To reduce the overshooting you return the smoothness. A vicious cycle with no escape. I'm no exception and my trend estimator suffers from annoying residual oscillations. Those oscillations are mainly because of the wide oscillations the price can make during explosive tendencies and throughout the wide ranges. In order to reduce the oscillations I wished to assess the cyclic components of the time series. If I can quantify it I could remove it. If I can remove it I could eliminate the oscillations.
Let's see in picture exactly what I mean. Here is the kind of oscillations I'd love to remove:
When the oscillations of this price are little enough the estimator absorbs them. However, if the amplitude of the oscillations gets larger they become a problem. To form this type of motion the market requires a while and therefore these oscillations can not have a too brief period. I target the oscillations of approx. 10 to 30 bars each cycle.
If you're also looking for a way to estimate these cycles, you certainly have heard about the Goertzel algorithm. It's very exact but it requires three full cycles to discover the frequency it is tunned on. You're able to detect just one predefined frequency. You surely also understand John Ehlers' function, namely the double differentiator and the homodyne filter. The double differentiator gives an effect quickly but it overshoots in the frequency domain: the detection is quickly but... wrong! The homodyne filter lags 21 bars and if a bicycle is detected it has usually already gone. I tried Hilbert wavelet pair change (https://www.forexsoutheast.asia/gene...ity-check.html) but as a result of end of sample impact of this wavelet change I might just have lag and not-so-acurate dimensions.
Here is a really easy solution
I use a bandpass filter to the range of frequencies I'm interested in. This way I remove both the large frequency (sound ) and the trend, so that I will not need to detrend the samples to assess the cycles. I take the premise that there's just one single big cycle at one time in this range. This is not always correct.
The output of this filter is approximately looking like a sine wave. I just need to take its Hilbert transform to assess the frequency, the amplitude and the phase (timing inside the cycle). The Hilbert transform of sin(x) is -cos(x). It occurs that the derivative of sin(x) is cos(x) that is the HT around the signal. The general form of the sine wave is A.sin(#969;t #966;-RRB-, where A is the amplitude, #969; is your frequency and #966; is your phase. The problem is that the derivative of A.sin(#969;t #966;-RRB- is A#969;.cos(#969;t #966;-RRB-. But we need A.cos(#969;t #966;-RRB-. The multipliive factor #969; isn't desired. We do not know the frequency since it is what we're trying the quote so we can not remove it. The integral of sin(x) is -cos(x) that is the HT. Computing the key of A.sin(#969;t #966;-RRB- we receive -(A/#969;).cos(#969;t #966;-RRB-. Now we have a factor 1/#969; to eliminate.
I simply mutiply the derivative and the integral. I get -A²cos²(#969;t #966;-RRB-. Taking the square root of the total value (to prevent signal problem) I get |A.cos(#969;t #966;-RRB-| that's the HT up to the signal. But the sign is the same as the one of this derivative and nothing is missing.
S = A.sin(#969;t #966;-RRB-
D = dS / dt = A#969;. cos(#969;t #966;-RRB-
I = #8747;S dt = -(A/#969;-RRB-. cos(#969;t #966;-RRB-
D.I = -A²cos²(#969;t #966;-RRB-
HT = sgn(D). #8730;|D.I|
From the in-phase and quadrature I receive the amplitude A=#8730;(S² HT²) along with the phase using atan(HT/S) up to quadrant identifiion. Taking two sequential measurements allows to locate the frequency that's the derivative of this phase.
Two small problems
- The measured amplitude is bigger than the real one because the signal is taken following the bandpass filter. But knowning the frequency response of this filter we could fix the amplitude now we understand the frequency. A_price = A_measured/ / |H(#969;-RRB-|, where H(#969;-RRB- is the transfer function of this filter.
- The signal isn't always made of one cycle along with the tip no longer works. The solution is to assess if the quote fits the few past samples or not.
About the lag
To get the results we need the key and the derivative of this signal output by the banbdpass filter and also the derivative of the resulting phase to get the frequency. The integration has no lag. The derivation has half an sample lag. We need another sample to assess the frequency. So we have one bar and a half lag on top of the filter. I use a brief IIR filter to get it as low as possible ! 1 bar. Clearly I get phase stimulation as the price to pay but I've near real-time estimation.
And the result
In red the output of this filter, in green the amplitude (not frequency corrected ) and in blue the instantaneous phase.
I hope this little finding may help somebody.