Spaces:
Runtime error
Runtime error
File size: 4,229 Bytes
5913c8f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
import numpy as np
import pandas as pd
from test_data import test_data
from typing import List, Dict, Optional, Union
class PatternLogic:
def __init__(self):
self.patterns = {
'channel': {'min_points': 4, 'confidence_threshold': 0.7},
'triangle': {'min_points': 3, 'confidence_threshold': 0.75},
'support': {'min_touches': 2, 'confidence_threshold': 0.8},
'resistance': {'min_touches': 2, 'confidence_threshold': 0.8},
'double_top': {'max_deviation': 0.02, 'confidence_threshold': 0.85},
'double_bottom': {'max_deviation': 0.02, 'confidence_threshold': 0.85}
}
self.test_data = test_data
def detect_channels(self, data: pd.DataFrame) -> Dict[str, Union[str, List[List[float]], float]]:
days = len(data)
base_price = 100
price_changes = np.random.normal(0.001, 0.02, days).cumsum()
base_prices = base_price * (1 + price_changes)
high_prices = base_prices * (1 + np.random.normal(0.01, 0.008, days))
low_prices = base_prices * (1 + np.random.normal(-0.01, 0.008, days))
timestamps = np.arange(days)
upper_channel: List[List[float]] = []
lower_channel: List[List[float]] = []
for i in range(days):
upper_channel.append([float(timestamps[i]), float(high_prices[i])])
lower_channel.append([float(timestamps[i]), float(low_prices[i])])
return {
'type': 'channel',
'upper': upper_channel,
'lower': lower_channel,
'confidence': 0.85
}
def find_support_resistance(self, data: pd.DataFrame) -> List[Dict[str, Union[str, List[List[float]], float]]]:
days = len(data)
base_price = 100
price_changes = np.random.normal(0.001, 0.02, days).cumsum()
close_prices = base_price * (1 + price_changes) * (1 + np.random.normal(0, 0.005, days))
timestamps = np.arange(days)
levels: List[Dict] = []
for i in range(1, days-1):
current_price = float(close_prices[i])
prev_price = float(close_prices[i-1])
next_price = float(close_prices[i+1])
if current_price > prev_price and current_price > next_price:
levels.append({
'type': 'resistance',
'coordinates': [[float(timestamps[i]), current_price]],
'confidence': 0.8
})
if current_price < prev_price and current_price < next_price:
levels.append({
'type': 'support',
'coordinates': [[float(timestamps[i]), current_price]],
'confidence': 0.8
})
return levels
def detect_triangles(self, data: pd.DataFrame) -> Optional[Dict[str, Union[str, List[List[float]], float]]]:
days = len(data)
base_price = 100
price_changes = np.random.normal(0.001, 0.02, days).cumsum()
base_prices = base_price * (1 + price_changes)
high_prices = base_prices * (1 + np.random.normal(0.01, 0.008, days))
low_prices = base_prices * (1 + np.random.normal(-0.01, 0.008, days))
timestamps = np.arange(days)
first_high = float(high_prices[0])
last_high = float(high_prices[-1])
first_low = float(low_prices[0])
last_low = float(low_prices[-1])
if last_high < first_high and last_low > first_low:
return {
'type': 'triangle',
'coordinates': [
[float(timestamps[0]), first_high],
[float(timestamps[-1]), last_high],
[float(timestamps[0]), first_low],
[float(timestamps[-1]), last_low]
],
'confidence': 0.75
}
return None
def validate_patterns(self, patterns: List[Dict]) -> List[Dict]:
validated = []
for pattern in patterns:
if pattern.get('confidence', 0) >= 0.8:
validated.append(pattern)
return validated
|