collect_dataset.py
4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import win32com.client
import pythoncom
import pandas as pd
import time
class XASessionEventHandler:
login_state = 0
def OnLogin(self, code, msg):
if code == "0000":
print("로그인 성공")
XASessionEventHandler.login_state = 1
else:
print("로그인 실패")
class XAQueryEventHandler:
query_state = 0
def OnReceiveData(self, code):
XAQueryEventHandler.query_state = 1
# ===================================================================================================================
instXASession = win32com.client.DispatchWithEvents("XA_Session.XASession", XASessionEventHandler)
with open('account.txt') as f:
id, passwd, cert_passwd = f.read().split()
instXASession.ConnectServer("hts.ebestsec.co.kr", 20001)
instXASession.Login(id, passwd, cert_passwd, 0, 0)
while XASessionEventHandler.login_state == 0:
pythoncom.PumpWaitingMessages()
datecut = ["20210526", "20190517", "20170426","20150421","20130409","20110407","20090413","20070404","20050404","20030326","20010316","19900305","19880616","19860926","19850316"]
res=pd.DataFrame()
for i in range(0, 14):
# ----------------------------------------------------------------------------
# T8413
# ----------------------------------------------------------------------------
instXAQueryT8413 = win32com.client.DispatchWithEvents("XA_DataSet.XAQuery", XAQueryEventHandler)
instXAQueryT8413.ResFileName = "C:\\eBEST\\xingAPI\\Res\\t8413.res"
instXAQueryT8413.SetFieldData("t8413InBlock", "shcode", 0, "207940")
instXAQueryT8413.SetFieldData("t8413InBlock", "gubun", 0, "2")
instXAQueryT8413.SetFieldData("t8413InBlock", "sdate", 0, datecut[i+1])
instXAQueryT8413.SetFieldData("t8413InBlock", "edate", 0, datecut[i])
instXAQueryT8413.SetFieldData("t8413InBlock", "comp_yn", 0, "N")
instXAQueryT8413.Request(0)
while XAQueryEventHandler.query_state == 0:
pythoncom.PumpWaitingMessages()
count = instXAQueryT8413.GetBlockCount("t8413OutBlock1")
stockdata=[]
datelist=[]
for j in range(count):
date = instXAQueryT8413.GetFieldData("t8413OutBlock1", "date", j)
open = instXAQueryT8413.GetFieldData("t8413OutBlock1", "open", j)
high = instXAQueryT8413.GetFieldData("t8413OutBlock1", "high", j)
low = instXAQueryT8413.GetFieldData("t8413OutBlock1", "low", j)
close = instXAQueryT8413.GetFieldData("t8413OutBlock1", "close", j)
vol = instXAQueryT8413.GetFieldData("t8413OutBlock1", "jdiff_vol", j)
datelist.append(date)
stockdata.append([date, open, high, low, close, vol])
df = pd.DataFrame(stockdata, columns=['날짜','시가','고가','저가','종가','거래량'], index=datelist)
print(df)
res=res.append(df)
XAQueryEventHandler.query_state = 0
time.sleep(3)
# instXAQueryT8413 = win32com.client.DispatchWithEvents("XA_DataSet.XAQuery", XAQueryEventHandler)
# instXAQueryT8413.ResFileName = "C:\\eBEST\\xingAPI\\Res\\t8413.res"
# instXAQueryT8413.SetFieldData("t8413InBlock", "shcode", 0, "005930")
# instXAQueryT8413.SetFieldData("t8413InBlock", "gubun", 0, "2")
# instXAQueryT8413.SetFieldData("t8413InBlock", "sdate", 0, "20190520")
# instXAQueryT8413.SetFieldData("t8413InBlock", "edate", 0, "20210526")
# instXAQueryT8413.SetFieldData("t8413InBlock", "comp_yn", 0, "N")
# instXAQueryT8413.Request(0)
# while XAQueryEventHandler.query_state == 0:
# pythoncom.PumpWaitingMessages()
# count = instXAQueryT8413.GetBlockCount("t8413OutBlock1")
# stockdata.clear()
# for i in range(count):
# date = instXAQueryT8413.GetFieldData("t8413OutBlock1", "date", i)
# open = instXAQueryT8413.GetFieldData("t8413OutBlock1", "open", i)
# high = instXAQueryT8413.GetFieldData("t8413OutBlock1", "high", i)
# low = instXAQueryT8413.GetFieldData("t8413OutBlock1", "low", i)
# close = instXAQueryT8413.GetFieldData("t8413OutBlock1", "close", i)
# vol = instXAQueryT8413.GetFieldData("t8413OutBlock1", "jdiff_vol", i)
# stockdata.append([date, open, high, low, close, vol])
# df2 = df.append(pd.DataFrame(stockdata, columns=['날짜','시가','고가','저가','종가','거래량']))
# print(df2)
res.drop_duplicates()
print(res)
res.to_csv('C:/Users/myung/OneDrive/바탕 화면/삼성바이오로직스.csv', sep=',', na_rep='NaN',index=False)