# Market Analysis

## An application of Wavelet Theory by LTSM-RNN-CNN Architecture

This project is a automated trading bot that can both buy long and short positions on the stock market utilizing the theoretical framework of wavelet theory to inform it's trade

## Agent Performance

Using the most recent 150 days of our data as validation on a random sample of tickers from the S&P-1500, The agent has a relative profit of ~0.05% per day compared to the underlying asset's performance (i.e. buy and hold). This translates to beating the underlying asset by ~20% over the year assuming compound gains from reinvestment.

## Source Code

The source code can be found here

## Features For Each Stock Ticker

- Open, Close, High, Low, EMA(12), EMA(26), MA(6), DI, MACD, and PSY(13)

The above features are preformed on data that's been differenced once i.e. $\widetilde{X}_t = \Delta X_t = X_t - X_{t-1}$. This is done as financial instruments tend to show a linear trend over a short time period, this can be tested using a stationary test such as the Augmented Dickey Fuller Test. Given the Trading bot cares more about price direction/action rather than price level the first difference will give us more relevant information.

Furthermore features were chosen to give a wide variety of information upfront to improve training efficiency.

## Theoretical Motivation

Usually the focus of analysis on a time series stays within the *time-domain*
but there's a set of tools and methods available for the *frequency-domain*.
Given a zero-mean stationary time series $X_t$ we can utilize the fact of
the spectral representation of a stationary process to write

Where $Z(\lambda)$ is a complex-valued process with uncorrelated increments.
In essence this means we can represent any stationary time series in the
*frequency-domain* and not lose any information (or vice-versa).

This is of particular importance when a process is known to exhibit some form of periodic behavior suppose $X_t$ is stochastic periodic series, i.e.

Given $\omega$ is a constant fix $\omega = \pi$ for simplicity, then we have

We see for $t = 0$

And for $t = 2$

In fact $\forall t: t = 2k;\enspace k \in \Z$

And $\forall t: t = 2k + 1;\enspace k \in \Z$

With a stochastic periodic function it returns to a particular mean at given
intervals. The *frequency-domain* would then give us insight at when $X_t$
completes one full period, I.e. it would spike at $t = 2$. While this is
trivial let

The frequency-domain for $W_t$ would give us a spike at $t = 1, 2$ Generalizing this the frequency domain allows us to extract periodic components/signals from a time series. If we wanted to give it an upward trend such that on average it would increase over time all we need to do is add $t$.

Note that under a difference operator $\Delta$

Without loss of generality we have

Which is another periodic stochastic time series.

If the periodicity is fixed like above then the intervals are regular and we can use the Spectral Fourier Transform. However, not all time series exhibit regular periodic intervals, especially financial instruments, and hence we will have use more generalized approach, Wavelets.

### A Wavelet

A Wavelet allows us to operate on the frequency-domain. We start by introducing
two functions: *mother wavelet* $\psi(t)$ (the wavelet function) and a
*father wavelet* $\phi(t)$ (the scaling function).

A low-pass and high-pass filter can be derived from both the mother and father
wavelet functions. The mother wavelet is a high-pass filter and the father
wavelet is the low-pass filter. A standard example of a low-pass filter is any
convolution, say a moving average. Where an example of a high-pass filter is a
differencing operation i.e. $X_t - X_{t-1}$. The low pass filter extracts the
lower-frequency patterns while the high-pass filter extracts the
higher-frequency patterns. Together these make up a *band-pass filter* which
allows you to exact and isolate both low and high signals from your data -
without implying the periodicity stays the same.

Given these two facts hold:

- A father wavelet can be seen in a CNN by definition you performing a convolution hence you are extracting low-frequency data
- A mother wavelet can be seen in a LTSM-RNN byallowing functional combinations of previous lags through LTSM cells (A difference operator is just a linear combination)

Thus LTSM-RNN-CNN architecture will allow an emergent property something similar to a trainable wavelet function in you have both the mother and father wavelet allowing you to exact frequency-domain information from the given time series.

## Theoretical Implications

Now given that for the U.S. market on average financial time series are highly cyclical, and trending upwards. Denote $P_t$ as the profit at time $t$, where $P_0 = 0$. Suppose we bought and held for the entire time, then

Suppose you bought at $t=0$ and then sold and rebought every cycle. Let $\gamma$ be the difference between the selling and rebuying price. For simplicity suppose the buy and hold strategy is represented by selling and immediately rebuying i.e. $\gamma$ = 0, then

Maximum profit would be selling the maximum value of each cycle and buying the minimum value of each cycle, however any $\gamma > 0$ will beat the strategy of buying and holding. Hence, being informed about the periodic behavior of the underlying time series can allow you to capture more profit.