Exercise

Create moving average and RSI features

We want to add historical data to our machine learning models to make better predictions, but adding lots of historical time steps is tricky. Instead, we can condense information from previous points into a single timestep with indicators.

A moving average is one of the simplest indicators - it's the average of previous data points. This is the function talib.SMA() from the TAlib library.

Another common technical indicator is the relative strength index (RSI). This is defined by:

\(RSI = 100 - \frac{100} {1 + RS}\)

\(RS = \frac{\text{average gain over } n \text{ periods}} {\text{average loss over } n \text{ periods}}\)

The n periods is set in talib.RSI() as the timeperiod argument.

A common period for RSI is 14, so we'll use that as one setting in our calculations.

Instructions

100 XP
  • Create a list of feature names (start with a list containing only '5d_close_pct').
  • Use timeperiods of 14, 30, 50, and 200 to calculate moving averages with talib.SMA() from adjusted close prices (lng_df['Adj_Close']).
  • Normalize the moving averages with the adjusted close by dividing by Adj_Close.
  • Within the loop, calculate RSI with talib.RSI() from Adj_Close and using n for the timeperiod.