xingAPI
[xingAPI, 파이썬] 주식종목조회 기능 구현하기
한무님
2022. 7. 6. 22:16
반응형
- 자동매매 프로그램 GitHub URL - https://github.com/mujomboy/xingProject.git
GitHub - mujomboy/xingProject
Contribute to mujomboy/xingProject development by creating an account on GitHub.
github.com
[Git] Github 에 있는 파이썬 프로젝트 파이참(PyCharm) 으로 가져오기
1. 파이참에 GitHub Repository URL 연결하기 파이참을 열어 줍니다... 만약 파이참을 열었는데 바로 프로젝트가 오픈 되었다면... 프로젝트를 닫아 주세요. Get From VCS 버튼을 클릭해 줍니다. URL 입력 후
bysik1109.tistory.com
- 주식종목조회 [t8430] 전체 코드
더보기
import pythoncom
import win32com.client
from event.query import QueryEvents
from event.session import SessionEvents
# 세션 객체 요청 및 객체 생성
session = win32com.client.DispatchWithEvents("XA_Session.XASession", SessionEvents)
url = 'demo.ebestsec.co.kr' # 모의 투자
# url = 'hts.ebestsec.co.kr' # 실제 투자
session.ConnectServer(url, 200001)
session.Login("아이디", "비번", "", 0, 0)
while SessionEvents.state == "":
# 로그인 상태 변경 메시지 채크
pythoncom.PumpWaitingMessages()
# 쿼리 이벤트 객체 가져오기
query = win32com.client.DispatchWithEvents("XA_DataSet.XAQuery", QueryEvents)
# 주식종목조회에 해당되는 t8430.res 파일이 있는 경로 가져오기
resfile_path = "C:\\Users\\bysik\\PycharmProjects\\xingProject\\res\\t8430.res"
# 조회 시 입력 파라미터 타입 명 t8430InBlock
inblock = "%sInBlock" % "t8430"
# 조회 시 리턴 파라미터 타입 명 t8430InBlock
outblock = "%sOutBlock" % "t8430"
# 경로를 통해 res 파일를 로드.
query.LoadFromResFile(resfile_path)
# 입력 파리미터를 초기화 합니다. 0 : 전체 조회, 1 : 코스피, 2 : 코스닥
query.SetFieldData(inblock, "gubun", 0, '0')
# 조회 요청
query.Request(0)
while not QueryEvents.state:
# 응답 대기
pythoncom.PumpWaitingMessages()
# 응답이 왔으므로 응답 대기 관련 state 값 초기화
QueryEvents.state = not QueryEvents.state
for i in range(query.GetBlockCount(outblock)):
# 리턴 데이터 추출
hname = query.GetFieldData(outblock, "hname", i).strip() # 종목명
shcode = query.GetFieldData(outblock, "shcode", i).strip() # 단축코드
expcode = query.GetFieldData(outblock, "expcode", i).strip() # 확장코드
etfgubun = query.GetFieldData(outblock, "etfgubun", i).strip() # ETF구분(1:ETF)
uplmtprice = query.GetFieldData(outblock, "uplmtprice", i).strip() # 상한가
dnlmtprice = query.GetFieldData(outblock, "dnlmtprice", i).strip() # 하한가
jnilclose = query.GetFieldData(outblock, "jnilclose", i).strip() # 전일가
memedan = query.GetFieldData(outblock, "memedan", i).strip() # 주문수량단위
recprice = query.GetFieldData(outblock, "recprice", i).strip() # 기준가
gubun = query.GetFieldData(outblock, "gubun", i).strip() # 구분(1:코스피, 2: 코스닥)
print(hname, shcode, expcode, etfgubun, uplmtprice, dnlmtprice, jnilclose, memedan, recprice, gubun)
# 세션 클래스
class SessionEvents:
state = ""
msg = ""
def OnLogin(self, code, msg):
SessionEvents.state = code
SessionEvents.msg = msg
def OnLogout(self):
print("OnLogout")
def OnDisconnect(self):
print("OnDisconnect")
# 쿼리 클래스
class QueryEvents:
state = False
code = ""
error = ""
msgCode = ""
msg = ""
def OnReceiveData(self, code):
QueryEvents.code = code
QueryEvents.state = True
def OnReceiveMessage(self, error, msgCode, msg):
QueryEvents.error = error
QueryEvents.msgCode = msgCode
QueryEvents.msg = msg
반응형