30
GPU Accelerated Backtesting and ML for Quant Trading Strategies GTC 2015 | San José | California Dr. Daniel Egloff [email protected] March 18, 2015

GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

  • Upload
    ngodieu

  • View
    224

  • Download
    11

Embed Size (px)

Citation preview

Page 1: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

GPU Accelerated Backtesting and ML for

Quant Trading Strategies

GTC 2015 | San José | California

Dr. Daniel Egloff

[email protected]

March 18, 2015

Page 2: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Motivation

Goals

– Execute automated algorithmic trading strategies

– Optimize risk return

Procedure

– Extract signals and build price forecasting indicators from market data

– Transform indicators into buy / sell decisions

– Apply portfolio risk management

Challenges

– Find relevant signals and indicators

– Engineer and parameterize trading decision

– Find optimal parameters

Approach

– Exploit parallelism in the computations

– Accelerate calculations by using a GPU cluster

Page 3: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Algo Trading Strategies

Market data

Input

Trading decision

Mathematical

operations

Output

Configurations

Rules

Page 4: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Example

fast moving average

slow moving average

Buy signal Sell signal

Page 5: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Markets

Futures market

– CME 50 liquid futures

– Other exchanges

Equity markets

– World stock indices

FX markets

– 10 major currency pairs

– 30 alternative currency pairs

Options markets

– Options on futures

– Options on indices

Page 6: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Strategy Universe

Configuration c Trading decision s(c)

1

-1

buy

sell

Utility U(s(c))

P&L / drawdown

Strategy configurations

Page 7: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Strategy Engineering

Challenge 1: How can we engineer a strategy

producing buy / sell decisions ?

Page 8: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Random Forests

Fi < ti

Fj < tj Buy

Buy

Fi < ti

Sell Fk < tk

Sell

Fi < ti

Fi < ti

Buy Fk < tk

Fi < ti

Fj < tj Sell

Fi < ti

Sell

Buy

Sell Buy

Sell Buy

Sell Buy

Sell Buy

Page 9: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Random Forests

Strategy configuration c

Features Random forest

Trading decision s(c)

1

-1

buy

sell

Page 10: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Training Random Forests

Bootstrapping to create training sets

C 4.5 algorithm for individual tree construction

Selecting subset of features for tree construction

Each node is associated with a subset of training samples

Recursive, starting at the root node

At each node execute divide and conquer algorithm to find locally optimal choice

– If samples are in same class (or few class) node is a leaf associated with that class

– If samples are in two or more classes

Calculate information gain for each feature

Select feature if largest information gain for splitting

Page 11: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Entropy

𝑇 = set of samples associated with node

𝐶1, … , 𝐶𝑛= classes of samples

Entropy

𝐸𝑛𝑡 𝑇 = − 𝑖=1𝑛 𝑓𝑟𝑒𝑞(𝐶𝑖,𝑇)

𝑇𝑙𝑜𝑔2

𝑓𝑟𝑒𝑞(𝐶𝑖,𝑇)

𝑇

Characterizes impurity of samples

Measure of uncertainty

Additive: impurity of several subsets is

sum of impurities

Page 12: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Information Gain

𝑇1, … , 𝑇𝑠 = subsets of 𝑇 generated by splitting on selected attribute

Information gain discrete feature

𝑔𝑎𝑖𝑛 𝑇1, … , 𝑇𝑠 = 𝐸𝑛𝑡 𝑇 − 𝑖=1𝑠 𝑇𝑖

𝑇𝐸𝑛𝑡 𝑇𝑖

Information gain continuous feature with optimal splitting threshold

𝑔𝑎𝑖𝑛 𝑡 = 𝑔𝑎𝑖𝑛 𝑇≤𝑡 , 𝑇>𝑡

𝑡∗ = 𝑎𝑟𝑔𝑚𝑎𝑥 𝑔𝑎𝑖𝑛 𝑡

Actual implementation uses ratio information gain over split ratio

Page 13: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Training Individual Trees

