Intro to Quant Trading Strategies (Lecture 3 of 10)
1. Introduction to Algorithmic Trading Strategies
Lecture 3
Trend Following
Haksun Li
haksun.li@numericalmethod.com
www.numericalmethod.com
2. References
2
๏ฝ Introduction to Stochastic Calculus with Applications.
Fima C Klebaner. 2nd Edition.
๏ฝ Estimating continuous time transition matrices from
discretely observed data. Inamura, Yasunari. April
2006.
๏ฝ Optimal Trend Following Trading Rules. Dai, Min and
Zhang, Qing and Zhu, Qiji Jim. 2011.
4. Brownian Motion
4
๏ฝ Independence of increments.
๏ฝ ๐๐ = ๐ต ๐ก โ ๐ต ๐ is independent of any history up to ๐ .
๏ฝ Normality of increments.
๏ฝ ๐๐ is normally distributed with mean 0 and variance ๐ก โ ๐ .
๏ฝ Continuity of paths.
๏ฝ ๐ต ๐ก is a continuous function of ๐ก.
5. Stochastic vs. Newtonian Calculus
5
๏ฝ Newtonian calculus: when you zoom in a function
enough, the little segment looks like a straight line,
hence the approximation:
๏ฝ ๐๐ = ๐ฬ ๐๐
๏ฝ Derivative exists.
๏ฝ The function is differentiable.
๏ฝ Stochastic calculus: no matter how much you zoom in
or how small ๐๐ is, the function still looks very zig-zag
and random. It is nothing like a straight line.
๏ฝ For Brownian motion, however much you zoom in, how
small the segment is, it still just looks very much like a
Brownian motion.
๏ฝ The function is therefore no where differentiable.
7. dBdB
7
๏ฝ ๐๐ 2 = ๐๐๐๐ = ๐๐
๏ฝ โซ ๐๐ต๐ก
2๐ก
0
โ โ ๐ ๐,๐
2๐
๐=1
๏ฝ ๐ ๐,๐ is of ๐ 0,
๐ก
๐
for all ๐.
๏ฝ โซ ๐๐ต๐ก
2๐ก
0
โ sum of variances of ๐ ๐,๐ โ ๐ก
๏ฝ Making ๐ โ โ or ๐๐ โ 0, we have
๏ฝ โซ ๐๐ต๐ก
2๐ก
0
= ๐ก, convergence in probability
๏ฝ ๐๐ต๐ก
2 = ๐๐, in differential form
๏ฝ ๐๐๐๐ก = 0
๏ฝ ๐๐ก๐๐ = 0
8. Asset Price Model
8
๏ฝ Want to model asset price movement.
๏ฝ Change in price = ๐๐
๏ฝ Change in price is not too meaningful as $1 change in a
penny stock is more significant than $1 change in GOOG.
๏ฝ Return =
๐๐
๐
๏ฝ Model return using two parts.
๏ฝ Deterministic: ๐๐๐, the predictable part. E.g., fixed deposit
interest rate.
๏ฝ Random/stochastic: ๐๐๐, where ๐ is the volatility of returns
and ๐๐ต is a sample from a probability distribution, e.g.,
Normal.
9. Geometric Brownian Motion
9
๏ฝ Asset price:
๐๐
๐
= ๐๐๐ + ๐๐๐ต
๏ฝ ๐๐ต: normally distributed
๏ฝ E ๐๐ต = 0
๏ฝ Variance = ๐๐. It is intuitive that ๐๐ต should be scaled by ๐๐
otherwise the (random) return drawn would be too big
from any Normal distribution for ๐๐ โ 0.
๏ฝ ๐๐ต = ๐ ๐๐, where ๐ is a standard Normal distribution.
๏ฝ Reasonably good model for stocks and indices.
๏ฝ Real data have more big rises and falls than this model
predicts, i.e., extreme events.
10. GBM Properties
10
๏ฝ Markov property: the distribution of the next price
S + ๐๐ depend only on the current price ๐.
๏ฝ E ๐๐ = E ๐๐๐๐ + ๐๐๐๐ต
๏ฝ = E ๐๐๐๐ + E ๐๐๐๐ต
๏ฝ = ๐๐๐๐ + ๐๐ E ๐๐ต
๏ฝ = ๐๐๐๐
๏ฝ Var ๐๐ = E ๐๐2 โ E ๐๐ 2 = ๐2 ๐2 ๐๐
๏ฝ ๐๐ต๐๐ = 0
๏ฝ ๐๐๐๐ = 0
๏ฝ ๐๐ต๐๐ต = ๐๐
11. Stochastic Differential Equation
11
๏ฝ Both ๐ and ๐ can be as simple as constants, deterministic
functions of ๐ก and ๐, or as complicated as stochastic functions
adapted to the filtration generated by ๐๐ก .
๏ฝ ๐๐๐ก = ๐๐๐ + ๐๐๐ต๐ก
๏ฝ ๐๐๐ก = ๐ ๐ก, ๐๐ก ๐๐ + ๐ ๐ก, ๐๐ก ๐๐ต๐ก
12. Univariate Itoโs Lemma
12
๏ฝ Assume
๏ฝ ๐๐๐ก = ๐ ๐ก ๐๐ + ๐๐ก ๐๐ต๐ก
๏ฝ ๐ ๐ก, ๐๐ก is twice differentiable of two real variables
๏ฝ We have
๏ฝ ๐๐ ๐ก, ๐๐ก =
๐๐
๐๐
+ ๐ ๐ก
๐๐
๐๐
+
๐๐ก
2
2
๐2 ๐
๐๐ฅ2 ๐๐ + ๐๐ก
๐๐
๐๐
๐๐ต๐ก
16. Stopping Time
16
๏ฝ A stopping time is a random time that some event is
known to have occurred.
๏ฝ Examples:
๏ฝ when a stock hits a target price
๏ฝ when you run out of money
๏ฝ when a moving average crossoveroccurs
๏ฝ When a stock has bottomed out is NOT a stopping
time because you cannot tell when it has reached its
minimum without future information.
18. Asset Model
18
๏ฝ Two state Markov model for a stockโs prices: BULL and
BEAR.
๏ฝ ๐๐ ๐ = ๐ ๐ ๐ ๐ผ ๐
๐๐ + ๐๐๐ต๐ , ๐ก โค ๐ โค ๐ < โ
๏ฝ The trading period is between time ๐ก, ๐ .
๏ฝ ๐ผ ๐ = 1,2 are the two Markov states that indicates the
BULL and BEAR markets.
๏ฝ ๐1 > 0
๏ฝ ๐2 < 0
๏ฝ ๐ =
โ๐1 ๐1
๐2 โ๐2
, the generator matrix for the Markov
chain.
20. Optimal Stopping Times
20
t T๐1 ๐1 ๐2 ๐2 ๐ ๐ ๐ ๐
๐ = 0, ฮ0 = ๐1, ๐1, ๐2, ๐2, โฏ
๐ = 0, ฮ0 = ๐1, ๐2, ๐2, ๐3, โฏ
BUY BUY BUYSELL SELL SELL
no position,
bank the $
no position,
bank the $
no position,
bank the $
LONG LONG LONG
21. Parameters
21
๏ฝ ๐ โฅ 0, interest free rate
๏ฝ ๐๐ก = ๐, the initial stock price
๏ฝ ๐ = 0,1 , the net position
๏ฝ ๐พ๐, the transaction cost for BUYs in percentage
๏ฝ ๐พ๐ , the transaction cost for SELLs in percentage
22. Expected Return (starting flat)
22
๏ฝ When ๐ = 0,
๏ฝ E0,๐ก ๐ ๐ก =
E ๐ก ๐ ๐ ๐1โ๐ก โ
๐ ๐ ๐
๐ ๐ ๐
1โ๐พ๐
1+๐พ ๐
๐ผ ๐ ๐<๐
๐ ๐ ๐ ๐+1โ๐ ๐โ
๐=1
๏ฝ You are long between ๐ ๐ and ๐ ๐ and the return is
determined by the price change discounted by the
commissions.
๏ฝ You are flat between ๐ ๐ and ๐ ๐+1 and the money grows at
the risk free rate.
23. Expected Return (starting long)
23
๏ฝ When ๐ = 1,
๏ฝ E1,๐ก ๐ ๐ก =
E ๐ก
๐ ๐1
๐
๐ ๐ ๐2โ๐1 1 โ ๐พ๐ โ
๐ ๐ ๐
๐ ๐ ๐
1โ๐พ๐
1+๐พ ๐
๐ผ ๐ ๐<๐
๐ ๐ ๐ ๐+1โ๐ ๐โ
๐=2
๏ฝ You sell the long position at ๐1.
24. Value Functions
24
๏ฝ It is easier to work with the log of the returns.
๏ฝ J0 ๐, ๐ผ, ๐ก, ฮ0 = E ๐ก ๐ ๐1 โ ๐ก + โ log
๐ ๐ ๐
๐ ๐ ๐
+ ๐ผ ๐ ๐<๐ log
1โ๐พ๐
1+๐พ ๐
+ ๐ ๐ ๐+1 โ ๐ ๐
โ
๐=1
๏ฝ J1 ๐, ๐ผ, ๐ก, ฮ1 =
E ๐ก log
๐ ๐1
๐
+ ๐ ๐2 โ ๐1 + log 1 โ ๐พ๐ + โ log
๐ ๐ ๐
๐ ๐ ๐
+ ๐ผ ๐ ๐<๐ log
1โ๐พ๐
1+๐พ ๐
+ ๐ ๐ ๐+1 โ ๐ ๐
โ
๐=2
25. Conditional Probability of Bull Market
25
๏ฝ ๐ ๐ = ๐ ๐ผ ๐ = 1 | ๐ ๐ ๐ข โถ 0 โค ๐ข โค ๐
๏ฝ ๐ ๐ is therefore a random process driven by the same
Brownian motion that drives ๐ ๐ข.
๏ฝ ๐๐ ๐ = โ ๐1 + ๐2 ๐ ๐ + ๐2 ๐๐ +
๐1โ๐2 ๐ ๐ 1โ๐ ๐
๐
๐๐ต๐
๏ฟฝ
๏ฝ ๐๐ต๐
๏ฟฝ =
๐ log ๐ ๐ โ ๐1โ๐2 ๐ ๐+๐2โ๐2 2โ ๐๐
๐
๏ฝ ๐๐ก+1 = ๐๐ก + ๐ ๐๐ก ฮ๐ก +
๐1โ๐2 ๐๐ก 1โ๐๐ก
๐2 log
๐๐ก+1
๐๐ก
๏ฝ ๐ ๐ = โ ๐1 + ๐2 ๐ + ๐2 โ
๐1โ๐2 ๐ 1โ๐ ๐1โ๐2 ๐+๐2โ๐2 2โ
๐2
๏ฝ Make ๐๐ก+1 between 0 and 1 if it falls outside the bounds.
26. Objective
26
๏ฝ Find an optimal trading sequence (the stopping times)
so that the value functions are maximized.
๏ฝ ๐๐ ๐, ๐ก = supฮ๐
๐ฝ๐ ๐, ๐, ๐ก, ฮ ๐
๏ฝ ๐๐: the maximum amount of expected returns
28. Principal of Optimality
28
๏ฝ Given an optimal trading sequence, ฮ ๐, starting from
time ๐ก, the truncated sequence will also be optimal for
the same trading problem starting from any stopping
times ๐ ๐ or ๐ ๐.
35. Problems
35
๏ฝ The buy entry signals are always delayed (as expected).
๏ฝ The market therefore needs to bull long enough for
the strategy to make profit.
๏ฝ If the bear market comes in too fast and hard, the exit
entry may not come in soon enough.
๏ฝ A proper stoploss from max drawdown may help.
36. Finding the Boundaries
36
๏ฝ At any time ๐ก, we need to find ๐ ๐
โ
and ๐๐
โ to determine
whether we buy or sell or go flat.
๏ฝ ๐ ๐
โ
is determined by
๏ฝ ๐1 ๐ ๐
โ
, ๐ก โ ๐0 ๐ ๐
โ
, ๐ก = log 1 + ๐พ๐
๏ฝ ๐๐
โ is determined by
๏ฝ ๐1 ๐๐
โ, ๐ก โ ๐0 ๐๐
โ, ๐ก = log 1 โ ๐พ๐
๏ฝ Need to compute ๐0 ๐, ๐ก and ๐1 ๐, ๐ก for all ๐ and ๐ก.
๏ฝ Solve the coupled PDEs.
46. Solving the System of Equations
46
๏ฝ We already know the values of ๐0
0
and ๐1
0
.
๏ฝ Starting from ๐ = 0, using ๐ด ๐, we iteratively solve for
๐0
๐+1
and ๐1
๐+1
until ๐ = ๐ โ 1.
๏ฝ There are totally M systems of equations to solve.
๏ฝ The equations are linear in the unknowns ๐0,๐
๐+1
and
๐1,๐
๐+1
if and only if ๐ฬ
0,๐
๐+1 2โ
= 0.
๏ฝ When the equations are linear, we can use Thomasโ
algorithm to solve for a tri-diagonal system of linear
equations.
๏ฝ Otherwise, we use an iterative scheme.