+ All Categories
Home > Documents > Examensarbete Systemarkitekturutbildningen Philip...

Examensarbete Systemarkitekturutbildningen Philip...

Date post: 04-Apr-2019
Category:
Upload: vuongphuc
View: 222 times
Download: 1 times
Share this document with a friend
39
SEQUENCE-TO-SEQUENCE LEARNING OF FINANCIAL TIME SERIES IN ALGORITHMIC TRADING 2017KSAI02 Examensarbete Systemarkitekturutbildningen Philip Arvidsson Tobias Ånhed
Transcript
Page 1: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

SEQUENCE-TO-SEQUENCE LEARNING

OF FINANCIAL TIME SERIES IN ALGORITHMIC TRADING

2017KSAI02

Examensarbete Systemarkitekturutbildningen

Philip Arvidsson Tobias Ånhed

Page 2: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

Systemarkitekturutbildningen är en kandidatutbildning med fokus på programutveckling. Utbildningen ger studenterna god bredd inom traditionell program- och systemutveckling, samt en spets mot modern utveckling för webben, mobila enheter och spel. Systemarkitekten blir en tekniskt skicklig och mycket bred programutvecklare. Typiska roller är därför programmerare och lösningsarkitekt. Styrkan hos utbildningen är främst bredden på de mjukvaruprojekt den färdige studenten är förberedd för. Efter examen skall systemarkitekter fungera dels som självständiga programutvecklare och dels som medarbetare i en större utvecklingsgrupp, vilket innebär förtrogenhet med olika arbetssätt inom programutveckling.

I utbildningen läggs stor vikt vid användning av de senaste teknikerna, miljöerna, verktygen och metoderna. Tillsammans med ovanstående teoretiska grund innebär detta att systemarkitekter skall vara anställningsbara som programutvecklare direkt efter examen. Det är lika naturligt för en nyutexaminerad systemarkitekt att arbeta som programutvecklare på ett stort företags IT-avdelning, som en konsultfirma. Systemarkitekten är också lämpad att arbeta inom teknik- och idédrivna verksamheter, vilka till exempel kan vara spelutveckling, webbapplikationer eller mobila tjänster.

Syftet med examensarbetet på systemarkitekturutbildningen är att studenten skall visa förmåga att delta i forsknings- eller utvecklingsarbete och därigenom bidra till kunskapsutvecklingen inom ämnet och avrapportera detta på ett vetenskapligt sätt. Således måste de projekt som utförs ha tillräcklig vetenskaplig och/eller innovativ höjd för att generera ny och generellt intressant kunskap.

Examensarbetet genomförs vanligen i samarbete med en extern uppdragsgivare eller forskningsgrupp. Det huvudsakliga resultatet utgörs av en skriftlig rapport på engelska eller svenska, samt eventuell produkt (t.ex. programvara eller rapport) levererad till extern uppdragsgivare. I examinationen ingår även presentation av arbetet, samt muntlig och skriftlig opposition på ett annat examensarbete vid ett examinationsseminarium. Examensarbetet bedöms och betygssätts baserat på delarna ovan, specifikt tas även hänsyn till kvaliteten på eventuell framtagen mjukvara. Examinator rådfrågar handledare och eventuell extern kontaktperson vid betygssättning.

BESÖKSADRESS: JÄRNVÄGSGATAN 5 · POSTADRESS: ALLÉGATAN 1, 501 90 BORÅS TFN: 033-435 40 00 · E-POST: [email protected] · WEBB: WWW.HB.SE/HIT

Page 3: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

Svensk titel: Sekvens-till-sekvens-inlärning av finansiella tidsserier inom algoritmisk handel Engelsk titel: Sequence-to-Sequence Learning of Financial Time Series in Algorithmic Trading Utgivningsår: 2017 Författare: Philip Arvidsson, Tobias Ånhed Handledare: Patrick Gabrielsson Abstract Predicting the behavior of financial markets is largely an unsolved problem. The problem has been approached with many different methods ranging from binary logic, statistical calculations and genetic algorithms. In this thesis, the problem is approached with a machine learning method, namely the Long Short-Term Memory (LSTM) variant of Recurrent Neural Networks (RNNs). Recurrent neural networks are artificial neural networks (ANNs)—a machine learning algorithm mimicking the neural processing of the mammalian nervous system—specifically designed for time series sequences. The thesis investigates the capability of the LSTM in modeling financial market behavior as well as compare it to the traditional RNN, evaluating their performances using various measures. Keywords: deep learning, machine learning, quantitative finance, algorithmic trading, black-box trading, lstm, rnn, time series forecasting, prediction, tensorflow, keras, forex, neural network, econometrics

Page 4: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

Sammanfattning Prediktion av den finansiella marknadens beteende är i stort ett olöst problem. Problemet har tagits an på flera sätt med olika metoder så som binär logik, statistiska uträkningar och genetiska algoritmer. I den här uppsatsen kommer problemet undersökas med maskininlärning, mer specifikt Long Short-Term Memory (LSTM), en variant av rekurrenta neurala nätverk (RNN). Rekurrenta neurala nätverk är en typ av artificiellt neuralt nätverk (ANN), en maskininlärningsalgoritm som ska efterlikna de neurala processerna hos däggdjurs nervsystem, specifikt utformat för tidsserier. I uppsatsen undersöks kapaciteten hos ett LSTM att modellera finansmarknadens beteenden och jämförs den mot ett traditionellt RNN, mer specifikt mäts deras effektivitet på olika vis. Nyckelord: finans, algoritmisk handel, tidsserier, prediktion, maskininlärning, forex, neurala nätverk, tensorflow, keras, kvantitativ finans, lstm, rnn, ekonometri

Page 5: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

This page intentionally left blank.

Page 6: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

S����������S������ L������� ��F�������� T��� S����� �� A����������

T������

Philip [email protected]

Tobias Å[email protected]

June 5, 2017

Page 7: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

“Research is to see what everybody else has seen, and tothink what nobody else has thought.”

ALBERT SZENT-GYORGYI

Page 8: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

����������������

We’d like to express our gratitude to our supervisor, Patrick Gabrielsson, without whom thisthesis would not have been written. He has guided us throughout our research and sharedwith us his vast knowledge and experience in the �elds of quantitative �nance and machinelearning.

��������

Predicting the behavior of �nancial markets is largely an unsolved problem. The problem hasbeen approached with many di�erent methods ranging from binary logic, statistical calculationsand genetic algorithms. In this thesis, the problem is approached with a machine learningmethod, namely the Long Short-Term Memory (����) variant of Recurrent Neural Networks(���s). Recurrent neural networks are arti�cial neural networks (���s)—a machine learningalgorithm mimicking the neural processing of the mammalian nervous system—speci�callydesigned for time series sequences. The thesis investigates the capability of the ���� inmodeling �nancial market behavior as well as compare it to the traditional ���, evaluatingtheir performances using various measures.

C�������

1 I����������� 11.1 Background and Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.1.1 Financial Markets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.1.2 Arti�cial Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.2 Current Research . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.3 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

2 T����� 32.1 Financial Markets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2.1.1 The Foreign Exchange Market . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.1.2 Technical Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.1.3 Japanese Candlesticks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.1.4 Chart Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.1.5 Technical Indicators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.1.6 Financial Measurements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.1.7 Non-stationary Market Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.1.8 Trading Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

2.2 Machine Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.2.1 Generalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.2.2 Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.2.3 Supervised Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.2.4 Arti�cial Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.2.5 Gradients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.2.6 Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.2.7 Over�tting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.2.8 Feature Extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.2.9 Recurrent Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.2.10 Vanishing Gradients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.2.11 Long Short-Term Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.2.12 Sequence-to-Sequence Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2.3 Algorithmic Trading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.3.1 Black-box Trading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.3.2 Non-stationarity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

ii

Page 9: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

3 R������ W��� 123.1 Financial Market Predictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123.2 Sequence-to-Sequence Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

4 M���������� 134.1 Method and strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

4.1.1 General Design Cycles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134.2 Ethical Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134.3 Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

4.3.1 Candidate Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134.3.2 Selected Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144.3.3 Discarded Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

4.4 Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144.4.1 Docker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144.4.2 Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144.4.3 Keras and TensorFlow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144.4.4 Other Third-party Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

4.5 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154.5.1 Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154.5.2 Preprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

4.6 Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154.6.1 Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154.6.2 Evaluation Measures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

5 E���������� 175.1 Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175.2 Model Con�gurations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175.3 Sliding Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175.4 Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175.5 Model Training . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185.6 Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

6 R������ 196.1 Measurements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

7 D ��������� 207.1 Financial Market Prediction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207.2 Visualizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

8 C��������� 21

9 F����� W��� 229.1 Trading Performance Estimates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229.2 Q-learning Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229.3 Exploring Larger Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229.4 Trying Di�erent Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229.5 Autoencoder Feature Extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

iii

Page 10: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

1 I�����������

Time series forecasting—predicting future values of vari-ables within temporal datasets—is largely an unsolvedproblem in complex or chaotic domains such as weather(e.g. humidity, temperature or wind speed) and economics(e.g. currency exchange rates or stock prices). Examiningthe problem with the latest models in the �eld of machinelearning—Long Short-Term Memory (����) based Recur-rent Neural Networks (���s)—gives rise to new hope ofbeing able to predict time series in these domains.

1.1 B��������� ��� M���������

Algorithmic trading combines the �elds of �nance and al-gorithmics/machine learning. To understand the problem,a high level of knowledge is desirable in both of these—and surrounding—domains. Below, �nancial markets andarti�cial neural networks as well as their connection toalgorithmic trading is brie�y summarized. It is furtherexplained in the Theory chapter.

1.1.1 Financial Markets

The price movements on �nancial markets are di�cult(even impossible, according to the e�cient-market hypoth-esis) to predict (Fama, 1995). If one were able to predictthe movements of, for example, stock prices or currencyexchange rates, that information could be used to “beatthe market” (essentially, buying before a rise and sellingbefore a drop) to continuously increase the value of aninvestment portfolio, yielding a positive net return.

Predicting a �nancial market, whether the methodused for prediction consists of a fundamental- or technicalapproach, rests on devising a certain successful strategy—looking at and considering a variety of factors and drawinga conclusion as to how the market should be operated on(for example, deciding at what point in time buying acertain �nancial asset is appropriate).

Furthermore, if the prediction process could be auto-mated through the application of an algorithm that decideswhen a �nancial asset should be bought or sold with somelevel of accuracy (with respect to its predictions of pricemovements)—in practice, letting the algorithm performtrading operations autonomously on the market with realmoney—the algorithm would turn out to be pro�table,generating continuous positive net returns over time.

Many attempts have been made with some success.During the last decade, there has been a rise in algorith-mic trading from roughly 20 percent of the volume on US

equity markets in 2005 up to over 50 percent in 2010 (Kaya,2016), and although the rise seems to have reached its limitduring recent years, the sheer volume of high-frequencytrading implies some level of credibility for the approach.

1.1.2 Arti�cial Neural Networks

Arti�cial neural networks (���s) have been applied forseveral decades to solve multinomial logistic regression(determining what class a data point belongs to) and linearregression (determining the value of a dependent vari-able as a function of a data point’s independent variables)problems. The early versions of ���s (e.g. the single layerperceptron) could only solve linearly separable problems(Rosenblatt, 1958). An ���, expressed in simple terms,is a mathematical model of a biological brain, consist-ing of neurons, synapses and dendrites, modeled by the��� through cells with scalars, activation functions andweights.

The earlier models were also unable to predict timeseries (i.e. determining what class a data point belongsto with respect to its movement over time, or determin-ing the value of a dependent variable as a function ofthe independent variables’ movement over time), some-thing that was later solved through the introduction ofrecurrent neural networks (Rumelhart et al., 1986), whichcan handle datasets with a temporal component dictatingtheir intradependence and distribution within the dataset,where each point in the sequence is correlated to previous(with respect to time) points through several known andunknown factors.

���s have historically had problems with vanishing(or exploding) gradients—meaning they break down dur-ing training and become unable to model the problem well(Pascanu et al., 2012)—but this problemwas solved throughthe introduction of the ����-based ���s (Hochreiter andSchmidhuber, 1997).

Regardless of whether the problem is approached as amatter of classi�cation or regression, the temporal aspectrenders algorithms and models not speci�cally designedfor the task impotent since one single input dataset couldbelong to several di�erent classes depending on where, intime or order, it appears in the data sequence. The problemshould therefore be examined with algorithms designedspeci�cally for time series, such as ����-based ���s.

Furthermore, sequence-to-sequence learning is amethod for training the network on output sequencesrather than providing it with a single target value follow-

1

Page 11: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

ing the input sequence; instead of giving the model aninput sequence and the next, single value following thesequence, the model is given the sequence following theinput sequence. This is also the method used to modelthe problem in this thesis and is explained in detail in thetheory chapter.

1.2 C������ R�������

Currently, in the �eld of machine learning, hiddenMarkov models (���s), dynamic Bayesian networks(���s), tapped-delay neural networks (����s) and ���sare commonly used to handle sequential datasets, andhave been applied with some degree of success to �nan-cial markets (Saad et al., 1998; Kita et al., 2012; Zhang,2004).

Although much research is currently being done intothe application of ����-based ���s in �nancial markets,the research is still in its infancy, and although the ����-based ��� has been around for twenty years, it is onlyrecently that progress is being made to a signi�cant de-gree in practice. The volume of data created in the pastcouple of years—about 90 percent of all data ever createdby humans (Devakunchari, 2014)—together with relativelycheap but very fast graphics processing units (���s), haveopened up the �eld of machine learning to more cost e�-cient research.

More recently, deep ����-based ���s have been ap-plied with some level of success in predicting future timeseries from historical sequential datasets; such as predict-ing the weather for the next twenty-four hours (Zaytarand Amrani, 2016), or predicting the next sequence ofwords in natural language (Sutskever et al., 2014).

While natural language has obvious structure (i.e.grammar), weather, instead, is seemlingy chaotic, con-

trolled by many unknown factors. Despite this; consider-ing the success in weather prediction, ����-based ���smay have the ability to take into account these factors,especially given an appropriate set of features—input dataprocessed to make certain aspects more prominent, thusenabling the algorithm to model the problem more easily.

1.3 P������ S��������

Financial time series are used ubiquitously in algorithmictrading. In algorithmic trading, it is imperative that accu-rate predictions are made about numerous variables (suchas volatility and returns) in order to time market entryand exit.

Since deep ����-based ���s (speci�cally usingsequence-to-sequence learning) have not been appliedwithin the algorithmic trading domain before, and sincethey have shown success in solving similar problems inother domains, it raises the question whether the tech-nique can be used to predict a future sequence of �nancialvariables that can be used to time both entry and exit posi-tions within a certain time horizon. The uniqueness of thisapproach is the prediction of sequences at a time, ratherthan predicting a single data point on each iteration.

Assuming that correlations exist along the temporaldimension of the dataset, the problem is reduced to amatter of �nding an appropriate set of features enhancingthe correlations, on which to train the ����-based ���.Expressed in a more concise manner, we attempt to an-swer the question:

— Can price movements on �nancial markets be mod-eled through the application of ����-based ���s, speci�-cally using sequence-to-sequence learning, and if so, howdoes the ����-based ��� compare to the traditional ���.

2

Page 12: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

2 T�����

A deeper understanding of all relevant aspects of the thesisproblem is laid out below. Some references are made with-out being explained further; these are to be considered aslying outside the delimitations of the thesis.

2.1 F�������� M������

A �nancial market is a trading market in which peopletrade �nancial securities, commodities and other assets. Amarket participant places a bid or ask for a certain secu-rity; a binding commitment to buy or sell that security. Allcurrent asks and bids are kept track of through the use ofthe order book. When a matching bid or ask is placed forthe same security, a transaction occurs—the asset changesownership to the purchasing party (having placed a bidfor the security) and money is transferred to the sellingparty (having placed an ask for the security).

The number of asks and bids in the market at any pointin time for a given price level comprises the market depth,and the total number of transactions (i.e. matching pairs ofasks and bids) comprise the traded volume in the market(as opposed to the number of contracts in the market; themarket volume).

Both bids and asks can be placed as market orders,meaning that they immediately trade at the current bestmarket price (not counting slippage costs). The alternativeis to specify a certain price—the limit price—guaranteeingthat the order will trade at a price no worse than the limitprice, hence sacri�cing immediacy for certainty in regardto the execution price (although the order risks not tradingat all if the market never reaches the limit price). In fact,it is these, so called, limit orders that show up in the limitorder book.

Transactions also carry transaction costs, includinga brokerage fee—a fee charged by the broker to conducttransactions between buyers and sellers, as well as anexchange fee.

2.1.1 The Foreign Exchange Market

The Foreign Exchange Market (�����) is a global decen-tralized �nancial market where currency pairs are traded.Although it is a �nancial market like many others, it hasa certain set of attributes distinguishing it from others: Itstraded volume is the largest of any �nancial market, it is aglobal, decentralized market, operated by a broker-dealer,as opposed to the more common centralized exchanges(except for e.g. ����� derivatives traded on the ���), it is

the most liquid asset of all asset classes, it is always open(except weekends), price movements are generally verysmall and broker-dealers o�er highly leveraged marginaccounts.

Market movements are announced as price interestpoints—pips (on other �nancial markets, known as ticks).A pip designates a minimal change in price. For example, aprice movement from 1.2345 to 1.2346 represents a singlepip, i.e. a tick step of 0.0001. A pip is always a change inthe last decimal digit of the price (the currency exchangerate).

2.1.2 Technical Analysis

Technical analysis of �nancial assets is a method for un-derstanding the asset value (and its movements) only bylooking at previous market data (i.e. volume, ask price, bidprice etc.). This can be looked at in contrast to fundamentalanalysis; estimating the intrinsic value of an asset by look-ing at various aspects and properties of the underlyingcompany, market sector and macroeconomic factors; forexample, looking at company �nancials or social trends.

In regard to the e�cient-market hypothesis—the hy-pothesis that the market is e�cient to such a degree thatthe market price immediately incorporates and re�ectsall relevant information (Fama, 1995)—the e�ectiveness oftechnical analysis is disputed: The quote “I realized thattechnical analysis didn’t work when I turned the chartupside down and didn’t get a di�erent answer” has beenattributed to Warren Bu�ett, who prefers a fundamen-tal analysis approach to investment. However, the over-whelming empirical evidence of market ine�ciencies sug-gests that the e�cient market hypothesis can be refuted,hence providing a solid foundation for predicting futuremarket behavior.

2.1.3 Japanese Candlesticks

Often a vital part in charting analysis, the Japanese candle-stick provides a simple method of aggregating and plottingmarket behavior (Nison, 2001). Instead of plotting eachindividual pip (resulting in a very �at and very wide chart),pips are aggregated into candlesticks. This method can beused with any time frame (dictating how many pips endup in a single candlestick).

The candlestick is made up of three parts: The realbody, the upper shadow and the lower shadow. The realbody is bounded by open and close prices for all pips in the

3

Page 13: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

time frame aggregated into the candlestick. For example,if the time frame is one minute, assuming that the lastpip has a higher value than the �rst pip, the lower edgeof the real body denotes the �rst pip encountered thatminute (the open price), while the upper edge of the realbody denotes the last pip during that minute (the closeprice). Otherwise, if the last pip has a lower value thanthe �rst pip, the lower edge denotes the last pip, and theupper edge denotes the �rst pip. Usually, candlesticks witha close price higher than open price are represented by awhite (sometimes green) body while candlesticks with aclose price lower than an open price are represented by ablack (sometimes red) body.

The top of the upper shadow, positioned on top of thereal body, represents the high price during that minute;the highest value encountered in any pip that occurredduring the time interval that the candlestick represents.Likewise, the bottom of the lower shadow represents thelow price; the lowest value found in any pip during thetime frame.

Together, these provide the open, high, low and closeprices during the candlestick’s time frame (often referredto as ���� data.)

Figure 2.1: The Japanese candlestick and its parts.

Chart patterns (see below) often rely on Japanese can-dlestick aggregation to provide meaningful indicationsregarding market behavior. Moreover, the time frame rep-resented by the candlestick can be set to any amount oftime, allowing the trader to perform the charting analysisusing various resolutions.

2.1.4 Chart Patterns

