ilonacodes
Patreon
Investor Profile
Contact

Financial tips and advice on investment for developers:
"Learn how to free extra monthly cash from your dev-salary for investment"

There are so many different ways to free money for investments without thinking of getting another job. Download my Top-15 cash-freeing tips cheat sheet.

Get my 15 Tips to Invest More Cash Monthly!
13
Jan
2021
#python
#developer
#investment
#trading
#stocks
#s&p500

How Developers Can Get Symbols From Stock Indexes With Python

13 Jan, 2021

source: Photo by Nataliya Vaitkevich from Pexels

If you are a software developer who is interested in day trading and stock investment, then you are definitely the one who is also looking for efficient ways to automate stock analysis and even develop your tools for custom trading or investment strategies.

With a software engineering background, I’m not an exception for this!

These days accurate data is the most precious asset for financial market participants. Hence, it’s not cheap.

No longer Yahoo Finance and Google Finance allow developers to access their full APIs. So, now when dealing with financial reference data, a significant part of the struggle is to get decent constituent data of stock indexes. For example, for my purposes, I can’t easily pull the list of stock symbols of the US indexes below anymore:

List of Indexes

If you day trade regularly or from time to time or use more advanced trading strategies with derivatives, you understand how it’s important to screen the market daily and view the list of companies in the major stock indexes for analysis.

In my case, I need the tickers/symbols of US stocks to implement my stock screener. That will allow me to select the best stocks in play for the upcoming intraday. The problem with the existing solutions:

‣ they are usually pricey (from $95 per month)

‣ they have lots of data that my strategies do not require

Unfortunately, there are very few free real-time data providers left. Still, Alpha Vantage is one of the data providers that allows sending 500 requests via their API per day before going premium, and it’s enough for me to implement the tools I needed.

If you want to learn more about what kind of tools I will implement or purchase for my investment and trading strategies, you can join my regular newsletter (+bonus: Top-15 tips to free more cash monthly).

My stock screener is a program that can filter stocks based on specific criteria. To loop through all stocks, I need to fetch the list of most US stock symbols.

There are several ways to scrape this data from financial websites, keeping it hardcoded as a list, or simply requesting the index data from Wikipedia or other index websites. Before fetching from web-pages, I inspected where the data is present in their HTML tags to find enclosed table tags inside. Despite that, I decided to make it easier and just downloaded the .html pages of S&P500, NASDAQ, etc., with the stock symbols into the tables:

import pandas as pd

sp_table = pd.read_html('sp500.html')
sp = sp_table[0]
sp.to_csv('sp500-symbols.csv', columns=['Symbol'])

sp = pd.read_csv('sp500-symbols.csv')
sp_symbols = sp['Symbol'].to_list()
print(sp_symbols)  

Output

The read_html function is scraping the stored web-page related to the S&P500 data and return a list of DataFrame objects. In my case, I am only willing to get the data from the “Symbol” column: the DataFrame object at index 0. Then I extract the table with results to my project directory using the to_csv function. Next, the to_list function converts the table’s data to the list type.

Do the same for the list of stocks in the NASDAQ:

nasdaq_table = pd.read_html('nasdaq.html')
nasdaq = nasdaq_table[0]
nasdaq.to_csv('nasdaq-symbols.csv', columns=['Symbol'])

nasdaq = pd.read_csv('nasdaq-symbols.csv')
nasdaq_symbols = nasdaq['Symbol'].to_list()
print(nasdaq_symbols)

Output

You can continue pulling symbols of different stock indexes from the stored web-pages. The last step is to concatenate the stock symbols into one list:

all_symbols = sp_symbols + nasdaq_symbols
print(all_symbols)

Output

This code should be capable of scraping the details of most symbols in the stock indexes.

The concatenated data could be saved as a CSV or a JSON file. Instead of writing the data to any file, you can also connect it to a MySQL database.

If you are in the world of trading, investment, and finance, you probably should keep an eye on thousands of stocks on various stock markets. Sometimes getting the “stock symbol for the company ABC” is not what you only need and that most APIs allow you to do for free.

In the next post, I will show you how the fetched stock symbols from the indexes have been applied to the tool I have implemented to find well-defined entry and exit points for one of my trading strategies.


Disclaimer: Author’s opinions are their own and do not constitute financial advice in any way whatsoever. Nothing published by Author constitutes an investment recommendation, nor should any data or content published by Author be relied upon for any investment activities.

Tags | #python #developer #investment #trading #stocks #s&p500
Next: Bitcoin Profit Calculator For Developers

My name is Ilona, and I'm a software engineer, founder, and blogger who is (besides programming) also passionate about startups, finance and investment

"I like to work hard for things that are worth it."
The latest articles:
Numbrs Review - App For Personal Finances (Germany & UK only)
How Developers Can Get Symbols From Stock Indexes With Python
Bitcoin Profit Calculator For Developers

Imprint
Privacy Policy
© 2021 ilonacodes
© 2021 ilonacodes
Imprint
Privacy Policy