Application of Wavelets to Detect Trend Reversals on Brazilian Stock Market

This study develops a method to detect trend reversals followed by significant drops in Brazilian Stock Market using wavelets. Applying the concept of the log-periodic power-law, whose oscillations present reduction in amplitude and period as the critical moment approaches (where there is a higher probability of market drop), it used the Continuous Wavelet Transform to detect the increasing oscillation frequency in the stock price, time series and generate sell signals. An algorithm was developed to test different kinds of wavelets and parameters to calculate the Wavelet Index, evaluating whose combination of parameters brings the best results and comparing these results with the existing Technical Analysis tools. The results show that the proposed method to calculate the Wavelet Index detects successfully the significant drops (over 10%) in the Brazilian Stock Market. Considering drops over 15%, there were losses due to early sales of 45% (average) in the search set and 43.9% in the test set, without false negatives, using mainly the Meyer wavelet. Its performance was also better than existing Technical Analysis tools, like MACD and RSI.


Introduction
Looking after maximizing the returns and improving the risk management of financial market investments, researchers and scientists develop, every year, new methods to predict the future behavior of the market variables.Among the different approaches to this question, the techniques to analyze the trends on the assets price are remarkably important, detecting the current trend or predicting its reversal [1] [2].

The Log-Periodic Power Law
The origins of the log-periodic power-law come from the critical phenomena theory, developed in the decade of 1960.The application of the complex systems model to the financial market is based on the premise that the interactions among investors generate a cooperative behavior, accelerating the uptrend and creating a bubble.The rise in prices brings the market to an instable condition, where a small perturbation bursts the bubble (the critical point, the system collapse).In this model, the market's internal conditions create the crashes and exogenous factors are only the triggers that bring the instability.So, the market itself progressively builds the bubble, in a self-organized process [3].
Equation (1) mathematically represents this model, where I(t) represents the asset price (or value) on instant t and t c is the instant where the crash is more probable.The parameter A is the asset price when t = t c , β is the power law exponent, ω is the oscillation angular log-frequency, ϕ is the oscillation phase, B and C are constants used to fit the function: The log-periodic power law characteristically reduces the amplitude and the period of its oscillations as the critical moment (t c ) approaches, and the wavelets are intended to detect this increase in oscillation frequency [4].

The Wavelet Transform
The Wavelet Transform is a particularly useful tool to analyze aperiodic and noise signals, characteristics that are common to financial market assets time series [11].
Using dilatation and translation operations on special waveforms, called wave- ( ) 2) Being ( ) ˆf ψ the Fourier Transform of ( ) t ψ , according to Equation (3), it must respect the admissibility condition described on Equation ( 4).This implies that the wavelet has no components on frequency zero, meaning that its mean is equal to zero.C g is the wavelet's admissibility condition and changes to each wavelet type.
( ) ( ) ( ) The Continuous Wavelet Transform (CWT) ( ) , T a b of signal x(t) is defined as the convolution of this signal with the wavelet for different values of a (the scale parameter, which represents the dilatation operation) and b (which represents the wavelet translation), as presented in Equation (5).Typically a weight-function of 1 a is used to keep the same energy all scales of the wavelets.
( ) ( ) In a more compact form: The CWT results in a tri-dimensional graph, where the axis X holds the time The total energy of a signal is the integral of its squared magnitude, the relative contribution of a scale a and a position b( ( ) , E a b ) to the signal energy is giv- en by Equation (8).The graph of ( ) , E a b is known as scalogram and is used to highlight the location and scale of the signal's dominant characteristics, in terms of energy [13].

