This commit is contained in:
dosang.yoon
2022-08-02 15:48:28 +09:00
parent 8d1c4ae041
commit a191fba93a
11 changed files with 279 additions and 53 deletions

View File

@@ -28,14 +28,6 @@ class HTS_122630 (HTS):
self.buySellChecker = BuySellChecker()
return
def checkTransaction(self, data):
size = len(data["close"])
last_index = size - 1
sell, weight = self.buySellChecker.getSellPriceAndWeight_15000(data, last_index)
buy, weight = self.buySellChecker.getBuyPriceAndWeight_15000(data, last_index)
return buy, weight, sell
def getSellingPrice(self, final_price):
# 만약 잔고가 있으면 장부가보다 5원 높게 매도한다.
jangoDic = self.requstJango()
@@ -92,20 +84,20 @@ class HTS_122630 (HTS):
result = self.getRealTime(self.stock_code, today, LAST_DATA)
# 규칙 기반의 분석을 통해서 볼린저밴드 상/하단을 계산한다.
data = self.buySellChecker.analyzeByRule(result)
data = self.buySellChecker.analyze(result)
# 사야 할 시점/가격과 팔아야 할 시점/가격을 체크한다.
bsLine, data = self.buySellChecker.checkTransaction(data, self.stock_code, True)
bs_buy_price = bsLine['buy']
bs_weight = bsLine['buy_weight']
bs_sell_price = bsLine['sell']
bsLine, data = self.buySellChecker.checkTransaction(data, self.stock_code, isRealTime=True)
bs_buy_price = bsLine['buy'][0]
bs_buy_weight = bsLine['buy_weight'][0]
bs_sell_price = bsLine['sell'][0]
data_size = len(data["close"])
final_price = data["close"][data_size-1]
if bs_buy_price > 0:
# 기본 100 주에 가중치를 추가해서 매수한다.
BUY_COUNT = int(self.buy_count * bs_weight)
BUY_COUNT = int(self.buy_count * bs_buy_weight)
# 매수를 주문한다.
orderNum = self.requestOrder(OrderType.buy, self.stock_code, BUY_COUNT, bs_buy_price)

View File

@@ -85,20 +85,20 @@ class HTS_252670 (HTS):
result = self.getRealTime(self.stock_code, today, LAST_DATA)
# 규칙 기반의 분석을 통해서 볼린저밴드 상/하단을 계산한다.
data = self.buySellChecker.analyzeByRule(result)
data = self.buySellChecker.analyze(result)
# 사야 할 시점/가격과 팔아야 할 시점/가격을 체크한다.
bsLine, data = self.buySellChecker.checkTransaction(data, self.stock_code, True)
bs_buy_price = bsLine['buy']
bs_weight = bsLine['buy_weight']
bs_sell_price = bsLine['sell']
bsLine, data = self.buySellChecker.checkTransaction(data, self.stock_code, isRealTime=True)
bs_buy_price = bsLine['buy'][0]
bs_buy_weight = bsLine['buy_weight'][0]
bs_sell_price = bsLine['sell'][0]
data_size = len(data["close"])
final_price = data["close"][data_size-1]
if bs_buy_price > 0:
# 기본 100 주에 가중치를 추가해서 매수한다.
BUY_COUNT = int(self.buy_count * bs_weight)
BUY_COUNT = int(self.buy_count * bs_buy_weight)
# 매수를 주문한다.
orderNum = self.requestOrder(OrderType.buy, self.stock_code, BUY_COUNT , bs_buy_price)

View File

