Advertisement
gry1994tistorycom

Untitled

Jul 1st, 2020
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. # 나스닥 데이터 받기 (2009년 이후)
  2.  
  3. import win32com.client
  4. import pandas as pd
  5.  
  6. # 기본 세팅
  7. objCpStatus = win32com.client.Dispatch('CpUtil.CpCybos')
  8.  
  9. bConnect = objCpStatus.IsConnect
  10. if (bConnect == 0):
  11.     print("PLUS가 정상적으로 연결되지 않음. ")
  12.     exit()
  13. else : print("Plus 연결 성공")
  14.  
  15. # 지수 코드네임, 이름 찾기
  16. objUscode = win32com.client.Dispatch('CpUtil.CpUsCode')
  17. codelist = objUscode.GetUsCodeList(2)
  18.  
  19. for i in codelist :
  20.     codename = objUscode.GetNameByUsCode(i)
  21.  
  22.  
  23. # COMP : 나스닥 종합
  24.  
  25. objForeindex = win32com.client.Dispatch('Dscbo1.CpSvr8300')
  26.  
  27. objForeindex.SetInputValue(0,"COMP") # 나스닥
  28. objForeindex.SetInputValue(1,ord("D")) # 일자별
  29. objForeindex.SetInputValue(3,9999) # 일자별
  30. objForeindex.BlockRequest()
  31.  
  32. cnt =  objForeindex.GetHeaderValue(3) # 데이터 수
  33.  
  34. date_list = []
  35. open_list = []
  36. high_list = []
  37. low_list = []
  38. close_list = []
  39. vol_list = []
  40.  
  41. while True :
  42.     for i in range(0,cnt) :
  43.         date_list.append(objForeindex.GetDataValue(0,i))
  44.         open_list.append(objForeindex.GetDataValue(1,i))
  45.         high_list.append(objForeindex.GetDataValue(2,i))
  46.         low_list.append(objForeindex.GetDataValue(3,i))
  47.         close_list.append(objForeindex.GetDataValue(4,i))
  48.         vol_list.append(objForeindex.GetDataValue(5,i))
  49.  
  50.     objForeindex.BlockRequest()
  51.     cnt =  objForeindex.GetHeaderValue(3) # 데이터 수
  52.     if  objForeindex.Continue == False : # 더 요청할 데이터가 없으면 break, 있으면 계속 요청!
  53.         break
  54.     response_codes = ["COMP"] * len(date_list)
  55.    
  56. dict1 = {'logDate' : date_list, 'stockcode': response_codes, 'priceOpen' :open_list, 'priceHigh' : high_list, 'priceLow' : low_list,
  57.          'priceClose' : close_list, 'volume' : vol_list}
  58.  
  59. df = pd.DataFrame(dict1,
  60.                   columns=['logDate', 'stockcode', 'priceOpen', 'priceHigh', 'priceLow', 'priceClose', 'volume']) # 2. 데이터프레임으로 만들기
  61. df = df.sort_values(by=['logDate'], ascending=True)
  62. df.to_csv("나스닥",header=True,index=False)
  63.  
  64. # DB에 저장하고 싶을 경우
  65. from sqlalchemy import create_engine
  66. engine = create_engine("mssql+pyodbc://서버명/DB명?driver=SQL+Server", echo=False)
  67. engine.connect() # 연결
  68. df.to_sql(name='LogDay', con=engine, if_exists='append', index=False) # df를 LogDay 테이블에 저장해줌
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement