Do you want to learn how to do fundamental analysis with Python? In this blog post, we will walk through a data-driven approach to analyzing stocks. We will use the Pandas library to load data into a data frame, and then use various techniques to analyze the data. We also use the Fundamental Analysis package to obtain fundamental data. This approach can be used for stocks.
Gathering the data and importing it into Python is the first step. After that, we need to do some data wrangling to get it into the right format. We will then use a variety of technical indicators to do the analysis. Finally, we will backtest our trading strategy on historical data to see how it would have performed.
The first step is to import the data into a Python pandas data frame. To do this, we will use the pandas_datareader library. This library allows us to load data from a variety of sources, including Yahoo Finance. We will also need to import the DateTime library, which we will use to set the start and end dates for our data.
We will use the following code to import the data:
import pandas_datareader.data as web
from datetime import datetime
start = datetime(2016, 12, 31)
end = datetime(2017, 12, 31)
df = web.DataReader(“AAPL”, “yahoo”, start, end)
This code will load the data for Apple Inc. (AAPL) from Yahoo Finance into a pandas data frame. The data will be downloaded for the period from December 31, 2016 to December 31, 2017.
Once the data is loaded, we can start doing some analysis. The first thing we will do is calculate the stock’s return over the period. To do this, we will use the pct_change() method. This method calculates the percentage change between the current row and the previous row. We will also use the head() method to print out the first five rows of the data frame, including the calculated return column.
df[‘return’] = df[‘Adj Close’].pct_change()
We can see from the output that Apple’s stock return was positive in four out of the five days in our data sample. The largest one-day return was about 0.84%.
Fundamental data can be downloaded and analyzed using the Fundamental Analysis package
To install it, type
pip install fundamentalanalysis
into your Terminal.
After we have installed the Fundamental Analysis package and imported the data, we can start to collect different fundamental metrics. For example, we can collect the market cap and enterprise value. We can also show recommendations of analysts. In addition, we can obtain DCFs over time and collect the balance sheet statements.
# Collect market cap and enterprise value
entreprise_value = fa.enterprise(ticker, api_key)
# Show recommendations of Analysts
ratings = fa.rating(ticker, api_key)
# Obtain DCFs over time
dcf_annually = fa.discounted_cash_flow(ticker, api_key, period=”annual”)
# Collect the Balance Sheet statements
balance_sheet_annually = fa.balance_sheet_statement(ticker, api_key, period=”annual”)
This is just a brief introduction to how you can do fundamental analysis with Python. In future blog posts, we will go into more detail on how to use the Fundamental Analysis package and other Python libraries to do more sophisticated fundamental analysis.