@@ -3,7 +3,6 @@ import pandas as pd
import plotly.graph_objects as go
from plotly import subplots
import os
import sqlite3
from hts.HTS import HTS
from stock.util.Stock2Vector import Stock2Vector
@@ -98,7 +97,7 @@ class Simulation (HTS):
# 그래프를 그린다.
"""
fig = go.Figure(hts=candle_data)
fig = go.Figure(data=candle_data)
fig.update_layout(title=stock_code + "_" + given_day)
fig.show()
"""
@@ -127,8 +126,8 @@ class Simulation (HTS):
return
def getData(self):
return
def analyzeAutoMode(self, data):
return data, None
def simulate(self, stock_code, today, type="rule"):
LAST_DATA = self.stock2Vector.getLastData(stock_code, today)
@@ -136,14 +135,14 @@ class Simulation (HTS):
result = self.stock2Vector.getRealTime(stock_code, today, LAST_DATA)
if type == "rule":
# 규칙 기반의 분석을 통해서 볼린저밴드 상/하단을 계산한다.
data = self.buySellChecker.analyzeByRule(result)
# 이동평균, RSI, MACD, 일목균형, 볼린저밴드 상/하단을 계산한다.
data = self.buySellChecker.analyze(result)
# 사야 할 시점과 팔아야 할 시점을 체크한다.
bsLine, data = self.buySellChecker.checkTransaction(data, stock_code, False)
elif type == "labeltest":
data = self.getData(result)
bsLine, data = self.buySellChecker.checkTransaction(data, stock_code, isRealTime=False)
elif type == "auto":
data, bsLine = self.analyzeAutoMode(result)
else:
data = None
data, bsLine = None, None
if data is not None:
# 그래프를 그린다.

View File

@@ -746,14 +746,7 @@ class BuySellChecker:
return sell, weight
def analyzeByRule(self, result):
def analyze(self, result):
open = result["open"]
close = result["close"]
high = result["high"]
@@ -1161,7 +1154,7 @@ class BuySellChecker:
data = data.fillna(close[0])
return data
def checkTransaction(self, data, stock_code, type=True):
def checkTransaction(self, data, stock_code, isRealTime=True):
# 4일치 중에서 앞에 2일은 제거한다.
date = data['date'].dt.date.unique().tolist()
data = data[data['date'].dt.date != date[0]]
@@ -1170,7 +1163,8 @@ class BuySellChecker:
# 어제 오늘 데이터로 분석
bsLine = {}
size = len(data["close"])
if type:
if isRealTime:
# isRealTime=True, 실시간 적용
last_index = size - 1
if stock_code == "252670":
@@ -1180,25 +1174,28 @@ class BuySellChecker:
sell, weight = self.getSellPriceAndWeight_15000(data, last_index)
buy, weight = self.getBuyPriceAndWeight_15000(data, last_index)
bsLine['buy'] = buy
bsLine['buy_weight'] = weight
bsLine['sell'] = sell
bsLine['sell_weight'] = weight
bsLine['buy'] = [buy]
bsLine['buy_weight'] = [weight]
bsLine['sell'] = [sell]
bsLine['sell_weight'] = [weight]
else:
# Type=False, 시뮬레이션 적용
bsLine['buy'] = [-1 for i in range(size)]
bsLine['weight'] = [-1 for i in range(size)]
bsLine['buy_weight'] = [-1 for i in range(size)]
bsLine['sell'] = [-1 for i in range(size)]
bsLine['sell_weight'] = [-1 for i in range(size)]
for i in range(size):
if stock_code == "252670":
sell, weight = self.getSellPriceAndWeight_3000(data, i)
buy, weight = self.getBuyPriceAndWeight_3000(data, i)
sell, sell_weight = self.getSellPriceAndWeight_3000(data, i)
buy, buy_weight = self.getBuyPriceAndWeight_3000(data, i)
else:
sell, weight = self.getSellPriceAndWeight_15000(data, i)
buy, weight = self.getBuyPriceAndWeight_15000(data, i)
sell, sell_weight = self.getSellPriceAndWeight_15000(data, i)
buy, buy_weight = self.getBuyPriceAndWeight_15000(data, i)
bsLine['buy'][i] = buy
bsLine['weight'][i] = weight
bsLine['buy_weight'][i] = buy_weight
bsLine['sell'][i] = sell
bsLine['sell_weight'][i] = sell_weight
return bsLine, data

View File

