156 lines
5.3 KiB
Python
156 lines
5.3 KiB
Python
import os
|
|
from datetime import datetime, timedelta
|
|
import pandas as pd
|
|
import plotly.graph_objects as go
|
|
import plotly
|
|
|
|
from hts.BuySellChecker import BuySellChecker
|
|
|
|
class WebViewer:
|
|
|
|
buySellChecker = None
|
|
|
|
def __init__(self):
|
|
self.buySellChecker = BuySellChecker()
|
|
return
|
|
|
|
def getCSV(self, type, fileName, given_day, result):
|
|
data = pd.read_csv(fileName)
|
|
|
|
days = data.날짜
|
|
time = data.시간
|
|
open = data.시가
|
|
close = data.종가
|
|
high = data.고가
|
|
low = data.저가
|
|
vol = data.거래량
|
|
start_time = datetime.strptime(given_day + " 090000", '%Y%m%d %H%M%S')
|
|
|
|
for i in range(len(data)):
|
|
if type == "S":
|
|
temp = datetime.strptime(str(days[i]) + " " + str(time[i]).zfill(4), '%Y%m%d %H%M%S')
|
|
else:
|
|
temp = datetime.strptime(str(days[i]) + " " + str(time[i]).zfill(4) + "00", '%Y%m%d %H%M%S')
|
|
if temp < start_time:
|
|
continue
|
|
|
|
if temp not in result["check"]:
|
|
result["check"].add(temp)
|
|
result["time"].append(temp)
|
|
result["open"].append(open[i])
|
|
result["close"].append(close[i])
|
|
result["high"].append(high[i])
|
|
result["low"].append(low[i])
|
|
result["vol"].append(vol[i])
|
|
return
|
|
|
|
def checkTransaction(self, data):
|
|
size = len(data["close"])
|
|
|
|
bsLine = {}
|
|
bsLine['buy'] = [-1 for i in range(size)]
|
|
bsLine['weight'] = [-1 for i in range(size)]
|
|
bsLine['sell'] = [-1 for i in range(size)]
|
|
|
|
for i in range(6, size-5):
|
|
if self.stock_code == "252670":
|
|
buy, weight, sell = self.buySellChecker.getPriceAndWeight1(data, i)
|
|
else:
|
|
buy, weight, sell = self.buySellChecker.getPriceAndWeight2(data, i)
|
|
bsLine['buy'][i] = buy
|
|
bsLine['weight'][i] = weight
|
|
bsLine['sell'][i] = sell
|
|
|
|
return bsLine
|
|
|
|
def draw(self, stock_code, given_day, data):
|
|
|
|
data['open'] = pd.to_numeric(data['open'])
|
|
data['high'] = pd.to_numeric(data['high'])
|
|
data['low'] = pd.to_numeric(data['low'])
|
|
data['close'] = pd.to_numeric(data['close'])
|
|
|
|
|
|
# 그래프를 설정한다.
|
|
bolinger_upper = go.Scatter(x=data['date'], y=data["upper"], name="upper", line_color='#8B4513')
|
|
bolinger_lower = go.Scatter(x=data['date'], y=data["lower"], name="lower", line_color='#8B4513')
|
|
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')
|
|
buy_check = go.Scatter(x=data['date'], mode='markers', name="buy_check")
|
|
candle_data = [candle_stick, bolinger_upper, bolinger_lower, buy_check]
|
|
|
|
# 그래프를 그린다.
|
|
# mothod #1
|
|
fig = go.FigureWidget(data=candle_data)
|
|
fig.update_layout(height=800, title=stock_code + "_" + given_day)
|
|
fig.show()
|
|
#plotly.offline.plot(fig, filename='file.html')
|
|
""" # mothod #2
|
|
fig = subplots.make_subplots(rows=1, cols=1, subplot_titles=('캔들'))
|
|
for trace in candle_data:
|
|
fig.append_trace(trace, 1, 1)
|
|
fig.update_layout(height=800, title=stock_code + "_" + given_day)
|
|
fig.show()
|
|
"""
|
|
|
|
return
|
|
|
|
def simulate(self, stock_code, type, GIVEN_DAY, IN_DIR, OUT_DIR, fileName):
|
|
result = {"check": set(),
|
|
"time": [],
|
|
"open": [],
|
|
"close": [],
|
|
"high": [],
|
|
"low": [],
|
|
"vol": []}
|
|
|
|
# 데이터를 가지고 온다.
|
|
self.getCSV(type, IN_DIR+"/"+fileName, GIVEN_DAY, result)
|
|
|
|
# 분석을 통해서 볼린저밴드 상/하단을 계산한다.
|
|
data = self.buySellChecker.analyze(result)
|
|
|
|
# 그래프를 그린다.
|
|
self.draw(stock_code, GIVEN_DAY, data)
|
|
|
|
return
|
|
|
|
if __name__ == "__main__":
|
|
|
|
PROJECT_HOME = os.path.join(os.path.dirname(os.path.join(os.path.dirname(os.path.join(os.path.dirname(__file__))))))
|
|
RSC_DIR = PROJECT_HOME + "/resources"
|
|
IN_DIR = RSC_DIR + "/raw"
|
|
OUT_DIR = RSC_DIR + "/tagging"
|
|
|
|
stock_code = "252670"
|
|
#stock_code = "122630"
|
|
start_date = "20220602"
|
|
|
|
webViewer = WebViewer()
|
|
zero_count_m, zero_count_S = 0, 10
|
|
for i in range(100000):
|
|
given_day = datetime.strptime(start_date, '%Y%m%d') - timedelta(i)
|
|
given_day = given_day.strftime('%Y%m%d')
|
|
|
|
"""
|
|
if zero_count_S < 10:
|
|
type = "S"
|
|
fileName = stock_code + "_" + given_day + "_" + type + ".csv"
|
|
if not os.path.isfile(IN_DIR+"/"+fileName):
|
|
zero_count_S += 1
|
|
else:
|
|
zero_count_S = 0
|
|
webViewer.simulate(stock_code, type, given_day, IN_DIR, OUT_DIR, fileName)
|
|
"""
|
|
if zero_count_m < 10:
|
|
type = "m"
|
|
fileName = stock_code + "_" + given_day + "_" + type + ".csv"
|
|
if not os.path.isfile(IN_DIR + "/" + fileName):
|
|
zero_count_m += 1
|
|
else:
|
|
zero_count_m = 0
|
|
webViewer.simulate(stock_code, type, given_day, IN_DIR, OUT_DIR, fileName)
|
|
|
|
if zero_count_m > 10 and zero_count_S > 10:
|
|
break
|
|
print ("done...")
|