an optimal control strategy for execution of large stock

38
An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs Brian Healy Stanford University Machine Learning in Finance Workshop 2021

Upload: others

Post on 10-Jun-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock

Orders Using LSTMsBrian Healy

Stanford University

Machine Learning in Finance Workshop 2021

Page 2: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Introduction

Overview

We consider the problem of executing large stock orders in a limit-order book using:

I Transaction costs modelled as a function of overall trading volume and convex intrade size.

I A long short term memory (LSTM) neural network to solve an optimization tominimize the total transaction costs accumulated when executing a large order.

I Industry-standard metrics such as time-weighted average price (TWAP) andvolume-weighted average price (VWAP) to evaluate the strategy.

I The example of liquidating a large position in individual stocks over the course ofa trading day.

We find, using recent market data, that that an LSTM strategy can out-performTWAP and VWAP-based strategies when the size of the trade is very large.

2 / 37

Page 3: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Introduction

The Problem

I Institutional investors will lose substantial amounts of money to /transactioncosts/slippage if their trades are not carefully placed.

I When an order representing a significant proportion of the average daily volumeof a stock needs to be executed, a naive strategy is to place a single, very largeorder on the exchange.

I This single order will consume most of the liquidity available in the limit-orderbook and will result in an average price per share that is far from the bestbid/offer.

3 / 37

Page 4: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Introduction

Our Proposal

I This liquidity issue can be avoided by sub-dividing the trade into smallersub-orders, which are then executed at scheduled points over a fixed time period.

I We cast the sub-dividing of the trade into small sub-orders as an optimal controlproblem.

I We utilize a long short term memory (LSTM) neural network to solve theminimization of the total transaction costs through finding the optimal controlstrategy for sub-dividing the trade.

4 / 37

Page 5: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Introduction

Background

Background

I Liquidity is made available by market makers who submit limit orders at thedifferent levels in an order book.

I Liquidity is consumed when a trader submits a market order to buy or sell.

I In the most basic case, a market order gets filled with limit orders available at thebest available ticks. If the order is very large, then it will consume all limit ordersat multiple ticks.

5 / 37

Page 6: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Introduction

Background

Background

I Market buy (sell) orders that consume multiple ticks will have a price per shareequal to the best offer (bid) plus (minus) a transaction cost.

I Statistical studies of order books have shown that this transaction cost is roughlydescribed as a convex function of the number of shares Almgren et al. (2005);Weber and Rosenow (2005) and that a better average price can be achieved bydividing the order into sub-orders to be executed sequentially over time. Almgrenet al. (2005)

I The liquidity consumed by a market order refills quickly in the time betweenscheduled sub orders, and therefore a simple description of the problem need onlyto consider the temporary impact on price.

I An optimal sub-order policy will minimize an objective that considers theexpected value of transaction costs over the entire trade.

6 / 37

Page 7: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Price Impact

Price Impact

I S it is the best bid/offer of the stock i = 1, 2, 3, . . . , d at time t.

I V it is the volume traded in the stock for i = 1, 2, 3, . . . , d at time t.

I The order book has limit-order distribution ρi (t, s) ≥ 0, where the units of s areticks relative to S i

t .

An order of ai shares consumes liquidity up to a relative price r it such that

ai =

∫ r it

1ρi (t, s)ds (1)

7 / 37

Page 8: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Price Impact

Price Impact

Assuming a trade does not shock the market but still has price impact, (trade size 1%- 10% of total volume), a simple form for ρ is:

ρi (t, s) =V it

ε|s − 1|β (2)

I ρ follows a power law with limit-orders distributed proportionally to volume

I β ∈ [0,∞) and ε > 0 is a scaling parameter.

I Volume is typically quoted as the result of some form of moving-averagecalculation. Since we seek to optimize execution of a large order during a singletrading day, V i

t will be the volume of trades in the minute before time t.

8 / 37

Page 9: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Price Impact

Price Impact

When relative price r it in (1) is computed with distribution ρi in (2), we see a pricethat is a concave function of order size divided by volume,

r it = 1 + sign(ai )

(ε(β + 1)

V it

|ai |) 1β+1

(3)

I r it can be viewed as the impacted price.

- Almgren and Chriss (2001) had β = 0 so that the order book has equal liquidity at allticks (impacted price is linear in a).

