1. Learn
  2. /
  3. Courses
  4. /
  5. Financial Trading in R

Exercise

The SMA and RSI functions

Welcome to the chapter on indicators! An indicator is a transformation of market data that is used to generate signals or filter noise. Indicators form the backbone of many trading systems, and the system you will build in this course uses several of them.

The simple moving average (SMA) and relative strength index (RSI) are two classic indicators. As you saw in Chapter 1, the SMA is an arithmetic moving average of past prices, while the RSI is a bounded oscillating indicator that ranges from 0 to 100. Their respective functions SMA() and RSI() both take in a series of prices, denoted by x and price respectively, and a lookback period n, for example:

SMA(x = Cl(GDX), n = 50)
RSI(price = Cl(GDX), n = 50)

In this exercise, you will practice calling the base functions for these indicators. The quantmod and TTR packages and SPY data have been loaded for you.

Instructions

100 XP
  • Create a 200-day SMA of the closing price of SPY. Call this spy_sma.
  • Create an RSI with a lookback period n of 3 days using the closing price of SPY. Call this spy_rsi.