@@ -0,0 +1,13 @@
2022-07-27 09:19:00,3060
2022-07-27 09:20:00,3070
2022-07-27 09:21:00,3070
2022-07-27 09:53:00,3075
2022-07-27 09:57:00,3075
2022-07-27 09:58:00,3075
2022-07-27 12:26:00,3090
2022-07-27 12:27:00,3090
2022-07-27 12:31:00,3090
2022-07-27 12:32:00,3090
2022-07-27 12:33:00,3090
2022-07-27 12:36:00,3090
2022-07-27 12:38:00,3090
1 2022-07-27 09:19:00 3060
2 2022-07-27 09:20:00 3070
3 2022-07-27 09:21:00 3070
4 2022-07-27 09:53:00 3075
5 2022-07-27 09:57:00 3075
6 2022-07-27 09:58:00 3075
7 2022-07-27 12:26:00 3090
8 2022-07-27 12:27:00 3090
9 2022-07-27 12:31:00 3090
10 2022-07-27 12:32:00 3090
11 2022-07-27 12:33:00 3090
12 2022-07-27 12:36:00 3090
13 2022-07-27 12:38:00 3090

View File

@@ -0,0 +1,8 @@
2022-07-27 09:05:00,3085
2022-07-27 09:06:00,3085
2022-07-27 09:08:00,3085
2022-07-27 09:34:00,3085
2022-07-27 10:29:00,3105
2022-07-27 10:30:00,3105
2022-07-27 13:07:00,3105
2022-07-27 13:24:00,3105
1 2022-07-27 09:05:00 3085
2 2022-07-27 09:06:00 3085
3 2022-07-27 09:08:00 3085
4 2022-07-27 09:34:00 3085
5 2022-07-27 10:29:00 3105
6 2022-07-27 10:30:00 3105
7 2022-07-27 13:07:00 3105
8 2022-07-27 13:24:00 3105

View File

@@ -0,0 +1,14 @@
2022-07-28 09:07:00,2975
2022-07-28 09:08:00,2985
2022-07-28 09:09:00,2995
2022-07-28 09:10:00,2995
2022-07-28 09:51:00,2995
2022-07-28 09:52:00,3000
2022-07-28 09:53:00,3000
2022-07-28 09:55:00,3000
2022-07-28 09:57:00,3000
2022-07-28 09:58:00,3005
2022-07-28 09:59:00,3005
2022-07-28 11:59:00,3005
2022-07-28 12:00:00,3010
2022-07-28 12:01:00,3010
1 2022-07-28 09:07:00 2975
2 2022-07-28 09:08:00 2985
3 2022-07-28 09:09:00 2995
4 2022-07-28 09:10:00 2995
5 2022-07-28 09:51:00 2995
6 2022-07-28 09:52:00 3000
7 2022-07-28 09:53:00 3000
8 2022-07-28 09:55:00 3000
9 2022-07-28 09:57:00 3000
10 2022-07-28 09:58:00 3005
11 2022-07-28 09:59:00 3005
12 2022-07-28 11:59:00 3005
13 2022-07-28 12:00:00 3010
14 2022-07-28 12:01:00 3010

View File

@@ -0,0 +1,9 @@
2022-07-28 09:32:00,3025
2022-07-28 10:23:00,3035
2022-07-28 10:37:00,3035
2022-07-28 10:38:00,3035
2022-07-28 10:41:00,3035
2022-07-28 10:44:00,3035
2022-07-28 10:46:00,3035
2022-07-28 12:24:00,3030
2022-07-28 14:27:00,3040
1 2022-07-28 09:32:00 3025
2 2022-07-28 10:23:00 3035
3 2022-07-28 10:37:00 3035
4 2022-07-28 10:38:00 3035
5 2022-07-28 10:41:00 3035
6 2022-07-28 10:44:00 3035
7 2022-07-28 10:46:00 3035
8 2022-07-28 12:24:00 3030
9 2022-07-28 14:27:00 3040

View File

@@ -0,0 +1,8 @@
2022-07-29 09:34:00,2945
2022-07-29 09:35:00,2945
2022-07-29 09:36:00,2945
2022-07-29 09:37:00,2945
2022-07-29 09:38:00,2950
2022-07-29 09:39:00,2950
2022-07-29 09:40:00,2950
2022-07-29 12:31:00,2990
1 2022-07-29 09:34:00 2945
2 2022-07-29 09:35:00 2945
3 2022-07-29 09:36:00 2945
4 2022-07-29 09:37:00 2945
5 2022-07-29 09:38:00 2950
6 2022-07-29 09:39:00 2950
7 2022-07-29 09:40:00 2950
8 2022-07-29 12:31:00 2990

