pattern_search
1from turtle import distance, title 2from unittest import result 3import pandas as pd 4import pattern_utils 5import Pattern as p 6import tendencyCalculator as tc 7import matplotlib.pyplot as plt 8from random import randint 9import normalize_utils as nu 10 11INCREMENT = 1 12 13def findCurrentPatterns(start_index, finish_index, company_dataframe, patterns_dictionary, company_name, intensive_search): 14 """Find patterns that are occuring the day of the search 15 16 Args: 17 start_index (int): starting size of the searching window 18 finish_index (int): ending size of the searching window 19 company_datadrame (dataframe): dataframe containing the company prices in the entire year 20 patterns_dictionary (Dict[]): dictionary containing types of patterns as keys an pattern data as value 21 company_name (str): name of the company where the search is taking place 22 Returns: 23 results (List[Pattern]): list of patterns found 24 """ 25 distances_dict = { 26 'double_top': [pattern_utils.BIG_NUMBER, -1], 27 'double_bottom': [pattern_utils.BIG_NUMBER, -1] 28 } 29 patterns_dictionary_copy = patterns_dictionary.copy() 30 results = [] 31 for i in range(finish_index, start_index, -1): # hay que añadir que itere por todos y que se quede con el de menor distancia 32 if len(patterns_dictionary_copy.keys()) == 1: 33 break 34 if i > company_dataframe.size: 35 i = company_dataframe.size 36 sliced_dataframe = company_dataframe.iloc[company_dataframe.size - i:] 37 normalized_vector = nu.normalizeVector(sliced_dataframe['Close'].tolist()) 38 new_pattern_type, distance = pattern_utils.findCommonPattern(normalized_vector, patterns_dictionary_copy) 39 if new_pattern_type != 'rest_normalized': 40 if intensive_search == True: 41 if distance < distances_dict[new_pattern_type][0]: 42 distances_dict[new_pattern_type] = [distance, i] 43 else: 44 distances_dict[new_pattern_type] = [distance, i] 45 patterns_dictionary_copy.pop(new_pattern_type) 46 47 for key in distances_dict: 48 if distances_dict[key][1] == -1: 49 continue 50 new_pattern = p.Pattern(key, company_dataframe[company_dataframe.size - distances_dict[key][1]:], company_name, str(company_dataframe.iloc[company_dataframe.size - i].name), str(company_dataframe.iloc[company_dataframe.size - 1].name), None) 51 results.append(new_pattern) 52 #patterns_dictionary_copy.pop(new_pattern_type) 53 54 return results 55 56def findHistoricPatterns(window_width, company_data, patterns_dictionary, company_name): 57 """Find patterns through historic data 58 Args: 59 window_width (int): fixed window size for the search 60 company_data (datarame): dataframe containing the company's close prices 61 atterns_dictionary (Dict[]): dictionary containing types of patterns as keys an pattern data as value 62 company_name (str): name of the company where the search is taking place 63 Returns: 64 patterns_found (List[Pattern]): list of patterns found 65 """ 66 patterns_found = [] 67 offset = 0 68 i = 0 69 while i < company_data.size - window_width - 1: 70 right_window_index = i + window_width 71 if right_window_index >= company_data.size: 72 break 73 sliced_dataframe = company_data.iloc[i:right_window_index] 74 normalized_vector = nu.normalizeVector(sliced_dataframe['Close'].tolist()) 75 new_pattern_type, best_distance_found = pattern_utils.findCommonPattern(normalized_vector, patterns_dictionary) 76 if new_pattern_type != 'rest_normalized' and new_pattern_type != '': 77 left_index, right_index = pattern_utils.enhanceDataframe(best_distance_found, new_pattern_type, sliced_dataframe['Close'].tolist(), patterns_dictionary, [1,2,3,4]) 78 dataframe_segment = sliced_dataframe[left_index:right_index] #Esto sin ventana mejorada 79 longer_dataframe = company_data[i + left_index:] #Quitar left_index si no se usa enhanced dataframe 80 pattern_tendency = tc.findPatternTendency(dataframe_segment, longer_dataframe, new_pattern_type) 81 if pattern_tendency != None: 82 new_pattern = p.Pattern(new_pattern_type, pattern_tendency[1], company_name, str(dataframe_segment.iloc[0].name), str(dataframe_segment.iloc[dataframe_segment.size - 1].name), pattern_tendency[0]) 83 patterns_found.append(new_pattern) 84 offset = sliced_dataframe.size 85 else: 86 offset = 0 87 i += INCREMENT + offset 88 return patterns_found
14def findCurrentPatterns(start_index, finish_index, company_dataframe, patterns_dictionary, company_name, intensive_search): 15 """Find patterns that are occuring the day of the search 16 17 Args: 18 start_index (int): starting size of the searching window 19 finish_index (int): ending size of the searching window 20 company_datadrame (dataframe): dataframe containing the company prices in the entire year 21 patterns_dictionary (Dict[]): dictionary containing types of patterns as keys an pattern data as value 22 company_name (str): name of the company where the search is taking place 23 Returns: 24 results (List[Pattern]): list of patterns found 25 """ 26 distances_dict = { 27 'double_top': [pattern_utils.BIG_NUMBER, -1], 28 'double_bottom': [pattern_utils.BIG_NUMBER, -1] 29 } 30 patterns_dictionary_copy = patterns_dictionary.copy() 31 results = [] 32 for i in range(finish_index, start_index, -1): # hay que añadir que itere por todos y que se quede con el de menor distancia 33 if len(patterns_dictionary_copy.keys()) == 1: 34 break 35 if i > company_dataframe.size: 36 i = company_dataframe.size 37 sliced_dataframe = company_dataframe.iloc[company_dataframe.size - i:] 38 normalized_vector = nu.normalizeVector(sliced_dataframe['Close'].tolist()) 39 new_pattern_type, distance = pattern_utils.findCommonPattern(normalized_vector, patterns_dictionary_copy) 40 if new_pattern_type != 'rest_normalized': 41 if intensive_search == True: 42 if distance < distances_dict[new_pattern_type][0]: 43 distances_dict[new_pattern_type] = [distance, i] 44 else: 45 distances_dict[new_pattern_type] = [distance, i] 46 patterns_dictionary_copy.pop(new_pattern_type) 47 48 for key in distances_dict: 49 if distances_dict[key][1] == -1: 50 continue 51 new_pattern = p.Pattern(key, company_dataframe[company_dataframe.size - distances_dict[key][1]:], company_name, str(company_dataframe.iloc[company_dataframe.size - i].name), str(company_dataframe.iloc[company_dataframe.size - 1].name), None) 52 results.append(new_pattern) 53 #patterns_dictionary_copy.pop(new_pattern_type) 54 55 return results
Find patterns that are occuring the day of the search
Args:
start_index (int): starting size of the searching window
finish_index (int): ending size of the searching window
company_datadrame (dataframe): dataframe containing the company prices in the entire year
patterns_dictionary (Dict[]): dictionary containing types of patterns as keys an pattern data as value
company_name (str): name of the company where the search is taking place
Returns:
results (List[Pattern]): list of patterns found
57def findHistoricPatterns(window_width, company_data, patterns_dictionary, company_name): 58 """Find patterns through historic data 59 Args: 60 window_width (int): fixed window size for the search 61 company_data (datarame): dataframe containing the company's close prices 62 atterns_dictionary (Dict[]): dictionary containing types of patterns as keys an pattern data as value 63 company_name (str): name of the company where the search is taking place 64 Returns: 65 patterns_found (List[Pattern]): list of patterns found 66 """ 67 patterns_found = [] 68 offset = 0 69 i = 0 70 while i < company_data.size - window_width - 1: 71 right_window_index = i + window_width 72 if right_window_index >= company_data.size: 73 break 74 sliced_dataframe = company_data.iloc[i:right_window_index] 75 normalized_vector = nu.normalizeVector(sliced_dataframe['Close'].tolist()) 76 new_pattern_type, best_distance_found = pattern_utils.findCommonPattern(normalized_vector, patterns_dictionary) 77 if new_pattern_type != 'rest_normalized' and new_pattern_type != '': 78 left_index, right_index = pattern_utils.enhanceDataframe(best_distance_found, new_pattern_type, sliced_dataframe['Close'].tolist(), patterns_dictionary, [1,2,3,4]) 79 dataframe_segment = sliced_dataframe[left_index:right_index] #Esto sin ventana mejorada 80 longer_dataframe = company_data[i + left_index:] #Quitar left_index si no se usa enhanced dataframe 81 pattern_tendency = tc.findPatternTendency(dataframe_segment, longer_dataframe, new_pattern_type) 82 if pattern_tendency != None: 83 new_pattern = p.Pattern(new_pattern_type, pattern_tendency[1], company_name, str(dataframe_segment.iloc[0].name), str(dataframe_segment.iloc[dataframe_segment.size - 1].name), pattern_tendency[0]) 84 patterns_found.append(new_pattern) 85 offset = sliced_dataframe.size 86 else: 87 offset = 0 88 i += INCREMENT + offset 89 return patterns_found
Find patterns through historic data
Args:
window_width (int): fixed window size for the search
company_data (datarame): dataframe containing the company's close prices
atterns_dictionary (Dict[]): dictionary containing types of patterns as keys an pattern data as value
company_name (str): name of the company where the search is taking place
Returns:
patterns_found (List[Pattern]): list of patterns found