- Many empirical studies conclude that impacted price is a concave function Almgrenet al. (2005); Bershova and Rakhlin (2013); Bouchaud (2010); Cont et al. (2014), suchas β = 1 (square-root function).

9 / 37

Page 10: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Price Impact

Price Impact

Combining (1), (2) and (3), the dollar amount of trading loss due to the price impactis:

loss(t, a) =d∑

i=1

S it

(∫ r it

1sρi (t, s)ds − ai

)(4)

= Cε,β

d∑i=1

S it (V i

t )− 1β+1 |ai |

β+2β+1

where Cε,β = 1β+2

(ε(β + 1))β+2β+1 .

I The convexity of (4) with respect to order size |ai | shows that large orders shouldbe divided into sub-orders to reduce transaction costs.

10 / 37

Page 11: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Execution Policies

Execution Policies

I Let Xt be a d-dimensional vector denoting the inventory in each of the stocks attime t (X i

t is the number of shares of stock i at time t).

I Let the initial inventory be the d-dimensional vector X0.

I Consider the situation where this inventory needs to be completely liquidated byterminal time T .

I An execution policy is a sequence of sub-orders, denoted by at fort = 1, 2, 3, . . . ,T such that

Xt = Xt−1 + at . (5)

I That is, ait is this execution policy’s time-t sub-order for trading in stock i , andthese sub-orders are chosen so that

XT = 0 . (6)

11 / 37

Page 12: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Execution Policies

Execution Policies

Using the loss function given in (4), the total price for sub-order ait is:

|ait |S it

(1 + Cε,βsign(ait)

(|ait |/V i

t

)1/(β+1))

(7)

and the average price is:

S it

(1 + Cε,βsign(ait)

(|ait |/V i

t

)1/(β+1))

(8)

12 / 37

Page 13: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Execution Policies

TWAP

Time-Weighted Average Price (TWAP)

I Let (at)t≤T be an execution policy.

I The TWAP for this policy is:

SiT =

1

T

T∑t=1

S it

1 + Cε,βsign(ait)

(|ait |V it

) 1β+1

(9)

I Commonly divide the order into pre-determined sub-orders ait = −X i0/T with

sub-orders being submitted every n minutes.

I This TWAP strategy clearly guarantees that terminal condition (6) is met.

I It ignores the evolution of volume and other features of the trading period.

13 / 37

Page 14: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Execution Policies

VWAP

Volume-Weighted Average Price (VWAP)

I Let (at)t≤T be an execution policy.

I The VWAP for this policy is:

SiV =

1

|x i0|

T∑t=1

|ait |S it

1 + Cε,βsign(ait)

(|ait |V it

) 1β+1

. (10)

I A VWAP strategy requires knowledge of the typical evolution of volume over atrading period.

I We can compute averages Vit of the volume of stock i at time t and use VWAP

strategy:

ait = −X i

0Vit∑T

t=1 Vit

for t ≤ T . (11)

I This simple VWAP strategy can be effective for single-day execution becausevolume follows a predictable “U” shape.

I A VWAP strategy such as (11) does not guarantee that the VWAP of (10) is

achieved, but it can be a close proxy if the estimated averages Vit have low noise.

14 / 37

Page 15: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Execution Policies

VWAP

Daily Volume

Figure 1: The “U”-shaped patterns of daily volume in Apple and Tesla. The U seems is morepronounced in Apple, namely, Apple has consistently high volumes in the mornings and near to theclose, and typically lower volumes during the mid-day trading hours. Tesla on the other hand, iscomparatively noisy, which will perhaps be an issue for the VWAP strategy of (11).

15 / 37

Page 16: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Algorithm

Process

I Let (St ,Vt) be a Markov process

I We find the execution policy that minimizes the total trading losses solvingsolving:

mina

E

[T∑t=1

d∑i=1

S it (V i

t )− 1β+1 |ai |

β+2β+1

](12)

Xt = Xt−1 + at and XT = 0

I This optimization can be solved by training an LSTM network.

I The solution can be compared with a TWAP and VWAP strategy.

I We use 5-minute execution intervals for all strategies to ensure a fair comparison.

16 / 37

Page 17: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Algorithm

LSTM Design

LSTM Design

I Algorithm 1 shows how the LSTM network is trained.

I We use the loss function given in equation (12).

I The output of the network is an inventory amount and an updated state for theLSTM network (the latter we denote as h).

I Time increments are 5 minutes but the intermediate data is still seen by theLSTM, which means the strategy is making full use of the available information.

17 / 37

Page 18: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Algorithm

LSTM Design

Algorithm 1: Training of LSTM Architecture for Optimal Execution Every 5 MinutesIn A 390-Minute Trading Day

Initialize parameters of LSTM units;

for k = 1 to NUM EPOCH do

X0 = x0, L = 0, t = 1;

while t < 390 do

Xt , ht = LSTM(ht−1,St ,Vt);

if mod(t,5)=0 then

L += loss(t,Xt − Xt−5);

endend

L += loss(390,−X385);

-Compute gradient of loss (backpropagation);

-Update parameters (gradient descent step);end

18 / 37

Page 19: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Algorithm

LSTM Design

LSTM Design

I The flow of state variables through the LSTM network is shown in Figure 2below.

I The network takes as input:

I Current stock prices

I Current volumes

I Current time value

I Current inventories

I The trading day has 390 minutes, and so the first trade occurs at time t = 4,with successive trades occurring every 5 minutes thereafter and the final tradeoccurring at minute 390 when the market closes.

18 / 37

Page 20: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Algorithm

LSTM Design

LSTM Design

Figure 2: Flow of variables through the LSTM

19 / 37

Page 21: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Data

Data

I We used minute-by-minute prices and average volumes from 7th December 2020through 5th March 2021

I The 12 large capitalization stocks listed below.

I We chose firms from the tech sector, the financial sector and some healthcarecompanies.

Name Avg Daily Vol (3m). Shares OutstandingAmazon 3.66M 503M

Facebook 19.9M 2.4BNetflix 8.59M 434.85MGoogle 1.76M 689.63MApple 111M 16.79B

Microsoft 29.3M 7.56BNvidia 8.1M 602.62MTesla 35M 1.08B

J.P. Morgan 14.8M 3.05BPfizer 35M 5.745B

Moderna 14.8M 400.5MJ & J 7.9M 2.633 B

20 / 37

Page 22: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Data

LSTM Initialisation

We trained the LSTM network for the inventory liquidation problem with:

I X i0 = 500, 000 shares for each i .

I 2 hidden layers, each with 50 nodes.

I Sigmoid activation functions.

I 10,000 epochs.

I 51 days from December 7th 2020 to February 19th 2021.

21 / 37

Page 23: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Data

LSTM Setup

Additional parameters:

I β = .67 so that the price impact in has a power of .6 as suggested by Almgrenet al. (2005)

I ε = 10−4

These values imply (roughly) that demand is met by market makers who place orderswithin 35 basis points of the best bid/offer.

Timing:

I Using GPUs, the runtime for training was less than 20 minutes.

I Using CPUs, the runtime is around 10 hours.

22 / 37

Page 24: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Results

Trading Costs for the 3 strategies

Date LSTM TWAP VWAP

2/22 438,238.00 506,620.25 444,790.062/23 380,255.50 437,266.25 384,744.122/24 489,168.19 527,133.88 485,574.222/25 357,856.78 371,715.75 363,918.472/26 379,620.81 426,681.12 387,767.783/1 493,586.19 586,260.75 494,846.473/2 520,365.53 621,989.44 538,451.563/3 372,745.38 409,027.16 385,030.283/4 296,482.75 299,098.81 296,558.343/5 288,553.75 316,294.66 290,124.97

Mean 401,687.31 450,208.81 407,180.59

I In 9 out of 10 days of out-of-sample testing, the VWAP accumulated a lossgreater than the LSTM strategy, with an overall mean loss that was almost$6,000 greater.

23 / 37

Page 25: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Results

I The total running cost for each of the 3 strategies on day March 2, 2021.

I Each strategy executed the liquidation of 500,000 shares of each of the 12 stocks.

I The total loss computation is that given in (4) with β = .67.

24 / 37

Page 26: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Results

Inventory Process

I The general pattern for inventory liquidation for LSTM and VWAP can bedescribed as an “S” shape relative to the linear TWAP

I High volume in the morning encourages the LSTM to submit larger sub-orders.VWAP behaves similarly was designed to.

I Similarly there is an increase in sub-order size in the final hour of the trading day,which is a time when volume is typically high.

I In particular, LSTM generates an increase in sub-order size in the final minutes oftrading as it has learned that there is typically a surge in volume then.

