Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 57

Analyzing Correlated Assets

Recommended resources

Hello! Welcome to the next lesson on your journey to building automated ICT trading systems.

In our last session, you learned how to programmatically isolate trading activity within specific ICT Killzones. This gave you the ability to tell your script when to look for opportunities. Today, we'll focus on teaching your script what to look at.

This lesson directly addresses the learning outcome: Fetch data for two correlated assets to analyze their price relationship. This skill is the essential first step toward automating one of the most powerful confirmation tools in the ICT arsenal: the Smart Money Technique (SMT) divergence.

The "Why": Correlation and SMT Divergence

In trading, no asset moves in a vacuum. Certain assets, especially in the Forex market, have a strong, predictable relationship with one another. ICT traders leverage these relationships to gain an edge.

A classic example is the inverse correlation between the US Dollar Index (DXY) and major currency pairs like EUR/USD. When the dollar strengthens (DXY goes up), EUR/USD typically falls, and vice versa.

USDX and EURUSD Inverse Correlation
An illustration of the strong inverse correlation between the US Dollar Index (USDX) and the EUR/USD pair on a weekly chart. Notice how their major turning points often mirror each other.

ICT takes this a step further by looking for small, temporary breakdowns in these correlations, known as SMT Divergence. This occurs when one asset fails to confirm the high or low made by its correlated counterpart, signaling a potential reversal.

SMT Divergence Illustration with GBP/USD and EUR/USD
A clear example of SMT Divergence. While GBP/USD makes a lower high, the correlated EUR/USD pair makes a higher high. An ICT trader sees this "crack" in the correlation as a sign of weakness and a potential opportunity to sell.

To build a bot that can spot these divergences, our first task is to fetch the price data for two related assets and numerically measure their relationship. That's our focus for today.


1. Understanding and Quantifying Correlation

Correlation measures the statistical relationship between two variables—in our case, the prices of two different assets. It is expressed as a correlation coefficient, a value between -1 and +1.

  • +1: Perfect positive correlation. The assets move in the same direction.
  • 0: No correlation. The assets' movements are random relative to each other.
  • -1: Perfect negative (or inverse) correlation. The assets move in opposite directions.

For a deeper understanding of the correlation coefficient, the following article provides an excellent foundation.

Python Correlation - A Practical Guide

The article 'Python Correlation - A Practical Guide' from Algotrading101 provides a clear definition of correlation in financial markets and explains the correlation coefficient.

Please read the first two sections, 'What is correlation?' and 'What is a correlation coefficient?'. Focus on understanding the -1 to +1 scale.


2. Fetching Market Data with the yfinance Library

To get the price data into our Python script, we will use the yfinance library. It's a popular, free, and straightforward tool for downloading historical market data from Yahoo Finance.

A crucial detail is knowing the correct "ticker" symbol for the asset you want. For stocks, it's simple (e.g., AAPL for Apple). For Forex and Futures, it's slightly different:

  • Forex: EURUSD=X, GBPUSD=X
  • Indices/Futures: ES=F (S&P 500 E-mini), NQ=F (Nasdaq 100 E-mini)
  • Indexes: ^GSPC (S&P 500 Index), DX-Y.NYB (US Dollar Index)

Let's watch a video that shows how to fetch data for multiple assets and combine it into a single pandas DataFrame.

Free Stock Prices in Python Made Easy | yFinance

The video 'Free Stock Prices in Python Made Easy | yFinance' by Ryan O'Connell gives a very clean and practical demonstration of using yfinance to download data for a list of tickers.

Watch from the beginning to 05:28. Pay close attention to: How to import the necessary libraries. How to create a list of tickers. The use of yf.download() to fetch data for all tickers at once. How the data is structured in the final DataFrame.


3. Building the Python Script Step-by-Step

Now, let's put it all together to fetch data for two correlated Forex pairs: EUR/USD and GBP/USD.

Step 3.1: Import Libraries and Define Parameters

First, we import the libraries we need and define which assets and time period we're interested in.

import yfinance as yf
import pandas as pd

