Skip to main content

Analyze Historical Stock Data


The purpose of this article is to explain the code that is used to analyze historical stock data for Amazon Inc. (AMZN) using the Python programming language. Specifically, the code reads stock data from a CSV file, calculates daily returns and a 30-day moving average, visualizes the data using a line plot and a candlestick chart, and calculates summary statistics for the returns.

The code begins by importing the pandas library and using it to read the stock data from a CSV file called "AMZN_stock_data.csv". The pandas library is a powerful tool for data manipulation and analysis and is commonly used in data science and financial analysis.

The read_csv() function is used to read the stock data from the CSV file. The parse_dates parameter is set to True to ensure that the "Date" column is read in as a datetime object, which is important for plotting the data later on. The index_col parameter is set to 'Date' to use the Date column as the index for the DataFrame. This allows for easy access to the data for a specific date. The head() method is then used to print the first 5 rows of the data to confirm that it was read correctly.

#Stock Data Analysis using Python
import pandas as pd

# Load stock data from csv file
stock_data = pd.read_csv("AMZN_stock_data.csv", parse_dates= True, index_col='Date')

# Print the first 5 rows of the data
print(stock_data.head())

The code then uses pandas to calculate the daily returns for the stock. Daily returns are a commonly used metric in stock analysis, as they indicate how much the stock has gone up or down on a given day. The returns are calculated by subtracting the previous day's closing price from the current day's closing price and dividing it by the previous day's closing price. This information is added as a new column to the DataFrame called "returns".

# Calculate daily returns
stock_data["returns"] = stock_data["Close"].pct_change()

A 30-day moving average is also calculated and added as a new column called "30d_ma". A moving average is a commonly used indicator in stock analysis and helps smooth out fluctuations in the stock price to show a clearer trend.

# Calculate 30-day moving average
stock_data["30d_ma"] = stock_data["Close"].rolling(window=30).mean()

# Print the first 5 rows of the data with the new columns
print(stock_data.head())

The code then uses the matplotlib library to create a line plot of the closing prices and the 30-day moving average. Matplotlib is a popular library for creating static plots in Python and is commonly used in data visualization. The plot is labeled with a title, x-axis label, and y-axis label.

# Visualize the data using matplotlib
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, figsize= (10,5))
plt.plot(stock_data["Close"])
plt.plot(stock_data["30d_ma"])
plt.title("30-Day Moving Average of AMZN Stock")
plt.xlabel("Date")
plt.ylabel("Closing Price")
plt.show()


Line Plot of AMZN Stock


Next, the code uses the plotly library to create a candlestick chart of the stock data. Candlestick charts are useful for visualizing the high, low, open and close prices of the stock, which helps to show price movements over time. Plotly is a library for creating interactive plots in Python.

# Create candlestick chart using plotly
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(rows=1, cols=1)
fig.add_trace(go.Candlestick(x=stock_data.index,
open=stock_data['Open'],
high=stock_data['High'],
low=stock_data['Low'],
close=stock_data['Close']))
fig.show()


Candle Stick Plot of AMZN Stock


Finally, the code uses the describe() method to calculate summary statistics for the returns, including the mean, standard deviation, minimum, and maximum. The describe() method is a quick and easy way to get a sense of the distribution of the data.


# Calculate summary statistics
Print(stock_data["returns"].describe())

The results are shown as given below:

count     251.000000

mean     -0.001114

std          0.031759

min        -0.140494

25%       -0.019834

50%       -0.001980

75%        0.019378

max        0.135359

Name: returns, dtype: float64

In summary, this code provides an example of how to analyze historical stock data using Python. It demonstrates how to read stock data from a CSV file, calculate important metrics such as daily returns and moving averages, and visualize the data using different types of plots. It also provides an example of how to calculate summary statistics for the data. The reader can run the code on any stock data and can compare the performance of the stock by analyzing the visualizations.

Comments

Contact Form

Name

Email *

Message *