Get startedGet started for free

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.

This exercise is part of the course

Financial Trading in R

View Course

Exercise instructions

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

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create a 200-day SMA
spy_sma <- SMA(___)

# Create an RSI with a 3-day lookback period
spy_rsi <- RSI(___)
Edit and Run Code