key: cord-0336188-ic1zepit authors: Zheng, Yiyang title: Neural Network and Order Flow, Technical Analysis: Predicting short-term direction of futures contract date: 2022-02-23 journal: nan DOI: 10.36227/techrxiv.19154276.v1 sha: 5fd7d9884bd57e24769d6d71d5c5a116f6c9fb27 doc_id: 336188 cord_uid: ic1zepit Predictions of short-term directional movement of the futures contract can be challenging as its pricing is often based on multiple complex dynamic conditions. This work presents a method for predicting the short-term directional movement of an underlying futures contract. We engineered a set of features from technical analysis, order flow, and order-book data. Then, Tabnet, a deep learning neural network, is trained using these features. We train our model on the Silver Futures Contract listed on Shanghai Futures Exchange and achieve an accuracy of 0.601 on predicting the directional change during the selected period. Like the equity market, the derivatives market is often dynamic and volatile. Predicting the trend of a futures contract can be a challenging task since its pricing is often based on multiple complex dynamic conditions. Even though some critics claiming [1] it is impossible to predict the financial market efficiently, researchers have demonstrated that complex algorithms may create predictive models that can out-perform random guessing consistently. Numerous methods have been proposed in this field. The more traditional method, including using time-series models like autoregressive integrated moving average (ARIMA), has been proposed in papers [2] [3] . Those methods tend to over-fit on past data and yield a poor result on future unseen data. Nabipour et al. [4] used various technical indicators to model the asset's return. In recent years, neural networks have been increasingly popular. Moghaddam et al. [5] comparatively study the use of Multilayer Perceptron (MLP) and Recurrent Neural Networks ( RNN ) on some unique features. Some alternative methods include using news reports and social media are also suggested. Li et al. suggest using news sentiment to model the future movements of stocks [6] , while [7] [8] [9] suggesting using sentiment analysis and language processing to generate predictive features. In this work, we present a method to predict the short-term directional movement of the futures contract. Our method involves a model that utilizes both technical analysis features and order-flow features that are generally applicable for both equities and derivatives and other features specifically designed for the futures contract. The main contributions of our work are: (1) We generate features from the order book and from times and trades. (2) We ensemble conventional technical analysis features and those generated from high-frequency book depth data ; (3)We apply filters to detect institutional traders from retail traders (4) The modeling of our features involved Purged Group Time Series Split in contrast to traditional cross-validation to prevent target leakages and over-fitting (5)as a result, the accuracy of our model is not state-of-the-art, but the prediction accuracy is relatively consistent across timespan regardless of market condition, which is overlooked in numerous of other works. (6) We can thus produce a high return on selected timespan with a basic trading strategy. The remainder of this paper is structured as follows: In the next section, we overview the problem and target construction. Then in section 3,4, we provide a method to generate features from the raw exchange data feed. Section 4 describes the design detail and section 5 focuses on training the model. In section 6, we provide a simple trading strategy and provide detailed performance statics of our model. Finally, in section 7, we conclude the research and identify a few possible directions for future research. We propose a general-applicable method to capture the trend of futures contracts through order-flow and technical analysis. To demonstrate and validate our method, we select Silver Futures Contract listed on Shanghai Futures Exchange, one of the most liquidated precious metal futures contracts in the world, with more than 30 billion yuan traded every session Table 1 compare this contract annual volume with similar contracts listed on other exchange. We selected the near-month silver contract listed on Shanghai Futures Exchange from January 1 st,2018, to Dec 31st,2021. This time frame reflects various market conditions, including a lengthy, less-volatile timeframe and increased volatility during the coronavirus pandemic, thus reflecting a general market condition and preventing over-fitting. Our goal is to predict the direction of movement on this particular contract in the next 15 minutes. To make our target more stable, we apply 2 minutes moving window and calculate VWAP to smooth the ultra-short-term spike, as defined below: where P α (t) represents rolling average of 120 snapshots ( 2 minutes) Then, we calculate the log return based on this smooth price: Finally, we calculate Target α (t) based on R α (t) . Since it is unlikely that volume-weighted price remained unchanged during the 15 minutes timespan, we ignore target where R α (t) = 0. Another consideration is the data-point with minimum movement .R α (t) with only a few tick changes can create noisy training targets and are not practical to utilize in actual market conditions. To resolve this, we remove those data-point|R α (t)| < θ In our case θ = 0.001. The total samples decreased from 38 ,294 ,652 before processing to 24 ,347, 353. Table 2 , the labels are evenly distributed across two labels during the time frame. 3 Data and pre-processing Shanghai Futures Exchange provides a snapshot-based order feed using CTP protocol. The feed aggregates the change during the last 500 milliseconds. Each feed contain multiple fields, including the trade and order-book information; we utilize the following fields: Quoted price of n-th level of order-book at snapshot i on bid-side 7. priceask n i Quoted price of n-th level of order-book at snapshot i on ask-side From the original market feed, we calculate several features for the convenience of further feature engineering. Those features are not directly used in our final model. We calculate the Open Position Change for the snapshot i using. We calculate the Volume Change for the snapshot i using. Since we already calculate the change of open interest and volume and for all timestamp,for a given contract,we can calculate the contracts opened and closed at snapshot i using: • Open Contracts We catalog snapshot i into several types based on directional change of price and open interest, using • Type 2 Type 2 snapshot indicates that the snapshot price is lower than the previous snapshot and open interest increase, presumably caused by market participants taking a short position increasing their position. • Type 3 Type 3 snapshot indicates that the snapshot price is higher than the previous snapshot and open interest decrease, presumably caused by market participants taking a long position decreasing their position. • Type 4 Type 4 snapshot indicates that the snapshot price is lower than the previous one, and open interest decreases, presumably caused by market participants taking a short position and decreasing their position. An OHLCV chart is a bar chart that shows open, high, low, and closing prices and overall volume for each period. We aggregate the bar every 1 minute (120 snapshots) in our usage. OHLCV charts are helpful since they show the data change over a period, and we can generate technical analysis features utilized by the model. This section provides details of the generation of features utilized in our model. Technical analysis, unlike fundamental analysis, attempts to find the pattern of price and volume, modeling supply and demand to predict price movement. Moreover, technical analysis can generate short-term trading signals. There is an extensive range of technical analyses; we utilize some of them using a 1-minute OHLCV bar generated from snapshot as described in section 3.2.4. We use all the indicators with parameters specified in the Table 3, Table 4 , Table 5 , Table 6 as features. Order book refers to an electronic list of buy and sell orders for a specific security or financial instrument. The order book provides detailed information about the sale-buy strength. • Bid-Ask Spread Accumulated spread at order-book level k at snapshot i ,using : Then, a rolling window is applied to represent order-book overall statics over the last m minutes(120*m snapshots). AccumulatedSpread k i For each snapshot , we calculate accumulated spread of top 5 levels (k=1,2,3,4,5) ,and rolling period m of 5,10,15,30 minutes thus generating 20 features • Volume imbalance Accumulated spread at order-book level k at snapshot i ,using : Then, a rolling window is applied to represent order-book overall statics over the last m minutes(120m snapshots). VolumeImbalance k i For each snapshot , we calculate volume imbalance of top 5 levels (k=1,2,3,4,5) ,and rolling period m of 5,10,15,30 minutes thus generating 20 features . As specified in section 3.2.3 ,we classify the snapshot into 4 catalogues.We generate features according to the percentage of each type during the last m minutes(120*m snapshots). Wwhere S represents a set of snapshots from i -120m to i . Further, we want to filter snapshots where larger orders take place,presumably placed by institution investors,thus,we have : where S represents a set of snapshots from i -120m to i, excluding snapshots with volume change more minor than 10 contracts. We calculate all four snapshot types with a rolling period m of 5,10,15,30 minutes for each snapshot, thus generating 32 features. In session 3.2.1 and 3.2.2, we derived data such as opened contracts and closed contracts from original snapshots. Utilizing those data, we calculate the following features : • Open Close Percentage Percentage of closed contracts in ratio to opened contracts. Percentage of open interest change. We calculate a rolling period m of 5,10,15,30 minutes for each snapshot, thus generating 8 features. We test the correlations between the features and the target before building the model,we perform a Pearson Correlation [12] between each feature and target,as defined below: The correlation distribution of all the features is shown in Table 7 We utilize TabNet as our model. TabNet uses sequential attention to choose which features to reason from at each decision step, enabling interpretability and more efficient learning as the learning capacity is used for the most salient features. [13] We utilize AUC-ROC (Area Under the Receiver Operating Characteristics ) as our metric.AUC -ROC curve is a performance measurement for the classification problems at various threshold settings. ROC represents a probability curve, and AUC represents separability. Our train data may contain a period of unbalanced labels, so using only accuracy or recall score is inappropriate. To calculate AUC-ROC, we first calculate true/false positive and true/false negative instances. A true positive occurs when the model correctly predicts the positive class. Similarly, a true negative is an outcome where the model correctly predicts the negative class. A false positive occurs when the model incorrectly predicts the positive class. Moreover, a false negative is when the model incorrectly predicts the negative class. Then,we calculate specificity and sensitivity using the formula: Specif icity = T rue N egatives T rue N egatives + F alse P ositives Sensitivity = T rue P ositives T rue P ositives + F alse N egatives Finally, we draw the curve using different thresholds and calculate the area under Sensitivity and 1 -Specificity. Hyper-parameters are a set of parameters that control the learning process and model structure. We do not specifically search for optimal hyper-parameter as it may lead to over-fitting, but we specify some parameters to control the width and depth of the model. Cross-validation is a method that splits the dataset into different parts and then uses a different fraction for training and validation. Cross-validation can effectively protect overfitting, specifically over a small or diverse dataset. Despite having multi-million rows of the dataset, the future data tend to be volatile in our instance. Thus, a single model will easily overfit the training dataset. However, the conventional cross-validation method carries the risk of overfitting. Since each of our training instances is not isolated but a time-series, without a gap, some features having some lag or window calculations in them may leak unseen information. Thus, we must introduce a gap to prevent the risk of leaking information. We utilize Purged Group Time Series Split for our model. In Purged Group Time Series Split, test indices must be higher than before in each split without any shuffling. A gap is introduced between the training and validation datasets to deal with lagged features. We split the dataset into five folds. A demonstration of the split is provided below: We train the model on the dataset with dates ranging from Jan 1st,2018 to Aug 31st,2021, with a sample size of 21,642,091. We perform a semi-supervised pre-training before training. The training curve of the model is shown below. In this section, we first validate the model on a test dataset, then with a basic trading strategy, we examine the model's profitability. We validate the model on the dataset with dates ranging from Sept 1st,2021, to Dec 1st,2021. The label distribution of the test dataset is shown in Table 9 . Since we perform a Cross-validation on the dataset , we have a total of 5 models , we first individually predict the probability of target to be 1 for each model , then ensemble the result using: We then convert the label predicted using a threshold of 0.5. We compare the label predicted with the ground truth. The confusion matrix is shown below : We further calculate some metrics on the prediction result. We perform backtesting to test the profitability of our model. To simplify our simulation, we make the following assumptions: • We always hold either 100% cash or 100% futures contract. • We ignore the fee charged by the exchange and broker. • We assume all our orders are filled precisely at the last traded price. • We utilize the minimum margin ratio (10%) required by the exchange. Input: Model predictions probabilitiesŷ 1 , . . . ,ŷ T , account holding state P X, threshold γ ≥ 0 P X ← N one γ ← 0.25 for t = 1, . . . , T do Loop over each 15 minute interval if P X = N one then Currently holding no position ifŷ t ≥ (0.5 + γ) then We only open position if the prediction is confident enough. Currently holding position if PX = Long ANDŷ t ≤ 0.5 then The prediction has reversed,we close position. Close Long Position P X ← N one end if if PX = Short ANDŷ t ≥ 0.5 then Close Short Position P X ← N one end if end if end for We simulate the trading using the same dataset used for validation and the Algorithm 1. The total return and price change during the period is shown in figure 6. Despite the futures contract price remaining nearly unchanged during the period, our model can still generate a relatively decent return during the period. We further calculate some common metrics to measure the model's performance. The model can achieve a decent return, but the daily performance tends to be volatile, which may be attributed to the high margin ratio we used in the simulation. This paper discussed a neural network model using multiple features generated from conventional technical analysis and market micro-structure like order flow and order book. We train these models using a specified cross-validation method. When ensembled, the model is profitable even with a basic trading strategy. Thus, we conclude that this method is sufficient to gain insight into the short-term directional change of the futures market. • Feature Engineering We may utilize alternative data, such as news and social media data, to generate features like sentiment scores. We may ensemble a non-neural-network model, for instance, a tree-based model with a neural network model to gain better accuracy. A study of the indian metal sector using time series decomposition-based approach Forecasting stock index returns using arima-svm, arima-ann, and arima-random forest hybrid models A multiscale modeling approach incorporating arima and anns for financial market volatility forecasting Deep learning for stock market prediction Stock market index prediction using artificial neural network Multi-element hierarchical attention capsule network for stock prediction Financial news-based stock movement prediction using causality analysis of influence in the korean stock market Stock price prediction using cnn and lstm-based deep learning models Stock price prediction using machine learning and lstm-based deep learning models Shanghai futures exchange volume report Pearson correlation coefficient