Assignment (Rule trading v2)
Copyright © Jen-Wen Lin 2018
1
STA457 Time series analysis assignment (Fall 2018)
Statistical properties of (moving-average) rule returns
Date: 23 October 2018
Jen-Wen Lin, PhD, CFA
1. Introduction
Technical indicator is widely used to generate trading signals by practitioners to make
trading decisions. The usual rule is to trade with the trend. In this case, the trader initiates a
position early in the trend and maintains that position as long as the trend continues.
In this assignment, you are asked to study the statistical properties of returns for applying
the oldest and most widely used method in technical indicators—moving averages.1
The structure of this paper is given as follows. Section 2 defines the trading rule (or
strategy). In Section 3 and 4, we formulate the trading return based on a given trading rule and
state the corresponding statistical properties, respectively. The questions for you to answer are
listed in Section 5. Finally, references and appendix are given in Section 6 and 7, respectively.
2. Trading rule
Suppose that at each time 𝑡, market participants predict the direction of the trend of asset
prices using a price-based forecast 𝐹#, where 𝐹# is a function of past asset prices
𝐹# = 𝑓(𝑃#, … , 𝑃#*+,-, … ).
1 The simplest rule of this family is the single moving average which says when the rate penetrates from below
(above) a moving average of a given length, a buy (sell) signal is generated.
Copyright © Jen-Wen Lin 2018
2
The above predictor is then converted to buy and sell trading signals 𝐵#: buy (+1) and sell (-1)
using, i.e.
{
“𝑆𝑒𝑙𝑙” ⇔ 𝐵# = −1 ⇔ 𝐹# < 0
"𝐵𝑢𝑦" ⇔ 𝐵# = +1 ⇔ 𝐹# > 0
Note that the signal of a trading rule is completely defined by one of the inequalities giving a
sell or buy order (if the position is not short, it is long).
For example, consider a trading rule based on the moving average of order five rule (𝑚 =
5). In this case, 𝑓 is given by
𝑓(𝑃#,… , 𝑃#*+,-) = 𝑃# −
∑ 𝑃#*BCBDE
5
.
In this case, we buy the asset (𝐵# = +1) at time 𝑡 + 1when
𝐹# > 0 ⟺ 𝑃# >
∑ 𝑃#*BCBDE
5
;
and sell the asset (𝐵# = −1) when
𝐹# < 0 ⟺ 𝑃# <
∑ 𝑃#*BCBDE
5
.
The Figure below illustrates the dynamics of the above 5-periods moving average method—
when the rate penetrates from below (above) the moving average of order five, a buy (sell)
signal is generated.
Copyright © Jen-Wen Lin 2018
3
For your assignment, we consider 𝐹# based on a moving-average technical indicator. In
general, for a given moving-average indicator, 𝐹# may be expressed as (a function of log
returns):
𝐹# = 𝛿 + I 𝑑K𝑋#*K
+*M
KDE
, (1)
where 𝑋# = 𝑙𝑛(𝑃# 𝑃#*-⁄ ), 𝛿 and 𝑑K are defined by a given trading rule (See Appendix for more
details). For this assignment, we assume 𝛿 = 0.
2.45
2 4
2.35
Price
2.25
2.2
Signal
13 17 21 25
33
37
41
45
Day
Return oscillator
•
Sett',
‘,s1
A
\
\
006
006 _
i
004
002
_ I N\r,
ii
i
- i
‘
1
Logretum i
t
t
\ 13:.C., 1
/0
N
1
, ‘ 0
sax" i
3.11
.402
-004
-036 I
9
:Simple moving average method
5-days moving average
9 13 17 21 25 29 33 37 II 45
Day
a
29 33 37 41 455 9 13 17 21 25
Day
Figure 3.1: The simple moving average method.
a. price series, b. signal time series, c return oscillator
48
Copyright © Jen-Wen Lin 2018
4
3. Rule returns
For the period [𝑡 − 1, 𝑡), a trader following a given technical rule establishes a position (long
or short) at time 𝑡 − 1, 𝐵#*-. The returns at time t made by applying such a decision rule is
called “ruled returns” and denoted as 𝑅#. Their value can be expressed as
𝑅# = 𝐵#*-𝑋# ⇔ R
𝑅# = −𝑋# 𝑖𝑓 𝐵#*- = −1
𝑅# = +𝑋# 𝑖𝑓 𝐵#*- = +1
T,
where 𝑋# = 𝑙𝑛(𝑃# 𝑃#*-⁄ ) denote the logarithm return over this period (assume no dividend
payout during period 𝑡).
Remark: 𝑅# is unconditional and unrealized returns. By unrealized we mean that rule returns
are recorded every day even if the position is neither closed nor reversed, but simply carries on.
Remark: We may define the realized returns as
𝑅U# = I𝑅#,V
W
VD-
,
where 𝐷 represents the stochastic duration of the position which will last 𝑛 days if
{𝐷 = 𝑛} ⇔ {𝐵#*- ≠ 𝐵#, 𝐵# = 𝐵#,- = ⋯ = 𝐵#,W*-, 𝐵#*W,- ≠ 𝐵#,W } .
Copyright © Jen-Wen Lin 2018
5
4. Statistical properties of rule returns
Under the assumption that 𝑋# follows a stationary Gaussian process, several statistical
properties of rule returns can be derived:
1. Unconditional expected return:
𝐸(𝑅#) = ^
2
𝜋
𝜎b ⋅ 𝑐𝑜𝑟𝑟(𝑋#, 𝐹#*-) ⋅ 𝑒𝑥𝑝 i−
𝜇kM
2𝜎k
Ml + 𝜇 m1 − 2𝛷 o−
𝜇k
𝜎k
pq, (2)
where 𝛷(ℎ) = ∫ t√2𝜋v
*-
𝑒𝑥𝑝{−𝑥M/2}𝑑𝑥
x
*y , 𝜇k = 𝐸(𝑋#), 𝜎b = 𝑣𝑎𝑟(𝑋#), 𝜇k = 𝐸(𝐹#), and
𝜎kM = 𝑣𝑎𝑟(𝐹#).
2. Unconditional variance:
𝑣𝑎𝑟(𝑅#) = 𝐸(𝑋#M) − 𝐸(𝑅)M = 𝜎bM + 𝜇bM − 𝐸(𝑅#)M.
Additionally, Kedem (1986) shows that the expected zero crossing rate for a stationary
process as the expected zero-crossing rate for a discrete-time, zero-mean, stationary Gaussian
sequence 𝑍# is given by
1
𝜋
𝑐𝑜𝑠*- 𝜌�(1),
where 𝜌�(1) denotes the autocorrelation function of {𝑍#} at lag one. Using the same
assumption, we can show that 𝐹# is stationary. Using this result, we may approximate the
expected length of the holding period2 for a given trading rule as
𝐻 =
𝜋
𝑐𝑜𝑠*- 𝜌k(1)
. (3)
2 Intuitively, the longer holding period, the larger the expected return on a trading rule.
Copyright © Jen-Wen Lin 2018
6
5. Questions
1. Derive the variance of the predictor 𝐹# given in Equation (1).
Hint: 𝜎kM = 𝑣𝑎𝑟(∑ 𝑑B𝑋#*B+*MBDE ).
2. Derive the expectation of the predictor 𝐹#.
Hint: 𝜇k = 𝐸(∑ 𝑑B𝑋#*B+*MBDE ).
3. Derive the autocorrelation function at lag one for the predictor.
Hint: 𝜌k(1) = 𝑐𝑜𝑟𝑟(𝐹#, 𝐹#*-).
4. Write a R function to calculate the expectation of the rule return for a given double MA
trading rule (See Appendix) and the expected length of the holding period.
Hint: Given asset price time series and a pair of integers, 𝑚 and 𝑟 (function arguments),
your function calculates the expected rule return 𝐸(𝑅#) and the expected length of holding
periods 𝐻.
5. Use a R function to download daily, weekly S&P500 index from Oct/01/2009 to
Sep/30/2018 from yahoo finance
Hint: adjusted Close and R quantmod library.
6. Write a R function to choose the optimal daily and weekly double MA trading rules (that
maximize the expected rule returns) for S&P500 index.
Hint: Find the 𝑚 and 𝑟 pair that has the highest 𝐸(𝑅#). For simplicity, let the maximum
values of 𝑚 be 250 and 52 for daily and weekly data, respectively.
7. Write a R function to calculate the in-sample trading statistics (cumulative return and
holding time) of your choice and compare them with your theoretical results.
Hint: Use the ratio of the cumulative return over the number of trading periods as the
estimate 𝐸(𝑅#).
8. (Optional) Run and back-test your daily trading rule using six months of rolling window.
Show the empirical trading statistics and show the difference between the theoretical
results.
Copyright © Jen-Wen Lin 2018
7
6. Reference
1. Acar, E. (1993). Economic evaluation of financial forecasting. (Unpublished Doctoral thesis,
City University London.)
2. Acar E. (200?), “Advanced trading rule”, Second edition. (Chapter 4. Expected returns of
directional forecasters).
3. Kedem (1986), “Spectral analysis and discrimination by zero-crossings”, Proceedings of IEEE,
Vol 74, No. 11, page 1477-1493.
Copyright © Jen-Wen Lin 2018
8
7. Appendix
Table 1: Return/Price signals equivalence
!"# $%& '()*+& ,-. /&(0%$&. )"1(-0 ,1&#,0& #2+&34 5' 6,- 7& '&&- !#")
8,7+&' 439 ,-. 43:; '(0-,+' ,#& .(<&#&-$ (- +&'' $%,- : *&# 6&-$ "! ,++ 6,'&' !"#
'()2+,$&. '&#(&'3 8%& +,#0&'$ .&1(,$("- 6")&' !#") ! ! =3==> ,-. ” ! =3=:;
!”# ! ! 9==3 ?1&- $%&-; #&$2#-‘ ‘(0-,+’ ,#& (.&-$(6,+ $” *#(6& ‘(0-,+’ (- ,$ +&,’$
@A *&# 6&-$ “! ,++ 6,’&’3 8%(‘ 6,’& #&*#&’&-$’ ,- 2**&# 7″2-. (- $&#)’ “! 7″$%
1″+,$(+($B ,-. ,1&#,0& #&$2#-‘ “1&# $&- B&,#’ !”# C-,-6(,+ ‘&#(&’ D8,B+”#; >@EFG
8,7+&’ : ,-. 4; **3 :: ,-. :4H3 I- $%& 7,'(‘ “! $%& &)*(#(6,+ #&’2+$’ *#&’&-$&.
(- 8,7+&’ 439 ,-. 43:; “-& 6,- ‘,!&+B 6″-6+2.& $%,$ #&$2#- ‘(0-,+’ +&,. $”
&”&-$(,++B $%& ‘,)& (-1&’$)&-$ ‘$#,$&0(&’ ,’ *#(6& ‘(0-,+’ !”# 1,+2&’ “! ! ,’
+,#0& ,’ 9==3
8%& -(6& !&,$2#& “! $%& +(-&,# #2+&’; &J*#&”&. 7B , +(-&,# 6″)7(-,$(“- “!
#&$2#-‘; (‘ $%,$ ($ (-6+2.&’ (- , 2-(C&. !#,)&/”#K ,2$”#&0#&”(1& *#&.(6$”#’
D7B 6″-‘$#26$(“-H ,-. $&6%-(6,+ ‘B’$&)’ D7B #&!”#)2+,$(“-H3 L(-,++B; ($ )2’$
7& &)*%,'(M&. $%,$; ,+$%”20% #,$%&# 0&-&#,+; +(-&,# #2+&’ .” -“$ 6″1&# ,++
$&6%-(6,+ #2+&’ 2’&. 7B *#,6$($(“-&#’3
!”#$% &'( “#$%&'()&*+# ,*-‘./, #0%*1./#’+#
N2+& O,#,)&$&#D’H O#(6& ‘&++ ‘(0-,+’ N&$2#- ‘&++ ‘(0-,+’
P()*+& “#.&# 2$ #
!
!”>
3!=
.32$”3
!
!”9
3!=
435$”3 # =
P()*+& Q5 ! # 9 .3 !
>
!
43 ! $!” 3 ” >%
R&(0%$&. Q5 ! # 9 .3 !
!” 3
&!$!” >%’$9
43 !
$!” 3%$!” 3 ” >%
9
?J*”-&-$(,+ Q5 > % . % =&! # 9 .3 ! .$>” .% 3 ‘&& 0&-&#,+(M,$(“-
Q”)&-$2) ! # 9
.3 ! > !”# 3 ! !” >&
.3 ! = !”# 3 (! !” >
43 ! >
S”27+& “#.&#’
!
&”>
3!=
632$”3 #
!
!”>
3!=
.32$”3
!
!”9
3!=
435$”3 # =
S”27+& Q5 ! % & # > 63 !
>
&
& .3 !
>
!
43 ! $!” &%$ 3 ) >%
!”# = * 3 * &” >
43 ! &$!” 3 ” >%
!”# & * 3 * !” >
T&-&#,+(M,$(“-
!
!”>
3!=
.32$”3 # = ‘ )
!
!”9
3!=
435$”3 # =&/($%G
43 ! ”
!
!”>
*!3) >
.* ,-. ‘ !
!
!”>
3!=
.3
>:4 741.’+#4 8&.4*’- “%/#,