{"id":1152,"date":"2016-01-27T11:27:50","date_gmt":"2016-01-27T16:27:50","guid":{"rendered":"http:\/\/www.xavignu.com\/?p=1152"},"modified":"2016-04-08T05:16:59","modified_gmt":"2016-04-08T09:16:59","slug":"python-script-to-download-all-nasdaq-and-nyse-ticker-symbols","status":"publish","type":"post","link":"https:\/\/www.xavignu.com\/?p=1152","title":{"rendered":"Python script to download all NASDAQ and NYSE ticker symbols"},"content":{"rendered":"<p>Below is a short <a href=\"http:\/\/www.python.org\" target=\"_blank\">python<\/a> script to get all <a href=\"http:\/\/www.nasdaq.com\" target=\"_blank\">NASDAQ<\/a> and <a href=\"https:\/\/www.nyse.com\/index\" target=\"_blank\">NYSE<\/a> common stock tickers. You can then use the resulting file to get a lot of info using <a href=\"https:\/\/pypi.python.org\/pypi\/yahoo-finance\" target=\"_blank\">yahoofinance<\/a> library.<\/p>\n<p>[python]<br \/>\n#!\/usr\/bin\/env python<\/p>\n<p>import ftplib<br \/>\nimport os<br \/>\nimport re<\/p>\n<p># Connect to ftp.nasdaqtrader.com<br \/>\nftp = ftplib.FTP(&#8216;ftp.nasdaqtrader.com&#8217;, &#8216;anonymous&#8217;, &#8216;anonymous@debian.org&#8217;)<\/p>\n<p># Download files nasdaqlisted.txt and otherlisted.txt from ftp.nasdaqtrader.com<br \/>\nfor ficheiro in [&#8220;nasdaqlisted.txt&#8221;, &#8220;otherlisted.txt&#8221;]:<br \/>\n        ftp.cwd(&#8220;\/SymbolDirectory&#8221;)<br \/>\n        localfile = open(ficheiro, &#8216;wb&#8217;)<br \/>\n        ftp.retrbinary(&#8216;RETR &#8216; + ficheiro, localfile.write)<br \/>\n        localfile.close()<br \/>\nftp.quit()<\/p>\n<p># Grep for common stock in nasdaqlisted.txt and otherlisted.txt<br \/>\nfor ficheiro in [&#8220;nasdaqlisted.txt&#8221;, &#8220;otherlisted.txt&#8221;]:<br \/>\n        localfile = open(ficheiro, &#8216;r&#8217;)<br \/>\n        for line in localfile:<br \/>\n                if re.search(&#8220;Common Stock&#8221;, line):<br \/>\n                        ticker = line.split(&#8220;|&#8221;)[0]<br \/>\n                        # Append tickers to file tickers.txt<br \/>\n                        open(&#8220;tickers.txt&#8221;,&#8221;a+&#8221;).write(ticker + &#8220;\\n&#8221;)<br \/>\n[\/python]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below is a short python script to get all NASDAQ and NYSE common stock tickers. You can then use the resulting file to get a lot of info using yahoofinance library. [python] #!\/usr\/bin\/env python import ftplib import os import re # Connect to ftp.nasdaqtrader.com ftp = ftplib.FTP(&#8216;ftp.nasdaqtrader.com&#8217;, &#8216;anonymous&#8217;, &#8216;anonymous@debian.org&#8217;) # Download files nasdaqlisted.txt and otherlisted.txt [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[62,3],"tags":[44,6,23,67,70],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/pTQgt-iA","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/1152"}],"collection":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1152"}],"version-history":[{"count":5,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/1152\/revisions"}],"predecessor-version":[{"id":1389,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/1152\/revisions\/1389"}],"wp:attachment":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}