Finding Higher Highs, Lower Lows, Lower Highs, and Higher Lows with Python
Step 1
import pandas as pd
import numpy as np
# Sample price data (replace with your own dataset)
data = {
'Date': ['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05'],
'Close': [100, 110, 105, 115, 112]
}
# Create a DataFrame
df = pd.DataFrame(data)
# Calculate higher highs and lower lows
df['HH'] = np.where(df['Close'] > df['Close'].shift(1), True, False)
df['LL'] = np.where(df['Close'] < df['Close'].shift(1), True, False)
print(df)
Step 2
# Calculate lower highs and higher lows
df['LH'] = np.where(df['Close'] < df['Close'].shift(1), True, False)
df['HL'] = np.where(df['Close'] > df['Close'].shift(1), True, False)
print(df)