Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Analyze OHLC Patterns | Financial Data Analysis with Python
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python for Traders

bookChallenge: Analyze OHLC Patterns

In this challenge, you will work with a small, hardcoded pandas DataFrame representing OHLC (Open, High, Low, Close) data for a stock over seven consecutive trading days. Your goal is to analyze the data to uncover specific price patterns that are of interest to traders.

Specifically, you will:

  • Calculate the daily range for each day;
  • Identify all days where the closing price was higher than both the open and the previous day's close.

This kind of analysis can help you spot bullish momentum in price action, which is a core skill for any trader.

123456789101112131415161718192021222324252627282930313233
import pandas as pd # Create a sample OHLC DataFrame for 7 days data = { "Date": [ "2024-06-01", "2024-06-02", "2024-06-03", "2024-06-04", "2024-06-05", "2024-06-06", "2024-06-07" ], "Open": [100, 102, 101, 103, 104, 105, 104], "High": [103, 104, 103, 105, 106, 107, 106], "Low": [99, 101, 100, 102, 103, 104, 103], "Close": [102, 103, 102, 104, 105, 106, 105] } df = pd.DataFrame(data) df["Date"] = pd.to_datetime(df["Date"]) df.set_index("Date", inplace=True) # Calculate daily range (High - Low) df["Range"] = df["High"] - df["Low"] # Identify days where Close > Open and Close > previous day's Close df["Prev_Close"] = df["Close"].shift(1) mask = (df["Close"] > df["Open"]) & (df["Close"] > df["Prev_Close"]) # Output the dates matching the pattern bullish_days = df[mask].index.strftime("%Y-%m-%d").tolist() print("Dates where Close > Open and Close > previous Close:") print(bullish_days)
copy

By running this code, you will see which dates meet the specified criteria. Calculating the daily range gives you a sense of volatility, while identifying days with strong closes compared to both the open and the previous close highlights persistent upward momentum. This approach is frequently used to spot short-term bullish signals in trading.

Task

Swipe to start coding

Create a pandas DataFrame containing OHLC data for 7 consecutive days with the following columns: Date, Open, High, Low, and Close. The dates should be in order and the data should be hardcoded, not loaded from a file or the internet.

Then, for each day:

  • Calculate the daily range (high minus low) and add it as a new column called Range.
  • Identify all days where the closing price is higher than both the open and the previous day's close.
  • Output a list of strings with the dates (in YYYY-MM-DD format) that match this pattern.

Your code should:

  • Use only the pandas library for data manipulation;
  • Print the list of dates that meet the criteria.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 5
single

single

Ask AI

expand

Ask AI

ChatGPT

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

close

bookChallenge: Analyze OHLC Patterns

Swipe to show menu

In this challenge, you will work with a small, hardcoded pandas DataFrame representing OHLC (Open, High, Low, Close) data for a stock over seven consecutive trading days. Your goal is to analyze the data to uncover specific price patterns that are of interest to traders.

Specifically, you will:

  • Calculate the daily range for each day;
  • Identify all days where the closing price was higher than both the open and the previous day's close.

This kind of analysis can help you spot bullish momentum in price action, which is a core skill for any trader.

123456789101112131415161718192021222324252627282930313233
import pandas as pd # Create a sample OHLC DataFrame for 7 days data = { "Date": [ "2024-06-01", "2024-06-02", "2024-06-03", "2024-06-04", "2024-06-05", "2024-06-06", "2024-06-07" ], "Open": [100, 102, 101, 103, 104, 105, 104], "High": [103, 104, 103, 105, 106, 107, 106], "Low": [99, 101, 100, 102, 103, 104, 103], "Close": [102, 103, 102, 104, 105, 106, 105] } df = pd.DataFrame(data) df["Date"] = pd.to_datetime(df["Date"]) df.set_index("Date", inplace=True) # Calculate daily range (High - Low) df["Range"] = df["High"] - df["Low"] # Identify days where Close > Open and Close > previous day's Close df["Prev_Close"] = df["Close"].shift(1) mask = (df["Close"] > df["Open"]) & (df["Close"] > df["Prev_Close"]) # Output the dates matching the pattern bullish_days = df[mask].index.strftime("%Y-%m-%d").tolist() print("Dates where Close > Open and Close > previous Close:") print(bullish_days)
copy

By running this code, you will see which dates meet the specified criteria. Calculating the daily range gives you a sense of volatility, while identifying days with strong closes compared to both the open and the previous close highlights persistent upward momentum. This approach is frequently used to spot short-term bullish signals in trading.

Task

Swipe to start coding

Create a pandas DataFrame containing OHLC data for 7 consecutive days with the following columns: Date, Open, High, Low, and Close. The dates should be in order and the data should be hardcoded, not loaded from a file or the internet.

Then, for each day:

  • Calculate the daily range (high minus low) and add it as a new column called Range.
  • Identify all days where the closing price is higher than both the open and the previous day's close.
  • Output a list of strings with the dates (in YYYY-MM-DD format) that match this pattern.

Your code should:

  • Use only the pandas library for data manipulation;
  • Print the list of dates that meet the criteria.

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Β 1. ChapterΒ 5
single

single

some-alt