Giving more credibility to the idea of technical analysis,there exist certain chart patterns that can be observed priorto speci�c market expectations and behavior (Kirkpatrickand Dahlquist, 2010). The patterns can be seen in the pricemovements of a �nancial security (sometimes combinedwith looking at the market volume or any other aspectof market behavior). As a concrete example of a pattern,there is the bull �ag, represented by a strong upwardsmovement (in the case of the bull �ag, called the �agpole,usually combined with a surge in market volume), afterwhich a trend channel (representing the �ag) is charted.When the price breaks through the upper boundary ofthe trend channel, the bull �ag pattern is completed and a

continuation of the rise is expected to follow.There is also the double top chart pattern (illustrated

below), which is a bearish reversal pattern; the pattern canbe identi�ed as two distinct price tops reaching roughlythe same level. If the price of the �nancial security dropsbelow a certain lower boundary of a trend channel chartedaround the pattern, it is expected that the price will dropfurther afterwards.

Figure 2.2: The bearish trend-reversal double top chartpattern.

There are a several di�erent chart patterns indicating andpredicting certain market behavior. They raise an inter-esting and relevant point, which will be touched uponagain when the intricacies of neural network models arediscussed below: If there are certain patterns recurringbefore certain market behavior, hypothetically, a properlycon�gured neural network model should be able to learnto identify them under the proper circumstances and thusshow potential in predicting market behavior.

These chart patterns are of great importance to thethesis problems, as—which will be detailed in the machinelearning section—recurring chart patterns is a vital compo-nent for machine learning algorithms to model problemswell.

No matter whether the trader is a human or a machine,chart patterns are a vital part of a technical analysis ap-proach to trading strategies. For the sake of the thesisproblem, we ascribe a high level of credibility to thesechart patterns—it is not part of this thesis to investigatetheir validity in technical analysis trading.

2.1.5 Technical Indicators

While the occurrence of chart patterns are used as indica-tors for trading in technical analysis, there are also othertechnical indicators in the form of certain measures andcomputations of market properties.

As part of the trading strategy, like chart patterns, thesetechnical indicators are used to aid in trading decisions. Allof the technical indicators are based on already-availablemarket information embedded in e.g. prices, volume etc.The technical indicators are computed on this informa-tion, giving evaluation measures that can be used to makecertain aspects of the market behavior more prominent.

4

Page 14: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

������ ������ ������� (���)

The Simple Moving Average is one of the most basic indi-cators used for trading. It is calculated by simply summinga certain number of price points (in sequence) togetherand dividing it by the number of price points sampled. Forexample, to calculate the ��� of a price at time t : SampleN points before t , add the prices together and divide theresulting value by N :

SMAN (t) = 1N

N�1’i=0

Price(t � i)

Equation 2.1: Simple moving average formula.

�������� �������� ����� (���)

The Relative Strength Index is a technical indicator used toassess the strength and weakness of a �nancial asset basedon the closing prices over a certain period of time. The��� measures the velocity and magnitude of price move-ments, computing the momentum—the rate of averagegain versus average loss in prices over a certain period.

The period used to calculate the ��� is normally setto fourteen days, but the period can also be arbitrarilychosen.

The ��� gives a value on a scale from 0 to 100, withlow and high values set at 30 and 70, respectively. It isreasoned that an ��� indicator showing a value lower than30 indicates that a �nancial asset is oversold, where as avalue higher than 70 indicates that it is overbought (Wilder,1978).

Although the ��� is slightly more complex to calculatethan the ���, a variation of the ���—called Cutler’s ���—provides a simpler way of calculating it. Cutler reasonedthat the original ���, introduced by Wilder (1978), using asmoothed moving average, provided very di�erent resultsdepending on what data point (time) in the sequence atwhich computation started. Because of this, Cutler’s ��� isinstead based on the ���:

D

N (t) = 1N

N�1’i=0

max(Price(t � i � 1) � Price(t � i), 0)

U

N (t) = 1N

N�1’i=0

max(Price(t � i) � Price(t � i � 1), 0)

RSN (t) = U

N (t)D

N (t)

RSIN (t) = 100 � 1001 + RSN (t)

Equation 2.2: Cutler’s relative strength index formula.

There are many more technical trading indicators. All of

them claim to measure various aspects of the data, andsome of them work together to provide a useful indication.

2.1.6 Financial Measurements

������� ������

Although not strictly a trading indicator, the spread (dif-ference between the best ask and best bid prices) is usedas a model feature in our setup. It is a measurement ofthe transaction cost associated with market orders and anindicator for the liquidity of the market. The formula forthe spread is trivial:

Spread(t) = Askt � Bidt

Equation 2.3: Bid-ask spread formula.

������ �����

A more sophisticated measurement is the Sharpe Ratio(Sharpe, 1966), which provides a way to analyze the risk-adjusted performance of �nancial assets, portfolios andtrading strategies, taking into account the risk factor. Alarger Shape ratio indicates better performance, while alower Sharpe Ratio indicates worse performance. More-over, comparing the Sharpe Ratio of two or more �nancial�nancial assets, portfolios or trading strategies, a greaterSharpe Ratio is characteristic of better performance withrespect to risk1. The Sharpe Ratio is calculated as follows:

Sa =E[Ra � Rb ]

�a

=E[Ra � Rb ]pVar [Ra � Rb ]

Equation 2.4: The Sharpe Ratio formula.

In the formula above Ra is the asset return, Rb is the returnon a benchmark asset (the risk free rate of return on anindex), E[Ra � Rb ] is the expected value of the excess ofthe asset return over the benchmark return and �a is thestandard deviation of the asset excess return.

2.1.7 Non-stationary Market Behavior

A stationary process is a stochastic process whose jointprobability distribution does not change when shifted intime. Consequently, measurements such as mean and stan-dard deviation remain constant.

Contrary to the above, non-stationarity implies thatthe stochastic process is heteroskedastic with respectto time, meaning that the expected variance changes(Tunnicli�e-Wilson, 1989). In turn, this means that mea-surements performed on a market might work well for

1A greater Sharpe Ratio implies higher return for the same risk or the same return for a lower risk

5

Page 15: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

technical analysis at one point in time, but might alsostop working when the market meta-behavior—the hy-pothetical set of hyper-parameters dictating the marketbehavior—changes.

A common technique used to transform a non-stationary process into a (weakly) stationary process, is toremove the trend from the process (for processes with a de-terministic trend) or to use di�erencing (for processes withone or more unit roots). Therefore, prices in �nancial timeseries are often di�erenced to create di�erence-stationaryprocesses. In fact, the normalized, �rst di�erence of �-nancial time series prices is commonly used. This is alsoknown as the return, and is calculated according to Equa-tion 2.5:

Return(t) = Price(t) � Price(t � 1)Price(t � 1)

To further stabilize the variance of the time series,log returns can also be used, i.e. Log(Return).

Equation 2.5: Return formula.

2.1.8 Trading Strategies

A trading strategy—a set of rules or meta-rules for decid-ing when market entry or exit decisions are appropriateto take—builds on top of the knowledge presented in thischapter. Multiple decisions need to be made when con-structing a trading strategy: A choice must be made inregard to what market or security to trade, which techni-cal indicators to use, how to read the indicators in a wayrelevant to the market or security and changes in marketregimes.

A trading strategy can be based on a single technicalindicator (Connors and Alvarez, 2008) or a multitude ofthem. The technical indicators used in successful tradingstrategies are generally trade secrets (Chan, 2013); �ndingthe best set of technical indicators to use in a trading strat-egy is thus one of the main challenges when developingone.

2.2 M������ L�������

Thinking about problem solving in general, it becomesobvious that di�erent problems present varying levels ofdi�culty. Some problems are trivial while others are in-�nitely complex, with most lying somewhere inbetween.Although not always true (and this touches upon theP = NP problem2), generally, the more trivial a problemand its solution, the more trivial its algorithm. For exam-ple, the problem of �nding prime numbers can be solvedby applying The Sieve of Eratosthenes (Horsley, 1772).Although computationally expensive (depending on howlarge prime numbers are being tested), the algorithm itself

consists of only a handful of very concise rules, repeatedover and over, that, when applied, de�nitely leads to asolution.

Other problems, however, are not as well understoodor well-de�ned. As an example, determining whether astove is hot is not trivial; it needs to be taken into accountfor what reason we are measuring it (determining whetherit is acceptable to place a piece of steel on the stove impliesa di�erent de�nition of the word hot compared to deter-mining whether it is a good idea to place your hand on it),whether the mean temperature of the stove is to be mea-sured, at what point in time the temperature needs to beknown, and so on. This creates a complex context for theproblem—a vast amount of factors working together, de-termining how the problem should be understood—whichis required to produce a trustworthy solution.

Entermachine learning: By providing a vast amount ofmeasurements of all factors deemed relevant, a machinelearning algorithm can be trained to infer the relevant con-text from the data and model the problem appropriately.This model can then be used by an intelligent agent tomake autonomous decisions. As such, a machine learningalgorithm can “reason” about the input data in the sensethat it can create models of concept delimitation—for exam-ple, it can determine where to draw the line between hotand not hot by looking at the input data in various waysand making its decision by looking at common factors invarious groups of data points.

Figure 2.3:An illustration of a machine learning algorithmlearning to create a decision boundary between two “con-cepts.”

All on its own, an algorithm (such as decision trees) can em-ulate the creation of these internal concepts without anykind of target data—i.e. without being told what to do withthe input data. Again, this works because the algorithmcan perform certain measurements on the data, groupingor splitting it into segments that �t certain criteria. This iscalled unsupervised learning. More relevant to this thesis,however, is supervised learning which is laid out in moredetail below.

2Informally, whether a problem whose solution can be quickly veri�ed can also be quickly computed.

6

Page 16: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

2.2.1 Generalization

Machine learning works by generalizing problems, givenspeci�c input data. That is, generalization means that amachine learning algorithm, through training, creates amodel of the problem and learns to produce solutions for it.If the model produces satisfactory solutions on previouslyunseen inputs, the algorithm has created a generalizedmodel of the problem. Goodfellow et al. (2016, p. 110)correctly points generalization out as the main area ofconcern in the �eld of machine learning:

The central challenge in machine learning isthat we must perform well on new, previouslyunseen inputs—not just those on which ourmodel was trained. The ability to perform wellon previously unobserved inputs is called gen-eralization.

2.2.2 Regression

