import pandas as pd from stockpredictor.analysis.Common import Common from stockpredictor.analysis.Stochastic import Stochastic from stockpredictor.analysis.RSI import RSI from stockpredictor.analysis.MACD import MACD class BuySellChecker: common = None stochastic = None rsi = None def __init__(self): self.common = Common() self.stochastic = Stochastic() self.rsi = RSI() self.macd = MACD() return def checkStatus(self, STOCK, last_index): status = set() # 정배열 체크 temp_status = self.common.check_RightArrange(STOCK) if temp_status != "": status.add(temp_status) # 돌파 체크 temp_status = self.common.check_Dolpa(STOCK, last_index, "5", "20") if temp_status != "": status.add(temp_status) temp_status = self.common.check_Dolpa(STOCK, last_index, "5", "60") if temp_status != "": status.add(temp_status) temp_status = self.common.check_Dolpa(STOCK, last_index, "5", "120") if temp_status != "": status.add(temp_status) temp_status = self.common.check_Dolpa(STOCK, last_index, "5", "240") if temp_status != "": status.add(temp_status) temp_status = self.common.check_Dolpa(STOCK, last_index, "20", "60") if temp_status != "": status.add(temp_status) temp_status = self.common.check_Dolpa(STOCK, last_index, "20", "120") if temp_status != "": status.add(temp_status) temp_status = self.common.check_Dolpa(STOCK, last_index, "20", "240") if temp_status != "": status.add(temp_status) temp_status = self.common.check_Dolpa(STOCK, last_index, "60", "120") if temp_status != "": status.add(temp_status) temp_status = self.common.check_Dolpa(STOCK, last_index, "60", "240") if temp_status != "": status.add(temp_status) temp_status = self.common.check_Dolpa(STOCK, last_index, "120", "240") if temp_status != "": status.add(temp_status) # 20일선 돌파 temp_status = self.common.check_Dolpa_Jiji(STOCK, last_index, '20') if temp_status != "": status.add(temp_status) # 60일선 돌파 temp_status = self.common.check_Dolpa_Jiji(STOCK, last_index, '60') if temp_status != "": status.add(temp_status) # 120일선 돌파 temp_status = self.common.check_Dolpa_Jiji(STOCK, last_index, '120') if temp_status != "": status.add(temp_status) # 240일선 돌파 #temp_status = self.common.check_Dolpa_Jiji(STOCK, last_index, '240') #if temp_status != "": # status.add(temp_status) # 20일선 지지 매수가 추천 temp_status = self.common.check_Dolpa_Jiji_20(STOCK, last_index) if temp_status != "": status.add(temp_status) # 음봉인데 어제보다 종가가 더 높은 경우 # 이 경우 정배열 상태인지도 함께 체크를 한다. higher_umbong_status = self.common.checkHigherUmbong(STOCK, last_index) if higher_umbong_status != "": status.add(temp_status) # GOLDENCROSS#1은 바로 매수하지 않고, 이 시점 이후로 5일선이 20일선을 하방으로 뚫었다가 다시 20일선을 상방으로 뚫는 순간 매수를 시도한다. # GOLDENCROSS#2은 바로 매수 가능 # GOLDENCROSS#3은 바로 매수 가능 temp_status = self.common.check_golded_cross(STOCK, last_index) if temp_status != "": status.add(temp_status) # YANGBONG # 어제 음봉 이후 장대양봉이었다면, 매수 temp_status = self.common.checkLongYangBongAfterUmBong(STOCK, last_index) if temp_status != "": status.add(temp_status) # Doji # 하락 추세에서 도지가 나오면 매수 temp_status = self.common.checkDoji(STOCK, last_index) if temp_status != "": status.add(temp_status) # Gravestone # 상승 추세에서 그레이브스톤이 나오면 매도 temp_status = self.common.checkGravestone(STOCK, last_index) if temp_status != "": status.add(temp_status) # Dragonfly # 하락 추세에서 드레곤플라이가 나오면 매수 temp_status = self.common.checkDragonfly(STOCK, last_index) if temp_status != "": status.add(temp_status) # Hammer temp_status = self.common.checkHammer(STOCK, last_index) # 하락 추세에서 해머가 나오면 매수 if temp_status != "": status.add(temp_status) # Hangingman temp_status = self.common.checkHangingman(STOCK, last_index) # 상승 추세에서 행잉맨이 나오면 매도 if temp_status != "": status.add(temp_status) # 상승장악형 (Engulfing) - 다음 날도 양봉이라면 매수 # 하락 추세에서 상승장악형이 나오면 매수 temp_status = self.common.checkEngulfingHigh(STOCK, last_index) if temp_status != "": status.add(temp_status) # 하락장악형 (Engulfing) # 상승 추세에서 하락장악형이 나오면 매도 temp_status = self.common.checkEngulfingLow(STOCK, last_index) if temp_status != "": status.add(temp_status) # 상승 포아형 (Harami) # 하락 추세에서 상승포아형이 나오면 매수 temp_status = self.common.checkHaramiHigh(STOCK, last_index) if temp_status != "": status.add(temp_status) # 하락 포아형 (Harami) # 상승 추세에서 하락포아형이 나오면 매도 temp_status = self.common.checkHaramiLow(STOCK, last_index) if temp_status != "": status.add(temp_status) # 관통형 (piercing) # 하락 추세에서 관통형이 나오면 매수 temp_status = self.common.checkPiercing(STOCK, last_index) if temp_status != "": status.add(temp_status) # 흑운형 (Dark-cloud) # 상승 추세에서 흑운형이 나오면 매도 temp_status = self.common.checkDarkCloud(STOCK, last_index) if temp_status != "": status.add(temp_status) # 샛별 (Morning start) # 하락 추세에서 샛별형이 나오면 매수 temp_status = self.common.checkMorningstar(STOCK, last_index) if temp_status != "": status.add(temp_status) # 저녁별 (Evening start) # 상승 추세에서 저녁별형이 나오면 매도 temp_status = self.common.checkEveningstar(STOCK, last_index) if temp_status != "": status.add(temp_status) return status def getPriceAndWeight1(self, data, i): buy, weight, sell = -1, -1, -1 if i >= 3: ################ ### sell 분석 ### ################ # 1. 볼린져밴드 상단이 최고와 종가 사이 아래에 있는 경우 매도한다. #if (data["high"][i] - data["close"][i]) / 2 + data["close"][i] > data["upper"][i]: # sell = data["high"][i] # 2. slow_k가 90이 넘으면 매도한다. if data["slow_k"][i] > 90: sell = data["high"][i] #if data["slow_k"][i] >= 85: # if data["slow_d"][i-1] < data["slow_k"][i-1] and data["slow_k"][i] < data["slow_d"][i]: # sell = data["high"][i] # 3. 2시 이후에는 최고가가 볼린져밴드 상단 위에 있으면 매도한다. if i > 300 and data["high"][i] > data["upper"][i]: sell = data["high"][i] ########################## ### buy 분석 ### ########################## if data["low"][i] < data["lower"][i] + 5 and data["open"][i] <= data["close"][i]: if data["slow_k"][i-1] < 30 and data["slow_k"][i] < 30: if data["slow_k"][i-1] < data["slow_k"][i]: buy = data["low"][i] if data["rsi"][i] < 25: if data["rsi"][i - 2] < data["rsis"][i - 2] and data["rsi"][i - 1] < data["rsis"][i - 1] and data["rsis"][i] < data["rsi"][i]: if data["close"][i] < data["avg5"][i]: buy = data["close"][i] else: buy = data["low"][i] weight = 1 ############################# ### STOCHASTIC weight 분석 ### ############################# if data["slow_k"][i] in (0, 1, 2, 3): weight = 1 if data["slow_k"][i] in (4, 5, 6, 7, 8): weight = 1 elif data["slow_k"][i] in (9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20): weight = 1 elif data["slow_k"][i] in (21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35): weight = 1 return buy, weight, sell def getPriceAndWeight2(self, data, i): buy, weight, sell = -1, -1, -1 ################ ### sell 분석 ### ################ # 1. 볼린져밴드 상단이 최고와 종가 사이 아래에 있는 경우 매도한다. if (data["high"][i] - data["close"][i]) / 2 + data["close"][i] > data["upper"][i]: sell = data["high"][i] if data["slow_k"][i] >= 85: if data["slow_d"][i - 1] < data["slow_k"][i - 1] and data["slow_k"][i] < data["slow_d"][i]: sell = data["high"][i] # 3. 2시 이후에는 최고가가 볼린져밴드 상단 위에 있으면 매도한다. if i > 300 and data["high"][i] > data["upper"][i]: sell = data["high"][i] ########################## ### STOCHASTIC buy 분석 ### ########################## if i < 40: pre_slow = data["slow_k"][i - 1] / data["slow_d"][i - 1] - 1 now_slow = data["slow_k"][i] / data["slow_d"][i] - 1 if pre_slow < 0 and 0 < now_slow: if data["slow_k"][i] <= 35: if (data["close"][i] - data["lower"][i]) / (data["upper"][i] - data["lower"][i]) < 0.35: if data["slow_k"][i - 1] < data["slow_d"][i - 1] and data["slow_d"][i] < data["slow_k"][i]: if data['avg10'][i] < data['avg5'][i]: if data["open"][i] < data["close"][i]: buy = data["close"][i] else: buy = data["low"][i] else: pre_slow = data["slow_k"][i - 1] / data["slow_d"][i - 1] - 1 now_slow = data["slow_k"][i] / data["slow_d"][i] - 1 if pre_slow < 0 and pre_slow < now_slow and -0.15 < now_slow: if data["slow_k"][i] <= 30: if (data["close"][i] - data["lower"][i]) / (data["upper"][i] - data["lower"][i]) < 0.35: if data["slow_k"][i - 1] < data["slow_d"][i - 1] and data["slow_d"][i] < data["slow_k"][i]: if data['avg10'][i] < data['avg5'][i]: if data["close"][i] < data["avg5"][i]: buy = data["close"][i] else: buy = data["low"][i] ############################# ### STOCHASTIC weight 분석 ### ############################# if data["slow_k"][i] in (0, 1, 2, 3): weight = 1 if data["slow_k"][i] in (4, 5, 6, 7, 8): weight = 1 elif data["slow_k"][i] in (9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20): weight = 1 elif data["slow_k"][i] in (21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35): weight = 1 return buy, weight, sell def getPriceAndWeight3(self, data, i): buy, weight, sell = -1, -1, -1 # 381: 어제 날짜 데이터 개수 if i >= 381 + 5: if data["macdo"][i] < 0 and data["macd"][i] < -5: if data["macd"][i-3] > data["macd"][i-2] and data["macd"][i-2] > data["macd"][i-1] and data["macd"][i-1] < data["macd"][i]: buy = data["close"][i] if data["slow_d"][i] > 90 and data["rsi"][i] > 65: if data["upper"][i] <= data["high"][i]: sell = data["close"][i] - 5 return buy, weight, sell def analyze(self, result): open = result["open"] close = result["close"] high = result["high"] low = result["low"] vol = result["vol"] close_df = pd.DataFrame(close) avg3_list = close_df.rolling(window=3).mean().fillna(close[0]).values.tolist() avg3 = [item[0] for item in avg3_list] avg5_list = close_df.rolling(window=5).mean().fillna(close[0]).values.tolist() avg5 = [item[0] for item in avg5_list] avg10_list = close_df.rolling(window=10).mean().fillna(close[0]).values.tolist() avg10 = [item[0] for item in avg10_list] avg20_list = close_df.rolling(window=20).mean().fillna(close[0]).values.tolist() avg20 = [item[0] for item in avg20_list] avg30_list = close_df.rolling(window=30).mean().fillna(close[0]).values.tolist() avg30 = [item[0] for item in avg30_list] avg60_list = close_df.rolling(window=60).mean().fillna(close[0]).values.tolist() avg60 = [item[0] for item in avg60_list] df = pd.DataFrame(close) max20 = df.rolling(window=20).mean() stddev20 = df.rolling(window=20).std() upper_df = max20 + (stddev20 * 2) # 상단 볼린저 밴드 lower_df = max20 - (stddev20 * 2) # 하단 볼린저 밴드 upper, lower = [], [] for i in range(len(upper_df)): if i < 10: upper.append(upper_df.values[0][0]) lower.append(lower_df.values[0][0]) else: upper.append(upper_df.values[i][0]) lower.append(lower_df.values[i][0]) point_temp = result["time"] STOCK = [] for i in range(len(open)): STOCK.append({'volume': vol[i], 'close': close[i], 'open': open[i], 'high': high[i], 'low': low[i], 'avg3': avg3[i], 'avg5': avg5[i],'avg10': avg10[i],'avg20': avg20[i],'avg30': avg30[i],'avg60': avg60[i]}) # stochastic 계산 stochastic_df = self.stochastic.apply(STOCK, n=30, m=5, t=5) stochastic_df = stochastic_df.fillna(100) fast_k = stochastic_df['fast_k'].values.tolist() slow_k = stochastic_df['slow_k'].values.tolist() slow_d = stochastic_df['slow_d'].values.tolist() # macd 계산 macd_df = self.macd.apply(STOCK, short=12, long=26, t=9) macd_df = macd_df.fillna(100) macd = macd_df['macd'].values.tolist() macds = macd_df['macds'].values.tolist() macdo = macd_df['macdo'].values.tolist() # rsi 계산 rsi_df = self.rsi.apply(STOCK, period=30, window=5) rsi_df = rsi_df.fillna(100) rsi = rsi_df['rsi'].values.tolist() rsis = rsi_df['rsis'].values.tolist() temp = {"date": point_temp, "open": open, "high": high, "low": low, "close": close, "volume": vol, "upper": upper, "lower": lower, "avg3": avg3, "avg5": avg5, "avg10": avg10, "avg20": avg20, "avg30": avg30, "avg60": avg60, "macd": macd, "macds": macds, "macdo": macdo, "fast_k": fast_k, "slow_k": slow_k, "slow_d": slow_d, "rsi": rsi, "rsis": rsis} data = pd.DataFrame(temp) df_final_time = pd.DatetimeIndex(point_temp) data.index = df_final_time return data def checkTransaction(self, data, stock_code): size = len(data["close"]) bsLine = {} bsLine['buy'] = [-1 for i in range(size)] bsLine['weight'] = [-1 for i in range(size)] bsLine['sell'] = [-1 for i in range(size)] for i in range(size): if stock_code == "252670": buy, weight, sell = self.getPriceAndWeight3(data, i) else: buy, weight, sell = self.getPriceAndWeight3(data, i) bsLine['buy'][i] = buy bsLine['weight'][i] = weight bsLine['sell'][i] = sell return bsLine