Haar Wavelet with Lifting and Incremental Option
Here's an indicator (Haar wavelet with lifting) which, similar to the other wavelet implementations I posted, can essentially break a time series down to constituent frequency components. However because this is implemented via lifting it reduces boundary effects To be more compliant with Daniel R.'s vision, this does not use numpy and all calculations are done internally with no libraries.
This can be used as a crossover indicator for someone who wants an off the shelf solution. For the more advanced user I've also implemented basic thresholding of the wavelet coefficients (threshlist), or you can pick out a wavelet level in isolation (coeffpick).
Further, it also has an incremental flag which although very computationally expensive (an inefficiency that I may refine later on) shows the current bar value without computing from future bars. I don't know that this really makes as much of a difference in practice as an indicator, however.
This is implemented in Python 3.
examplelwt = bbhaarlift(self.data, coefflev=9, coeffpick = 3, blnincremental = True, threshlist=[1, 1, 1, 1, 0, 0, 0, 0, 0])
Where coeffpick and threshlist are mutually exclusive use one or the other. If both are provided, coeffpick takes precedence. The above picks out the third wavelet level out of nine total.
Because this uses lifting, 2**n bars are needed for calculation. If coefflev isn't provided, the routine uses the next lowest power of 2 bars. Ie. all the bars it can given your input data length.
And here's the code:
''' Author: B. Bradford adapted from information provided by bearcave.com and Ian Kaplan. MIT License Copyright (c) B. Bradford Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ''' import backtrader as bt # input lengths must be a power of two for haar with lifting: class bbhaarlift(bt.Indicator): plotinfo = dict(subplot=False) lines = ('haarlift', ) params = (('coefflev', None), ('coeffpick', None), ('blnincremental', False), ('threshlist', )) #2**coefflev data points required or 2**coefflev @staticmethod def next_power_of_2(x): return 1 if x == 0 else 2 ** (x.bit_length() -1) def once(self, start, end): self.blnforward = 1 self.blninverse = 2 #create dummy line to manipulate and desired 2** data length (max or selected) lstinputdata = self.data.array[:] #make a copy of the list/array if self.params.coefflev is None: #max available data length pow2padding = self.next_power_of_2(len(lstinputdata)) #input needs to be power of two data points else: #selected 2** from coefflev pow2padding = 2 ** self.params.coefflev if self.next_power_of_2(len(lstinputdata)) < pow2padding: raise ValueError("COEFF LEVEL %s GREATER THAN MAX DATA LENGTH %s AVAILABLE" % (self.params.coefflev, self.next_power_of_2(len(lstinputdata)))) if self.params.coefflev is None: self.params.coefflev = pow2padding.bit_length() - 1 # If specific coeffpick selected set it to one, and others to zero: if self.params.coeffpick is not None: self.params.threshlist =  * self.params.coeffpick +  * ( self.params.coefflev - self.params.coeffpick) dpcnt = len(lstinputdata) - pow2padding #only do loop once at end of array if blnincremental not true #Count backwards through line to leave most recent value in place if incremental: while dpcnt > 0: dpcnt -= 1 #decrement immediately as list slicing is zero-based inlist = self.data.array[dpcnt:dpcnt + pow2padding] ftresult = bbhaarlift.forwardtrans(self, vec=inlist) #forward transform threshedresult = bbhaarlift.wvltthreshold(coeffary=ftresult, threshlist=self.params.threshlist) itresult = bbhaarlift.inversetrans(self, vec=threshedresult) #inverse transform dummyaryout = lstinputdata[:dpcnt] + itresult + lstinputdata[dpcnt + pow2padding:] lstinputdata = dummyaryout # self.lines.array[pow2padding:] + itresult #Only run once if not doing incremental: if not self.params.blnincremental: break self.lines.array = lstinputdata def wvltthreshold(coeffary, threshlist): print ("bbwaveletlifting: len(coeffary).bit_length()", len(coeffary).bit_length()) if len(coeffary).bit_length() < len(threshlist): raise ValueError('WARNING: THRESHOLD LIST LENGTH IS SMALLER THAN NUMBER OF COEFF LEVELS.') threshedresult = coeffary[:] for i in range(1, len(threshlist)): for x in range(2**i, 2**(i+1)): print ("bbwaveletlifting: i, x, len(threshlist) ", i, x, len(threshlist)) threshedresult[x] = coeffary[x] * threshlist[i] return threshedresult def split(vec, N): start = 1 end = N - 1 while (start < end): #Could do this with list comprehension, but want to stick to the original java structure: for i in range(start, end, 2): tmp = vec[i] vec[i] = vec [i + 1] vec[i+1] = tmp start += 1 end -= 1 return vec def merge(self, vec, N): half = N >> 1 start = half-1 end = half while (start > 0): for i in range(start, end, 2): tmp = vec[i] vec[i] = vec[i+1] vec[i+1] = tmp start -= 1 end += 1 def forwardtrans(self, vec): ''' The result of forwardTrans is a set of wavelet coefficients ordered by increasing frequency and an approximate average of the input data set in vec. The coefficient bands follow this element in powers of two (e.g., 1, 2, 4, 8...). ''' N = len(vec) print ("forwardtrans N: ", N) #set for counter: n = N print ("forwardtrans n: ", n) while n > 1: print ("forwardtrans split...") vec = bbhaarlift.split(vec, n) print("forwardtrans predict...") vec = bbhaarlift.predict (self, vec, n, self.blnforward) print("update split...") vec = bbhaarlift.update (self, vec, n, self.blnforward) n = n >> 1 return vec def inversetrans(self, vec): N = len(vec) n = 2 while n <= N: bbhaarlift.update(self, vec, n, self.blninverse) bbhaarlift.predict (self, vec, n, self.blninverse) bbhaarlift.merge(self, vec, n) n = n << 1 return vec def predict(self, vec, N, direction): half = N >> 1 cnt = 0 for i in range (0, half, 1): predictval = float(vec[i]) j = int(i+half) if (direction == self.blnforward): vec[j] = vec[j] - predictval elif (direction == self.blninverse): vec[j] = vec[j] + predictval else: debug.print("haar: predict: bad direction value") return vec def update(self, vec, N, direction): half = N >> 1 for i in range (0, half, 1): j = int(i + half) updateVal = float(vec[j] / 2.0) if (direction == self.blnforward): vec[i] = vec[i] + updateVal elif (direction == self.blninverse): vec[i] = vec[i] - updateVal else: debug.print("haar update: bad direction value") return vec
And a screenshot:
This screenshot shows the wavelet indicator as "testindicator2" plotted over the main data series. You can see that the lower frequency band makes a good long term indicator in a trending market. As the markets contract into choppiness, you may switch to a higher resolution wavelet or a smaller coefficient level which results in a shorter length of input series data used.
I'm considering making a daubechies wavelet indicator with lifting which certain people are likely highly interested in but because of the enormous amount of time involved I might charge. I'm sure someone will find a bug or two and any feedback is appreciated.
Fixed a bug when an odd combination of parameters is supplied. This is the same chart with haar and no incremental option. Blocky, but exactly the same concept:
@backtrader Thanks -- in the same vein I greatly admire your work on Backtrader. It's excellent.