patern harmoni110

import 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['Date'] = pd.to_datetime(data['Date'])
data.set_index('Date', inplace=True)
return data


def calculate_rsi(data, period=14):
data['RSI'] = ta.momentum.RSIIndicator(data['Close'], window=period).rsi()
return data

def detect_positive_divergence(data):
detected_patterns = []
for i in range(1, len(data) - 1):
if (data['Close'] < data['Close'][i-1] and
data['RSI'] > data['RSI'][i-1]):
detected_patterns.append((data.index, 'Positive Divergence'))
return detected_patterns


def detect_harmonic_patterns(data):
detected_patterns = []
fib_ratios = {
'Gartley': [0.618, 0.786],
'Bat': [0.5, 0.886],
'Butterfly': [0.786, 1.27],
'Crab': [0.382, 1.618]
}

for i in range(2, len(data) - 2):
for pattern, ratios in fib_ratios.items():
high1 = data['High'][i-2]
high2 = data['High']
low1 = data['Low'][i-2]
low2 = data['Low']

if high1 < high2 and low1 < low2:
retracement = (high2 - low1) / (high1 - low1)
if ratios[0] <= retracement <= ratios[1]:
detected_patterns.append((data.index, f'Bullish {pattern}'))
elif high1 > high2 and low1 > low2:
retracement = (low2 - high1) / (low1 - high1)
if ratios[0] <= retracement <= ratios[1]:
detected_patterns.append((data.index, f'Bearish {pattern}'))
return detected_patterns


def plot_patterns(data, patterns):
plt.figure(figsize=(14, 7))
plt.plot(data['Close'], label='Price')
plt.plot(data['RSI'], label='RSI')

for pattern in patterns:
if pattern[1] == 'Positive Divergence':
plt.scatter(pattern[0], data['Close'][pattern[0]], color='orange', label='Positive Divergence')
elif 'Bullish' in pattern[1]:
plt.scatter(pattern[0], data['Close'][pattern[0]], color='green', label=pattern[1])
elif 'Bearish' in pattern[1]:
plt.scatter(pattern[0], data['Close'][pattern[0]], color='red', label=pattern[1])

plt.legend()
plt.show()

data = load_data()
data = calculate_rsi(data)
patterns = detect_positive_divergence(data) + detect_harmonic_patterns(data)
plot_patterns(data, patterns)
Chart PatternsHarmonic Patterns

Отказ от ответственности