def test_behaviour_may_change_with_batch_or_stream_processing_using_euclidean_distance_1( self): data = [(item.get('x'), item.get('y')) for item in self.sample_1()] stream_result = pip(data, 300, distance='euclidean', stream_mode=True) batch_result = pip(data, 300, distance='euclidean', stream_mode=False) self.assertNotAlmostEqualCurves(stream_result, batch_result)
def test_behaviour_may_change_with_batch_or_stream_processing_using_euclidean_distance_3( self): data = self.sample_3() stream_result = pip(data, 300, distance='euclidean', stream_mode=True) batch_result = pip(data, 300, distance='euclidean', stream_mode=False) self.assertNotAlmostEqualCurves(stream_result, batch_result)
def test_result_should_always_be_the_same_for_a_given_input(self): data = [(item.get('x'), item.get('y')) for item in self.sample_1()] data_length = len(data) first_result = pip(data, data_length/10) for i in range(3): self.assertAlmostEqualCurves( self.pip(data, data_length/10), first_result, "Result has changed on iteration {}".format(i+1) )
def test_result_should_always_be_the_same_for_a_given_input(self): data = [(item.get('x'), item.get('y')) for item in self.sample_1()] data_length = len(data) first_result = pip(data, data_length / 10) for i in range(3): self.assertAlmostEqualCurves( self.pip(data, data_length / 10), first_result, "Result has changed on iteration {}".format(i + 1))
def patterntype(x): if x == 1: # Double Bottom Pattern return pip(__PATTERN_DOUBLE_BOTTOM, 7), 7 if x == 2: # return pip(__CUP_WITH_HANDLE, 7), 7 if x == 3: # Scallops Pattern return pip(__PATTERN_SCALLOPS, 7), 7 if x == 4: # Three Rising Valleys Pattern return pip(__THREE_RISING_VALLEYS, 7), 7 if x == 5: # Round Bottoms Pattern return pip(__ROUNDING_BOTTOMS, 7), 7 if x == 6: # Triangles Descending Pattern return pip(__TRIANGLES_DESCENDING, 7), 7 if x == 7: # return pip(__ROUNDING_TOPS, 7), 7 if x == 8: # return pip(__BUMP_AND_RUN_REVERSAL_BOTTOM, 7), 7 if x == 9: # Triple Bottom Pattern return pip(__PATTERN_TRIPLE_BOTTOM, 10), 10 else: # DEFAULT. exception route return None
def findpattern(p_type, p_low, p_high, period): # p_type : pattern type index # p_low : low price; it's used to search shcode # p_high : high price; it's used to search shcode # period : pattern period start_time = time.time() compare, n = pttype.patterntype(p_type) # choose pattern by p_type rows = shquery.makequerylist(p_low, p_high) # stock filtering print("p_low=", p_low, "와 p_high=", p_high, " 조건에 부합한 종목코드 : ", rows) data_array = [] for row in rows: # each stock tmps = shquery.getstockprice(row) # get stock data from DB temp_array = [] # stock price data pip_array = [] # list of found index sim_array = [] # list of found index i = -1 for tmp in tmps: # append data for PIP i += 1 temp_array.append([i, int(tmp['closeprice'])]) for k in range(0, len(temp_array) - period): # find relevant index result = pip(temp_array[k:k + period], n) r_row, p_value = pearsonr( numpy.array(result)[:, 1], numpy.array(compare)[:, 1]) if r_row > 0.7: print("종목코드 ", row, "에서 index가 ", k, "일 때, 피어슨상관계수 : ", r_row) pip_array.append(k) # k is index sim_array.append(r_row) # fill data if pip_array: data_array.append(row) data_array.append(pip_array) data_array.append(sim_array) print("┌기간=", period, "에서 패턴발견 알고리즘 적용결과 리스트 구현") for i in range(0, len(data_array)): print(data_array[i]) # measure of running-time print("--- processing time : %s seconds ---" % (time.time() - start_time)) return data_array
def test_behaviour_changes_according_to_the_distance_function_used_2(self): data = self.sample_2() vertical_result = pip(data, 300, distance='vertical') euclidean_result = pip(data, 300, distance='euclidean') self.assertNotAlmostEqualCurves(vertical_result, euclidean_result)
def test_behaviour_changes_according_to_the_distance_function_used_1(self): data = [(item.get('x'), item.get('y')) for item in self.sample_1()] vertical_result = pip(data, 300, distance='vertical') euclidean_result = pip(data, 300, distance='euclidean') self.assertNotAlmostEqualCurves(vertical_result, euclidean_result)
def test_behaviour_may_change_with_batch_or_stream_processing_using_euclidean_distance_3(self): data = self.sample_3() stream_result = pip(data, 300, distance='euclidean', stream_mode=True) batch_result = pip(data, 300, distance='euclidean', stream_mode=False) self.assertNotAlmostEqualCurves(stream_result, batch_result)
def test_behaviour_may_change_with_batch_or_stream_processing_using_euclidean_distance_1(self): data = [(item.get('x'), item.get('y')) for item in self.sample_1()] stream_result = pip(data, 300, distance='euclidean', stream_mode=True) batch_result = pip(data, 300, distance='euclidean', stream_mode=False) self.assertNotAlmostEqualCurves(stream_result, batch_result)
def get_pip_dates(df, k): return _pip_array_to_dates(df, pip(_pandas_to_xy_array(df), k))
def test_behaviour_changes_according_to_the_distance_function_used_3(self): data = self.sample_3() vertical_result = pip(data, 300, distance='vertical') euclidean_result = pip(data, 300, distance='euclidean') self.assertNotAlmostEqualCurves(vertical_result, euclidean_result)
# Fetch rows = cursor.fetchall() i = -1 array = [] graph = [] for row in rows: #print(row) i = i + 1 #print(pip([i, int(row['closeprice'])], 5)) #print([i, int(row['closeprice'])]) array.append([i, int(row['closeprice'])]) graph.append(int(row['closeprice'])) #print(array) # Perceptually Important Points result = pip(array, 7) print(result) plt.plot(graph) # Perceptually Important Points index = [] data = [] for idx in result: i = i + 1 index.append(idx[0]) data.append(idx[1]) #index = [0,43,101,174,339,456,499] #data = [5500, 6470, 9870, 7080, 10800, 7280, 7950] plt.plot(index, data) plt.grid(True) plt.show()
graph_array = [] sample_array = [[0, 8000], [1, 4000], [2, 4000], [3, 8000], [4, 4000], [5, 4000], [6, 8000]] # Double Bottom graph #sample_array = [[0, 8000], [1, 5500], [2, 6500], [3, 5500], [4, 6000], [5, 5000], [6, 4000]] # Triangles,Descending, up breakout (6) #sample_array = [[0, 6000], [1, 5500], [2, 6500], [3, 6000], [4, 7000], [5, 6500], [6, 8000]] # Three Rising Valleys, up breakout (4) #sample_array = [[0, 8000], [1, 6000], [2, 5500], [3, 5000], [4, 5500], [5, 6000], [6, 8000]] # Rounding Bottoms, up breakout (5) 그림이 좀 이상... #sample_array = [[0, 4000], [1, 5250], [2, 6500], [3, 5000], [4,6500], [5, 6000], [6, 7500]] # Cup with Handle #sample_array = [[0, 4000], [1, 4700], [2, 5400], [3, 6000], [4,5400], [5, 4700], [6, 7000]] # Rounding Tops #sample_array = [[0, 6000], [1, 6500], [2, 6000], [3, 6250], [4,5000], [5, 6250], [6, 6000]] # Bump-and-Run Reversal Bottoms for row in rows: i += 1 graph_array.append([i, int(row['closeprice'])]) # append data for PIP # Find Pattern using pearson correlation coefficient sample = pip(sample_array, 7) plt.plot(numpy.array(sample)[:, 0], numpy.array(sample)[:, 1]) # plot result of sample pip # plt.show() pip_array = [] # list of found index for k in range(0, len(graph_array) - 30): result = pip(graph_array[k:k + 30], 7) a = numpy.array(result) # print(a[:, 1]) r_row, p_value = pearsonr( numpy.array(result)[:, 1], numpy.array(sample)[:, 1]) if r_row > 0.7: print("종목코드 ", shcode, "에서 index가 ", k, "일 때, 피어슨상관계수 : ", r_row) print(rows[k]) pip_array.append(k)
import pymysql from fastpip import pip import mysqlinfo # Open database connection myhost, myport, myuser, mypassword, mydb, mycharset = mysqlinfo.getmysqlinfo() conn = pymysql.connect(host=myhost, port=myport, user=myuser, password=mypassword, db=mydb, charset=mycharset) # prepare a cursor object using cursor() method cursor = conn.cursor() # execute SQL query using execute() method. sql = "SELECT * FROM stockprice WHERE shcode='000020'" cursor.execute(sql) # Fetch a single row using fetchone() method. rows = cursor.fetchall() # print(rows) # total rows # print(rows[0]) # first row # print(rows[1]) # second row i = -1 for row in rows: print(row) i = i + 1 print(pip([i,int(row['closeprice'])], 5)) # disconnect from server conn.close()