The Volatility Contraction Pattern (VCP)
Step 1
yfinance
Step 2
matplotlib
Step 3
!pip install yfinance matplotlib
Step 4
import yfinance as yf
import matplotlib.pyplot as plt
import pandas as pd
symbol = 'AAPL'
start_date = '2022-01-01'
end_date = '2023-01-01'
data = yf.download(symbol, start=start_date, end=end_date)
Step 5
def identify_vcp(data: pd.DataFrame, contraction_threshold: float = 0.1) -> pd.DataFrame:
vcp = []
for i in range(2, len(data)):
contraction = (data['High'].iloc[i] - data['Low'].iloc[i]) / data['Close'].iloc[i]
previous_contraction = (data['High'].iloc[i - 1] - data['Low'].iloc[i - 1]) / data['Close'].iloc[i - 1]
if contraction < contraction_threshold and contraction < previous_contraction:
vcp.append(i)
return data.iloc[vcp]
vcp_instances = identify_vcp(data)
Step 6
plt.figure(figsize=(10,5))
plt.plot(data['Close'], label='Close Price')
plt.scatter(vcp_instances.index, vcp_instances['Close'], color='red', label='VCP')
plt.title('Volatility Contraction Pattern')
plt.xlabel('Date')
plt.ylabel('Close Price')
plt.legend()
plt.show()