Materials and Methods
This research considered the closing prices/values of Bovespa Index (IBOV) and other 14 stocks listed at BM & FBOVESPA Stock Exchange (from Sao Paulo, Brazil), between Jan/02/2008 and Jul/29/2016, (because the presentation of the dissertation was in November of 2016), discarding the periods when there were days that these assets were not negotiated and adjusting for dividends, splits and merges.
Table 1 lists all assets considered, as well as the dates of the first and last days considered.
The first step was to analyze the assets' time series to identify the uptrend movements that ended on drops equal to or larger than 5%, 10%, 15%, 20%, 25% and 30%, in order to evaluate the Wavelet Index performance in these different conditions.To isolate each uptrend movement, an algorithm identified its end (the drop that follows the uptrend) and then previous local minima defined its start.
Figure 1 and Figure 2 shows the IBOV time series identifying the start and end of the movements that were follow by drops larger than 10% and 20% (respectively).The movements that were shorter than 30 days were discarded, as they were not long enough to provide satisfactory application of the CWT.The algorithm obtained 1529 movements, being 611 longer than 30 days.
The functions submitted to the Wavelet Transform must belong to the L²(R) space [14], so it is necessary to remove the movement's trend before analyzing them.
A non-linear regression using least squares identified the trend of each movement, according to the Equation ( 9):   ( ) ( ) The subtraction this trend (which is a power law) from the model proposed in Equation ( 1) highlights the movements' log-periodic oscillations, with amplitude modulated according to the distance between t and t c : The Figure 3 shows an example of movement originally with its trend and then after the trend removal.
The movements, without its trends, went through a normalization process, so its values were between 0 and 1, and then submitted to the CWT, resulting in a iBusiness bi-dimensional coefficients matrix.The squared value this matrix is the movement's scalogram, used in the subsequent analysis.
The proposed Wavelet Index calculation has four steps: 1) Calculate the difference between the larger coefficient and the smaller coefficient for each scalogram column.The larger this different, more significant is the correlation between the signal and the wavelet to a given scale (or to a given wavelet pseudo-frequency).
2) Obtain the scale of the larger coefficient, to each column that the difference calculated is larger than a given threshold (the "coefficients threshold").
3) Calculate the Wavelet Index according to Equation (11), where WI is the Wavelet Index, N is the total quantity of scales used and n coefmax is the scale where the larger coefficient happened.On the columns that did not reach the coefficients threshold, the Wavelet Index is equal to zero.

