patern harmoni110import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import ta
def load_data():
data = pd.read_csv('historical_data.csv')
data = pd.to_datetime(data )
data.set_index('Date', inplace=True)
return data
def calculate_rsi(data, period=14):
data = ta.momentum.RSIIndicator(data , window=period).rsi()
return data
def detect_positive_divergence(data):
detected_patterns =
for i in range(1, len(data) - 1):
if (data < data and
data > data ):
detected_patterns.append((data.index , 'Positive Divergence'))
return detected_patterns
def detect_harmonic_patterns(data):
detected_patterns =
fib_ratios = {
'Gartley': ,
'Bat': ,
'Butterfly': ,
'Crab':
}
for i in range(2, len(data) - 2):
for pattern, ratios in fib_ratios.items():
high1 = data
high2 = data
low1 = data
low2 = data
if high1 < high2 and low1 < low2:
retracement = (high2 - low1) / (high1 - low1)
if ratios <= retracement <= ratios :
detected_patterns.append((data.index , f'Bullish {pattern}'))
elif high1 > high2 and low1 > low2:
retracement = (low2 - high1) / (low1 - high1)
if ratios <= retracement <= ratios :
detected_patterns.append((data.index , f'Bearish {pattern}'))
return detected_patterns
def plot_patterns(data, patterns):
plt.figure(figsize=(14, 7))
plt.plot(data , label='Price')
plt.plot(data , label='RSI')
for pattern in patterns:
if pattern == 'Positive Divergence':
plt.scatter(pattern , data [pattern ], color='orange', label='Positive Divergence')
elif 'Bullish' in pattern :
plt.scatter(pattern , data [pattern ], color='green', label=pattern )
elif 'Bearish' in pattern :
plt.scatter(pattern , data [pattern ], color='red', label=pattern )
plt.legend()
plt.show()
data = load_data()
data = calculate_rsi(data)
patterns = detect_positive_divergence(data) + detect_harmonic_patterns(data)
plot_patterns(data, patterns)