Regression, as opposed to classi�cation (determining whatdiscrete class a data point belongs to), is used to estimatereal-numbered variables from relationships between de-pendent and independent variables. That is, when a prob-lem has a solution that is not discrete (i.e. binary; yes/no,or trinary; left, forward, right and so on) but rather, is real-numbered (e.g. temperature or length), regression is used.An ��� can be con�gured to handle both problems ofregression and classi�cation by selecting the appropriateactivation function for the neural network’s �nal layer.

2.2.3 Supervised Learning

Supervised learning means that a machine learning algo-rithm is trained by providing it with target data. Explainedmore colloquially, it can be understood as showing a tod-dler di�erent objects and telling her what the di�erentobjects are, over and over again until the toddler can iden-tify the di�erent objects on her own. Algorithmically, weare now talking about arti�cial neural networks, which, asthe name implies, bears certain similarities to the humanbrain. Machine learning algorithms trained with super-vised learning can learn to identify a number of factorsthat are not easily de�ned by logic systems. Going backto the example with the stove presented earlier; by pre-senting the ��� with a temperature (and other featuresrelevant to identifying the correct answer) and telling itwhether that temperature is hot or not (as indicated by,for example, a human supervisor making an informed de-cision), the ��� eventually learns to model the problemby modifying its internal state.

With enough training, the ��� can then be presentedwith data and asked whether the data represents a hotor a cold stove, making decisions very close to the onesmade by the supervisor during the learning phase. Notonly that, but being presented with unseen data, the ���will show an ability to make a sane decision in regard to

the data representing a hot stove or a cold stove—the ���has learned to make decisions on its own, on previouslyunseen data.

2.2.4 Arti�cial Neural Networks

Firstly, an arti�cial neural network has a hierarchical struc-ture and uses a learning algorithm for modeling a problem,a concept based on the biological brain. The algorithm isa simplistic abstraction of neural processing in the mam-malian central nervous sytem; a neuron is stimulated (aweight sum of input values are fed into a neuron), the neu-ron is activated (the weighted sum is passed through theactivation function) and its output (activation) is relayedto neighboring neurons (fed to neurons in the next layer).Although arti�cial neural networks can be con�gured inmany di�erent ways, all setups normally consist of aninput layer (a number of input nodes or neurons), one ormore hidden layers, and an output layer.

Training an ��� consists of two major phases; thefeed-forward phase and the backpropagation phase. Dur-ing the feed-forward phase, weighted sums of the inputsare forward-propagated through the network and passedthrough each neuron’s activation function. At the end ofthe feed-forward phase, the output error between the ac-tual output and the true output is calculated, after whichthe backpropagation phase begins. During the backpropa-gation phase, the error is backward-propagated throughthe network. Lastly, the weights (see illustration below)are adjusted to reduce the error.

Figure 2.4: A small ��� with arrows between nodes rep-resenting the weights.

An input value is fed to each input node and the result-ing values are multiplied by their respective weights—connection strengths to neurons in the next layer (illus-trated by the lines between cells in �g. 2.4 above)—addedtogether and then passed on as input values to nodes inthe next layer in the network. For supervised learning—training the network by providing it with target data—theoutput node values are then compared to the target data,an error is calculated and the weights are adjusted accord-ingly. On the next iteration, the resulting output nodevalues will be a little bit closer to the target data. The pro-cess is repeated until the network has reached a certainlevel of pro�ciency in modeling the problem and providingaccurate output values. The end result is a set of weightsthat gives the algorithm a way of modeling the problem,thus producing correct output for a given set of inputs.

7

Page 17: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

��� ������������ �����

First, input is fed to the input layer cells. For the inputlayer, these means that their �nal output values are set tothe data being fed to the rest of network.

After that, the output is calculated for each node orcell in consecutive layers according to Equation 2.6 duringthe feed-forward phase:

� (x) = 11 + e�x

outc = �

’p

outp · weightp,c + �!

where p is a cell in the previous layer and� is the bias value from the previous layer.

Equation 2.6: ��� cell output formula using the logisticfunction as the activation function.

Or, in pseudocode:

function ���_������(c)net 0for p in ��������_����� (c ) do