# Define the tickers for the correlated assets
# For SMT analysis, you could use ['EURUSD=X', 'GBPUSD=X'] or ['EURUSD=X', 'DX-Y.NYB']
tickers = ['EURUSD=X', 'GBPUSD=X']

# Define the time period for the data
start_date = '2023-01-01'
end_date = '2023-12-31'

Step 3.2: Download the Data

Using yf.download(), we pass our list of tickers. yfinance is smart enough to fetch all of them and align them by date in a single DataFrame.

# Download the historical data
data = yf.download(tickers, start=start_date, end=end_date)

print("--- Raw Downloaded Data (first 5 rows) ---")
print(data.head())

When you run this, you'll notice the columns have multiple levels (e.g., 'Open', 'High', 'Low', 'Close' for each ticker). This is a MultiIndex DataFrame. For our analysis, we only need the closing prices.

Step 3.3: Isolate Close Prices and Handle Missing Data

Let's simplify our DataFrame to only contain the 'Close' price for each asset. We also use .dropna() to remove any rows where data might be missing for one of the assets.

# Select only the 'Close' prices
close_prices = data['Close']

# Remove any rows with missing values
close_prices = close_prices.dropna()

print("\n--- Cleaned Close Prices (first 5 rows) ---")
print(close_prices.head())

Step 3.4: Calculate Percentage Returns

This is a critical step. Analyzing the correlation of raw prices can be misleading because most assets drift up over time (a common trend). By calculating the daily percentage change (pct_change), we normalize the data and compare their daily movements, which gives a much more accurate correlation measure.

Python Correlation - A Practical Guide

Let's revisit the Algotrading101 article to understand exactly why we use returns instead of prices. This is a very important concept in quantitative analysis.

Read the section titled 'Correlation of returns versus prices'. The explanation here is key to building robust analysis.

Now, let's apply it in our code:

# Calculate the daily percentage returns
returns = close_prices.pct_change().dropna()

print("\n--- Daily Percentage Returns (first 5 rows) ---")
print(returns.head())

Step 3.5: Calculate the Correlation

Finally, with our prepared returns DataFrame, calculating the correlation is a single command in pandas: .corr(). This generates a correlation matrix.

# Calculate the correlation matrix
correlation_matrix = returns.corr()

print("\n--- Correlation Matrix ---")
print(correlation_matrix)

The resulting matrix shows the correlation coefficient for every pair of assets in the DataFrame. The diagonal will always be 1.0, as an asset is perfectly correlated with itself. The off-diagonal value (e.g., the correlation between EURUSD=X and GBPUSD=X) is what we are interested in. A value close to 1.0 confirms a strong positive correlation.

Test your understanding!

You have just calculated the correlation for EUR/USD and GBP/USD. Now, modify the tickers list in the script to analyze the relationship between the US Dollar Index and EUR/USD. The ticker for the US Dollar Index is DX-Y.NYB. After running the script, what kind of correlation do you expect to see, and what does the resulting number tell you?

Show answer

You would change the tickers list to: tickers = ['EURUSD=X', 'DX-Y.NYB']

You should expect to see a strong negative correlation. The resulting correlation coefficient will be a negative number close to -1 (e.g., -0.92). This numerically confirms the inverse relationship shown in the chart earlier: when the Dollar Index goes up, EUR/USD tends to go down.


Conclusion

Excellent work! You have successfully written a Python script to fetch data for two assets and quantitatively analyze their relationship. This is a crucial building block for your automated trading bot. You are no longer just looking at charts; you are teaching your program to see and measure the hidden relationships that drive the market.

Key Takeaways:

  • yfinance is your tool for fetching historical price data for stocks, forex, and indices.
  • You can download data for multiple assets at once by passing a list of tickers to yf.download().
  • For accurate correlation analysis, it's essential to work with percentage returns (.pct_change()), not raw prices.
  • The .corr() method in pandas provides a simple way to calculate the correlation matrix for your assets.

Preview for the Next Lesson:
Now that you can fetch and correlate data, the next logical step is to detect specific patterns within that relationship. In our next lesson, you will implement a basic algorithm to programmatically identify SMT divergence. We will use the data you learned to fetch today and write logic to find those critical moments where correlated assets diverge, generating a powerful confirmation signal for your trading bot.