Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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.

Opgave

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.

Løsning

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 7
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

close

bookChallenge: Detect Trend Reversals with Moving Averages

Stryg for at vise menuen

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.

Opgave

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.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 7
single

single

some-alt