[Python] 미국 주식 종목코드

16851 단어 FTPSymbolpythoncsvFTP

Outline

NYSE, NASDAQ에 상장되어 있는 주식의 종목코드를 구해옵니다.

Reference

Code

# Import
from ftplib import FTP
import csv

# Connect FTP Server with user anonymous, passwd anonymous@
ftp = FTP('ftp.nasdaqtrader.com')
ftp.login()

# Move directory
ftp.cwd('symboldirectory')

# Check file list
ftp.retrlines('LIST')

파일 리스트를 보면 받아야 할 파일이 있는 것을 볼 수 있습니다.

04-19-21  09:17AM                 5061 bondslist.txt
04-19-21  09:17AM             44645839 bxoptions.txt
04-16-21  09:34PM               109554 bxo_lmm.csv
04-19-21  09:17AM               842609 bxtraded.txt
04-19-21  08:20AM              5747858 gmniListedStrikesWithOptionIds.zip
04-19-21  08:18AM              6124861 iseListedStrikesWithOptionIds.zip
04-19-21  08:21AM              5767167 mcryListedStrikesWithOptionIds.zip
04-19-21  09:17AM              3729290 mfundslist.txt
04-19-21  09:17AM               212519 mpidlist.txt
04-19-21  09:17AM               287844 nasdaqlisted.txt
04-19-21  09:19AM               842617 nasdaqtraded.txt
04-19-21  09:17AM             56373587 options.txt
07-07-15  03:41PM                35264 otclist.txt
04-19-21  09:17AM               467744 otherlisted.txt
06-15-13  04:00AM                  251 pbot.csv
04-19-21  08:17AM              6396544 phlxListedStrikesWithOptionIds.zip
04-16-21  09:33PM               306973 phlxoptions.csv
03-12-21  10:32PM              3588617 phlxStrikesOld.zip
04-19-21  09:17AM               842612 psxtraded.txt
04-19-21  03:00AM       <DIR>          regnms
04-16-21  11:00PM       <DIR>          regsho
02-14-19  09:17PM       <DIR>          regshopilot
02-14-19  09:17PM       <DIR>          regshopilotlist
04-19-21  04:15AM       <DIR>          shorthalts
03-03-21  11:41AM                    6 test.txt
04-19-21  09:17AM                 3256 TradingSystemAddsDeletes.txt

필요한 파일 두 개를 가져와서 PC에 저장합니다.

# Get Nasdaq Symbols
nasdaq_file = 'nasdaqlisted.txt'
with open(nasdaq_file, 'wb') as fp:
  ftp.retrbinary('RETR ' + nasdaq_file, fp.write)

# Get Other Market Symbols
other_file = 'otherlisted.txt'
with open(other_file, 'wb') as fp:
  ftp.retrbinary('RETR ' + other_file, fp.write)

각 파일에서 필요한 종목들의 코드만 리스트로 저장합니다.

# Get Symbol from Nasdaq File
result_nasdaq = []
with open(nasdaq_file, 'r') as nasdaq_list:
  nasdaq_reader = csv.reader(nasdaq_list, delimiter='|')
  # Skip Header
  next(nasdaq_reader)
  
  for row in nasdaq_reader:
    if row[3] == 'Y':  # Skip Test Symbol
      continue
    if row[4] != 'N':  # Skip if Financial Status isn't normal
      continue
    # Add Symbol to list
    result_nasdaq.append(row[0])

# Get Symbol from Other Market File
result_nyse = []
with open(other_file, 'r') as other_list:
  other_reader = csv.reader(other_list, delimiter='|')
  # Skip Header
  next(other_reader)
  
  for row in other_reader:
    if row[2] != 'N':  # Skip if symbol isn't in NYSE
      continue
    if row[4] == 'Y':  # Skip if symbol is ETF
      continue
    if row[6] == 'Y':  # Skip if symbol is Test Security
      continue
    # Add Symbol to list
    result_nyse.append(row[0])

리스트를 확인하면 코드가 잘 들어왔는지, 개수는 몇 개 인지 확인 할 수 있습니다.

print(str(len(result_nasdaq)) + " " + str(result_nasdaq[0:10]) + " " + str(result_nasdaq[-10:-1]))
# Result
4466 ['AACG', 'AACQ', 'AACQU', 'AACQW', 'AAL', 'AAME', 'AAOI', 'AAON', 'AAPL', 'AAWW'] ['ZNTL', 'ZS', 'ZSAN', 'ZUMZ', 'ZVO', 'ZWRK', 'ZWRKU', 'ZWRKW', 'ZYNE']

print(str(len(result_nyse)) + " " + str(result_nyse[0:10]) + " " + str(result_nyse[-10:-1]))
# Result
3497 ['A', 'AA', 'AAC', 'AAC.U', 'AAC.W', 'AAIC', 'AAIC$B', 'AAIC$C', 'AAN', 'AAP'] ['ZEN', 'ZEPP', 'ZH', 'ZIM', 'ZNH', 'ZTO', 'ZTR', 'ZTS', 'ZUO']

좋은 웹페이지 즐겨찾기