328 lines
13 KiB
Python
328 lines
13 KiB
Python
import pandas as pd
|
|
from stockpredictor.analysis.Common import Common
|
|
from stockpredictor.analysis.Stochastic import Stochastic
|
|
from stockpredictor.analysis.RSI import RSI
|
|
|
|
class BuySellChecker:
|
|
|
|
common = None
|
|
stochastic = None
|
|
rsi = None
|
|
|
|
def __init__(self):
|
|
self.common = Common()
|
|
self.stochastic = Stochastic()
|
|
self.rsi = RSI()
|
|
|
|
return
|
|
|
|
def checkStatus(self, STOCK, last_index):
|
|
status = set()
|
|
|
|
# 정배열 체크
|
|
temp_status = self.common.check_RightArrange(STOCK, last_index)
|
|
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
|
|
|
|
################
|
|
### 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]
|
|
# 3. 2시 이후에는 최고가가 볼린져밴드 상단 위에 있으면 매도한다.
|
|
if i > 300 and data["High"][i] > data["upper"][i]:
|
|
sell = data["High"][i]
|
|
##########################
|
|
### STOCHASTIC buy 분석 ###
|
|
##########################
|
|
if i < 40:
|
|
if data["slow_k"][i] <= 10:
|
|
if data["slow_k"][i - 1] < data["slow_d"][i - 1] and data["slow_d"][i] < data["slow_k"][i]:
|
|
if data["Close"][i] < data["avg5"][i]:
|
|
buy = data["Close"][i]
|
|
else:
|
|
buy = data["Low"][i]
|
|
else:
|
|
if data["slow_k"][i] <= 35:
|
|
if data["slow_k"][i-1] < data["slow_d"][i-1] and data["slow_d"][i] < data["slow_k"][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
|
|
|
|
###################
|
|
### RSI buy 분석 ###
|
|
###################
|
|
if 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 = 2
|
|
|
|
|
|
return buy, weight, sell
|
|
|
|
def getPriceAndWeight2(self, data, i):
|
|
buy, weight, sell = -1, -1, -1
|
|
|
|
if data["slow_k"][i] < 25:
|
|
buy = data["Low"][i]
|
|
if data["slow_k"][i] > 90:
|
|
sell = data["High"][i]
|
|
|
|
return buy, weight, sell
|
|
|
|
|
|
def analyze(self, result):
|
|
df = pd.DataFrame(result["close"])
|
|
|
|
max20 = df.rolling(window=10).mean()
|
|
stddev20 = df.rolling(window=10).std()
|
|
upper_df = max20 + (stddev20 * 2) # 상단 볼린저 밴드
|
|
lower_df = max20 - (stddev20 * 2) # 하단 볼린저 밴드
|
|
|
|
window = 5
|
|
open = result["open"]
|
|
close = result["close"]
|
|
high = result["high"]
|
|
low = result["low"]
|
|
vol = result["vol"]
|
|
|
|
close_df = pd.DataFrame(close)
|
|
avg1_list = close_df.rolling(window=1).mean().fillna(close[0]).values.tolist()
|
|
avg1 = [item[0] for item in avg1_list]
|
|
avg2_list = close_df.rolling(window=2).mean().fillna(close[0]).values.tolist()
|
|
avg2 = [item[0] for item in avg2_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]
|
|
avg40_list = close_df.rolling(window=40).mean().fillna(close[0]).values.tolist()
|
|
avg40 = [item[0] for item in avg40_list]
|
|
avg50_list = close_df.rolling(window=50).mean().fillna(close[0]).values.tolist()
|
|
avg50 = [item[0] for item in avg50_list]
|
|
avg60_list = close_df.rolling(window=60).mean().fillna(close[0]).values.tolist()
|
|
avg60 = [item[0] for item in avg60_list]
|
|
|
|
upper, lower = [], []
|
|
for i in range(len(upper_df)):
|
|
if i < window:
|
|
upper.append(upper_df.values[window - 1][0])
|
|
lower.append(lower_df.values[window - 1][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(result["open"])):
|
|
STOCK.append({'volume': vol[i], 'close': close[i], 'open': open[i],
|
|
'high': high[i], 'low': low[i], 'avg5': avg2[i],
|
|
'avg20': avg5[i], 'avg60': avg10[i], 'avg120': avg20[i],
|
|
'avg240': avg30[i]})
|
|
|
|
# stochastic 계산
|
|
stochastic_df = self.stochastic.apply(pd.DataFrame(STOCK))
|
|
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()
|
|
|
|
# rsi 계산
|
|
rsi_df = self.rsi.apply(pd.DataFrame(STOCK))
|
|
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,
|
|
"avg1": avg1, "avg2": avg2, "avg5": avg5, "avg10": avg10, "avg20": avg20, "avg30": avg30, "avg40": avg40, "avg50": avg50, "avg60": avg60,
|
|
"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 |