init
This commit is contained in:
566
hts/HTS.py
566
hts/HTS.py
@@ -5,7 +5,7 @@ from datetime import datetime, timedelta
|
||||
import pandas as pd
|
||||
from enum import Enum
|
||||
import plotly.graph_objects as go
|
||||
|
||||
from stockpredictor.analysis.Common import Common
|
||||
|
||||
# enum 주문 상태 세팅용
|
||||
class EorderBS(Enum):
|
||||
@@ -30,10 +30,12 @@ class HTS:
|
||||
|
||||
objCpCybos = None
|
||||
objCpCodeMgr = None
|
||||
common = None
|
||||
|
||||
stock = []
|
||||
|
||||
def __init__(self):
|
||||
self.common = Common()
|
||||
#self.connect()
|
||||
return
|
||||
|
||||
@@ -505,25 +507,23 @@ class HTS:
|
||||
|
||||
size = len(result["open"])
|
||||
window = 5
|
||||
open = [result["open"][i] for i in range(0, size, window)]
|
||||
close = [result["close"][i-1] for i in range(window-1, size, window)]
|
||||
for i in range(len(open)-len(close)):
|
||||
close.append(result["close"][len(result["close"])-1])
|
||||
high = [max(result["high"][i:i+window]) for i in range(0, size, window)]
|
||||
low = [min(result["low"][i:i+window]) for i in range(0, size, window)]
|
||||
vol = [sum(result["vol"][i:i+window]) for i in range(0, size, window)]
|
||||
open = result["open"]
|
||||
close = result["close"]
|
||||
high = result["high"]
|
||||
low = result["low"]
|
||||
vol = result["vol"]
|
||||
|
||||
close_df = pd.DataFrame(close)
|
||||
ma2_list = close_df.rolling(window=2).mean().fillna(close[0]).values.tolist()
|
||||
ma2 = [item[0] for item in ma2_list]
|
||||
ma5_list = close_df.rolling(window=5).mean().fillna(close[0]).values.tolist()
|
||||
ma5 = [item[0] for item in ma5_list]
|
||||
ma10_list = close_df.rolling(window=10).mean().fillna(close[0]).values.tolist()
|
||||
ma10 = [item[0] for item in ma10_list]
|
||||
ma15_list = close_df.rolling(window=15).mean().fillna(close[0]).values.tolist()
|
||||
ma15 = [item[0] for item in ma15_list]
|
||||
ma20_list = close_df.rolling(window=20).mean().fillna(close[0]).values.tolist()
|
||||
ma20 = [item[0] for item in ma20_list]
|
||||
avg5_list = close_df.rolling(window=3).mean().fillna(close[0]).values.tolist()
|
||||
avg5 = [item[0] for item in avg5_list]
|
||||
avg20_list = close_df.rolling(window=5).mean().fillna(close[0]).values.tolist()
|
||||
avg20 = [item[0] for item in avg20_list]
|
||||
avg60_list = close_df.rolling(window=10).mean().fillna(close[0]).values.tolist()
|
||||
avg60 = [item[0] for item in avg60_list]
|
||||
avg120_list = close_df.rolling(window=20).mean().fillna(close[0]).values.tolist()
|
||||
avg120 = [item[0] for item in avg120_list]
|
||||
avg240_list = close_df.rolling(window=40).mean().fillna(close[0]).values.tolist()
|
||||
avg240 = [item[0] for item in avg240_list]
|
||||
|
||||
upper, lower = [], []
|
||||
for i in range(len(upper_df)):
|
||||
@@ -534,16 +534,14 @@ class HTS:
|
||||
upper.append(upper_df.values[i][0])
|
||||
lower.append(lower_df.values[i][0])
|
||||
|
||||
point_temp = [result["time"][i] for i in range(size) if i % window == 0]
|
||||
upper_temp = [upper[i] for i in range(size) if i % window == 0]
|
||||
lower_temp = [lower[i] for i in range(size) if i % window == 0]
|
||||
point_temp = result["time"]
|
||||
|
||||
temp = {"Date": point_temp, "Open": open, "High": high, "Low": low, "Close": close, "Volume": vol, "ma2": ma2, "ma5": ma5, "ma10": ma10, "ma15": ma15, "ma20": ma20}
|
||||
temp = {"Date": point_temp, "Open": open, "High": high, "Low": low, "Close": close, "Volume": vol, "avg5": avg5, "avg20": avg20, "avg60": avg60, "avg120": avg120, "avg240": avg240}
|
||||
data = pd.DataFrame(temp)
|
||||
df_final_time = pd.DatetimeIndex(point_temp)
|
||||
data.index = df_final_time
|
||||
|
||||
return data, upper_temp, lower_temp
|
||||
return data, upper, lower
|
||||
|
||||
def draw(self, given_day, data, upper, lower, bsLine):
|
||||
buy_line = bsLine['buy']
|
||||
@@ -555,11 +553,11 @@ class HTS:
|
||||
data['Low'] = pd.to_numeric(data['Low'])
|
||||
data['Close'] = pd.to_numeric(data['Close'])
|
||||
data['Volume'] = pd.to_numeric(data['Volume'])
|
||||
data['ma2'] = pd.to_numeric(data['ma2'])
|
||||
data['ma5'] = pd.to_numeric(data['ma5'])
|
||||
data['ma10'] = pd.to_numeric(data['ma10'])
|
||||
data['ma15'] = pd.to_numeric(data['ma15'])
|
||||
data['ma20'] = pd.to_numeric(data['ma20'])
|
||||
data['avg5'] = pd.to_numeric(data['avg5'])
|
||||
data['avg20'] = pd.to_numeric(data['avg20'])
|
||||
data['avg60'] = pd.to_numeric(data['avg60'])
|
||||
data['avg120'] = pd.to_numeric(data['avg120'])
|
||||
data['avg240'] = pd.to_numeric(data['avg240'])
|
||||
|
||||
buy_colors = []
|
||||
for i in range(len(buy_line)):
|
||||
@@ -581,290 +579,246 @@ class HTS:
|
||||
sell_check = go.Scatter(x=data['Date'], y=sell_line, mode='markers', name="sell", marker=dict(size=14, color=sell_colors, line_width=0))
|
||||
bolinger_upper = go.Scatter(x=data['Date'], y=upper, name="upper", line_color='#8B4513')
|
||||
bolinger_lower = go.Scatter(x=data['Date'], y=lower, name="lower", line_color='#8B4513')
|
||||
ma2 = go.Scatter(x=data['Date'], y=data['ma2'], name="ma2", line_color='#FF0000')
|
||||
ma5 = go.Scatter(x=data['Date'], y=data['ma5'], name="ma5", line_color='#F43B86')
|
||||
ma10 = go.Scatter(x=data['Date'], y=data['ma10'], name="ma10", line_color='#F0A500')
|
||||
ma15 = go.Scatter(x=data['Date'], y=data['ma15'], name="ma15", line_color='#14279B')
|
||||
ma20 = go.Scatter(x=data['Date'], y=data['ma20'], name="ma20", line_color='#000000')
|
||||
avg5 = go.Scatter(x=data['Date'], y=data['avg5'], name="avg5", line_color='#FF0000')
|
||||
avg20 = go.Scatter(x=data['Date'], y=data['avg20'], name="avg20", line_color='#F43B86')
|
||||
avg60 = go.Scatter(x=data['Date'], y=data['avg60'], name="avg60", line_color='#F0A500')
|
||||
avg120 = go.Scatter(x=data['Date'], y=data['avg120'], name="avg120", line_color='#14279B')
|
||||
avg240 = go.Scatter(x=data['Date'], y=data['avg240'], name="avg240", line_color='#000000')
|
||||
|
||||
|
||||
candle_stick = go.Candlestick(x=data['Date'], open=data['Open'], high=data['High'], low=data['Low'], close=data['Close'], increasing_line_color='red', decreasing_line_color='blue')
|
||||
|
||||
# 그래프를 그린다.
|
||||
fig = go.Figure(data=[candle_stick, bolinger_upper, bolinger_lower, buy_check, sell_check, ma2, ma10, ma15, ma20])
|
||||
fig = go.Figure(data=[candle_stick, bolinger_upper, bolinger_lower, buy_check, sell_check, avg5, avg20, avg60, avg120, avg240])
|
||||
fig.update_layout(title=given_day + "_2x")
|
||||
fig.show()
|
||||
return
|
||||
|
||||
def checkTransaction(self, data, upper, lower):
|
||||
low = data["Low"]
|
||||
high = data["High"]
|
||||
close = data["Close"]
|
||||
open = data["Open"]
|
||||
ma2 = data["ma2"]
|
||||
ma10 = data["ma10"]
|
||||
ma15 = data["ma15"]
|
||||
ma20 = data["ma20"]
|
||||
def checkStatus(self, STOCK, last_index):
|
||||
status = set()
|
||||
|
||||
# 정배열 체크
|
||||
temp_status = self.common.check_RightArrange(STOCK, last_index)
|
||||
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 checkTransaction_Realtime(self, data, upper, lower):
|
||||
size = len(data["Close"])
|
||||
STOCK = []
|
||||
for i in range(size):
|
||||
STOCK.append({'close': data["Close"][i], 'open': data["Open"][i], 'high': data["High"][i], 'low': data["Low"][i], 'avg5': data["avg5"][i], 'avg20': data["avg20"][i], 'avg60': data["avg60"][i], 'avg120': data["avg120"][i]})
|
||||
|
||||
bsLine = {}
|
||||
bsLine['buy'] = [-1 for i in range(len(lower))]
|
||||
bsLine['sell'] = [-1 for i in range(len(lower))]
|
||||
# buy 체크
|
||||
for i in range(3, len(lower)):
|
||||
#1
|
||||
if close[i] < ma2[i] < lower[i]:
|
||||
if low[i-2] == open[i-2] < close[i-2] < high[i-2]:
|
||||
if low[i-1] == close[i-1] < open[i-1] < high[i-1]:
|
||||
if low[i] == close[i] < open[i] < high[i]:
|
||||
bsLine['buy'][i+1] = close[i] + 5
|
||||
bsLine['sell'][i + 1] = close[i] + 15
|
||||
continue
|
||||
|
||||
#2
|
||||
if lower[i-2] > lower[i-1] and lower[i-1] < lower[i]:
|
||||
if lower[i-2] > low[i-2]:
|
||||
if open[i-2] < close[i-2] and open[i-1] < close[i-1] and open[i] < close[i]:
|
||||
bsLine['buy'][i + 1] = open[i]
|
||||
bsLine['sell'][i + 1] = open[i] + 10
|
||||
continue
|
||||
i = size - 1
|
||||
status = self.checkStatus(STOCK, i)
|
||||
count_1 = 0
|
||||
if "arrange_" in status: count_1 += 1
|
||||
if "arrange_" in status and "HIGHERUMBONG_" in status: count_1 += 1
|
||||
if "20_" in status: count_1 += 1
|
||||
if "60_" in status: count_1 += 1
|
||||
if "120_" in status: count_1 += 1
|
||||
if "240_" in status: count_1 += 1
|
||||
if "5-20_" in status: count_1 += 1
|
||||
if "GOLDEN#2_" in status: count_1 += 1
|
||||
if "GOLDEN#3_" in status: count_1 += 1
|
||||
if "UMYANG_" in status: count_1 += 1
|
||||
if "DOJI_" in status: count_1 += 1
|
||||
if "DRAGONEFLY_" in status: count_1 += 1
|
||||
if "HAMMER_" in status: count_1 += 1
|
||||
if "ENHIGH_" in status: count_1 += 1
|
||||
if "HAHIGH_" in status: count_1 += 1
|
||||
if "PIERCING_" in status: count_1 += 1
|
||||
if "MORNINGSTAR_" in status: count_1 += 1
|
||||
count_0 = 0
|
||||
if "GRAVESTONE_" in status: count_0 += 1
|
||||
if "HANGINGMAN_" in status: count_0 += 1
|
||||
if "ENLOW_" in status: count_0 += 1
|
||||
if "HALOW_" in status: count_0 += 1
|
||||
if "DARKCLOUD_" in status: count_0 += 1
|
||||
if "EVENINGSTAR" in status: count_0 += 1
|
||||
|
||||
if lower[i-2] > lower[i-1] > lower[i]:
|
||||
# 3
|
||||
if high[i-2] == open[i-2] > close[i-2] > low[i-2] and close[i-2] < ma2[i-2]:
|
||||
if high[i-1] > open[i-1] > close[i-1] == low[i-1] and close[i-1] < ma2[i-1]:
|
||||
if ma2[i-1] == ma2[i] and low[i] == open[i] < close[i] < high[i]:
|
||||
bsLine['buy'][i + 1] = close[i]
|
||||
bsLine['sell'][i + 1] = close[i] + 10
|
||||
continue
|
||||
if count_0 == 0 and count_1 > 0:
|
||||
bsLine['buy'][i + 1] = STOCK[i]['close'] - 5
|
||||
bsLine['sell'][i + 1] = STOCK[i]['close']
|
||||
if count_0 > 0:
|
||||
bsLine['sell'][i + 1] = STOCK[i]['close'] + 5
|
||||
|
||||
#4
|
||||
if upper[i] < upper[i-2] < upper[i-1]:
|
||||
if ma10[i-2] > ma10[i-1] > ma2[i-2] > ma2[i-1] == ma2[i]:
|
||||
if high[i-2] == open[i-2] > close[i-2] == low[i-2]:
|
||||
if high[i-1] == open[i-1] > close[i-1] == low[i-1]:
|
||||
if high[i] > close[i] > open[i] == low[i]:
|
||||
bsLine['buy'][i + 1] = close[i]
|
||||
bsLine['sell'][i + 1] = close[i] + 5
|
||||
continue
|
||||
|
||||
#5
|
||||
if upper[i-2] > upper[i-1] and upper[i-1] < upper[i]:
|
||||
if lower[i - 2] > lower[i - 1] and lower[i - 1] < lower[i]:
|
||||
if open[i-1] < ma2[i-1] and open[i-1]==low[i-1] < close[i-1]==high[i-1]:
|
||||
if open[i]==low[i] < close[i] == high[i]:
|
||||
if close[i] > ma2[i] and close[i] > ma10[i] and close[i] > ma15[i] and close[i] > ma20[i]:
|
||||
bsLine['buy'][i+1] = close[i] + 5
|
||||
bsLine['sell'][i + 1] = close[i] + 25
|
||||
continue
|
||||
|
||||
#6
|
||||
if lower[i] < lower[i-2] < lower[i-1]:
|
||||
if lower[i-2] < low[i-2] < close[i-2] < open[i-2] == high[i-2]:
|
||||
if low[i-1] < lower[i-1] < close[i-1]==open[i-1] < high[i-1]:
|
||||
if low[i] < lower[i] < close[i] == open[i] < high[i]:
|
||||
bsLine['buy'][i + 1] = close[i]
|
||||
bsLine['sell'][i + 1] = close[i] + 5
|
||||
continue
|
||||
|
||||
#7
|
||||
if low[i-2] < open[i-2] == close[i-2] == high[i-2]:
|
||||
if low[i - 1] < open[i - 1] == close[i - 1] == high[i - 1]:
|
||||
if low[i] < open[i] < close[i] == high[i]:
|
||||
if ma2[i] < close[i] and ma10[i] < close[i] and ma15[i] < close[i] and ma20[i] < close[i]:
|
||||
if lower[i-2] < lower[i-1] < lower[i]:
|
||||
if upper[i-2] > upper[i-1] > upper[i]:
|
||||
bsLine['buy'][i + 1] = close[i]
|
||||
bsLine['sell'][i + 1] = close[i] + 10
|
||||
continue
|
||||
|
||||
#8
|
||||
if low[i-2] < close[i-2] < open[i-2] == high[i-2]:
|
||||
if low[i-1] < open[i-1] < close[i-1] == high[i-1]:
|
||||
if low[i] < open[i] < close[i] == high[i]:
|
||||
if lower[i-1] < lower[i] < lower[i-2]:
|
||||
if upper[i] < upper[i-2] < upper[i-1]:
|
||||
bsLine['buy'][i + 1] = open[i]
|
||||
bsLine['sell'][i + 1] = open[i] + 5
|
||||
continue
|
||||
|
||||
#9
|
||||
if low[i-2] == open[i-2] < close[i-2] < high[i-2]:
|
||||
if low[i-1] == close[i-1] < open[i-1] < high[i-1]:
|
||||
if low[i] < open[i] == close[i] < high[i] and close[i-1] < close[i]:
|
||||
if low[i-2] == low[i-1] and open[i-2] == close[i-1] and close[i-2] == open[i-1] and high[i-2] == high[i-1]:
|
||||
bsLine['buy'][i + 1] = open[i]
|
||||
bsLine['sell'][i + 1] = open[i] + 15
|
||||
continue
|
||||
|
||||
#10
|
||||
if low[i-2] == close[i-2] < open[i-2] == high[i-2]:
|
||||
if low[i-1] == open[i-1] < close[i-1] == high[i-1]:
|
||||
if low[i] == close[i] < open[i] == high[i]:
|
||||
if high[i-2] == high[i-1] == high[i]:
|
||||
if low[i-2] == low[i] < low[i-1]:
|
||||
bsLine['buy'][i + 1] = open[i-1]
|
||||
bsLine['sell'][i + 1] = open[i-1] + 5
|
||||
continue
|
||||
|
||||
# 11
|
||||
if low[i-2] < close[i-2] == open[i-2] < high[i-2]:
|
||||
if low[i-1] < close[i - 1] < open[i - 1] == high[i - 1]:
|
||||
if low[i] < close[i] == open[i] < high[i]:
|
||||
if low[i-2] == low[i] and high[i-2] == high[i] and low[i-1] < low[i-2] and high[i-1] < high[i-2]:
|
||||
if ma2[i - 2] > ma2[i - 1] > ma2[i]:
|
||||
bsLine['buy'][i + 1] = close[i]
|
||||
bsLine['sell'][i + 1] = close[i] + 15
|
||||
continue
|
||||
|
||||
# 12
|
||||
if low[i - 2] < close[i - 2] < open[i - 2] < high[i - 2]:
|
||||
if low[i - 1] == open[i - 1] < close[i - 1] < high[i - 1]:
|
||||
if low[i] == open[i] < close[i] == high[i]:
|
||||
if lower[i-2] > lower[i-1] and ma2[i-2] > ma2[i-1] and ma2[i-1] < ma2[i]:
|
||||
bsLine['buy'][i + 1] = close[i-1]
|
||||
bsLine['sell'][i + 1] = close[i-1] + 5
|
||||
continue
|
||||
|
||||
# 13
|
||||
if low[i-2] == open[i-2] < close[i-2] < high[i-2]:
|
||||
if low[i-1] == close[i-1] < open[i-1] == high[i-1]:
|
||||
if low[i] == open[i] < close[i] < high[i]:
|
||||
if low[i-2] == low[i] > low[i-1]:
|
||||
bsLine['buy'][i + 1] = close[i]
|
||||
bsLine['sell'][i + 1] = close[i] + 5
|
||||
continue
|
||||
|
||||
# 14
|
||||
if low[i-2] < open[i-2] == close[i-2] < high[i-2]:
|
||||
if low[i-1] < open[i-1] == close[i-1] < high[i-1]:
|
||||
if low[i] < open[i] == close[i] < high[i]:
|
||||
if low[i-2] < ma2[i-2] < high[i-2] and low[i-1] < ma2[i-1] < high[i-1] and low[i] < ma2[i] < high[i]:
|
||||
if high[i - 2] < ma10[i - 2] and high[i - 1] < ma10[i - 1] and high[i] < ma10[i]:
|
||||
bsLine['buy'][i + 1] = close[i]
|
||||
bsLine['sell'][i + 1] = close[i] + 5
|
||||
continue
|
||||
|
||||
# 15
|
||||
if low[i - 2] < close[i - 2] < open[i - 2] == high[i - 2]:
|
||||
if low[i-2] < lower[i-2] and high[i-2] < upper[i-2]:
|
||||
if low[i - 1] == open[i - 1] < close[i - 1] <= high[i - 1]:
|
||||
if low[i - 1] > lower[i - 1] and high[i - 1] < upper[i - 1]:
|
||||
if low[i] <= open[i] < close[i] == high[i]:
|
||||
if low[i] > lower[i] and high[i] > upper[i]:
|
||||
bsLine['buy'][i + 1] = close[i] - 5
|
||||
bsLine['sell'][i + 1] = close[i] + 5
|
||||
continue
|
||||
|
||||
# 16
|
||||
if low[i - 3] < close[i - 3] < open[i - 3] == high[i - 3]:
|
||||
if low[i-3] > lower[i-3] and high[i-3] < upper[i-3]:
|
||||
if low[i - 2] < close[i - 2] < open[i - 2] == high[i - 2]:
|
||||
if low[i - 2] < lower[i - 2] and high[i - 2] < upper[i - 2]:
|
||||
if low[i - 1] <= open[i - 1] < close[i - 1] == high[i - 1]:
|
||||
if low[i - 1] < lower[i - 1] and high[i - 1] > upper[i - 1]:
|
||||
if low[i] < open[i] == close[i] < high[i]:
|
||||
if low[i] < lower[i]:
|
||||
bsLine['buy'][i + 1] = close[i] - 5
|
||||
bsLine['sell'][i + 1] = close[i] + 5
|
||||
continue
|
||||
|
||||
# 16
|
||||
if low[i - 2] == close[i - 2] == open[i - 2] < high[i - 2]:
|
||||
if low[i - 2] < lower[i - 2]:
|
||||
if low[i - 1] == close[i - 1] == open[i - 1] < high[i - 1]:
|
||||
if low[i] == open[i] < close[i] == high[i]:
|
||||
bsLine['buy'][i + 1] = close[i]
|
||||
bsLine['sell'][i + 1] = close[i] + 20
|
||||
continue
|
||||
|
||||
# 17
|
||||
if low[i - 3] == open[i - 3] == close[i - 3] < high[i - 3]:
|
||||
if high[i - 3] > upper[i - 3]:
|
||||
if low[i - 2] < open[i - 2] == close[i - 2] < high[i - 2]:
|
||||
if high[i - 2] > upper[i - 2] and low[i - 2] < lower[i - 2]:
|
||||
if low[i - 1] < open[i - 1] == close[i - 1] < high[i - 1]:
|
||||
if low[i - 1] < lower[i - 1]:
|
||||
if low[i] == open[i] < close[i] <= high[i]:
|
||||
bsLine['buy'][i + 1] = close[i] + 5
|
||||
bsLine['sell'][i + 1] = close[i] + 20
|
||||
continue
|
||||
|
||||
# 18
|
||||
if open[i - 3] > close[i - 3]:
|
||||
if low[i - 3] < lower[i - 3] and low[i - 3] < ma2[i - 3]:
|
||||
if low[i - 2] == close[i - 2] < open[i - 2] <= high[i - 2]:
|
||||
if low[i - 2] < lower[i - 2] and low[i - 2] < ma2[i - 2]:
|
||||
if low[i - 1] < open[i - 1] == close[i - 1] <= high[i - 1]:
|
||||
if close[i - 2] < close[i - 1]:
|
||||
if low[i] < open[i] == close[i] <= high[i]:
|
||||
if close[i - 1] < close[i]:
|
||||
bsLine['buy'][i + 1] = close[i]+5
|
||||
bsLine['sell'][i + 1] = close[i] + 20
|
||||
continue
|
||||
|
||||
# 19
|
||||
if low[i-3] < open[i - 3] < close[i - 3] == high[i-3] < ma20[i-3]:
|
||||
if low[i - 2] < close[i - 2] == open[i - 2] <= high[i - 2] < ma20[i-2]:
|
||||
if low[i - 1] < open[i - 1] <= close[i - 1] == high[i - 1] < ma20[i-1]:
|
||||
if low[i] <= open[i] < close[i] == high[i]:
|
||||
if open[i-3] <= open[i-2] <= open[i-1] or close[i-2] <= close[i-1] <= close[i]:
|
||||
bsLine['buy'][i + 1] = close[i]+5
|
||||
bsLine['sell'][i + 1] = close[i] + 15
|
||||
continue
|
||||
return bsLine
|
||||
|
||||
"""
|
||||
def checkTransaction(self, data, upper, lower):
|
||||
low = data["Low"]
|
||||
high = data["High"]
|
||||
close = data["Close"]
|
||||
open = data["Open"]
|
||||
ma2 = data["ma2"]
|
||||
ma5 = data["ma5"]
|
||||
ma10 = data["ma10"]
|
||||
ma15 = data["ma15"]
|
||||
ma20 = data["ma20"]
|
||||
|
||||
# 살 시점인지 체크
|
||||
# 볼린저밴드 하단에 연속으로 같은 가격이 왔을 때,
|
||||
# 해당 하단 가격 + 5원에 매수를 시도함
|
||||
check = False
|
||||
buy_line = [-1 for i in range(len(lower))]
|
||||
for i in range(3, len(lower)):
|
||||
# 하락 추세에서는 매수하지 않는다.
|
||||
if ma2[i] < ma5[i] < ma10[i]:
|
||||
continue
|
||||
def checkTransaction_Simulation(self, data, upper, lower):
|
||||
size = len(data["Close"])
|
||||
STOCK = []
|
||||
for i in range(size):
|
||||
STOCK.append({'close': data["Close"][i], 'open': data["Open"][i], 'high': data["High"][i], 'low': data["Low"][i], 'volume':data["Volume"][i], 'avg5': data["avg5"][i], 'avg20': data["avg20"][i], 'avg60': data["avg60"][i], 'avg120': data["avg120"][i], 'avg240': data["avg240"][i]})
|
||||
|
||||
for j in range(i-3, i):
|
||||
if (low[j] < lower[j]) and (low[i] < lower[i] and low[j] == low[i]):
|
||||
#buy_line[i] = low[i]
|
||||
check = True
|
||||
break
|
||||
if check and i < len(lower) - 1:
|
||||
buy_line[i+1] = low[i] + 5
|
||||
check = False
|
||||
bsLine = {}
|
||||
bsLine['buy'] = [-1 for i in range(len(lower))]
|
||||
bsLine['sell'] = [-1 for i in range(len(lower))]
|
||||
|
||||
if low[i-2] < lower[i-2] and low[i-1] < lower[i-1] and low[i] < open[i] == close[i] < high[i] and open[i] - low[i] == high [i] - open[i]:
|
||||
if not (open[i-2] < close[i-2] and open[i-1] < close[i-1]) and not (open[i-2] == close[i-2] or open[i-1] == close[i-1]):
|
||||
buy_line[i+1] = high[i]
|
||||
for i in range(5, size-1):
|
||||
status = self.checkStatus(STOCK, i)
|
||||
count_1 = 0
|
||||
if "arrange_" in status: count_1 += 1
|
||||
if "arrange_" in status and "HIGHERUMBONG_" in status: count_1 += 1
|
||||
if "20_" in status: count_1 += 1
|
||||
if "60_" in status: count_1 += 1
|
||||
if "120_" in status: count_1 += 1
|
||||
if "240_" in status: count_1 += 1
|
||||
if "5-20_" in status: count_1 += 1
|
||||
if "GOLDEN#2_" in status: count_1 += 1
|
||||
if "GOLDEN#3_" in status: count_1 += 1
|
||||
if "UMYANG_" in status: count_1 += 1
|
||||
if "DOJI_" in status: count_1 += 1
|
||||
if "DRAGONEFLY_" in status: count_1 += 1
|
||||
if "HAMMER_" in status: count_1 += 1
|
||||
if "ENHIGH_" in status: count_1 += 1
|
||||
if "HAHIGH_" in status: count_1 += 1
|
||||
if "PIERCING_" in status: count_1 += 1
|
||||
if "MORNINGSTAR_" in status: count_1 += 1
|
||||
count_0 = 0
|
||||
if "GRAVESTONE_" in status: count_0 += 1
|
||||
if "HANGINGMAN_" in status: count_0 += 1
|
||||
if "ENLOW_" in status: count_0 += 1
|
||||
if "HALOW_" in status: count_0 += 1
|
||||
if "DARKCLOUD_" in status: count_0 += 1
|
||||
if "EVENINGSTAR" in status: count_0 += 1
|
||||
|
||||
# 2일과 5일선이 10일 선 위로 올라오면 매수한다.
|
||||
if (ma2[i-1] < ma10[i-1] and ma5[i-1] < ma10[i-1]) and (ma10[i] < ma5[i] < ma2[i]):
|
||||
buy_line[i+1] = low[i] + 5
|
||||
if count_0 == 0 and count_1 > 0:
|
||||
bsLine['buy'][i + 1] = STOCK[i]['close'] - 5
|
||||
bsLine['sell'][i + 1] = STOCK[i]['close']
|
||||
if count_0 > 0:
|
||||
bsLine['sell'][i+1] = STOCK[i]['close'] + 5
|
||||
|
||||
# 팔 시점 체크
|
||||
# 산 가격에 5원 위로 매도를 건다.
|
||||
sell_line = [-1 for i in range(len(lower))]
|
||||
for i in range(len(buy_line)):
|
||||
if buy_line[i] > 0:
|
||||
for j in range(i+1, len(buy_line)):
|
||||
# 5원 이득을 보고 판다.
|
||||
if close[j] >= buy_line[i] + 5:
|
||||
sell_line[j] = buy_line[i] + 5
|
||||
break
|
||||
return bsLine
|
||||
|
||||
return buy_line, sell_line
|
||||
"""
|
||||
|
||||
def simulate(self, stock_code, given_day):
|
||||
#timecheckList = pd.read_csv("timecheck.csv").values.tolist()
|
||||
@@ -885,7 +839,7 @@ class HTS:
|
||||
data, upper, lower = self.analyze(result)
|
||||
|
||||
# 사야 할 시점과 팔아야 할 시점을 체크한다.
|
||||
bsLine = self.checkTransaction(data, upper, lower)
|
||||
bsLine = self.checkTransaction_Simulation(data, upper, lower)
|
||||
|
||||
# 그래프를 그린다.
|
||||
self.draw(given_day, data, upper, lower, bsLine)
|
||||
@@ -930,7 +884,7 @@ class HTS:
|
||||
data, upper, lower = self.analyze(result)
|
||||
|
||||
# 사야 할 시점과 팔아야 할 시점을 체크한다.
|
||||
bsLine = self.checkTransaction(data, upper, lower)
|
||||
bsLine = self.checkTransaction_Realtime(data, upper, lower)
|
||||
|
||||
buy_line = bsLine['buy']
|
||||
|
||||
@@ -959,7 +913,7 @@ class HTS:
|
||||
if total_byu_amt < 7000000:
|
||||
self.requestOrder("2", stock_code, buy_count , price)
|
||||
|
||||
## 매도 주문 (아래 잔고를 체킇서 매도를 호출하는 것으로 시도한다.)
|
||||
## 매도 주문 (아래 잔고를 체크해서 매도를 호출하는 것으로 시도한다.)
|
||||
#time.sleep(60)
|
||||
#self.requestOrder("1", stock_code, buy_count , price + 5)
|
||||
print("BUY", second, price)
|
||||
@@ -980,34 +934,22 @@ class HTS:
|
||||
if jangoDic and len(jangoDic.keys()) > 0:
|
||||
for code in jangoDic:
|
||||
total_byu_amt = jangoDic[code]['매입금액']
|
||||
|
||||
if jangoDic[code]['매도가능'] > 0:
|
||||
# 가장 마지막 현재가를 가져온다.
|
||||
if len(result['close']) > 0:
|
||||
last_price = result['close'][len(result['close'])-1]
|
||||
else:
|
||||
last_price = 0
|
||||
# 현재가가 장부가보다 크다면 현재가 + 5원 매도한다.
|
||||
if last_price > int(jangoDic[code]['장부가']):
|
||||
# 현재가가 장부가보다 크다면,
|
||||
self.requestOrder("1", stock_code, jangoDic[code]['매도가능'], last_price)
|
||||
else:
|
||||
# 장부가가 현재가보다 크다면,
|
||||
# 장부가의 마지막 자리수를 가져온다.
|
||||
last_number = int(jangoDic[code]['장부가']) % 10
|
||||
# 장부가 가격의 마지막 자리를 0으로 만든다. (2092 -> 2090, 2098 -> 2090)
|
||||
# 장부가 가격의 마지막 자리를 0으로 만든다. (2090 -> 2090, 2092 -> 2090, 2098 -> 2090)
|
||||
sell_price = int(jangoDic[code]['장부가'] / 10) * 10
|
||||
# 만약 오후 1시 이전이라면 한 호가 (5원) 더 올려서 매도한다.
|
||||
if datetime.now() < datetime.strptime(given_day + " 104000", '%Y%m%d %H%M%S'):
|
||||
if lower != None:
|
||||
if lower[len(lower)-2] < lower[len(lower)-1]:
|
||||
if datetime.strptime(given_day + " 092000", '%Y%m%d %H%M%S') < datetime.now() < datetime.strptime(given_day + " 104000", '%Y%m%d %H%M%S'):
|
||||
lower_size = len(lower)
|
||||
if lower != None and lower_size > 3:
|
||||
if lower[lower_size-3] < lower[lower_size-2] < lower[lower_size-1]:
|
||||
sell_price += 5
|
||||
|
||||
# 장부가의 마지막 자리수를 가져온다.
|
||||
last_number = int(jangoDic[code]['장부가']) % 10
|
||||
if last_number in [0, 1, 2]:
|
||||
# 장부가의 마지막 자리수가 0,1,2 라면 (2090, 2091, 2092 -> 2095 에 매도)
|
||||
self.requestOrder("1", stock_code, jangoDic[code]['매도가능'], sell_price + 5)
|
||||
elif last_number in [3, 4, 5, 6]:
|
||||
|
||||
# 장부가의 마지막 자리수가 3,4,5,6 라면 (2093, 2094, 2095, 2096 -> 2100 에 매도)
|
||||
self.requestOrder("1", stock_code, jangoDic[code]['매도가능'], sell_price + 10)
|
||||
else:
|
||||
@@ -1035,9 +977,7 @@ if __name__ == "__main__":
|
||||
#hts.currentStock(stock_code)
|
||||
#hts.printStockData(stock_code, given_day)
|
||||
|
||||
given_days = ['20210909','20210910','20210913','20210914','20210915','20210916','20210917','20210923','20210924','20210927','20210928','20210929','20210930']
|
||||
given_days = ['20210909','20210913','20210914','20210915','20210916']
|
||||
given_days = ['20210913']
|
||||
given_days = ['20210909','20210910','20210913','20210914','20210915','20210916','20210917','20210923','20210924','20210927','20210928','20210929','20210930','20211001']
|
||||
for given_day in given_days:
|
||||
hts.simulate(stock_code, given_day)
|
||||
|
||||
|
||||
1047
hts/HTS_5min.py
Normal file
1047
hts/HTS_5min.py
Normal file
File diff suppressed because it is too large
Load Diff
@@ -128,6 +128,8 @@ class Common:
|
||||
|
||||
return score
|
||||
|
||||
# YANGBONG
|
||||
# 어제 음봉 이후 장대양봉이었다면, 매수
|
||||
def checkLongYangBongAfterUmBong(self, stock, i):
|
||||
if i > 0:
|
||||
if stock[i-1]['close'] < stock[i-1]['open']: # 어제가 음봉인지 체크
|
||||
@@ -160,6 +162,7 @@ class Common:
|
||||
return "GRAVESTONE_"
|
||||
return ""
|
||||
|
||||
# 하락 추세에서 드레곤플라이가 나오면 매수
|
||||
def checkDragonfly(self, stock, i):
|
||||
# 하락 추세이고, 그저께, 어제 음봉이고, 오늘 드레곤플라이인지 체크한다
|
||||
if i > 1:
|
||||
|
||||
Reference in New Issue
Block a user