Creating a Stock Research Terminal with Python

In this post, we are going to learn how to create a stock research terminal using Python. The Python script will run in the terminal where we will be able to request financial data for our stock analysis.

For instance, we will type in the terminal “IS MSFT” and that will return the Income Statement for Microsoft. We will also have the possibility to indicate the number of quarters that we want to get data. And the option to download the income statement into an Excel file. On top of that we will build many other functions.

How the stock terminal will work?

The Python stock research terminal that we are going to build in this post will be quite easy to use. We are going to build it in a way that will enable us to request reports, prices, etc. directly from the terminal.

The script will run continuously waiting for our commands. For example, if we want to get the latest three dividends payments made by a company, we will input below highlighted code.

welcome to terminal, select option dividends AAPL
number_qts? 3 (to express that we want to get only the last three dividends)

#outcome:
2021-02-11 : Dividend was :0.205
2020-11-12 : Dividend was :0.205
2020-08-13 : Dividend was :0.82

It will work similarly for all other stock research functions that we will build into the code.

Stock Research Terminal Python
Stock Research Terminal Python

What functions we will have in the Stock Research Terminal?

Below are the functions that we will build to the Python investing terminal.

  • Stocks Historical Prices presented as Candlestick Chart
  • Income Statement and option to export to Excel
  • Profile of a company
  • Balance Sheet of the company
  • Estimated value of the company using DCF method
  • And Dividend payments made by a company

Of course, you can build any other functionalities on top of the ones that we will build together. You can use different scripts presented in the Python for Finance section of the blog in order to find new ideas.

Creating a Stock Research Terminal with Python
Photo by Pixabay on Pexels.com

How to build a Stock Research Terminal in Python?

To build a Python stock research tool, we will need to have below packages installed. I have included a link showing how to install them.

  • Pandas to create Pandas DataFrames containing the financial statements of companies
  • Requests to make requests to the API
  • Plotly to plot historical prices

In addition, we need to get an API from financialmodelingprep since it is the site where we will get all financial data. We can get a free API upon registration. It will offer a few free requests per day.

Let’s start with the code below. First thing is to import all packages and provide the api_key that we obtained from financialmodelingprep API:

import requests
import pandas as pd
import plotly.graph_objects as go

import pandas as pd
from datetime import datetime

api_key = 'your api key'

With that, we have built the backbone of the stock research program. Note that to make it run continuously, we will use a While True statement. Then the input method will request us to enter a command in the terminal that will be stored in the comman variable.

For example, if we pass IS AAPL, the script will execute the income_statement(stock) function containing the required code to request the income_statement for AAPL. Note, that we need to pass the ticker after a blank space. That way, we will store the ticker of the company in the stock variable.

while True:
    comman = input('stock?')
    try:
        stock = comman.split(' ')[1]
    except:
        pass
    if comman == 'IS ' + stock :
        income_statement(stock)
    elif comman == 'profile ' + stock:
        profile(stock)
    elif comman == 'BS ' + stock:
    	balance_sheet(stock)
    elif comman == 'dividends ' + stock:
    	dividends(stock)
    elif comman == 'DCF ' + stock:
    	valuation_dcf(stock)
    elif comman == 'quit':
        break
    elif comman == 'prices ' + stock:
        historical_prices(stock)
    else:
        print('Invalid Command.')

Retrieving the Income Statement

The first function of our terminal will be the retrieval of income statement data. We start by asking how many quarters do we want to retrieve data for. Then, based on the answer, we make the API request to retrieve the income statement for the last n quarters.

Finally, we convert the function into a dictionary and ask the user if the retrieve should be exported into an Excel file.

def income_statement(stock):
 	number_qts = input('number_qts').strip()
 	IS = requests.get(f'https://financialmodelingprep.com/api/v3/income-statement/{stock}?period=quarter&limit={number_qts}&apikey={api_key}').json()
 	IS = pd.DataFrame.from_dict(IS)
 	print(IS.T)
 	save_to_csv = input('save_to_csv? y or n').strip()
 	if save_to_csv == 'y':
 		IS.to_csv('BS.csv')

In the next post we will continue building the other functions for our stock research terminal. For now, it should already work for the Income Statement if you run below code.

Note that once it runs, you can check if it works by typing IS AAPL to retrieve the IS from Apple. Then, you can type quit in order to exit the program.

In the continuation of this post, we will see how to further improve our stock investing terminal!

Below is a video explaining step by step how to build the stock research terminal with Python: