How to download list of stock symbols from Nasdaq

Other Day I got a question from one of the clients using our financial utility application. They wanted to know how do I get list of symbols of all stocks listed on Nasdaq. There is no rocket science or secret about it. So I thought I will share some code here.

Nasdaq symbols download

Nasdaq provides this list on their FTP folder. You can access all details from here. As you can see from this that it is very well documented. Only thing you need is some kind of implementation that can download this data and parse it as per your needs. Since I do most of work in C# or Swift, I am going to share C# code that I have in our daily scheduled job that pulls this list and processes it further for more statistical data download.

public class StockExchangeService
{
        private const string NasdaqBaseFtpUrl = "ftp://ftp.nasdaqtrader.com/";
        private const string NasdaqSymbolsFtpFolder = "symboldirectory";
        private const string NasdaqSymbolsFileName = "nasdaqlisted.txt";
        private const string OthersListedSymbolsFileName = "otherlisted.txt";
        private StorageConfiguration _storageConfiguration;
        private readonly string _symbolsStorageFolder;

        public StockExchangeService(StorageConfiguration storage)
        {
            _storageConfiguration = storage;
            _symbolsStorageFolder = Path.Combine(storage.RootStorageFolder, "symbols");
            if (!Directory.Exists(_symbolsStorageFolder)) Directory.CreateDirectory(_symbolsStorageFolder);
        }
        public List<StockSymbol> GetStockSymbols()
        {
            var symbols = DownloadSymbolsForNasdaq();
            return symbols;
        }

        private List<StockSymbol> DownloadSymbolsForNasdaq()
        {
            var symbolsList = new List<StockSymbol>();
            try
            {
                var ftpUrl = $"{NasdaqBaseFtpUrl}{NasdaqSymbolsFtpFolder}/{NasdaqSymbolsFileName}";
                var ftpClient = new WebClient();
                var symbolsFileData = ftpClient.DownloadData(ftpUrl);
                var symbolsFileName = $"NasdaqSymbols_{DateTime.UtcNow:yyyyMMdd}.txt";
                var symbolsFile = Path.Combine(this._symbolsStorageFolder, symbolsFileName);
                File.WriteAllBytes(symbolsFile, symbolsFileData);
                var lines = File.ReadAllLines(symbolsFile);
                if (lines.Length < 2) return symbolsList;
                // Ignore first and last line
                for (var i = 1; i < lines.Length; i++)
                {
                    var stockSymbol = ParseNasdaqSymbolData(lines[i]);
                    if (null != stockSymbol)
                    {
                        symbolsList.Add(stockSymbol);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return symbolsList;
        }
        /// 
        /// Its pipe delimited data in following format
        /// Symbol|Security Name|Market Category|Test Issue|Financial Status|Round Lot Size|ETF|NextShares
        /// 
        /// 
        /// 
        private static StockSymbol ParseNasdaqSymbolData(string data)
        {
            var dataItems = data.Split("|".ToCharArray());
            if (!dataItems.Any()) return null;
            var symbol = new StockSymbol
            {
                Symbol = dataItems[0].Trim(),
                SecurityName = dataItems[1].Trim(),
                MarketCategory = dataItems[2].Trim(),
                Test = dataItems[3].Trim().ToLower() == "y",
                FinancialStatus = dataItems[4].Trim(),
                RoundLot = Convert.ToInt32(dataItems[5].Trim())
            };
            return symbol;
        }
}
public class StockSymbol
{
        public string Symbol { get; set; }
        public string Exchange { get; set; }
        public string SecurityName { get; set; }
        public string MarketCategory { get; set; }
        public bool Test { get; set; }
        public string FinancialStatus { get; set; }
        public bool Etf { get; set; }
        public int? RoundLot { get; set; }
}

You can call above object to download symbols using code below.

static void Main(string[] args)
{
            var storageConfiguration = new StorageConfiguration {RootStorageFolder = Environment.CurrentDirectory};
            var xService = new StockExchangeService(storageConfiguration);
            var symbols = xService.GetStockSymbols();
            if (symbols == null) return;
            foreach (var symbol in symbols)
            {
                //TODO: Get previous market close data
                System.Console.WriteLine($"{symbol.Symbol} : {symbol.SecurityName}");
            }
}

I hope this code can provide you sufficient information to get started with your own implementation of stock data processing. If you have questions about stock data analysis, feel free to drop me a line.

comments powered by Disqus

Search

Social

Weather

Monthly Posts