View File

@@ -0,0 +1,5 @@
2022-07-29 10:07:00,2985
2022-07-29 10:49:00,2990
2022-07-29 12:53:00,3010
2022-07-29 14:15:00,3015
2022-07-29 14:16:00,3015
1 2022-07-29 10:07:00 2985
2 2022-07-29 10:49:00 2990
3 2022-07-29 12:53:00 3010
4 2022-07-29 14:15:00 3015
5 2022-07-29 14:16:00 3015

View File

@@ -1,8 +1,189 @@
import sqlite3
import os
import csv
from math import nan
import pandas as pd
import plotly.graph_objects as go
from plotly import subplots
from hts.HTS import HTS
from hts.BuySellChecker import BuySellChecker
class LabelMaker (HTS):
buySellChecker = None
class LabelMaker:
def __init__(self):
super().__init__(RESOURCE_PATH)
self.buySellChecker = BuySellChecker()
return
def checkTransaction(self, data):
bsLine = {}
size = len(data["close"])
# Type=False, 시뮬레이션 적용
bsLine['buy'] = [-1 for i in range(size)]
bsLine['buy_weight'] = [-1 for i in range(size)]
bsLine['sell'] = [-1 for i in range(size)]
bsLine['sell_weight'] = [-1 for i in range(size)]
for i in range(size-60):
min_price, min_price_c, max_price, max_price_c = 9999999, -1, 0, -1
for c in range(i, i+60):
if data["close"][c] > max_price:
max_price = data["close"][c]
max_price_c = c
if data["close"][c] < min_price:
min_price = data["close"][c]
min_price_c = c
if min_price_c > 0:
bsLine['buy'][min_price_c] = min_price
bsLine['buy_weight'][min_price_c] = 1
if max_price_c > 0:
bsLine['sell'][max_price_c] = max_price
bsLine['sell_weight'][min_price_c] = 1
return bsLine, data
def draw(self, stock_code, given_day, data, bsLine):
buy_line = bsLine['buy']
sell_line = bsLine['sell']
# 그래프 설정을 위한 변수를 생성한다.
data = data.astype({'open': 'int',
'high': 'int',
'low': 'int',
'close': 'int',
'volume': 'int',
'avg3': 'float',
'avg5': 'float',
'avg10': 'float',
'avg20': 'float',
'avg30': 'float',
'avg60': 'float',
'fast_k': 'float',
'slow_k': 'float',
'slow_d': 'float',
'rsi': 'float',
'rsis': 'float'
})
buy_colors = []
for i in range(len(buy_line)):
if buy_line[i] < 0:
buy_colors.append("#ffffff")
buy_line[i] = nan
else:
buy_colors.append("#ff00ff")
sell_colors = []
for i in range(len(sell_line)):
if sell_line[i] < 0:
sell_colors.append("#ffffff")
sell_line[i] = nan
else:
sell_colors.append("#00ced1")
# 그래프를 설정한다.
buy_check = go.Scatter(x=data['date'], y=buy_line, mode='markers', name="buy", marker=dict(size=14, color=buy_colors, line_width=0))
sell_check = go.Scatter(x=data['date'], y=sell_line, mode='markers', name="sell", marker=dict(size=14, color=sell_colors, line_width=0))
upper = go.Scatter(x=data['date'], y=data["upper"], name="upper", line_color='#000000')
lower = go.Scatter(x=data['date'], y=data["lower"], name="lower", line_color='#000000')
avg3 = go.Scatter(x=data['date'], y=data["avg3"], name="avg3", line_color='#1469F4')
avg5 = go.Scatter(x=data['date'], y=data["avg5"], name="avg5", line_color='#089B5B')
avg10 = go.Scatter(x=data['date'], y=data["avg10"], name="avg10", line_color='#ff00ff')
avg20 = go.Scatter(x=data['date'], y=data["avg20"], name="avg20", line_color='#8F8203')
avg30 = go.Scatter(x=data['date'], y=data["avg30"], name="avg30", 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')
volume_line = go.Scatter(x=data['date'], y=data["volume"], mode='lines', name='volume')
#fast_k_line = go.Scatter(x=hts['date'], y=hts["fast_k"], mode='lines', name='fast_k')
macd_line = go.Scatter(x=data['date'], y=data["macd"], mode='lines', name='macd')
macd_s_line = go.Scatter(x=data['date'], y=data["macds"], mode='lines', name='macds')
macd_o_line = go.Scatter(x=data['date'], y=data["macdo"], mode='lines', name='macdo')
slow_k_line = go.Scatter(x=data['date'], y=data["slow_k"], mode='lines', name='slow_k')
slow_d_line = go.Scatter(x=data['date'], y=data["slow_d"], mode='lines', name='slow_d')
rsi_line = go.Scatter(x=data['date'], y=data["rsi"], mode='lines', name='rsi')
rsis_line = go.Scatter(x=data['date'], y=data["rsis"], mode='lines', name='rsis')
candle_data = [candle_stick, upper, lower, avg3, avg5, avg10, avg20, avg30, buy_check, sell_check]
volume_data = [volume_line]
macd_data = [macd_line, macd_s_line, macd_o_line]
stochastic_data = [slow_k_line, slow_d_line]
rsi_data = [rsi_line, rsis_line]
"""
fig = go.Figure(data=candle_data)
df = pd.DataFrame(bsLine)
df = df.fillna(-1)
buy_count = len(df.loc[df["buy"] > 0])
sell_count = len(df.loc[df["sell"] > 0])
fig.update_layout(height=1000, title=stock_code + "_" + given_day + "_" + str(buy_count)+","+str(sell_count))
fig.show()
"""
fig = subplots.make_subplots(rows=5, cols=1, subplot_titles=('캔들', "거래량", "MACD", "스토캐스틱", "RSI"))
for trace in candle_data:
fig.append_trace(trace, 1, 1)
for trace in volume_data:
fig.append_trace(trace, 2, 1)
for trace in macd_data:
fig.append_trace(trace, 3, 1)
for trace in stochastic_data:
fig.append_trace(trace, 4, 1)
for trace in rsi_data:
fig.append_trace(trace, 5, 1)
#fig.update_xaxes(nticks=5)
#fig.update_layout(height=1800, title=stock_code + "_" + given_day, xaxis_rangeslider_visible=False)
df = pd.DataFrame(bsLine)
df = df.fillna(-1)
buy_count = len(df.loc[df["buy"] > 0])
sell_count = len(df.loc[df["sell"] > 0])
fig.update_layout(height=3000, title=stock_code + "_" + given_day + "_" + str(buy_count)+","+str(sell_count))
fig.show()
return
def writeLabelFile(self, bsLine, data, ymd):
outFileName = os.path.join(self.RESOURCE_PATH, "tmp", ymd+".sell.csv")
with open(outFileName, "w", encoding="utf-8") as outFp:
writer = csv.writer(outFp)
for i, price in enumerate(bsLine["sell"]):
if price != -1:
writer.writerow([data['date'][i], bsLine["sell"][i]])
outFileName = os.path.join(self.RESOURCE_PATH, "tmp", ymd + ".buy.csv")
with open(outFileName, "w", encoding="utf-8") as outFp:
writer = csv.writer(outFp)
for i, price in enumerate(bsLine["buy"]):
if price != -1:
writer.writerow([data['date'][i], bsLine["buy"][i]])
return
def makeCandidate(self, stock_code, ymd="20220727"):
result = {"check": set(), "time": [], "open": [], "close": [], "high": [], "low": [], "vol": [], "label": []}
self.getDBData(stock_code, ymd, result)
data = self.buySellChecker.analyze(result)
bsLine, data = self.checkTransaction(data)
self.writeLabelFile(bsLine, data, ymd)
self.draw(stock_code, ymd, data, bsLine)
return
if __name__ == "__main__":
labelMaker = LabelMaker()
PROJECT_HOME = os.path.join(os.path.dirname(os.path.join(os.path.dirname(os.path.join(os.path.dirname(__file__))))))
RESOURCE_PATH = os.path.join(PROJECT_HOME, "resources")
labelMaker = LabelMaker()
stock_code = "252670"
#stock_code = "122630"
labelMaker.makeCandidate(stock_code, "20220727")