Labels from positive or negative market returns

Selected

features

Permuted

labels

Label

Permutations

to

sort features

All features

Samples / observations

Page 14: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Training Individual Trees

Selected

features

Permuted

labels

Permutations

to

sort features

WeightsPermuted

weights

Permutations

to

sort features

Label

All features

2 0 1 4 0 2 ………

Page 15: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Training Individual Trees

Entropy criterion for best feature and split

Entropy for

every split

Permuted

labelsPermuted

weights 2. Optimal feature Fi

1. Optimal split ti

Fi < ti

Page 16: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Training Individual Trees

Recursively refine classification: mask data according to classification

Selected

features

Permuted

labels

Label

Permutations

to

sort features

WeightPermuted

weights

Permutations

to

sort features

Page 17: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Training Individual Trees

FeaturesPermuted

labels

Label

Permutations

to

sort features

WeightPermuted

weights

Permutations

to

sort features

Recursively refine classification: mask data according to classification

Page 18: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

GPU Implementation

Parallelism at multiple levels

– Multiple trees, one for each set of weights

– Independent features

– Independent split points

– Multiple nodes further down the tree

GPU kernels can be implemented with standard primitives

– Random number generation for weights

– Parallel scan (cumulative sum)

– Parallel map

– Parallel reduction to find optimal feature and split

Page 19: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Speedup

0

10

20

30

40

50

60

20 50 100 20 50 100 20 50 100 20 50 100

1 2 4 8

20000

50000

100000

Depth

Features

Samples

Page 20: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Strategy Backtesting

Challenge2: How to choose best trading

strategy ?

Page 21: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Walk Forward Optimization

In sample Out of sample

3-6 months 1 month

In sample Out of sample

In sample Out of sample

shift by 1 month

Page 22: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Trading P&L

Market returns r(ti)

r(ti) = log(P(ti) / P(ti-1))

ti-1 ti

P(ti-1)

P(ti)

r

Trading decision s(c)

P&L(c) = <s(c), r>

r r r r

1 -1 1 1 -1

…..

…..

…..

…..

Market prices Market returns

time

Page 23: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Optimal Configuration

s(c)

s(c)

r

s(c)

s(c)

s(c)

s(c)

s(c)

s(c)

P&L

=

Configurations

pick configuration with largest P&L

x

Page 24: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Bootstrapping Trading P&L

s(c)

s(c)

r

s(c)

s(c)

s(c)

s(c)

s(c)

s(c)

P&L

=

Configurations

pick configuration with largest P&L

x

w w w w

x

x

x

x

xww

Weights

r .* w

Page 25: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Hypothesis Tests

Null Hypothesis

Trading P&L<= 0

Alternative Hypothesis

Trading P&L > 0

Page 26: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Trading P&L Distribution

optimal configurations for each weight

c* for w = 1

p-value for test 0

P&L

x

x

x

x

x

x

Page 27: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

White Reality Check

Scale exposure according to distance from 0

Do not trade if negative returns

c* for w = 1

0p-value

c* for w = 1

0p-value

Market 1 Market n……

Page 28: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

GPU Implementation

Parallelism at multiple levels

– Multiple markets

– Independent in-sample / out-of-sample windows

– Independent strategy configurations

– Independent time steps for utility functions such as mean return

GPU kernels can be implemented with standard primitives

– Random number generation

– Matrix multiplication (almost, up to return vector scaling the weights)

– Parallel reduction

Page 29: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

GPU Implementation

GPU grid

– Multiple markets

– Independent in-sample / out-of-sample windows

Per GPU

– Independent strategy configurations

– Independent time steps for utility functions such as mean return

Page 30: GPU Accelerated Backtesting and ML for Quant Trading ...on-demand.gputechconf.com/gtc/2015/presentation/S5126-Daniel... · GPU Accelerated Backtesting and ML for Quant Trading Strategies

Dr. Daniel Egloff

[email protected]

March 18, 2015

Questions ?