Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Detect Trend Reversals with Moving Averages | Financial Data Visualization and Trend Analysis
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python for Financial Analysts

bookChallenge: Detect Trend Reversals with Moving Averages

Moving averages are powerful tools in financial analysis, especially when it comes to identifying trends and potential reversals in stock prices. By smoothing out short-term fluctuations, moving averages help you focus on the underlying direction of a price series. Two of the most commonly used moving averages are the 50-day and 200-day simple moving averages (SMAs). When the shorter-term 50-day SMA crosses above the longer-term 200-day SMA, this is known as a golden cross and is often interpreted as a bullish signal, suggesting a potential upward trend. Conversely, when the 50-day SMA crosses below the 200-day SMA, it forms a death cross, which can signal a bearish reversal or downward trend. Detecting these crossover events can be crucial for making informed trading decisions.

12345678910
import pandas as pd import numpy as np # Create a DataFrame with two years of daily closing prices np.random.seed(42) dates = pd.date_range(start="2022-01-01", end="2023-12-31", freq="B") # Business days price = np.cumsum(np.random.randn(len(dates)) * 2 + 0.1) + 100 # Simulated price path df = pd.DataFrame({"Close": price}, index=dates) print(df.head())
copy

To tackle this challenge, start by calculating the 50-day and 200-day simple moving averages for the closing prices in your DataFrame. You can use the rolling method in pandas to compute these averages efficiently. Once you have the moving averages, plot the closing price together with both moving averages on a single chart to visualize trends and potential reversals. Next, identify the dates where the 50-day moving average crosses above the 200-day moving average (golden cross) and where it crosses below (death cross). Print out these crossover dates to highlight the key moments of trend reversals in the data.

Task

Swipe to start coding

Given a DataFrame containing daily closing prices for a stock over two years, your task is to analyze trend reversals using moving averages.

  • Calculate the 50-day simple moving average and the 200-day simple moving average for the "Close" column.
  • Plot the closing price, the 50-day SMA, and the 200-day SMA together on a single chart.
  • Identify the dates when the 50-day SMA crosses above the 200-day SMA (golden cross).
  • Identify the dates when the 50-day SMA crosses below the 200-day SMA (death cross).
  • Print the golden cross and death cross dates.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 7
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

How do I calculate the 50-day and 200-day moving averages in pandas?

Can you show me how to plot the closing price with both moving averages?

How can I detect and print the golden cross and death cross dates?

close

bookChallenge: Detect Trend Reversals with Moving Averages

Swipe to show menu

Moving averages are powerful tools in financial analysis, especially when it comes to identifying trends and potential reversals in stock prices. By smoothing out short-term fluctuations, moving averages help you focus on the underlying direction of a price series. Two of the most commonly used moving averages are the 50-day and 200-day simple moving averages (SMAs). When the shorter-term 50-day SMA crosses above the longer-term 200-day SMA, this is known as a golden cross and is often interpreted as a bullish signal, suggesting a potential upward trend. Conversely, when the 50-day SMA crosses below the 200-day SMA, it forms a death cross, which can signal a bearish reversal or downward trend. Detecting these crossover events can be crucial for making informed trading decisions.

12345678910
import pandas as pd import numpy as np # Create a DataFrame with two years of daily closing prices np.random.seed(42) dates = pd.date_range(start="2022-01-01", end="2023-12-31", freq="B") # Business days price = np.cumsum(np.random.randn(len(dates)) * 2 + 0.1) + 100 # Simulated price path df = pd.DataFrame({"Close": price}, index=dates) print(df.head())
copy

To tackle this challenge, start by calculating the 50-day and 200-day simple moving averages for the closing prices in your DataFrame. You can use the rolling method in pandas to compute these averages efficiently. Once you have the moving averages, plot the closing price together with both moving averages on a single chart to visualize trends and potential reversals. Next, identify the dates where the 50-day moving average crosses above the 200-day moving average (golden cross) and where it crosses below (death cross). Print out these crossover dates to highlight the key moments of trend reversals in the data.

Task

Swipe to start coding

Given a DataFrame containing daily closing prices for a stock over two years, your task is to analyze trend reversals using moving averages.

  • Calculate the 50-day simple moving average and the 200-day simple moving average for the "Close" column.
  • Plot the closing price, the 50-day SMA, and the 200-day SMA together on a single chart.
  • Identify the dates when the 50-day SMA crosses above the 200-day SMA (golden cross).
  • Identify the dates when the 50-day SMA crosses below the 200-day SMA (death cross).
  • Print the golden cross and death cross dates.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 7
single

single

some-alt