주식 종목 정보는 다양한 사이트에서 다양한 방법(크롤링, API, 다운로드)으로 가져올 수 있습니다.

오늘 소개하는 방법은 TwelveData에서 주식 종목을 가져오는 방법입니다.

https://twelvedata.com/

 

Twelve Data | Stock, Forex, and Crypto Market Data APIs

Try Twelve Data financial APIs for free and get instant access to stock, forex, cryptocurrency, and fundamental market data. Real time, historical, and EOD data.

twelvedata.com

 

 

TwelveData에서는 다양한 국가(미국, 한국, 중국, 일본, 유럽 곳곳까지)의 종목 정보를 가져올 수 있어서 매우 좋은거 같습니다. 그리고, 파이썬 코딩도 비교적 간단합니다. (저만 알고 싶은 곳이지만, 과감히 올려봅니다.) 또한 TwelveData에서는 종목 정보 외에도 주가 정보를 가져올 수 있는데, 무료 정책으로 하루에 800번의 API호출이 가능합니다. 현재 저는 무료 정책을 사용중이며, 주요 종목의 주가를 일별로 추적하기에는 800번의 API 호출이면 충분한거 같습니다.(단, 1분에 8번 제한이 있습니다.)

 

종목 정보를 가져오는 파이썬 소스는 아래와 같습니다. 참고하셔서 각자 환경에 맞게 개발하시면 될거 같습니다.

감사합니다.

import requests
import pandas as pd

def GetStockListFromTwelveData(_country, _type):
    url = "https://api.twelvedata.com/stocks"
    response = requests.get(url)
    js_txt = response.json()
    df = pd.DataFrame(js_txt['data'])  # dict에서 js_txt 키에 해당하는 값만 가져오기
    # print(df.country.unique()) 국가 값 종류 확인
    # print(df.type.unique()) Type 값 종류 확인
    # print(df.columns) # 컬럼 정보 확인
    df2 = df[(df.country == _country) & (df.type == _type)]  # 특정조건 값 가져오기

    return df2

# Contry
# ['Germany' 'China' 'South Korea' 'Hong Kong' 'Malaysia' 'Taiwan' 'India'
# 'United Kingdom' 'Italy' 'Mexico' 'Saudi Arabia' 'Poland' 'Japan''Sweden'
# 'Philippines' 'Thailand' 'Netherlands' 'Denmark' 'Spain''Hungary' 'South Africa'
# 'Brazil' 'Ireland' 'Canada' 'United States''Greece' 'Romania' 'Indonesia' 'Finland'
# 'Switzerland''United Arab Emirates' 'Kuwait' 'Argentina' 'Chile' 'Colombia'
# 'New Zealand' 'Belgium' 'Israel' 'Qatar' 'Russia' 'Botswana' ''
# 'Turkey''Portugal' 'Lithuania' 'Czech Republic' 'Estonia' 'Iceland' 'Latvia''Egypt']

# Type
# ['Common Stock' 'American Depositary Receipt' 'REIT' 'Unit'
#  'Global Depositary Receipt' 'Depositary Receipt' 'Preferred Stock'
#  'Limited Partnership' 'Structured Product' 'Right' 'Warrant' 'Trust'
#  'ETF' 'Mutual Fund']


pd.set_option('display.max_columns',None) # df 출력시 모든 컬럼 출력하도록 처리
pd.set_option('display.width',1000) # df 출력시, 한 로우가 따로 출력되지 않도록 충분한 길이 설정

df = GetStockListFromTwelveData('United States','Common Stock')
# Columns:
print(df)

 

 

 

+ Recent posts