collect_dataset.py 4.37 KB
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)