Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Challenge: Analyze OHLC Patterns | Financial Data Analysis with Python
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.

Tarefa

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.

Solução

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 5
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

close

bookChallenge: Analyze OHLC Patterns

Deslize para mostrar o 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.

Tarefa

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.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 5
single

single

some-alt