diff --git a/hts/BuySellChecker.py b/hts/BuySellChecker.py index 10cea4a..1cfd384 100644 --- a/hts/BuySellChecker.py +++ b/hts/BuySellChecker.py @@ -1,187 +1,27 @@ +import math 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 +from stockpredictor.analysis.IchimokuCloud import IchimokuCloud class BuySellChecker: common = None stochastic = None rsi = None + ichimokuCloud = None def __init__(self): self.common = Common() self.stochastic = Stochastic() self.rsi = RSI() self.macd = MACD() + self.ichimokuCloud = IchimokuCloud() 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 @@ -299,15 +139,26 @@ class BuySellChecker: buy, weight, sell = -1, -1, -1 # 381: 어제 날짜 데이터 개수 - if i >= 381+1: + if i >= 381+3: + # 매수 분석 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] + # 표준편차를 이용한 매매 + mean = (data["avg3"][i] + data["avg5"][i] + data["avg10"][i] + data["avg20"][i] + data["avg30"][i])/5 + vsum = (data["avg3"][i] - mean) ** 2 + (data["avg5"][i] - mean) ** 2 + (data["avg10"][i] - mean) ** 2 + (data["avg20"][i] - mean) ** 2 + (data["avg30"][i] - mean) ** 2 + variance = vsum / 5 + std = math.sqrt(variance) + if std < 1: + sell = 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 @@ -374,6 +225,14 @@ class BuySellChecker: rsi = rsi_df['rsi'].values.tolist() rsis = rsi_df['rsis'].values.tolist() + # ichimokuCloud 계산 + # ichimokuCloud_df = self.ichimokuCloud.apply(STOCK, c=9, b=26, l=52) + # ichimokuCloud_df = rsi_df.fillna(100) + # changeLine = rsi_df['changeLine'].values.tolist() + # baseLine = rsi_df['baseLine'].values.tolist() + # leadingSpan1 = rsi_df['leadingSpan1'].values.tolist() + # leadingSpan2 = rsi_df['leadingSpan2'].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, diff --git a/hts/HTS_122630.py b/hts/HTS_122630.py index 34534a6..bc4730e 100644 --- a/hts/HTS_122630.py +++ b/hts/HTS_122630.py @@ -146,6 +146,8 @@ class HTS_122630 (HTS): self.cancelOrderList(orderList) # 매도 가격을 가져온다. + result = self.getRealTime(self.stock_code, lastday, today) + final_price = result["close"][len(result["close"])-1] selling_count, selling_price = self.getSellingPrice(final_price) # 분석되 가격으로 매도 요청한다. if selling_count != 0 and selling_price != 0: diff --git a/hts/HTS_252670.py b/hts/HTS_252670.py index 8efb8f3..83e9ad5 100644 --- a/hts/HTS_252670.py +++ b/hts/HTS_252670.py @@ -147,6 +147,8 @@ class HTS_252670 (HTS): self.cancelOrderList(orderList) # 매도 가격을 가져온다. + result = self.getRealTime(self.stock_code, lastday, today) + final_price = result["close"][len(result["close"]) - 1] selling_count, selling_price = self.getSellingPrice(final_price) # 분석되 가격으로 매도 요청한다. if selling_count != 0 and selling_price != 0: diff --git a/hts/Simulation.py b/hts/Simulation.py index ae2fe7c..5f4e714 100644 --- a/hts/Simulation.py +++ b/hts/Simulation.py @@ -181,11 +181,8 @@ class Simulation: if __name__ == "__main__": stock_codes = { - #"252670": [('20220620', '20220621'), ('20220621', '20220622'), ('20220622', '20220623'), ('20220623', '20220624')], - "122630": [('20220620', '20220621'), - ('20220621', '20220622'), - ('20220622', '20220623'), - ('20220623', '20220624')], + "252670": [('20220620', '20220621'), ('20220621', '20220622'), ('20220622', '20220623'), ('20220623', '20220624')], + "122630": [('20220620', '20220621'), ('20220621', '20220622'), ('20220622', '20220623'), ('20220623', '20220624')], # "252670": [('20220620', '20220621')], # "122630": [('20220620', '20220621')] } diff --git a/stockpredictor/analysis/IchimokuCloud.py b/stockpredictor/analysis/IchimokuCloud.py index 02b2f72..661c9dd 100644 --- a/stockpredictor/analysis/IchimokuCloud.py +++ b/stockpredictor/analysis/IchimokuCloud.py @@ -1,9 +1,4 @@ import pandas as pd -import datetime -from plotly import tools, subplots -import plotly.offline as offline -import plotly.graph_objs as go -import plotly.io as po from stockpredictor.analysis.Common import Common @@ -18,28 +13,6 @@ class IchimokuCloud: self.common = Common() return - def draw(self, stock): - item_name = stock["NAME"] - item_code = stock["CODE"] - - df = pd.DataFrame(stock["PRICE"]) - leadingSpan1 = go.Scatter(x=df.DATE, y=df['leadingSpan1'], name="선행스팬1", line_color='#8B4513') - leadingSpan2 = go.Scatter(x=df.DATE, y=df['leadingSpan2'], name="선행스팬2", line_color='#4169E1') - candle = go.Candlestick(x=df.DATE, open=df.open, high=df.high, low=df.low, close=df.close, increasing_line_color= 'red', decreasing_line_color= 'blue') - data = [leadingSpan1, leadingSpan2, candle] - - layout = go.Layout(title='{} MACD 그래프'.format(item_name)) - fig = subplots.make_subplots(rows=1, cols=1, shared_xaxes=True) - - for trace in data: - fig.append_trace(trace, 1,1) - - fig = go.Figure(data=data, layout=layout) - - path = "/Users/dsyoon/workspace/StockPredictor/resources/analysis/html" - po.write_html(fig, file=path + "/ichimokuCloud_" + item_code+'.html', auto_open=False) - return fig - # c=9, b=26, l=52 def apply(self, df, c=9, b=26, l=52): # 입력받은 값이 dataframe이라는 것을 정의해줌