I To avoid the costs associated with a failure to execute the entire inventory, theLSTM finds a non-myopic control sequence that will execute both in a timely wayand with consideration given to the possibility of an end-of-day volume surge.

25 / 37

Page 27: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Results

Inventory

Inventory Process

26 / 37

Page 28: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Results

Inventory

Inventory Process

27 / 37

Page 29: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Results

Losses

Loss Process

I A typical day’s pattern for total running loss also shows “S” shape for allstrategies.

I As the TWAP doesn’t execute as fast as either LSTM or VWAP in the morning ithas lower losses.

I As the day progresses, the TWAP will incur higher transaction costs as itcontinues submitting orders during the mid-day hours when the volume is lowerand the price action is not as favorable.

I We can see the acceleration in loss accrual by LSTM in the final hour of the daydue to it’s heavier volume in the closing munutes.

28 / 37

Page 30: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Results

Losses

Loss Process

29 / 37

Page 31: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Results

Actions

Actions

I Focusing on the actions taken, we see something similar to the inventory results.

I LSTM and VWAP submit large sub-orders at those times that typically have thehighest trading volume.

I This is is by construction in the case of VWAP but has been learned by theLSTM from the training data.

30 / 37

Page 32: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Results

Actions

Actions

31 / 37

Page 33: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Results

Actions

Actions

32 / 37

Page 34: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Results

VWAPs

Comparison of VWAPs

Ticker LSTM TWAP VWAP

AMZN 3134.5156 3134.4297 3134.5034FB 263.8812 263.8801 263.8812

NFLX 550.8960 550.8882 550.8948GOOGL 2074.3865 2074.2847 2074.3630AAPL 126.1221 126.1219 126.1221MSFT 234.8160 234.8151 234.8161NVDA 545.7809 545.7782 545.7806TSLA 702.4022 702.4010 702.4024JPM 150.7749 150.7744 150.7751PFE 33.6946 33.6945 33.6946

MRNA 149.4903 149.4900 149.4902JNJ 159.4356 159.4355 159.4364

VWAP for the 3 execution strategies - 2nd March 2021.

33 / 37

Page 35: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Results

VWAPs

Comparison of VWAPs

I The LSTM and VWAP strategies completed the liquidation with better averageprice than TWAP.

I LSTM does not consistently outperform the VWAP strategy.

I However, LSTM does outperform in the cases of Amazon and Google, both ofwhich have average daily volume less than 2 million shares, which means that the500,000 shares being executed are double-digit percentages of the daily volume.

I This suggests that LSTM outperforms when the trade size is extremely large,otherwise a simple VWAP strategy is fine.

34 / 37

Page 36: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Conclusion

Conclusion

I LSTM can be used for optimal execution of large stock orders in a limit-orderbook.

I The strategy clearly out-performs TWAP and is slightly better than VWAP-basedstrategies in out-of-sample testing.

I LSTM and VWAP produce similar results because volume is the primary factorfor both.

I LSTM learns the role of volume in determining minute-by-minute transactioncosts and manages to uncover some essential principles for large-order execution.It is possible that out-performance of the LSTM is due to its ability to aggregateacross multiple stocks and detect effects such as heteroskedasticity among boththe price and the volume time series, but future studies should be performed toverify these claims.

35 / 37

Page 37: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Conclusion

Questions ?

36 / 37

Page 38: An Optimal Control Strategy for Execution of Large Stock

An Optimal Control Strategy for Execution of Large Stock Orders Using LSTMs

Conclusion

References

Robert Almgren and Neil Chriss. Optimal execution of portfolio transactions. Journalof Risk, 3:5–40, 2001.

Robert Almgren, Chee Thum, Emmanuel Hauptmann, and Hong Li. Direct estimationof equity market impact. Risk, 18(7):58–62, 2005.

Nataliya Bershova and Dmitry Rakhlin. The non-linear market impact of large trades:Evidence from buy-side order flow. Quantitative finance, 13(11):1759–1778, 2013.

Jean-Philippe Bouchaud. Price impact. Encyclopedia of quantitative finance, 2010.

Rama Cont, Arseniy Kukanov, and Sasha Stoikov. The price impact of order bookevents. Journal of financial econometrics, 12(1):47–88, 2014.

Philipp Weber and Bernd Rosenow. Order book approach to price impact.Quantitative Finance, 5(4):357–364, 2005.

37 / 37