Skip to content

Instantly share code, notes, and snippets.

@mjdargen
Last active December 19, 2025 00:37
Show Gist options
  • Select an option

  • Save mjdargen/7f95630d02640419a0a38da3c008d387 to your computer and use it in GitHub Desktop.

Select an option

Save mjdargen/7f95630d02640419a0a38da3c008d387 to your computer and use it in GitHub Desktop.

Revisions

  1. mjdargen revised this gist Feb 14, 2021. 1 changed file with 25 additions and 2 deletions.
    27 changes: 25 additions & 2 deletions retrieve_nasdaq_securities.py
    Original file line number Diff line number Diff line change
    @@ -44,6 +44,8 @@

    import ftplib
    import os
    import datetime
    from pprint import pprint

    try:
    DIR_PATH = os.path.dirname(os.path.realpath(__file__))
    @@ -57,8 +59,8 @@
    try:
    ftp.login()
    ftp.cwd(f'/{directory}/')
    with open(f'{DIR_PATH}/{filename}', 'wb') as fp:
    res = ftp.retrbinary("RETR " + filename, fp.write)
    with open(f'{DIR_PATH}/{filename}', 'wb') as f:
    res = ftp.retrbinary("RETR " + filename, f.write)
    if not res.startswith('226 Transfer complete'):
    print('Download failed')
    if os.path.isfile(f'{DIR_PATH}/{filename}'):
    @@ -68,3 +70,24 @@
    print('FTP error:', e)
    if os.path.isfile(f'{DIR_PATH}/{filename}'):
    os.remove(f'{DIR_PATH}/{filename}')

    # read into a dictionary
    with open(f'{DIR_PATH}/{filename}', 'r') as f:
    content = f.read().splitlines()

    header = content[0].split('|')
    date = content[-1].split('|')[0][:]
    date = date[len('File Creation Time: '):]
    date = datetime.datetime.strptime(date, '%m%d%Y%H:%M')
    content = content[1:-1]
    print(date)
    print(header)

    securities = []
    for c in content:
    c = c.split('|')
    security = {}
    for i in range(len(c)):
    security[header[i]] = c[i]
    securities.append(security)
    pprint(securities)
  2. mjdargen created this gist Feb 14, 2021.
    70 changes: 70 additions & 0 deletions retrieve_nasdaq_securities.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    # http://www.nasdaqtrader.com/trader.aspx?id=symboldirdefs

    # NASDAQ-Listed Securities
    # File Name: nasdaqlisted.txt
    # FTP Directory: ftp://ftp.nasdaqtrader.com/symboldirectory
    # Field Name Definition
    # Symbol The one to four or five character identifier for each
    # NASDAQ-listed security.
    # Security Name Company issuing the security.
    # Market Category The category assigned to the issue by NASDAQ based on
    # Listing Requirements. Values:
    # Q = NASDAQ Global Select MarketSM
    # G = NASDAQ Global MarketSM
    # S = NASDAQ Capital Market
    # Test Issue Indicates whether or not it is a test security (y/n).
    # Finan. Status Indicates when an issuer has failed to submit its
    # regulatory filings on a timely basis, has failed to meet
    # NASDAQ's continuing listing standards, and/or has filed for
    # bankruptcy. Values include:
    # D = Deficient: Issuer Failed to Meet NASDAQ Continued Listing rqmts
    # E = Delinquent: Issuer Missed Regulatory Filing Deadline
    # Q = Bankrupt: Issuer Has Filed for Bankruptcy
    # N = Normal (Default): Issuer Is NOT Deficient, Delinquent, or Bankrupt.
    # G = Deficient and Bankrupt
    # H = Deficient and Delinquent
    # J = Delinquent and Bankrupt
    # K = Deficient, Delinquent, and Bankrupt
    # Round Lot Indicates the number of shares that make up a round lot for
    # the given security.
    # ETF Indicates whether it is an exchange traded fund (y/n).
    # NextShares Indicates whether it is a NextShare fund (y/n).
    # File Creation: The last row of each Symbol Directory text file contains a
    # timestamp that reports the File Creation Time. The file
    # creation time is based on when NASDAQ Trader generates the
    # file and can be used to determine the timeliness of the
    # associated data. The row contains the words File Creation
    # Time followed by mmddyyyyhhmm as the first field, followed
    # by all delimiters to round out the row. An example:
    # File Creation Time: 1217200717:03|||||

    # Header
    # Symbol|Security Name|Market Category|Test Issue|Financial Status|Round Lot Size|ETF|NextShares
    # Values separated by pipes ('|') and rows separated be newlines

    import ftplib
    import os

    try:
    DIR_PATH = os.path.dirname(os.path.realpath(__file__))
    except NameError:
    DIR_PATH = os.getcwd() # for colab

    with ftplib.FTP('ftp.nasdaqtrader.com') as ftp:
    directory = 'symboldirectory'
    filename = 'nasdaqlisted.txt'

    try:
    ftp.login()
    ftp.cwd(f'/{directory}/')
    with open(f'{DIR_PATH}/{filename}', 'wb') as fp:
    res = ftp.retrbinary("RETR " + filename, fp.write)
    if not res.startswith('226 Transfer complete'):
    print('Download failed')
    if os.path.isfile(f'{DIR_PATH}/{filename}'):
    os.remove(f'{DIR_PATH}/{filename}')

    except ftplib.all_errors as e:
    print('FTP error:', e)
    if os.path.isfile(f'{DIR_PATH}/{filename}'):
    os.remove(f'{DIR_PATH}/{filename}')