[Python_trading] 파이썬 알고리즘 트레이딩_종목코드 가져오기2 (GetStockListByMarket)

#7.py
import win32com.client

instCpCodeMgr = win32com.client.Dispatch("CpUtil.CpCodeMgr")

#GetStockListByMarket() : 메서드의 인자로 1을 전달하면 유가증권시장의 종목을 파이썬 튜플 형태로 반환받는다. 
codeList = instCpCodeMgr.GetStockListByMarket(1)

kospi = {} #Dictionary
for code in codeList:
    name = instCpCodeMgr.CodeToName(code)
    kospi[code] = name

#kospi 딕셔너리를 csv파일로 저장
f = open("G:\\내 드라이브\\python_API_Programming\\kospi.csv",'w')
for key, value in kospi.items():
    f.write("%s,%s\n"%(key,value))
f.close()


#7_2.py
import win32com.client

instCpCodeMgr = win32com.client.Dispatch("CpUtil.CpCodeMgr")
codeList = instCpCodeMgr.GetStockListByMarket(1)

for i, code in enumerate(codeList):
    secondCode = instCpCodeMgr.GetStockSectionKind(code) #GetStockSectionKind() 를 활용하면 종목이 주권, ETF, ETN  인지 알 수 있다. 
    name = instCpCodeMgr.CodeToName(code)
    print(i,code, secondCode,name)

댓글