FMP

FMP

Creating a Stock Research Terminal Using 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 re

Creating a Stock Research Terminal Using Python

Sep 11, 2023 5:26 PM - Rajnish Katharotiya

twitterlinkedinfacebook
blog post cover photo

Image credit: Firmbee.com

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. On top of that we will build many other functions.

Photo by Pixabay on Pexels.com

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.

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

  • 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.

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

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.

Other Blogs

Sep 11, 2023 - Rajnish Katharotiya

P/E Ratios Using Normalized Earnings

Price to Earnings is one of the key metrics use to value companies using multiples. The P/E ratio and other multiples are relative valuation metrics and they cannot be looked at in isolation. One of the problems with the P/E metric is the fact that if we are in the peak of a business cycle, earni...

blog post title

Sep 11, 2023 - Rajnish Katharotiya

What is Price To Earnings Ratio and How to Calculate it using Python

Price-to-Earnings ratio is a relative valuation tool. It is used by investors to find great companies at low prices. In this post, we will build a Python script to calculate Price Earnings Ratio for comparable companies. Photo by Skitterphoto on Pexels Price Earnings Ratio and Comparable Compa...

blog post title

Oct 17, 2023 - Davit Kirakosyan

VMware Stock Drops 12% as China May Hold Up the Broadcom Acquisition

Shares of VMware (NYSE:VMW) witnessed a sharp drop of 12% intra-day today due to rising concerns about China's review of the company's significant sale deal to Broadcom. Consequently, Broadcom's shares also saw a dip of around 4%. Even though there aren’t any apparent problems with the proposed solu...

blog post title
FMP

FMP

Financial Modeling Prep API provides real time stock price, company financial statements, major index prices, stock historical data, forex real time rate and cryptocurrencies. Financial Modeling Prep stock price API is in real time, the company reports can be found in quarter or annual format, and goes back 30 years in history.
twitterlinkedinfacebookinstagram
2017-2023 © Financial Modeling Prep