TensorFlow and Fractal Patterns

It was finding patterns in volatility that gave me second place in a trading competition. Not a competition with amateurs but against a hedge fund manager, quants, and professional traders. The only reason it was not a first place finish was one mistake I made on the last day.

Since that time I have discovered some incredible algorithms based upon fractals to model volatility within exchange traded markets. Now, I am experimenting with applying machine learning to the fractal models using TensorFlow.

TensorFlow is an Open Source Machine Learning Framework by Google. It supports a variety of platforms and can even be used for Deep Learning. Before jumping in we should understand what a tensor is. For those of us with little math beyond Calculus, it is worth some background.

A tensor is a mathematical term for a geometric object that describes linear relations between vectors, scalars, and other tensors. In most basic terms, it is a multidimensional array. Nothing scary. We have seen these before.

During my first trading competition I was working for a hedge fund and developing software to assist human traders. My experience with trading at the time was limited to a simulation game I played as a teenager on my 386 DOS based computer.

However, I wanted to win as first place meant a bonus and call for a promotion. Opening the trading environment for the first time was overwhelming. It was full of time-series charts, a few controls, and blinking everything.

After settling into the noise of it all, I noticed the clusters of volatility. Later learning from Benoit Mandelbrot’s book, The Misbehavior of Markets, that markets behave like seas.

Prices are impossible to predict due to their almost random nature. However, volatility is somewhat predictable in a manor similar to weather. Watching a chart or ocean on a calm day, one notices little change. In contrast and sometimes without warning, things can change to periods of large waves or market jumps.

As I zoomed out on an asset’s chart, it became smooth. Zooming in, the lines became more bumpy. Representing small changes along the trend. Going further to a single day, the lines were more bumpy still. Later, I learned that this pattern of volatility shares many properties with fractals. That is, as one zooms into the object, the smaller parts resemble the larger ones.

Fractals are made with recursion. Meaning a simple equation is solved with the result being assigned to one of the variables of the next iteration within a continuos loop. Mandelbrot believed that these patterns are unique for each exchange traded asset. A hypothesis gained by studying cotton markets of the 1800's.

During my trading competition, I found patterns in the charts. It seemed assets were moving up or down to a point then would reverse. If the climb was fast the fall was usually fast. When prices fell slowly, they would rise slow too. All with few exception.

One of the most intriguing things about fractals is the Coastline Paradox. This is where the scale of measure matters. If one was to measure the British Coastline with a ruler (12 inches), it would be longer than if one measured it with a yard stick (36 inches) due to more irregularity accounted for. As scale increases, things appear more smooth. Once zoomed in, the bumps and sharp edges become more visible.

The goal here is to discover patterns of volatility for the assets we are interested in. Just like measuring a coastline, the pattern will be more noticeable the smaller our measuring device.

After placing a trade based on a volatility trend, I would nervously watch the price. It would follow the trend. After a few movements of the ticker, I closed the trade. This worked. However, I also needed to get my work done and did not have time to watch a chart. Therefore, I decided to go against the trend.

It does not matter if you go with or against the trend. The only difference is the timing. If an asset is moving upward, you can take a long position. Closing it profitable after some movement. Just be careful about the timing. In contrast, you can take a short position, wait a little longer, then close it for a profit too.

There is probably many exceptions to this but as a quick and dirty strategy it worked. My account had doubled in size since the beginning of the competition and I was poised to win. That was until one stupid mistake.

Applying TensorFlow

Before moving forward, it is important to realize what we want to accomplish. First, we must measure patterns to create a type of fingerprint. If you ever used Google Correlate then you realize that many datasets look alike at a large scale. Therefore we are only going to look at zoomed in detail of assets pricing.

TensorFlow has a sample we can use to get started. It is for classifying flowers based on the length and width of their pedals. However, it does classify based upon measurement, so we can take it as a starting point and make adjustments as needed.

In traditional programming we would build a model. However, in TensorFlow, we can rely upon our application creating its own model from the data provided. That is, if we feed enough representative examples into the proper machine learning model type, the program will determine the relationship between the parameters for us.

To begin, we must train the system. In our case, it will be supervised learning since the data is labelled. So here are the steps:

Import Dataset

The training set contains the examples we’ll use to train the model. In our case it is historic stock prices by minute. In addition, we will have a test set used to evaluate the trained models effectiveness.

To keep things simple we will use Backtest Data historic stock information as our training set. Please note that it is not free but is the most detailed available. This detail is what we need to discover volatility patterns.

Part of the data will be used for training and the other part for testing. This means we split the list into two datasets. It does not matter how it is split. We just need a set for testing that is different than the training data.

Create Feature Columns

The Feature Column is a data structure that tells your model how to interpret the data in each feature. Each object describes an input to the model. For this purpose, the options are numeric, indicator, and embedding.

A numeric column will be represented as a floating point number since we are dealing with decimals. Indictor columns are for named categories such as a stock symbol. However, since we are dealing with thousands of different categories, it is best to use an embedding type. This is because an embedding column stores categorical data in a lower-dimensional vector than an indicator column.

Select the Model Type

In order to discover volatility patterns we will use a neural network as our model type. A neural network is a highly-structured graph, organized into one or more hidden layers.

There are several categories of neural networks. We’ll be using a fully connected neural network, which means that the neurons in one layer take inputs from every neuron in the previous layer. The more hidden layers we allow, the stronger our model — requiring more data to train.

Train the Model

The network is built, but we still need to put data into it. This is where the training comes in. TensorFlow provides built in functions for this. We just need to ensure all of our settings are correct then load the training set.

Evaluate the Model’s Effectiveness

To determine the effectiveness of our model, we need to pass some stock symbols to it. The model should process the stock symbols giving us a classification for the pattern. The expectation is a different classification for each stock symbol.

Our goal is to find a unique volatility pattern for each stock symbol we provide. A kind of fingerprint for the asset. The reason for this is we are building the beginning of a Deep Learning based trading strategy.

On the last day of my trading competition, I made a mistake. There was no volatility pattern I could detect with a simple chart. Therefore I decided to take a gamble. Making a less than ideal trade.

Let the Trained Model Make Predictions

It would be nice if this just worked. However, like most things, it is not that easy. What happens is that we just built something that may work. However, it will need some tweaks to do what is needed.

What we want is a unique pattern. Based upon how you fulfilled the coding requirements, you may have gotten that. However, it is more than likely you ended up something entirely different. No problem. Just make a few adjustments and run it again.

As I made a gamble during the last few hours of the trading competition, I knew it was not smart. However, I wanted to win and was not certain that my profits were better than the other participants. After all, my boss, the Hedge Fund Manager, made $30,000 the very first day of the competition.

The trade started to move in the opposite direction I needed. “No problem”, I thought as I still had time. However, it kept moving against me. “Maybe just a few more minutes and it will not be too bad,” I nervously concluded.

Just then I was assigned a new coding project and left the trade open. Actually, I forgot about it. At the end of the day I was told to close all positions. That one gamble took 30% of my profits. That is how I came in second and why I now do everything possible to eliminate risk.

Summary

I hope you were not disappointed by the lack of sample code. The reason for this is that all of the code needed is explained on the TensorFlow website.

Deep Learning is not so much about code as it is about data. It is not a straight forward algorithm that creates the application. Instead, the machine makes its own application from the data provided.

Like what you read? Give Todd Moses a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.