init
This commit is contained in:
@@ -15,7 +15,7 @@ class DataDownloader (HTS):
|
||||
return
|
||||
|
||||
# 주식 현재가 조회
|
||||
def writeStockData(self, stock_code, given_day, type, outDir):
|
||||
def writeStockData(self, stock_code, given_day, type, outfp):
|
||||
objCpCybos = win32com.client.Dispatch("CpUtil.CpCybos")
|
||||
bConnect = objCpCybos.IsConnect
|
||||
if (bConnect == 0):
|
||||
@@ -39,8 +39,6 @@ class DataDownloader (HTS):
|
||||
size = objStockChart.GetHeaderValue(3)
|
||||
|
||||
if size > 0:
|
||||
outfp = open(outDir + "/" + stock_code + "_" + given_day + "_" + type + ".csv", mode="w", encoding="utf-8")
|
||||
outfp.write("%s,%s,%s,%s,%s,%s,%s\n" % ("날짜", "시간", "시가", "고가", "저가", "종가", "거래량"))
|
||||
for i in range(size - 1, -1, -1):
|
||||
day = objStockChart.GetDataValue(0, i)
|
||||
time = objStockChart.GetDataValue(1, i)
|
||||
@@ -50,7 +48,6 @@ class DataDownloader (HTS):
|
||||
close = objStockChart.GetDataValue(5, i)
|
||||
vol = objStockChart.GetDataValue(6, i)
|
||||
outfp.write("%d,%s,%d,%d,%d,%d,%d\n" % (day, str(time).zfill(4), start, high, low, close, vol))
|
||||
outfp.close()
|
||||
|
||||
return size
|
||||
|
||||
@@ -65,13 +62,18 @@ if __name__ == "__main__":
|
||||
stock_code = "122630"
|
||||
dataDownloader = DataDownloader()
|
||||
|
||||
outFp_m = open(RESOURCE_DIR + "/" + stock_code + "_m.csv", mode="w", encoding="utf-8")
|
||||
outFp_m.write("%s,%s,%s,%s,%s,%s,%s\n" % ("날짜", "시간", "시가", "고가", "저가", "종가", "거래량"))
|
||||
outFp_S = open(RESOURCE_DIR + "/" + stock_code + "_S.csv", mode="w", encoding="utf-8")
|
||||
outFp_S.write("%s,%s,%s,%s,%s,%s,%s\n" % ("날짜", "시간", "시가", "고가", "저가", "종가", "거래량"))
|
||||
|
||||
zero_count_m, zero_count_S = 0, 0
|
||||
for i in range(1, 100000):
|
||||
given_day = datetime.today() - timedelta(i)
|
||||
|
||||
# '차트 주가 - 월(M), 주(W), 일(D), 시(H), 분(m), 초(S) 차트 요청
|
||||
if zero_count_m < 10:
|
||||
count_m = dataDownloader.writeStockData(stock_code, given_day.strftime('%Y%m%d'), 'm', RESOURCE_DIR)
|
||||
count_m = dataDownloader.writeStockData(stock_code, given_day.strftime('%Y%m%d'), 'm', outFp_m)
|
||||
if count_m == 0:
|
||||
zero_count_m += 1
|
||||
else:
|
||||
@@ -79,7 +81,7 @@ if __name__ == "__main__":
|
||||
print(given_day, "m", count_m)
|
||||
|
||||
if zero_count_S < 10:
|
||||
count_S = dataDownloader.writeStockData(stock_code, given_day.strftime('%Y%m%d'), 'S', RESOURCE_DIR)
|
||||
count_S = dataDownloader.writeStockData(stock_code, given_day.strftime('%Y%m%d'), 'S', outFp_S)
|
||||
if count_S == 0:
|
||||
zero_count_S += 1
|
||||
else:
|
||||
@@ -88,4 +90,7 @@ if __name__ == "__main__":
|
||||
|
||||
if zero_count_m > 10 and zero_count_S > 10:
|
||||
break
|
||||
|
||||
outFp_m.close()
|
||||
outFp_S.close()
|
||||
print ("done...")
|
||||
|
||||
155
hts/util/WebViewer.py
Normal file
155
hts/util/WebViewer.py
Normal file
@@ -0,0 +1,155 @@
|
||||
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...")
|
||||
@@ -2,4 +2,5 @@ pandas
|
||||
plotly
|
||||
matplotlib
|
||||
pandas_datareader
|
||||
bs4
|
||||
bs4
|
||||
ipywidgets==7.0.0
|
||||
Reference in New Issue
Block a user