Master of Science (M.Sc.) in Software Engineering
Total Page:16
File Type:pdf, Size:1020Kb
Master of Science (M.Sc.) in Software Engineering Twitter sentiments for high frequency trading Viktor Víðisson Instructor: Jacqueline Clare Mallett June 2021 Copyright Viktor Víðisson June 2021 Abstract Twitter has become an influential source for news which can have impact on market behaviour, with many users monitoring news agency feeds directly without editorial reviews. Headlines from central bank leaders as well as top government officials can have significant impact on the markets. In this thesis we examined whether there is any correlation between a group of tweets quoting the same headline and measurable differences in the market before and after a given headline. Can this help high frequency trading firms to tune their strategies by filtering important headlines from the vast ocean of headlines by decreasing exposure when market making? In this thesis tweets were grouped by the number of references to the headline limited to 1 hour. We compared ratios of trading measurements before and after the headline came out. We trained a Recurrent Neural Network (RNN) using Long Short Term Memory (LSTM) cells telling us if the sentiment in the headline affects the US treasury futures or the SP 500 stock market future. We found that training initially on datasets that started trending after causing a noticeable initial reaction in the market helped the neural network to classify tweets as impact-full or not. Training a network that can take in a live feed from Twitter and other news agencies sources can help high frequency trading firms react automatically to incoming headlines. 2 Útdráttur Twitter er byrjað að vera áhrifamikil upptök frétta sem geta haft áhrif á markaðs hegðun þar sem margir notendur fylgjast með frétta veitum og tweeta beint án ritstýringar. Fyrirsagnir frá stjórnendum seðlabanka sem og háttsettra stjórnmála fólks getur haft mikil áhrif á markaðinn. Í þessari ritgerð skoðum við fylgni þess að hópur fólks tweetar um fyrirsögn og mælan- legrar breytingar í hegðun á markaðnum fyrir og eftir fyrirsögnina. Getur þetta hjálpað hátíðni viðskipta fyrirtækjum að stylla algrímin sín sjálfvirkt með því að sigta út mikilvægar fyrirsagnir úr stóra hafinu af fréttum með því að minnka stærðir hjá viðskiptavökum? Í þessari ritgerð þá voru tweets flokkuð eftir hversu mikla umfjöllun þau fengu næsta klukkutímann. Við bárum saman hlutföll á markaðs mælingum fyrir og eftir fyrirsögnina. Við þjálfuðum enturtekið tauganet "Recurrent neural network (RNN)" og no- tuðum "Long short term memory cells (LSTM)" til að segja okkur hvort að fyrirsagnir munu hafa áhrif á Bandarísku skuldabréfa framtíðar samningana eða "SP 500" hlutabréfa vísitölu framtíðar samninginn. Við komumst að því að þjálfa tauganetið upphaflega á gagnasafni af tweetum sem fengu mikla um- fjöllun og höfðu sjáanleg áhrif á markaðinn, hjálpaði tauganetinu að ákveða hvort að ný tweets hefðu áhrif á markaðinn eða ekki. Að þjálfa tauganet sem getur tekið við beinu streymi af tweetum og öðrum fréttaveitum getur hjálpað hátíðni viðskipta fyrirtækjum að bregðast sjálfkrara við nýjum fyrirsögnum. 3 Twitter sentiments for high frequency trading Master of Science (M.Sc.) in Software Engineering Viktor Víðisson June 2021 Thesis of 60 ECTS credits submitted to the School of Science and Engineering at Reykjavík University in partial fulfillment of the requirements for the degree of Student: Viktor Víðisson Examining Committee: Jacqueline Clare Mallett Examining Committee: Kristinn R Þórisson Examining Committee: Stephan Schiffel Examining Committee: Dean Christakos 4 Contents List of Figures 8 List of Tables 10 1 Introduction 14 1.1 Introduction . 14 1.2 Related work . 16 2 Background 19 2.1 Limit order book . 19 2.1.1 Limit order book tutorial . 20 2.2 Sweep info and Stamina info . 26 2.2.1 Sweep info . 26 2.2.2 Stamina info . 28 2.3 Liquidity . 30 2.4 Zoom plot explanation . 31 2.4.1 Zoom plot tutorial . 31 2.4.2 Matching algorithms . 33 2.5 High Frequency Trading . 35 2.6 Market Making . 36 2.7 Market reactions . 38 3 Methods 42 3.1 Experimental design data preparation . 42 3.2 Code Flow . 45 3.3 Preparing tweets . 45 3.3.1 alike_next_hour . 48 3.4 Market impact analysis . 50 3.5 TA library . 52 3.6 Create datasets . 53 5 3.7 Dataset impact analysis . 55 3.8 Data augment the input data . 56 3.9 Prepare the data for the ANN . 57 3.10 Define the ANN model . 60 3.11 Training the ANN model . 63 3.12 Optimizers for the ANNs . 65 3.13 Regularization for the ANNs . 67 3.14 Training schedule . 69 3.15 Evaluation metrics . 70 4 Results 74 4.1 Dataset impact analysis results . 74 4.2 Evaluation metrics results . 76 5 Conclusion 80 5.1 Future work . 80 References 83 6 Appendix 87 6.1 Highest retweets . 87 6.2 Best results per optimizer . 91 6.3 Best results per evaluation metric . 96 6.4 Code . 101 6.4.1 Sort twitter . 101 6.4.2 Alike next hour . 104 6.4.3 Get product . 108 6.4.4 Prepare market data . 111 6.4.5 Tweets to train base . 113 6.4.6 Technical analysis, TA library . 120 6.4.7 Group sweep . 133 6.4.8 Classification metrics . 135 6 6.4.9 Regularize XY . 137 6.4.10 Class weights Output biases . 139 6.4.11 Get sentences . 140 6.4.12 Plot history . 141 6.4.13 Split XY . 142 6.4.14 Define model . 142 6.4.15 Fit model . 145 6.4.16 Grid searching configurations . 146 6.4.17 Predict sample . 150 7 List of Figures 1 Empty limit order book . 21 2 One open order in the limit order book on the bid price 9 . 21 3 Two open orders in the wide limit order book . 22 4 The book is one tick wide now . 22 5 Liquid limit order book . 23 6 An order crossing the book, 1st trade for 100 lots at price 10 . 23 7 A 1000 lot order crossed the book on the bid, matching the 200 lots resting on the bid and establishing the remaining 800 lots on the new best offer price 9 . 24 8 2500 resting orders on the bid price 8 get matched in two different sweep events, a) being a single trade report, b) being multiple trade reports . 25 9 Zoom plot demo part 1 . 33 10 Zoom plot demo part 2 . 33 11 LIFFE’s matching algorithm for Short Sterling . 35 12 James Bullard the president of the Federal Reserve bank of St. Luis talks about a 50 basis point rate cut in an interview . 38 13 John Williams the president of the Federal Reserve bank of New York gave an interview the day before. The following day the bank made a statement that his interview got misinterpreted 39 14 FOMC president Jeromy Powell gives his opinion about shift- ing trend in the Nonfarm payroll number . 40 15 The trade meeting between Trump and Xi got postponed . 40 16 White house looking into the possibility of firing the FOMC president Jerome Powell . 41 17 Trump announced he was going to delay increasing tariffs on Chinese imports . 41 18 All correlation cases number of consecutive cases . 47 8 19 Zoomed in on correlation cases number of consecutive cases . 47 20 Cases where a tweet had y-axis many tweets in the following hour with a minimum correlation of 0.5 to it. 49 21 Cases where a tweet had y-axis many tweets in the following hour with a minimum correlation of 0.5 to it. 49 22 FOMC members and White house advisors . 53 23 The keras model when we use bidirectional LSTM layers and Batch normalization . 62 24 Dataset impact analysis results . 76 25 Best results by evaluation metric . 77 26 Results by optimizer plotted against ANNs . 78 9 List of Tables 1 Sweep info, rows of data for each contract . 27 2 Stamina info, rows of data for each contract . 29 3 Spread contracts, rows of data for each contract. Notice the difference in amount of sweep info rows and stamina info rows 30 4 Sweep info demo . 32 5 Stamina info demo . 32 7 Adam settings and results . 92 8 Adamax settings and results . 93 9 Nadam settings and results . 94 10 RMSprop settings and results . 95 11 Highest precision settings . 97 12 Highest recall settings . 97 13 Highest AUC settings . 98 14 Highest f score settings . 98 15 Highest MCC settings . 99 16 Highest Youden settings . 99 17 Highest C kappa settings . 100 10 List of Algorithms 1 Codeflow.............................. 45 2 Determining which future contract to use for market impact analysis of a tweet . 50 3 High level view of the market impact analysis . 51 4 Group sweep . 53 5 Creating datasets . 54 6 Dataset impact analysis . 55 7 Tokenizer . 58 8 Embedding matrix . 58 9 Define model . 60 10 Predict model . 64 11 Grid searching NN model settings . 68 12 The other binary settings in the grid search . 68 13 Get best classification results and model settings . 69 14 Training schedule . 70 11 List of Abbreviations ANN Artificial Neural Network. 16, 78 AUC Area under the ROC curve. 70 CME Chicago Mercantile Exchange. 26 CNN Convolution Neural Network. 17 DIJA Down Jones Industrial Average. 16, 17 EST Eastern Standard Time. 42 FED Federal Reserve. 15 FIFO First In First Out. 33 FN False Negative. 70 FOMC Federal Open Market Committee. 15 FP False Positive. 71 HFT High Frequency Trading..