STA457 Time Series Analysis Assignment

R Language Assignment Question

Statistical properties of (moving-average) rule returns

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.

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.













006 _
004 _ IN

9 13 17 21 25 29 33 37 II 45 Day


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

Return oscillator

i A

iโ€˜\ 002 โ€“ i 1


it\ Logretum t





Settโ€™, โ€˜,s1

\ 13:.C,. 1 01/



i 3.11


9 13 17 21 25

33 37 41 45


: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



), ๐›ฟ and ๐‘‘ are defined by a given trading rule (See Appendix for more details). For this assignment, we assume ๐›ฟ = 0.

๐น=๐›ฟ+I๐‘‘๐‘‹ , (1)


K #*K


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


๐‘…U # = I ๐‘… # , V , VD-

where ๐ท represents the stochastic duration of the position which will last ๐‘› days if {'{๐ท=๐‘›}'}โ‡”{'{๐ต#*- โ‰ ๐ต#,๐ต# =๐ต#,- =โ‹ฏ=๐ต#,W*-,๐ต#*W,- โ‰ ๐ต#,W }'}.



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 ๐œ‡M ๐œ‡

๐ธ(๐‘…)=^ ๐œŽ โ‹…๐‘๐‘œ๐‘Ÿ๐‘Ÿ(๐‘‹,๐น )โ‹…๐‘’๐‘ฅ๐‘iโˆ’ k l+๐œ‡m1โˆ’2๐›ทoโˆ’ kpq, (2) #๐œ‹b##*-2๐œŽM ๐œŽ

where ๐›ท(h) = โˆซx tโˆš{'2๐œ‹v*- ๐‘’๐‘ฅ๐‘{โˆ’๐‘ฅM/2}'} ๐‘‘๐‘ฅ, ๐œ‡ = ๐ธ(๐‘‹ ), ๐œŽ = ๐‘ฃ๐‘Ž๐‘Ÿ(๐‘‹ ), ๐œ‡ = ๐ธ(๐น ), and


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.

๐œŽM = ๐‘ฃ๐‘Ž๐‘Ÿ(๐น ). k#




๐ป= ๐œ‹ . (3) ๐‘๐‘œ๐‘ *- ๐œŒk(1)

5. Questions

1. Derive the variance of the predictor ๐น given in Equation (1). #

Hint: ๐œŽM = ๐‘ฃ๐‘Ž๐‘Ÿ(โˆ‘+*M ๐‘‘ ๐‘‹ ). k BDE B #*B

2. Derive the expectation of the predictor ๐น . #

Hint: ๐œ‡ = ๐ธ(โˆ‘+*M ๐‘‘ ๐‘‹ ). k BDE B #*B

3. Derive the autocorrelation function at lag one for the predictor. Hint: ๐œŒ (1) = ๐‘๐‘œ๐‘Ÿ๐‘Ÿ(๐น , ๐น ).


k # #*-

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.

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.

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.

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.

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 ๐ธ(๐‘…#).

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.

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 ๔ฐ๔ฐŽ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


Price sell signals

Pt `

Xm๔ฐƒ1 j๔ฐ0



a j ๔ฐ m1


aj๔ฐ m๔ฐƒj ๔ฐm๔ฐŽm ๔ฐƒ 1๔ฐ๔ฐ‘a2

1 b a b 0 Ym ๔ฐŠ 2

aj ๔ฐa๔ฐŽ1๔ฐƒa๔ฐj


aj ๔ฐ1forj๔ฐm๔ฐƒ1Y aj ๔ฐ0forj6๔ฐm๔ฐƒ1

r๔ฐƒ1 m๔ฐƒ1

XbjPt๔ฐƒj `XajPt๔ฐƒj j๔ฐ0 j๔ฐ0


bj ๔ฐ1rY aj ๔ฐm1


ajPt๔ฐƒj `0 j๔ฐ0

Simple order

Simple MA

Weighted MA

Exponential MA


Double orders

Double MA


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.