Results and Discussion
In order to find the best combination of parameters to calculate the Wavelet Index, the described algorithm tested different combinations of wavelets, number of scales, coefficient thresholds and Wavelet Index thresholds: or maximum number of scales to each movement; • Coefficients threshold: value between 0.10 and 1.00, spaced of 0.01; • Wavelet Index Threshold: values between 0.10 and 1.00, spaced of 0.02.
The movements randomly constituted two groups, a search group (to evaluate the combination of parameters with best performance) and the test group (to test the best combination outside the initial group).Each combination has its performance evaluated at each movement, using two different criteria: • Early Sale Losses (ESL): usually, the Wavelet Index generates a sell signal before the time series reaches its peak value.Therefore, there is a loss due to the continuation of the uptrend after selling the asset.The algorithm calculates the ESL for each movement then sums all values and considers it as a percentage of the total uptrend.• False Negative (FN): happens when the combination does not result in a sell signal before the drop.The algorithm disregarded combinations that generate false negatives, as an FN may imply in severe losses to an investor.The first test applied the described algorithm to all movements, separated according to the drop size.Therefore, the movements ended in drops equal to or larger than 5%, 10%, 15%, 20%, 25% and 30% were split randomly in search and test groups.
Table 2 brings the best combinations results in both groups, for each drop threshold.
The next test separated the movements by asset and applying the same procedure presented before.Table 3 brings the best combinations results by asset.
The next analysis separated the movements ending in drops equal to or larger than 5% and 10% from the movements ending in drops from 15% to 30%, looking for performance differences according to the drop size that follows the movements.The best combinations results for the first set are on Table 4, while the second set results are on Table 5.
Table 6 presents the frequency which every wavelet had the best performance on the tests.The Haar wavelet was the best in 25.5% tests (13 times), followed by Meyer wavelet with 23.5% (12 times).The Morlet and Symlet 5 wavelet were the best in only 1 case each (2%) and Daubechies 2 wavelet did not have the best performance in any test.Looking only at the test by asset for drops of 15% to 30%, the results are slightly different, as the Meyer wavelet was the best to 4 assets, followed by Haar and Gaussian1 wavelets ( 8 shows a similar analysis, but comparing the results by asset, on the three tests that separated the movements based on this criteria. The tests that separated the movements by asset had better performance than the test that mixed movements from different assets.The performance also differs among assets with no obvious pattern, as there are significant differences even between assets from the same company (as PETR3/PETR4 and VALE3/VALE5).
This is an evidence that every asset has different characteristics and, consequently, will present different performance when subject to the same Technical Analysis indicator [15].In order to compare the performance of WI and Technical Analysis indicator, like MACD and RSI [16] [17], the test considered time series from all assets, evaluating every drop threshold separately.The buy signals were the local minima (the start of the movements) from each drop threshold and the WI calculation used the best combination of parameters, according to Table 4 and Table 5.
Table 9 brings the average performance for each indicator, together with the return of the assets through the studied period (based on a buy-and-hold strategy).

Conclusions
This research evaluated the applicability of the Wavelet Index to detect trend re- These results also point that the Wavelet Index had limited success when detecting trend reversals followed by minor drops (5% and 10%).The critical phenomena model using the log-periodic power law based the development of this Index, therefore this result indicates weak adherence of the minor drops to the proposed model.
It's interesting to highlight that performance using the maximum number of scales to the Continuous Wavelet Transform was better than the performance using only 30 scales in 60.8% of the tests.
The Wavelet Index performance to detect trend reversals on the studied assets was superior to the tested Technical Analysis tools (MACD and RSI) to drop equal to or larger than 10%, 15%, 20%, 25% and 30%.An investment simulation based on the sell signals generated by Wavelet Index had average performance of

(
with length equal to the original signal), the different scales (frequency component) go in the axis Y and the calculated coefficient values ( ( ) , T a b ) are in the axis Z.This graph is usually a contour curve, where the different colors represent the magnitude of ( ) , T a b .

Figure 1 .
Figure1.IBOV with the movements followed by drops equal to or larger than 10% highlighted.The red marks are the movement start and the green marks, the movement end (Source: Author).

Figure 2 .
Figure2.IBOV with the movements followed by drops equal to or larger than 20% highlighted.The red marks are the movement start and the green marks, the movement end (Source: Author).

Figure 3 .
Figure 3. Movement with trend line in red (above).Same interval, with the trend removed (below).

4 )
Compare the Wavelet Index with a new threshold (the "Wavelet Index Threshold"), considering a trend reversal imminent, and generating a sell signal, when the Wavelet Index is equal to or larger than this threshold.The performance comparison between the Wavelet Index and the TechnicalAnalysis tools considered that, given the same buy signals (the local minima from the identified movements); an investor would respect the sell signals generated for each indicator, separately.Then the investor would wait until the next buy signal, when he invests 100% of the available resources.All algorithms used Python, except for the CWT calculations, which used the MATLAB©'s Wavelet Toolbox™, from The Mathworks, Inc. D. G. Penof, A. A. Belardi DOI: 10.4236/ib.2017.9400567 iBusiness Used wavelets: performed tests with the Haarwavelets (haar), Daubechies to N = 2 (db2) and N = 4 (db4), Meyer (meyr), Gaussian-1 st derivative (gaus1) e 4 th derivative (gaus4), Symmlet (sym5), Coiflet (coif3), Mexican Hat (mexh) and Morlet (morl); • Number of scales used in the CWT: 30 scales (minimum movements length)

Table 1 .
Assets and periods considered.

Table 7
compares the results of the four tests performed, based on the average ESL and the quantity of FN to the search and test sets.Table

Table 7 .
Comparison of results: the four performed tests (Source: Author).

Table 8 .
Comparison of results: by asset (Source: Author).
versals in Brazilian stock market, testing different combinations of wavelets, number of scales and thresholds to generate sell signals and detect drops of different intensities.The test results indicate that the proposed method to calculate the Wavelet Index detects successfully trend reversals followed by significant drops (over 10%) in the Brazilian stock market.When evaluating drops over 15% on the selected assets (Bovespa Index and 14 stocks from BM & FBOVESPA), the Wavelet Index presented average early sale losses of 45.0% in the search group and 43.9% in the test group, with no false negatives, using Meyer wavelet in 4 cases, Haar and Gaussian 1 wavelets in 3 cases each.

Table 9 .
Performance evaluation: average return by drop (Source: Author).