net net + ���_������(p) ⇥������(p, c)end forout ����������(net + ����(��������_�����(c))return out

end function

Algorithm 2.1: Pseudocode for calculating cell output inan ���.

After this, the error needs to be calculated. This is doneby calculating the sum of squared errors3 of the outputneurons. The goal is to minimize it (during the backpropa-gation phase), but �rst, it needs to be calculated, which isdone using Equation 2.7:

Etotal =12

’o

(targeto � outo)2

where o is a cell in the output layer

Equation 2.7: Squared error calculation of an ��� output.

At this point, the feed-forward phase is completed and anerror (deviation from target output) has been calculated.It is time to move on to the backpropagation phase of the��� learning process.

��� ��������������� �����

After the error has been calculated, it is propagated backthroughout the ���, starting at the output nodes. Foreach weight in each layer, the gradient—the partial deriva-tive of the error function with respect to each weight—iscomputed. Using the value of the gradient, the weight is

adjusted for each node. The magnitude of the change tothe network’s internal state for each learning iterationis dictated by a learning rate—a multiplicative variabledictating the rate of change in the internal state.

For each node, the delta (� ) value is computed for theoutput layer neurons:

�o =@Etotal@outo

⇥ @outo@neto

=@Etotal@outo

⇥ � 0(neto )

where neto is the weighted sum of all inputs to anode (logits).

Equation 2.8: Computing the � value for an output neu-ron.

Then, iterating through the layers backwards, all deltavalues are computed:

�c =

’h

�h ⇥ weightc,h

!⇥ � 0(netc)

where h is a cell in the next layer in the ���and c is the cell that the delta value is beingcomputed for.

Equation 2.9: Computing the � value for a hidden layerneuron.

After this, the computed delta values are used to adjust theweights in the ��� and the next activation of the modelwith the same input data will produce a lower error value.

2.2.5 Gradients

As shown above, gradients—partial derivatives—are cal-culated during the backpropagation phase to adjust theinternal state into one that more aptly can reproduce thesupervised solution to the problem—the target data. Theadjustment done to the weights is proportional to thegradient; a gradient of larger magnitude means a largeradjustment made to the associated weight in the ���scomposition.

The chain rule is applied to compute the gradient foreach node throughout the ��� (starting at an output nodeor neuron):

(f � �)0 = (f 0 � �) · �0 = f

0(�(x)) · �0(x)

Equation 2.10: The chain rule, used for computing gradi-ents in the ���.

3The sum of squared errors is one method of calculating the total error, used for the sake of communicating the theory of neural network internals. Itis not the only method viable for the purpose of calculating the total error value..

8

Page 18: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

That is; how much a weight in the ��� needs to be ad-justed is dependent on its gradient with respect to eachnode going back to the output node whose error is beingminimized—the chain rule applied in sequence to each neu-ron along the way. For deeper ���s, this is not problem-free, something that is discussed in the vanishing gradientssection below.

2.2.6 Optimization

The gradients described above can be optimized in dif-ferent ways. Not only are there several di�erent errorfunctions (also called objective functions or loss functions),but there are also several di�erent methods for optimizingweights. The weight optimization function is called thegradient descent optimization method. This optimizationmethod—not to be confused with the error or objectivefunction—is the method used for minimizing the output ofthe error function through an iterative process (training).

���������� �������� ������� (���)

Perhaps the most basic of optimization methods, theStochastic Gradient Descent is an incremental gradientdescent, �nding a local minima or maximum through aniterative process, where weights are adjusted little by little,repeatedly.

��������������� ����������� (�������)

The Root-Mean-Square Propagation is an improvementover the Stochastic Gradient Descent. Compared to the���, it introduces the concept of momentum, maintaininga more direct route towards a local minima (Tieleman andHinton, 2012).

�������� ������ ���������� (����)

Adaptive moment estimation—generally called Adam—is,in practice, a patched version of the root-mean-squarepropagation method. They are identical except for the factthat Adam uses running averages of both the gradients aswell as their magnitudes (Kingma and Ba, 2014).

2.2.7 Over�tting

If the ��� is con�gured incorrectly in regard to the prob-lem to be solved, over�tting can occur. It can be intuitivelyunderstood through visual representation (see below), butcan also be described in the following way: Over�ttingoccurs when the ��� is too complex (i.e. consists of toomany neurons, layers or a combination thereof) in re-gard to the training data volume and/or problem complex-ity: The multi-dimensional polynomial represented by the���s internal state intersects every data point in the train-ing data—but—inbetween the data points, it deviates fromthe expected or reasonable positioning. This renders the��� able to solve any problem presented in the training

data perfectly, but unable to solve any problem that it hasnot already encountered in the training data. Over�ttingcan be solved by removing neurons from the network (i.e.simplifying the ��� con�guration) or providing a greatervolume of training data, which, in turn, �lls up the spacesinbetween the previous training set data points, prevent-ing polynomial deviation.

Figure 2.5: Over�tting—the solid curve is a more sanerepresentation of the data points, compared to the dottedcurve, which has been over�tted.

Another way of countering this problem is a method calleddropout (Hinton et al., 2014). In e�ect, it does exactly whatis prescribed as a solution above; reducing the complexityof the ���. However, it does this not by recon�guring thenetwork permanently, but by randomly disabling nodesduring training. That is, at the beginning of a training iter-ation, a certain portion (called the dropout probability) ofthe neurons is disabled, and the iteration is performed as ifthe disabled neurons were not part of the model. After theiteration has completed fully, the neurons are re-enabledand another set of neurons are selected randomly againto be disabled on the next training iteration. Dropout isonly applied during training.

2.2.8 Feature Extraction

The extraction and selection of features can be very im-portant depending on the problem. Rather than feeding aneural network model raw input data—data that has notbeen touched—which might work well depending on theproblem being solved, certain computations are performedon the input data to create features. Feature extractioninvolves performing computations on the input data tocreate new features, making certain aspects of the inputdata more prominent to the neural network model as wellas reducing redundancy. This, in turn, reduces trainingtime, allowing the ��� to model the problem faster as wellas reducing over�tting (Bermingham et al., 2015).

On top of feature extraction, feature selection is per-formed: Certain features are kept while others are dis-carded from the input data. For example, when featuresare highly correlated with each other, only one of the fea-tures might be kept, reducing the dimensionality, whichalso helps reduce training time and model over�tting.

Another important aspect regarding features needs to

9

Page 19: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

be brought up; feature scaling (also called data normal-ization). Feature scaling is a method used to standardizethe range (i.e. featuremin to featuremax ) of features. Forexample, it is unwanted for one feature to have a verylarge range, and another, independent feature variable, tohave a very small range. The larger a range is comparedto other feature variable ranges, the more it will dictatethe value of the error function, causing the error functionto “focus” more on the large range features than on others,mistakingly ascribing the large range feature a greaterimportance in the optimization process, which leads to amuch slower convergence of neural network weights.

All in all, feature extraction, selection and scaling playan important role in optimizing neural network trainingfor speci�c problem solving tasks.

2.2.9 Recurrent Neural Networks

A recurrent neural network (���) is an arti�cial networkthat is “rolled out” in the sense that connections betweenneurons form a cycle. Essentially, what was understood asthe depth in the ��� is more akin to the width—the lengthof the input sequence—of the ���. While ���s can also bedeep, the architecture of (deep) ���s could be viewed asmultiple ���s stacked with lateral connections betweenlayers.

The ���, rather than merely outputting values for-ward through the layers in the network (as does the ���),output values are also fed back as inputs to previous layers,hence creating a feedback loop. This creates an internalstate in the neural network—a short-term memory allow-ing information to persist or linger—which takes into ac-count previous input values when processing newer ones.In practice, this means that a single, speci�c value cancarry di�erent meanings depending on previous inputs tothe ���, or expressed in another way; that speci�c valuecan mean something di�erent depending on where, in thesequence of values, it is encountered. As a trivial exam-ple; in natural language, the same word carries di�erentmeanings depending on where, in a sentence, it occurs.

Perhaps self-explanatory, this is why ���s work wellfor time series; each input a�ects the next iteration (essen-tially creating a memory), and so the order of the inputsbecomes important, meaning that the ��� takes entiresequences into account when modeling a problem ratherthan single inputs. It is not necessary for the data pointsin the sequence to be evenly spaced in time, although thatis the most common scenario depending on the problembeing modeled.

There are several di�erent ��� architectures, somemore known and used than others. Due to the nature of���s and the application of the chain rule for calculatinggradients, they have all been plagued by the vanishinggradients problem.

2.2.10 Vanishing Gradients

It turns out that the gradient in multi-layer neural net-works is unstable. Not only that; the stability decreasesexponentially with depth (or, in the case of ���s, alsosequence length).

As the chain rule is applied throughout the neuralnetwork—explained in the description of the backpropa-gation phase earlier—the delta values comprise a series ofmultiplications. Continuing the use of the logistic func-tion from earlier in this example, the problem of vanishinggradients is easy to understand.

As the maximum derivative of the logistic function is14 , a long series of multiplications throughout the nodesin the neural network converge to zero.

0(x) = e

x

(1 + ex )2

0(0) = 14

Equation 2.11: The derivative of the logistic function andits maximum value at x = 0.

In an ���, the depth (width or roll out in the ����) dictatesthe length of the multiplication series: In the last hiddenlayer, for example, the chain rule gives the following gra-dient computation:

�c =@Etotal@outc

⇥ @outc@netc

=@Etotal@outc

⇥ � 0(netc )

@Etotal@weightc

= �c ⇥@netc@weightc

Equation 2.12: Gradient calculation formula.

As the gradient is computed for cells farther upstream inthe neural network, the multiplication becomes longerand longer—more and more delta values are multipliedtogether, resulting in a convergence ( 14

n where n is thenumber of cells along the path from the current node tothe output node, ignoring other multiplicative factors)towards zero in the limit.

2.2.11 Long Short-Term Memory

In 1997, Hochreiter and Schmidhuber introduced the LongShort-Term-Memory (����). Building upon the traditional���, they introduced three gates that manage the internalcell state: The input, output and forget gates.

During training, these gates are used to decide howto adjust the internal state to adapt it to the informationbeing fed to the ����. For example, the �rst step is to de-cide how much of the alread-existing internal cell state tothrow away. This is managed by the forget gate. Then, the

10

Page 20: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

input gate decides which of the internal values to update,after which the internal state is adjusted and adapted tothe new information. Lastly, the output gate decides whatthe ���� will output. Although this output is based on theinternal state, it is not the entire state—rather, how muchof the internal state, and what parts of the internal state,are output, is managed by the output gate.

These gates work together to carefully protect the in-ternal state from the vanishing gradient problem—the for-get gate can decide to “turn o�” adjustments completelyfor many training iterations if they are not deemed nec-essary by the ����, which in turn reduces the problemand vastly increases the information persistence in theinternal state.

It should be noted, however, that the problem is notfundamentally solved, but rather, its e�ect is reduced tothe point of ����-based ���s beingmuchmore competentat modeling long-term temporal relationships in the inputsequences compared to traditional ���s.

2.2.12 Sequence-to-Sequence Learning

Sequence-to-sequence learning implies that the networkis being trained to produce sequences as output, from in-put sequences. Rather than producing a single output datapoint predicted to follow the input sequence, the ����produces an output sequence, predicted as the sequenceto follow the provided input sequence. For example, pro-vided with a natural language sentence of words, the en-tire following sentence (sequence of words) would be pre-dicted. This approach is also used in machine translation(Sutskever et al., 2014); an input sentence is provided, andan output sentence is predicted in the foreign language.For �nancial time series, this implies feeding the ����witha sequence of data points and training it on the sequenceof data points following the input sequence. That is, the���� is provided with (for example) �ve minutes of dataas the input sequence and the following next �ve minutesof data as the target data sequence. Sequence-to-sequencelearning, thus, involves a many-to-many mapping of thetime series data.

2.3 A���������� T������

Algorithmic trading (also known as algo-trading, auto-mated trading and black-box trading) is a way of automat-ing trading by delegating market entry and exit decisionsto an algorithm (i.e. letting an algorithm decide when tobuy, sell or short a certain stock).

2.3.1 Black-box Trading

Although an algorithm is normally thought of as beingconstructed by human reasoning with steps laid out in se-

quence, it should now be understood as amachine learningalgorithm learning to construct solutions to various prob-lems. The ��� models the problem, emulating a way ofreasoning about it. Thus, algorithmic trading ties togetherthe knowledge presented in the theory chapter, buildingupon it to create a machine learning algorithm attemptingto understand aspects of �nancial market behavior.

The term black-box trading hints at this; the intricaciesof a trained neural network cannot be understood (or, putin a simpler way, it is not known why the ��� takes cer-tain decisions, and it cannot be known easily by looking atthe internal state of the ��� without actually activating itwith some input data). In practice, the internal state of the��� represents an algorithm that produces a conclusionin regard to a presented problem.

Despite this, it is expected that the ���models marketbehavior to some extent to provide relevant output data.

Since algorithmic trading algorithms can be fully au-tomated, under the right circumstances (for example, lowdata transfer latency to the broker and a high level ofcomputational power together with an e�ective algorithm[in terms of market behavior prediction performance]),returns could potentially be generated arbitrarily quickly,though being limited by the available trading capital andmarket liquidity. This fact is backed up by real-world algo-rithmic trading algorithms generating positive net returns,although most of these models are trade secrets.

2.3.2 Non-stationarity

Machine learning algorithms, used in a trading scenarioas explained above, model the trading problem by rely-ing on recurring patterns in the input data. Due to non-stationarity, two identical �nancial time series input se-quences acquired at two di�erent points in time may havedi�erent solutions (i.e. output sequence predictions). Justas a human trader continously needs to adapt to chang-ing market conditions, an automated trading system alsoneeds to continously adapt its behavior.

This problem is a fact of complex time series andcannot be fully overcome, but can be countered to somedegree by, for example, using input time series from afairly short period of time if the neural model is not com-plex enough: The market behavior today is vastly di�erentcompared to before the stock market crash in 2008—justas human traders cannot use the same strategies today asthey used back in 2007, a machine learing trading algo-rithm trained on data from that time period would havedeveloped a strategy that does not work in today’s market.

— Lastly, with all knowledge in the theory chapter inmind, the methodology chapter and our particular experi-ments will become easily understandable, as well as issuesencountered during our research.

11

Page 21: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

3 R������ W���

As much as we were inspired by related work, we alsoencountered the problem of breaking new ground duringour research. Especially in regard to the application of the����-based ��� on �nancial markets using sequence-to-sequence learning, �nding related work turned out to besomewhat di�cult.

3.1 F�������� M����� P����������

Enormous amounts of research has beenmade into the pre-diction of �nancial markets, and we have waded througha large volume of it during the thesis work. We foundVahala (2016) particularly helpful in approaching the prob-lem of �nancial market prediction using the methods inthis thesis.

When it comes to feature extraction, we did not havethe time to test all features that we felt were applicableto the problem. A very interesting and inspiring piece ofwork in regard to feature extraction is Chan (2013).

3.2 S����������S������ L�������

The bulk volume of research on sequence-to-sequencelearning has been done on machine translation. Althoughalgorithmic trading is an entirely di�erent problem, themodel con�guration is very similar in its setup. The the-ory behind sequence-to-sequence learning with an ����model is covered by Sutskever et al. (2014), wherein thepractical application of sequence-to-sequence learning isexplained as well.

Chollet (2015)—speci�cally the Keras documentation—provides a deep insight into the intricacies of sequence-to-sequence learning and its practical applications.

Although modeling the stochastic processes on �nan-cial markets is a di�erent (and perhaps more complex)problem compared to modeling long-term weather, Zaytarand Amrani (2016) provides a deeper insight into the the-ory of the ����-based ���, detailing it further than whatwas done in this thesis, as well was providing a solid foun-dation for understanding the use of the ���� in modelingand predicting stochastic processes.

12

Page 22: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

4 M����������

Up to this point, the theory behind algorithmic trading hasbeen explained thoroughly. In this chapter, the methodsand approaches used to carry out the experiments will bedetailed.

4.1 M����� ��� ��������

According to Robson (2011), it is feasible to conduct theexperiments using a framework appropriate for investi-gating the performance of the di�erent setups and mod-els. Depending on the framework used, the setup used toevaluate the artifact is con�gured to provide quantitativemeasures of the model’s performance. Here, we have con-structed a setup for the experiments suitable for the thesisproblem, based on Design Science Research according toHevner and Chatterjee (2010), applying their thoughts onartifact evaluation. Furthermore, we try to present theexperiments and their results in a manner that providesfurther insight and knowledge to professionals in the �eld,enabling them to build upon our results.

We could also have approached the thesis problem us-ing more qualitative methods, but the established practiceis to approach this kind of problem using Design ScienceResearch (Hevner and Chatterjee, 2010), hence that is ourmethod of choice. When designing the speci�cs of theresearch experiments, we also considered Hevner’s sevenprinciples for information systems research; an artifacthas been produced (the benchmark software), the problemis extremely relevant to the trading domain (the de�nitiveobstacle in e�cient trading is price indeterminism) andour evaluative measures have been picked with care toprovide insight into actual market performance.

We could also have used the Scienti�c Method and anexperiment for evaluation, but chose not to do so becausedeveloping an artifact was part of the thesis assignment,and thus Design Science Research was appropriate.

4.1.1 General Design Cycles

More speci�cally, during the development of the artifactand writing the thesis, we used a design research cycle,re�ning the artifact many times over to produce the �nalresults presented below Vaishnavi and Kuechler (2007).This iterative approach let us work continuously on theartifact during the time of writing the thesis, convergingtoward the experiments laid out below. The cycle itself isdescribed by Vaishnavi and Kuechler (2007):

“Once a tentative design is settled on, the nextstage is actual development. This is a creativestage where the design is further re�ned andan actual artifact is produced through manyiterations. This is the only phase of GDC [Gen-eral Design Cycles] that requires a construc-tivist methodology. Once an implementation(or prototype) is ready, it is evaluated accord-ing to functional speci�cation implicit or ex-plicit in the suggestion. Empirical methods areoften used in evaluation. It is important to de-termine how well an artifact works, and re-searchers should use methods and techniquessimilar to theory testing including action re-search, controlled experiments, simulation, orscenarios. There are iterations and feedbackinvolved in these stages cited as circumscrip-tion.”

At the end of the cyclic process, a conclusion is reachedand presented as experiment results.

4.2 E������ I�����

Although much can be said about ethical issues regarding�nancial securities, �nancial markets and what aspects ofthe markets can potentially be analyzed and/or exploitedfor �nancial gain, the discussion is not part of the thesis.

4.3 A�������

There are, of course, di�erent ways to approach the the-sis problem and answer the research question. The ap-proaches we picked for setting up the experiments is notbetter or worse than other approaches in any de�nite orabsolute manner. The research question relies heavily onreal-numbered evaluation measures, narrowing down thenumber of feasible approaches to the quantitative kind.The candidate approaches and the rationale behind our�nal picks are laid out below. In the experimental setups,the neural networks are used as benchmark technology,enabling us to numerically evaluate their performanceusing statistical measures.

4.3.1 Candidate Approaches

We discussed several di�erent approaches during the ini-tial stages of our thesis research. Eventually, we came up

13

Page 23: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

with several candidate approaches, some more relevantthan others. Many were discarded quickly, while otherswere discussed thoroughly, with the more relevant onesdetailed below.

����������� ������� ����������

Applying an autoencoder to the raw input data wouldproduce an interesting “compressed” view of the data(bounded by entropy), potentially allowing us to keeponly relevant data. This is mentioned in the Future Workchapter as it is still a very interesting approach to the prob-lem. We decided against it due to constraints on time andcomputational resources.

������������ � ������� ��������

The ultimate measure of performance of predictions, build-ing a trading strategy would give de�nite answers to thequestion regarding predicted market behavior. Again lim-ited by time, this was decided against.

����������� ���������� ��������

There are many statistical evaluation measures to choosefrom. For the sake of the thesis problem—comparing the���� against the traditional ���—we found ���� and���� to be relevant. We decided to implement this ap-proach as it provides real-numbered evaluation measures,giving reproducible and testable results.

���������� ������

Although not adequate for answering the research ques-tion on their own, graphs provide an intuitive sense forthe problem and our models’ performance. For the skilledtechnical trader, this intuitive approach can be as usefulas real-valued measures. This approach was also imple-mented.

4.3.2 Selected Approaches

There are a couple of reasons for our particular choiceof approach in investigating the research question; �rsto�, it is important that real-numbered measures are pre-sented as a way of displaying de�nite and reproducibleevaluation of the models’ performance. Secondly, althoughtrading algorithms rely entirely on data, visualizing theresults can give an intuitive sense of the models’ ability topredict market behavior: Although these graphs do not an-swer any questions de�nitively, they serve the purpose ofreinforcing conclusions drawn from evaluation measures.

4.3.3 Discarded Approaches

There are many other methods for evaluating the perfor-mance with the most important aspect of the predictions

being whether they can be used for successful trading onthe Foreign Exchange Market. For example, a buy-and-hold decision based on predictions could be simulated, onwhich returns could be computed, giving a trading per-formance estimate of the predictions. Unforunately, therewas not enough time to perform such experiments.

4.4 T���������

A plethora of techonology exists to aid in performing ourparticular thesis experiments and although there are manyviable alternatives, a choice had to be made. The rationalebehind most choices made in regard to technology waspopularity (i.e. avoiding esoteric or unknown alternatives),availability, performance and relevance to the scienti�ccommunity, thus facilitating reproducibility of our experi-ments and results to a greater extent.

4.4.1 Docker

A lesson learned; the same technology sometimes worksdi�erent on di�erent computers. Thankfully, Docker en-abled us to reproduce and deploy an identical environmenton di�erent machines, making collaboration much easier.Take note!

Although not required to perform the experiments,Docker made it so much easier to collaborate using di�er-ent hardware con�gurations, allowing everyone involvedto reproduce the setup on their machine.

4.4.2 Python

We used Python 3 (speci�cally version 3.5.2+) as the pro-gramming language of choice as it is ubiquitously usedby the scienti�c computing community and academics allover the world. Documentation is rich and there is a vastamount of tools and third-party libraries available.

4.4.3 Keras and TensorFlow

Keras—a high-level deep learning library (Chollet, 2015)—was used to implemented the machine learning algorithmmodels. It is ideal as it provides simple setup and executionof machine learning algorithms, making it almost trivialto perform practical research within the �eld.

A TensorFlow (Abadi et al., 2015) backend was usedin conjunction with Keras, enabling us to easily performcomputations on the computer’s GPU and increasing com-putational performance many times over.

Much happened during the time of writing the thesisin regard to the development of both Keras and Tensor-Flow. Despite both libraries providing rich documentation,they both changed to such a degree during the writing ofthe thesis that we had to rework our source code manytimes over. During the �rst weeks of writing the thesis,

1PyPI is the Python Package Index, a repository of software for the Python programming language that can be installed via the pip command.

14

Page 24: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

TensorFlow was not available on PyPI1 and so we had tocompile it manually. As of �nishing this thesis, both Kerasand TensorFlow can be installed easily via the Pythonpackage manager.

4.4.4 Other Third-party Libraries

For computational e�ciency, pandas and numpy were ourgo-to solutions, providing native code computational ef-�ciency in Python. Graphs and visualizations were donewith matplotlib. Keras uses h5py for saving and loadingmodels in the ���5 �le format.

4.5 D���

Although there are many di�erent �nancial assets and�nancial markets with various degreens of liquidity etc,we needed freely available high-frequency data to per-form intraday trading experiments. This fact limited ourchoices of data sources, so many were ruled out early inthe process.

4.5.1 Selection

The requirement on our part of high-frequency data tocarry out the experiments led us to the ����� market, forwhich freely available market data can be acquired. Weobtained the data from Dukascopy2 by opening a trad-ing account and downloading bulk, high-frequency data.The currency exchange between the Euro (€) and the U.S.dollar ($) was our choice of data for model training as itconstitutes a highly liquid �nancial market.

Beyond the selection of a data source, we had to limitthe amount of data to put through the neural model, beinglimited by computational resources. We initially decidedto build a model for prediction of market behavior bylooking at the past three months of �nancial data (Novem-ber through January), but later decided on using the twoweeks of trading data prior to the trading day to performpredictions on.

The acquired data contained tick data, i.e. entries foreach pip change, thus providing us with asks and bids,market volume and timestamps. We did not have accessto the market depth—the number of bids and asks at eachprice level.

4.5.2 Preprocessing

During preprocessing, irrelevant parts are removed, miss-ing values are �lled in3, normalization and regularizationis done and so on, essentially compiling the data to a setuseful for computing features on and using for modeltraining.

All parameters relevant to training of our networkmodel—considering our technical analysis approach—were already part of the acquired high-frequency data.

The pip data was aggregated into one-minute candle-sticks, containing ���� prices, with missing candlesticksforwared-�lled from the previously existing candlestickin order to avoid look-ahead bias.

The magnitude of price changes on the ����� marketare small—which poses a problem for the neural networkmodel as explained in the theory chapter—but it couldnot be normalized over an entire data set as that wouldviolate the rule of not looking into the future, since, in alive trading scenario, the model would only have accessto already-seen prices, which would make it impossibleto normalize the price with future prices in mind. Instead,this was solved during feature extraction through carefulchoice of features.

4.6 E���������

The models were evaluated in several ways. Partly, themodels’ predictions were visualized to provide an intu-itive overview of their performance. To provide a real-numbered estimate of their performance, measurementswere computed on their predictions.

4.6.1 Visualization

Although merely giving an intuitive sense of the models’performance, visualizing the results still provides an im-portant insight into to the performance of the models. Thegraphs are interesting to look at in comparison to eachother; it is evident that the ���� model is superior to thetraditional ���, displaying a higher ability in predictingmarket behavior (see Appendix A, B, C and D).

Furthermore, these graphs were used to con�rm theintegrity of the models—that they did what they weresupposed to do.

4.6.2 Evaluation Measures

A more important aspect of the model evaluation processis the quanti�cation of the models’ performance, usingreal-valued measurements. Using the mean absolute per-centage error (����) and root-mean-square error (����),the models were evaluated and compared against eachother. The models were evaluated at several points in timeto avoid serendipitous results.

���� �������� ���������� ����� (����)

The Mean Absolute Percentage Error is a measure of pre-diction accuracy of a forecasting method. It is used to

2Website: https://www.dukascopy.com/3Depending on the problem being solved, missing values can be ignored, interpolated or forward-�lled. In our case, forward-�lling was the only

viable alternative; using previous values and copying them forward in time to �ll the holes in order to avoid look-ahead bias.

15

Page 25: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

compute the accuracy of a trend estimation as a percent-age.

M =100n

n’t=1

����At � FtAt

����where At is the actual value and Ft is the forecast value

Equation 4.1: The mean absolute percentage error for-mula.

��������������� ����� (����)

The Root-Mean-Square Error is a measure of di�erencebetween actual values and values predicted by a model. Itrepresents a standard deviation of the di�erences betweenactual predicted values.

RMSE(�̂ ) =qMSE(�̂ )

whereMSE is the mean square error of the predictedvalues.

Equation 4.2: The root-mean-square error formula.

16

Page 26: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

5 E����������

The experiments were carried out many times over. Partlybecause the model was being tweaked to provide betterpredictions, partly because new features were being testedand partly because discussion and literature studies dur-ing the thesis research work pointed us in new directions.Regardless, presented below are our �nal approaches tothe problems and their setups.

5.1 M�����

The choice of neural network types were decided from thestart—the ����-based ��� and the traditional ���–butthe network architecture (layers, cells, dropout probabilityetc) was tweaked and adjusted many times over. We beganour work by reading literature, building upon previous re-searchers experience, eventually coming up with a modelsetup that seemed adequate for our kind of approach. Be-yond that, we intuitively added and removed layers andcells in each layer, trying to understand the setup better.

5.2 M���� C�������������

Our �nal setup for the ���� was a model with six layers;an input layer with 2048 cells, a dropout layer with 20%dropout probability, a hidden layer with 1024 cells, an-other dropout layer with 20% dropout probability, anotherhidden layer with 512 cells and a �nal layer with the samedropout probability again, ending with a time-distributeddense output layer with linear activation functions.

For the ���, the �nal con�guration was again six lay-ers; an input layer with 1024 cells, a dropout layer with20% dropout probability, a hidden layer with 512 cells, an-other dropout layer with 20% dropout probability, anotherhidden layer with 256 cells and a �nal layer with the samedropout probability again, ending with a time-distributeddense output layer with linear activation functions, thushaving the same con�guration as the ����.

5.3 S������W�����

The models were setup to take input sequences of ten datapoints and to predict the ten minutes following the inputsequence, thus con�gured as a many-to-many (10-to-10)neural prediction model. We used a sliding window witha width of ten data points—representing ten minutes ofdata—and moved it forward one data point (one minute)for each training iteration.

5.4 F�������

Using high-frequency data, all features used were basedon a technical analysis approach, thoroughly discussedin the theory chapter. Although several di�erent featurecon�gurations were attempted, we eventually settled onthe ones detailed below. All were normalized by using anonline normalization function, Equation 5.1

norm(x , t) = xt�xt�1xt�1

Equation 5.1: The normalization function used.

�������

F1(t) = norm(Cbid , t)

Equation 5.2: The �rst extracted feature—close bid re-turns.

Where Cbid is the close bid of an ���� data point in thesequence at time t .

������

F2(t) = norm(SMA

5(Vbid ), t)

Equation 5.3: The second extracted feature—a �ve-pointperiod simple moving average of the market bid volume.

Where Vbid is the bid volume.

�������� �������� �����

F3(t) = norm(RSI10(Cbid ), t)

Equation 5.4: The third extracted feature—a ten-point rel-ative strength index computed on the close bid.

������

F4(t) = norm(Spread, t)

Equation 5.5: The fourth extracted feature—the marketprice spread.

The features were then scaled by hand to provide a stan-dardized range across the independent variables; each fea-ture was scaled independently on a few training datasetsto produce values of roughly equal range, as well as to

17

Page 27: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

keep values within the range of �1 and 1. This was doneon training data to prevent introducing future data into themodel, thus, the normalization might not produce valuesof equal range or within the limits for test data to the samedegree. It proved better than not performing this “manual”normalization at all, without which weights would havebecome skewed in the model.

5.5 M���� T�������

Each model was trained for a set amount of iterations;10,000. It should be noted that the ���� is more computa-tionally intensive and thus required more time to reachthe same number of iterations.

Training was done on the fourteen consecutive tradingdays prior to the prediction day. Predictions were limitedto February 9th, 2017 to avoid encountering many di�er-ent marking regimes during the training data time period,

potentially simplifying generalization of market behaviorand thereby potentially increasing model performance.

5.6 H�������

Training was done for many di�erent setups on severaldi�erent machines during our research. For the sake ofcompleteness, the hardware speci�cation of the machineused to train our �nal models used in the experiments ispresented in Table 5.1 below.

Motherboard MSI Z97–5Memory Hyper-X Savage 16GBGraphics card MSI Nvidia Geforce GTX 760 2GBProcessor Intel i5–4690KStorage Samsung 840 EVO 250GB

Table 5.1: The hardware confguration of the system used formodel training.

18

Page 28: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

6 R������

Below, our results are presented. See the appendix for graphed visualizations of the predictions. All predictions aremade on ����� market data for the ���/��� currency pair from February 9th, 2017—the day following the fourteenconsecutive trading days of data that the models were trained on.

6.1 M�����������

06.00–07.00 07.00–08.00���� ��� ���� ���

MAPE 51.0738% 135.0695% 253.9935% 237.0991%RMSE 0.0261 0.2096 0.0455 0.0981

08.00–09.00 09.00–10.00���� ��� ���� ���

MAPE 147.7115% 154.5905% 131.6576% 120.2149%RMSE 0.0592 0.1735 0.1857 0.1843

10.00–11.00 11.00–12.00���� ��� ���� ���

MAPE 109.234 % 377.7257% 250.9250% 348.9639%RMSE 0.3678 0.1159 0.1083 0.1265

12.00–13.00 13.00–14.00���� ��� ���� ���

MAPE 152.8492% 217.5117% 170.5649% 225.3889%RMSE 0.1166 0.1689 0.1001 0.1468

14.00–15.00 15.00–16.00���� ��� ���� ���

MAPE 159.9678% 168.4376% 166.1331% 255.8209%RMSE 0.1472 0.1836 0.2515 0.2559

Table 6.1: Measurement results over a day of trading. The better result is denoted with bold numbers.

19

Page 29: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

7 D���������

Looking at the graphs (see Appendix A, B, C and D), al-ready is it evident that the application of our ����-based��� using sequence-to-sequence learning is vastly supe-rior to the traditional ��� model. This is also con�rmedby looking at the evaluation measurements (Table 6.1) per-formed on the predictions: The ���� outperforms the ���on most evaluation measures, proving to be more muchmore apt at modeling market behavior compared to thetraditional ���.

7.1 F�������� M����� P���������

Reaching back to the original research question; using theset of features selected for the experiments setup in thisthesis, the ����-based ��� was unable to model marketbehavior to such a degree that a de�nitive call could bemade as to whether the model generalized for the marketregime encountered during training or not.

Many attempts have been made at predicting �nancialmarket behavior by modeling price returns, with somedisplaying a certain level of success. In any case, a largerdata set could potentially provide more information, al-lowing the ���� generalize better for market behavior.That, however, would also require a much larger model(i.e. a deeper model con�guration with many more cells ineach layer) which, in turn, would place further demandson computational capacity and time, which, in the end,put a limit on our ability to pursue the problem further.The more complex the model, and the larger the dataset,the worse the ��� performs in comparison to the ����.

7.2 V�������������

Although the predictions are visualized (see Appendix A,B, C and D) in the form of graphs, there are interestingthings to note. Below, we reason about the behavior of themodels’ ability in predicting market behavior.

The vanishing gradient problem becomes somewhatevident in the graphs (see Appendix A, B, C and D). The

��� is unable to produce much more than a simple recur-ring pattern.

�������� �

The predictions produced by the ���� (solid line) are in-tuitively accurate and could possibly have been used fortrading. The large fall is not predicted by themodel, but thepredicted price movements still follow the market trendwell up to that point.

Looking at the ��� (dashed line) predictions, there isno evident generalization of market behavior.

�������� �

Again, we see the general market trend predicted to anextent by the ����, giving an interesting result.

Looking at the ��� predictions again, we �nd no evi-dence of the market behavior being modeled to any usefulextent.

�������� �

Although not giving accurate predictions, the ���� seemsto have modeled the double top chart pattern: After twotops and a price fall breaking the bottom of the trend chan-nel; the model predicts a continuation of the price drop, aswould likely be expected by a human trader performingtechnical trading analysis on the chart.

The ��� produces no useful predictions.

�������� �

Here, we see the ���� model the general market trendover the prediction period and onwards, but missing animportant opportunity for pro�table returns; the largeprice movement during the prediction period. The ����did not model the market behavior well enough.

The ��� produces no useful predictions.

20

Page 30: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

8 C���������

Looking at the results, it is obvious that the ����-based��� performed much better than the traditional ���. Weconclude that, given the same time series data, on thepremises in the experiments, the ���� model is muchmore competent at generalizing the time series problems.The ��� is greatly improved upon by the ���� thanks toits handling of the vanishing gradient problem.

The ���� displayed some ability in modeling marketbehavior (see chapter Discussion) while the ��� couldnot handle even the relatively small amount of input dataused in the experiments. For complex problems requiringdeep and/or wide neural network architectures, the ���performs worse compared to the ����. However, given asimple enough input problem, the ��� proves to be a vi-able alternative under certain circumstances given the factthat it places a lower demand on computational resources,potentially modeling the problem faster (measured in real-world time) compared to the ����. This, however, onlyapplies to problems of trivial di�culty relative to the com-plex problems that the ���� is able to model.

The predictions in the graphs are interesting, and al-though some promise of modeling market behavior isdisplayed, it is likely that the model is encountering a pat-tern similar to one seen during the training data. A largermodel with more input data would likely have modeledthe behavior of the market to a greater extent, producingeven better predictions of price movements.

In retrospect—now that all experiments have been per-formed and their results compiled—our �rm belief is thatq-learning is more suited to the type of problem posed inthis thesis (Gabrielsson and Johansson, 2015). Althoughwe have neither proved nor disproved the random walkhypothesis (Fama, 1995), the statistical distribution of pricemarket movements are (a) too complex to model usingthe small-scale ����models in our setup, (b) require largeamounts of data and (c) consume—given appropriate con-�guration with respect to layers and number of cells aswell as training data volume—enormous computationalresources.

21

Page 31: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

9 F����� W���

Limited, as we all are, by time, there was no opportunityto test all ideas that we had. It would be interesting tocontinue working with this, approaching di�erent aspectsof the problem in new ways. Below are some ideas thatwe had during the time of writing this thesis.

9.1 T������ P���������� E��������

It would be interesting to performance more in-depth ex-periments on the predictions such as building a tradingstrategy upon the models’ predictions and evaluating trad-ing performance.

9.2 Q��������� A�������

Although some interesting results were attained with re-spect to market behavior predictions, it would be evenmore interesting to approach the problem with q-learning.Q-learning is not about predicting future values (i.e. re-turns etc.) but rather about predicting appropriate behav-ior. In regard to the thesis problem, this implies predictingappropriate market actions or operations; predicting whento buy or sell a �nancial asset. Although the problem hasbeen approached with q-learning before (Gabrielsson andJohansson, 2015), we were unable to �nd any publicationsthat have approached it with a deep learning model thatsolves the vanishing/exploding gradient problem, i.e. the����-based ��� used in this thesis. Combining q-learningwith a ����-based ��� would be very interesting indeed.

9.3 E�������� L����� M�����

Due to time constraints, training was limited to relativelysmall datasets andmodels. A largermodel, although requir-ing signi�cantly more training time and computationalresources, might be able to provide more accurate pre-dictions than the model presented in the results in thisthesis.

9.4 T����� D�������� F�������

The market data contains all relevant data for a strictlytechnical analysis, but feature extraction and selectionmakes relevant data aspects more prominent. Ceterisparibus, �nding a better set of features could producemore accurate predictions.

Although not necessarily well-suited for regression,it would be interesting to see the results of training amodel on the seven features selected by Gabrielsson andJohansson (2015).

9.5 A���������� F������ E���������

Instead of manual feature extraction, backtesting on alarge amount of data, one could hypothetically build anautoencoder to compress the data, limiting the autoen-coder to fewer and fewer cells and layers until a minimalworking con�guration has been found, and then use it forfeature extraction and using the output as input for the���� model. It would be very interesting to see whetherthe ���� could model market behavior using the autoen-coder output as features.

22

Page 32: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

B�����������

Abadi, M., Agarwal, A., Barham, P., Brevdo, E., Chen, Z.,Citro, C., Corrado, G. S., Davis, A., Dean, J., Devin, M.,Ghemawat, S., Goodfellow, I., Harp, A., Irving, G., Isard,M., Jia, Y., Jozefowicz, R., Kaiser, L., Kudlur, M., Leven-berg, J., Mané, D., Monga, R., Moore, S., Murray, D., Olah,C., Schuster, M., Shlens, J., Steiner, B., Sutskever, I., Tal-war, K., Tucker, P., Vanhoucke, V., Vasudevan, V., Viégas,F., Vinyals, O., Warden, P., Wattenberg, M., Wicke, M.,Yu, Y., and Zheng, X. (2015). TensorFlow: Large-scalemachine learning on heterogeneous systems. Softwareavailable from tensor�ow.org.

Bermingham, M. L., Pong-Wong, R., Spiliopoulou, A., Hay-ward, C., Rudan, I., Campbell, H., Wright, A. F., Wilson,J. F., Agakov, F., Navarro, P., et al. (2015). Applicationof high-dimensional feature selection: evaluation forgenomic prediction in man. Scienti�c reports, 5.

Chan, E. (2013). Algorithmic Trading: Winning Strategiesand Their Rationale. Wiley, 1 edition.

Chollet, F. (2015). Keras. https://github.com/fchollet/keras.

Connors, L. and Alvarez, C. (2008). Short Term TradingStrategies That work.

Devakunchari, R. (2014). Analysis on big data over theyears. International Journal of Scienti�c and ResearchPublications, 4(1).

Fama, E. F. (1995). Random walks in stock market prices.Financial analysts journal, 51(1):75–80.

Gabrielsson, P. and Johansson, U. (2015). High-frequencyequity index futures trading using recurrent reinforce-ment learning with candlesticks.

Goodfellow, I., Bengio, Y., and Courville,A. (2016). Deep Learning. MIT Press.http://www.deeplearningbook.org.

Hevner, A. and Chatterjee, S. (2010). Design Research inInformation Systems: Theory and Practicee. Springer.

Hinton, G., Srivastava, N., Krizhevsky, A., Sutskever, I.,and Salakhutdinov, R. (2014). Dropout: A simple wayto prevent neural networks from over�tting. Journal ofMachine Learning Research, (15):1929–1958.

Hochreiter, S. and Schmidhuber, J. (1997). Long short–termmemory. Neural Computation, 9(8):1733–1780.

Horsley, S. (1772). The sieve of eratosthenes. being anaccount of his method of �nding all the prime num-bers, by the rev. samuel horsley, f. r. s. PhilosophicalTransactions, 62(1775):327–347.

Kaya, O. (2016). High-frequency trading—reaching thelimits.

Kingma, D. and Ba, J. (2014). Adam: A method for stochas-tic optimization. arXiv preprint arXiv:1412.6980.

Kirkpatrick, C. D. and Dahlquist, J. R. (2010). TechnicalAnalysis: The Complete Resource for Financial MarketTechnicians. Financial Time Press, 2 edition.

Kita, E., Zuo, Y., Harada, M., and Mizuno, T. (2012). Appli-cation of bayesian network to stock price prediction.

Nison, S. (2001). Japanese Candlestick Charting Techniques.Prentice Hall Press, 2 edition.

Pascanu, R., Mikolov, T., and Bengio, Y. (2012). On thedi�culty of training recurrent neural networks.

Robson, C. (2011). Real world research: a resources for usersof social research methods in applied settings. Wiley, 3edition.

Rosenblatt, F. (1958). The perceptron: A probabilisticmodel for information storage and organization in thebrain. Psychological Review, 65(6):386–408.

Rumelhart, D. E., Hinton, G. E., and Williams, R. J. (1986).Learning representations by back-propagating errors.Nature, 323:533–536.

Saad, E. W., Prokhorov, D. V., and II, D. C. W. (1998). Com-parative study of stock trend prediction using time delay,recurrent and probabilistic neural networks.

Sharpe, W. F. (1966). Mutual fund performance. The Jour-nal of business, 39(1):119–138.

Sutskever, I., Vinyals, O., and Le, Q. V. (2014). Sequence tosequence learning with neural networks. InAdvances inneural information processing systems, pages 3104–3112.

Tieleman, T. and Hinton, G. (2012). Lecture 6.5-rmsprop:Divide the gradient by a running average of its recentmagnitude. COURSERA: Neural networks for machinelearning, 4(2).

Tunnicli�e-Wilson, G. (1989). Non-linear and non-stationary time series analysis. Journal of Time SeriesAnalysis, 10(4):385–386.

23

Page 33: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

Vahala, J. (2016). Prediction of �nancial markets usingdeep learning.

Vaishnavi, V. K. and Kuechler, W. (2007). Design ScienceResearch methods and Patterns: Innovating Informationand Communication Technology. Auerbach Publications,1 edition.

Wilder, J. W. (1978). New Concepts in Technical TradingSystems. Trend Research, 1st edition.

Zaytar, M. A. and Amrani, C. E. (2016). Sequence to se-quence weather forecasting with long. InternationalJournal of Computer Applications, 143(11).

Zhang, Y. (2004). Prediction of �nancial time series withhidden markov models.

24

Page 34: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

A������� A

25

Page 35: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

A������� B

26

Page 36: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

A������� C

27

Page 37: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

A������� D

28

Page 38: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

This page intentionally left blank.

Page 39: Examensarbete Systemarkitekturutbildningen Philip ...hb.diva-portal.org/smash/get/diva2:1142444/FULLTEXT01.pdf · box trading, lstm, rnn, time series forecasting , prediction, tensorflow,

Högskolan i Borås är en modern högskola mitt i city. Vi bedriver utbildningar inom ekonomi och informatik, biblioteks- och informationsvetenskap, mode och textil, beteendevetenskap och lärarutbildning, teknik samt vårdvetenskap. På institutionen Handels- och IT-högskolan (HIT) har vi tagit fasta på studenternas framtida behov. Därför har vi skapat utbildningar där anställningsbarhet är ett nyckelord. Ämnesintegration, helhet och sammanhang är andra viktiga begrepp. På institutionen råder en närhet, såväl mellan studenter och lärare som mellan företag och utbildning. Våra ekonomiutbildningar ger studenterna möjlighet att lära sig mer om olika företag och förvaltningar och hur styrning och organisering av dessa verksamheter sker. De får även lära sig om samhällsutveckling och om organisationers anpassning till omvärlden. De får möjlighet att förbättra sin förmåga att analysera, utveckla och styra verksamheter, oavsett om de vill ägna sig åt revision, administration eller marknadsföring. Bland våra IT-utbildningar finns alltid något för dem som vill designa framtidens IT-baserade kommunikationslösningar, som vill analysera behov av och krav på organisationers information för att designa deras innehållsstrukturer, bedriva integrerad IT- och affärsutveckling, utveckla sin förmåga att analysera och designa verksamheter eller inrikta sig mot programmering och utveckling för god IT-användning i företag och organisationer. Forskningsverksamheten vid institutionen är såväl professions- som design- och utvecklingsinriktad. Den övergripande forskningsprofilen för institutionen är handels- och tjänsteutveckling i vilken kunskaper och kompetenser inom såväl informatik som företagsekonomi utgör viktiga grundstenar. Forskningen är välrenommerad och fokuserar på inriktningarna affärsdesign och Co-design. Forskningen är också professionsorienterad, vilket bland annat tar sig uttryck i att forskningen i många fall bedrivs på aktionsforskningsbaserade grunder med företag och offentliga organisationer på lokal, nationell och internationell arena. Forskningens design och professionsinriktning manifesteras också i InnovationLab, som är institutionens och Högskolans enhet för forskningsstödjande systemutveckling.

BESÖKSADRESS: JÄRNVÄGSGATAN 5 · POSTADRESS: ALLÉGATAN 1, 501 90 BORÅS TFN: 033-435 40 00 · E-POST: [email protected] · WEBB: WWW.HB.SE/HIT


Recommended