DEV Community

Cover image for Decoding Stock Performance: A Pandas Journey
Bahman Shadmehr
Bahman Shadmehr

Posted on

Decoding Stock Performance: A Pandas Journey

Welcome to another exciting exploration into the world of financial data analysis! In this blog post, we'll embark on a mission to analyze the performance of a chosen stock using Pandas, a powerhouse library for data manipulation and analysis in Python.

Step 1: Importing Libraries and Fetching Data

Let's start by importing the necessary libraries and downloading historical stock data for the desired symbol (replace "AAPL" with your preferred stock symbol).

import pandas as pd
import yfinance as yf
import matplotlib.pyplot as plt
import seaborn as sns

# Download historical stock data
ticker_symbol = "AAPL"
stock_data = yf.download(ticker_symbol, start="2020-01-01", end="2023-01-01")
Enter fullscreen mode Exit fullscreen mode

Task 1: Daily Percentage Change in Closing Price

Our first task is to calculate and visualize the daily percentage change in the stock's closing price.

stock_data['Daily_Return'] = stock_data['Close'].pct_change()
plt.figure(figsize=(12, 6))
plt.plot(stock_data.index, stock_data['Daily_Return'], label='Daily Returns')
plt.title('Daily Percentage Change in Closing Price')
plt.xlabel('Date')
plt.ylabel('Percentage Change')
plt.legend()
plt.show()
Enter fullscreen mode Exit fullscreen mode

Task 2: Moving Average of Closing Price

Next, we calculate and visualize the moving average of the closing price over a specified time period (e.g., 50 days).

stock_data['MA_50'] = stock_data['Close'].rolling(window=50).mean()
plt.figure(figsize=(12, 6))
plt.plot(stock_data.index, stock_data['Close'], label='Closing Price')
plt.plot(stock_data.index, stock_data['MA_50'], label='50-day Moving Average', linestyle='--', color='red')
plt.title('Closing Price and 50-day Moving Average')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.show()
Enter fullscreen mode Exit fullscreen mode

Task 3: Trading Volume Over Time

Now, let's analyze and visualize the stock's trading volume over time.

plt.figure(figsize=(12, 6))
plt.bar(stock_data.index, stock_data['Volume'], color='blue', alpha=0.7)
plt.title('Trading Volume Over Time')
plt.xlabel('Date')
plt.ylabel('Volume')
plt.show()
Enter fullscreen mode Exit fullscreen mode

Task 4: Cumulative Returns

Moving on, we calculate and visualize the cumulative returns of the stock.

stock_data['Cumulative_Return'] = (1 + stock_data['Daily_Return']).cumprod() - 1
plt.figure(figsize=(12, 6))
plt.plot(stock_data.index, stock_data['Cumulative_Return'], label='Cumulative Returns')
plt.title('Cumulative Returns Over Time')
plt.xlabel('Date')
plt.ylabel('Cumulative Return')
plt.legend()
plt.show()
Enter fullscreen mode Exit fullscreen mode

Task 5: Identifying Significant Price Movements

Lastly, we identify and visualize significant price movements, such as peaks and troughs.

plt.figure(figsize=(12, 6))
sns.lineplot(x=stock_data.index, y=stock_data['Close'], label='Closing Price')
plt.title('Identifying Significant Price Movements')
plt.xlabel('Date')
plt.ylabel('Price')
plt.scatter(stock_data.index[stock_data['Daily_Return'] > 0], 
            stock_data['Close'][stock_data['Daily_Return'] > 0], 
            marker='^', color='green', label='Positive Daily Return')
plt.scatter(stock_data.index[stock_data['Daily_Return'] < 0], 
            stock_data['Close'][stock_data['Daily_Return'] < 0], 
            marker='v', color='red', label='Negative Daily Return')
plt.legend()
plt.show()
Enter fullscreen mode Exit fullscreen mode

Conclusion

Congratulations! You've just navigated through a series of tasks to analyze the performance of a stock using Pandas. Each visualization and calculation provides a unique perspective on the stock's behavior, helping investors make informed decisions.

Feel free to adapt and expand upon these analyses based on your specific interests and requirements. Keep exploring the vast world of financial data analysis with Pandas, and happy coding!

Top comments (0)