R语言代写 STA457 Time series analysis assignment

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. Tradingrule

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

∑C 𝑃 𝑓(𝑃,…,𝑃 )=𝑃− BDE #*B.

# #*+,- #

5 In this case, we buy the asset (𝐵# = +1) at time 𝑡 + 1when

∑C 𝑃
𝐹 >0⟺𝑃 > BDE #*B;

and sell the asset (𝐵# = −1) when

∑C 𝑃
𝐹 <0⟺𝑃 < BDE #*B.

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.

##

5

##

5

Copyright © Jen-Wen Lin 2018

2.45

24

2.35

Price

2.25

2.2

Signal

006

006 _
004 _ IN

9 13 17 21 25 29 33 37 II 45 Day

a

5 9 13 17 21 25 29 33 37 41 45 Day

Return oscillator

i A

i‘\ 002 – i 1

i\r,

it\ Logretum t

.402

-004

-036

Sett’, ‘,s1

\ 13:.C,. 1 01/

N,‘0

sax”

i 3.11

I

9 13 17 21 25

Day

33 37 41 45

3

:Simple moving average method 5-days moving average

For your assignment, we consider 𝐹 based on a moving-average technical indicator. In #

= 𝑙𝑛(𝑃 ⁄𝑃

where 𝑋
# ##*- K

Figure 3.1: The simple moving average method.

general, for a given moving-average indicator, 𝐹 may be expressed as (a function of log #

a. price series, b. signal time series, c return oscillator

+*M

returns):

), 𝛿 and 𝑑 are defined by a given trading rule (See Appendix for more details). For this assignment, we assume 𝛿 = 0.

𝐹=𝛿+I𝑑𝑋 , (1)

#
KDE

K #*K

48

Copyright © Jen-Wen Lin 2018

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𝑅# =−𝑋# 𝑖𝑓 𝐵#*- =−1T, 𝑅#=+𝑋# 𝑖𝑓 𝐵#*-=+1

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

W

𝑅U # = I 𝑅 # , V , VD-

where 𝐷 represents the stochastic duration of the position which will last 𝑛 days if {𝐷=𝑛}⇔{𝐵#*- ≠𝐵#,𝐵# =𝐵#,- =⋯=𝐵#,W*-,𝐵#*W,- ≠𝐵#,W }.

4

Copyright © Jen-Wen Lin 2018

5

4. Statisticalpropertiesofrulereturns

Under the assumption that 𝑋# follows a stationary Gaussian process, several statistical properties of rule returns can be derived:

1. Unconditional expected return:

2 𝜇M 𝜇

𝐸(𝑅)=^ 𝜎 ⋅𝑐𝑜𝑟𝑟(𝑋,𝐹 )⋅𝑒𝑥𝑝i− k l+𝜇m1−2𝛷o− kpq, (2) #𝜋b##*-2𝜎M 𝜎

kk
where 𝛷(h) = ∫x t√2𝜋v*- 𝑒𝑥𝑝{−𝑥M/2} 𝑑𝑥, 𝜇 = 𝐸(𝑋 ), 𝜎 = 𝑣𝑎𝑟(𝑋 ), 𝜇 = 𝐸(𝐹 ), and

*y

2. Unconditional variance:

𝑣𝑎𝑟(𝑅 ) = 𝐸(𝑋M) − 𝐸(𝑅)M = 𝜎M + 𝜇M − 𝐸(𝑅 )M. ##bb#

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

2 Intuitively, the longer holding period, the larger the expected return on a trading rule. Copyright © Jen-Wen Lin 2018

𝜎M = 𝑣𝑎𝑟(𝐹 ). k#

k#b#k#

#

𝐻= 𝜋 . (3) 𝑐𝑜𝑠*- 𝜌k(1)

5. Questions

1. Derive the variance of the predictor 𝐹 given in Equation (1). #

Hint: 𝜎M = 𝑣𝑎𝑟(∑+*M 𝑑 𝑋 ). k BDE B #*B

  1. Derive the expectation of the predictor 𝐹 . #

    Hint: 𝜇 = 𝐸(∑+*M 𝑑 𝑋 ). k BDE B #*B

  2. Derive the autocorrelation function at lag one for the predictor. Hint: 𝜌 (1) = 𝑐𝑜𝑟𝑟(𝐹 , 𝐹 ).

6

k # #*-

  1. 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.

    .

  2. 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.
  3. 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.

    .

  4. 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.

  5. (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.

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 𝐻

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

Hint: Use the ratio of the cumulative return over the number of trading periods as the

estimate 𝐸(𝑅#).

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

134 Advanced Trading Rules
Table 1: Return/Price signals equivalence

Table 4.1

Return/price signals equivalence

Return sell signals

dj 􏰍􏰎m􏰃j􏰏􏰎m􏰃j􏰃1􏰏 2

see generalization

dj􏰍1

Xm􏰃2

j􏰍0

dj 􏰍􏰎m􏰃r􏰏􏰎j􏰒 1􏰏

for0􏰉 j􏰉 r􏰃1 dj 􏰍r􏰎m􏰃j􏰃1􏰏

forr􏰉 j􏰉 m􏰃1 m􏰃2

 􏰒 X dj Xt􏰃j ` 0Ywith:

j􏰍0 m􏰃1 m􏰃1 dj 􏰍 􏰃 X ai and  􏰍 X aj

i􏰍j􏰒 1 j􏰍0

Parameter(s)

Price sell signals

Pt `

Xm􏰃1 j􏰍0

ajPt􏰃j

m􏰊2

a j 􏰍 m1

m􏰊2

aj􏰍 m􏰃j 􏰐m􏰎m 􏰃 1􏰏􏰑a2

1 b a b 0 Ym 􏰊 2

aj 􏰍a􏰎1􏰃a􏰏j

m􏰊2

aj 􏰍1forj􏰍m􏰃1Y aj 􏰍0forj6􏰍m􏰃1

r􏰃1 m􏰃1

XbjPt􏰃j `XajPt􏰃j j􏰍0 j􏰍0

mbr􏰊1

bj 􏰍1rY aj 􏰍m1

Xm􏰃1

ajPt􏰃j `0 j􏰍0

Rule
Simple order

Simple MA

Weighted MA

Exponential MA

Momentum

Double orders

Double MA

Generalization

Xm􏰃2 j􏰍0

djXt􏰃j `0 dj 􏰍􏰎m􏰃j􏰃1􏰏

djXt􏰃j `0

for the simple and weighted moving average rule.4 As can be seen from Tables 4.2 and 4.3, signals are di􏰁erent in less than 3 per cent of all cases for simulated series. The largest deviation comes from ” 􏰍 0.001 and ‘ 􏰍 0.03, for m 􏰍 200. Even then, returns signals are identical to price signals in at least 97 per cent of all cases. This case represents an upper bound in terms of both volatility and average returns over ten years for ®nancial series (Taylor, 1986: Tables 3 and 4, pp. 33 and 34). On the basis of the empirical results presented in Tables 4.2 and 4.3, one can safely conclude that return signals lead to essentially the same investment strategies as price signals for values of m as large as 200.

CopyrighTt ©heJen-Wicen fLeina2t0u1r8e of the linear rules, expressed by a linear combination of returns, is that it includes in a uni®ed framework autoregressive predictors (by construction) and technical systems (by reformulation). Finally, it must