Get startedGet started for free

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.

This exercise is part of the course

Machine Learning for Finance in Python

View Course

Exercise instructions

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

Hands-on interactive exercise

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

feature_names = ____  # a list of the feature names for later

# Create moving averages and rsi for timeperiods of 14, 30, 50, and 200
for n in [____]:

    # Create the moving average indicator and divide by Adj_Close
    lng_df['ma' + str(n)] = talib.SMA(lng_df['Adj_Close'].values,
                              timeperiod=n) / lng_df[____]
    # Create the RSI indicator
    lng_df['rsi' + str(n)] = talib.____(lng_df['Adj_Close'].____, timeperiod=____)
    
    # Add rsi and moving average to the feature name list
    feature_names = feature_names + ['ma' + str(n), 'rsi' + str(n)]

print(feature_names)
Edit and Run Code