This commit is contained in:
dosang.yoon
2022-06-24 21:25:57 +09:00
parent 0d523678a6
commit 0b4a87b334
5 changed files with 176 additions and 281 deletions

View File

@@ -369,3 +369,125 @@ class HTS:
break
return
# 주식 현재가 조회
def writeStockData(self, stock_code, given_day):
objCpCybos = win32com.client.Dispatch("CpUtil.CpCybos")
bConnect = objCpCybos.IsConnect
if (bConnect == 0):
print("PLUS가 정상적으로 연결되지 않음. ")
exit()
# 차트 객체 구하기
objStockChart = win32com.client.Dispatch("CpSysDib.StockChart")
outfp = open("./data/"+stock_code+"_"+given_day+".csv", mode="w", encoding="utf-8")
objStockChart.SetInputValue(0, 'A' + stock_code) # 종목 코드
objStockChart.SetInputValue(1, ord('1')) # 1: 기간으로 조회, 2: 개수로 조회
objStockChart.SetInputValue(2, given_day) # 기간 조회 시, 시작일
objStockChart.SetInputValue(3, given_day) # 기간 조회 시, 종료일
objStockChart.SetInputValue(4, 400) # 조회 시 가져오는 Line 개수
objStockChart.SetInputValue(5, [0, 1, 2, 3, 4, 5, 8]) # 날짜,시간,시가,고가,저가,종가,거래량
objStockChart.SetInputValue(6, ord('m')) # '차트 주가 - 월(M), 주(W), 일(D), 시(H), 분(m), 초(S) 차트 요청
objStockChart.SetInputValue(7, 1)
objStockChart.SetInputValue(9, ord('1')) # 수정주가 사용
objStockChart.BlockRequest()
size = objStockChart.GetHeaderValue(3)
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)
start = objStockChart.GetDataValue(2, i)
high = objStockChart.GetDataValue(3, i)
low = objStockChart.GetDataValue(4, i)
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
def write(self, day, result):
#날짜,시간,시가,고가,저가,종가,거래량
#20210909,900,2070,2070,2070,2070,0
outFp = open(day+".csv", mode="w", encoding="UTF-8")
outFp.write("날짜,시간,시가,고가,저가,종가,거래량\n")
for i in range(len(result["time"])):
outFp.write("%s,%s,%s,%s,%s,%s,%s\n"%(
result["time"][i].strftime('%Y%m%d'),
result["time"][i].strftime('%H%M'),
result["open"][i],
result["high"][i],
result["low"][i],
result["close"][i],
result["vol"][i]))
outFp.close()
return
# 주식 현재가 조회
def getRealTime(self, stock_code, given_day, result):
int_given_day = int(given_day)
objCpCybos = win32com.client.Dispatch("CpUtil.CpCybos")
bConnect = objCpCybos.IsConnect
if (bConnect == 0):
print("PLUS가 정상적으로 연결되지 않음. ")
exit()
# 차트 객체 구하기
objStockChart = win32com.client.Dispatch("CpSysDib.StockChart")
objStockChart.SetInputValue(0, 'A'+stock_code) # 종목 코드
objStockChart.SetInputValue(1, ord('1')) # 1: 기간으로 조회, 2: 개수로 조회
objStockChart.SetInputValue(2, given_day) # 기간 조회 시, 시작일
objStockChart.SetInputValue(3, given_day) # 기간 조회 시, 종료일
objStockChart.SetInputValue(4, 400) # 조회 시 가져오는 Line 개수
objStockChart.SetInputValue(5, [0, 1, 2, 3, 4, 5, 8]) # 날짜,시간,시가,고가,저가,종가,거래량
objStockChart.SetInputValue(6, ord('m')) # '차트 주가 - 월(M), 주(W), 일(D), 시(H), 분(m), 초(S) 차트 요청
objStockChart.SetInputValue(7, 1)
objStockChart.SetInputValue(9, ord('1')) # 수정주가 사용
objStockChart.BlockRequest()
size = objStockChart.GetHeaderValue(3)
#print("날짜", "시간", "시가", "고가", "저가", "종가", "거래량")
start_time = datetime.strptime(given_day + " 090000", '%Y%m%d %H%M%S')
for i in range(size-1, -1, -1):
int_day = objStockChart.GetDataValue(0, i)
int_time = objStockChart.GetDataValue(1, i)
if int_day < int_given_day:
continue
time = datetime.strptime(str(int_day)+" "+str(int_time).zfill(4)+"00", '%Y%m%d %H%M%S')
if time < start_time:
continue
open = objStockChart.GetDataValue(2, i)
close = objStockChart.GetDataValue(5, i)
high = objStockChart.GetDataValue(3, i)
low = objStockChart.GetDataValue(4, i)
vol = objStockChart.GetDataValue(6, i)
if len(result["check"]) == 0:
result["check"].add(start_time)
result["time"].append(start_time)
result["open"].append(open)
result["close"].append(open)
result["high"].append(open)
result["low"].append(open)
result["vol"].append(0)
if time not in result["check"]:
result["check"].add(time)
result["time"].append(time)
result["open"].append(open)
result["close"].append(close)
result["high"].append(high)
result["low"].append(low)
result["vol"].append(vol)
return