diff --git a/hts/HTS.py b/hts/HTS.py index a47fbb0..da7e62d 100644 --- a/hts/HTS.py +++ b/hts/HTS.py @@ -1,10 +1,10 @@ -import win32com.client +#import win32com.client import time import os from datetime import datetime, timedelta import pandas as pd from enum import Enum -#import plotly.graph_objects as go +import plotly.graph_objects as go # enum 주문 상태 세팅용 @@ -514,8 +514,8 @@ class HTS: vol = [sum(result["vol"][i:i+window]) for i in range(0, size, window)] close_df = pd.DataFrame(close) - ma3_list = close_df.rolling(window=3).mean().fillna(close[0]).values.tolist() - ma3 = [item[0] for item in ma3_list] + 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() @@ -538,7 +538,7 @@ class HTS: 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] - temp = {"Date": point_temp, "Open": open, "High": high, "Low": low, "Close": close, "Volume": vol, "ma3": ma3, "ma5": ma5, "ma10": ma10, "ma15": ma15, "ma20": ma20} + temp = {"Date": point_temp, "Open": open, "High": high, "Low": low, "Close": close, "Volume": vol, "ma2": ma2, "ma5": ma5, "ma10": ma10, "ma15": ma15, "ma20": ma20} data = pd.DataFrame(temp) df_final_time = pd.DatetimeIndex(point_temp) data.index = df_final_time @@ -552,7 +552,7 @@ class HTS: data['Low'] = pd.to_numeric(data['Low']) data['Close'] = pd.to_numeric(data['Close']) data['Volume'] = pd.to_numeric(data['Volume']) - data['ma3'] = pd.to_numeric(data['ma3']) + 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']) @@ -578,17 +578,17 @@ 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') - ma3 = go.Scatter(x=data['Date'], y=data['ma3'], name="ma3", line_color='#F43B86') - ma5 = go.Scatter(x=data['Date'], y=data['ma5'], name="ma5", line_color='#6D9886') - ma10 = go.Scatter(x=data['Date'], y=data['ma10'], name="ma10", line_color='#406343') + 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='#212121') + ma20 = go.Scatter(x=data['Date'], y=data['ma20'], name="ma20", 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, ma3, ma5, ma10, ma15, ma20]) + fig = go.Figure(data=[candle_stick, bolinger_upper, bolinger_lower, buy_check, sell_check, ma2, ma5, ma10, ma15, ma20]) fig.update_layout(title=given_day + "_2x") fig.show() return @@ -598,6 +598,11 @@ class HTS: high = data["High"] close = data["Close"] open = data["Open"] + ma2 = data["ma2"] + ma5 = data["ma5"] + ma10 = data["ma10"] + ma15 = data["ma15"] + ma20 = data["ma20"] # 살 시점인지 체크 # 볼린저밴드 하단에 연속으로 같은 가격이 왔을 때, @@ -605,20 +610,28 @@ class HTS: 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 + 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 + 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] + # 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 + # 팔 시점 체크 # 산 가격에 5원 위로 매도를 건다. sell_line = [-1 for i in range(len(lower))] @@ -799,11 +812,11 @@ 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'] + given_days = ['20210909','20210910','20210913','20210914','20210915','20210916','20210917','20210923','20210924','20210927','20210928','20210929'] #given_days = ['20210929'] - #for given_day in given_days: - # hts.simulate(stock_code, given_day) + for given_day in given_days: + hts.simulate(stock_code, given_day) - hts.buyRealTime(stock_code, given_day) + #hts.buyRealTime(stock_code, given_day) print ("done...")