
Correlation Analysis: Why Your Diversification Might Be Fake
Many traders think they're diversified because they trade multiple instruments. But if those instruments are correlated, you're really just taking one big position. Measuring Correlation import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt def calculate_correlations ( price_data , window = 60 ): """ price_data: DataFrame with columns as instruments, rows as dates """ returns = price_data . pct_change (). dropna () correlation_matrix = returns . rolling ( window ). corr () return returns . corr () # Static correlation # Example data = pd . DataFrame ({ ' ES ' : es_prices , # S&P 500 futures ' NQ ' : nq_prices , # Nasdaq futures ' YM ' : ym_prices , # Dow futures ' GC ' : gc_prices , # Gold ' CL ' : cl_prices , # Crude Oil ' EURUSD ' : eur_prices , ' GBPUSD ' : gbp_prices , }) corr = calculate_correlations ( data ) Visualizing Correlations def plot_correlation_heatmap ( corr_matrix ): plt . figure ( figsize = ( 10 , 8 )) sns . heatmap ( corr_matrix
Continue reading on Dev.to Python
Opens in a new tab




