Get Started

Calculate the ADX

The average directional movement index (ADX) was developed by J. Welles Wilder as an indicator of trend strength. It combines two other indicators, the plus directional index (+DI) and minus directional indicator (-DI), and is obtained using lengthy calculations. However, with Python, you can calculate it with one line of code. In this exercise, you will implement your first ADX indicator using daily price data of the Tesla stock.

The historical daily price data has been loaded in stock_data. Also, talib has been imported for you.

This is a part of the course

“Financial Trading in Python”

View Course

Exercise instructions

  • Calculate the ADX using the appropriate function from talib, and the High, Low and Close columns in the stock_data. Save it in a new column ADX_14.
  • Calculate the ADX . This time change the default time period to 21, and save it in a new column ADX_21.
  • Print the last five rows of stock_data.

Hands-on interactive exercise

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

# Calculate the ADX with the default time period
stock_data['ADX_14'] = ____(stock_data['____'],
                            stock_data['____'], 
                            stock_data['____'])

# Calculate the ADX with the time period set to 21
stock_data['ADX_21'] = ____

# Print the last five rows
print(stock_data.____())

This exercise is part of the course

Financial Trading in Python

IntermediateSkill Level
4.3+
17 reviews

Learn to implement custom trading strategies in Python, backtest them, and evaluate their performance!

Let's dive into the world of technical indicators—a useful tool for constructing trading signals and building strategies. You’ll get familiar with the three main indicator groups, including moving averages, ADX, RSI, and Bollinger Bands. By the end of this chapter, you’ll be able to calculate, plot, and understand the implications of indicators in Python.

Exercise 1: Trend indicator MAsExercise 2: Calculate and plot two EMAsExercise 3: SMA vs. EMAExercise 4: Strength indicator: ADXExercise 5: Understand the ADXExercise 6: Calculate the ADX
Exercise 7: Visualize the ADXExercise 8: Momentum indicator: RSIExercise 9: Understand the RSIExercise 10: Calculate the RSIExercise 11: Visualize the RSIExercise 12: Volatility indicator: Bollinger BandsExercise 13: Understand Bollinger BandsExercise 14: Implement Bollinger Bands

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free