Stock market analysis involves analyzing historical price data, trends, and market indicators to make informed decisions. Python provides powerful libraries like Pandas, NumPy, Matplotlib, and yfinance to fetch, clean, visualize, and analyze stock market data.
Key Objectives:
✔ Fetching real-time stock market data
✔ Analyzing historical trends
✔ Calculating technical indicators
✔ Predicting stock price movements
1. Installing Required Libraries
Ensure you have the necessary libraries installed before proceeding.
bashCopyEditpip install pandas numpy matplotlib seaborn yfinance scikit-learn
2. Fetching Stock Market Data
We use yfinance
to fetch stock data from Yahoo Finance.
import pandas as pd
import yfinance as yf
# Fetch Apple (AAPL) stock data
df = yf.download("AAPL", start="2023-01-01", end="2024-01-01")
# Display the first 5 rows
print(df.head())
✔ Data Includes: Open, High, Low, Close, Adjusted Close, and Volume
📌 3. Data Preprocessing
Handling Missing Values
# Check for missing values
print(df.isnull().sum())
# Fill missing values using forward fill
df.fillna(method='ffill', inplace=True)
Converting Dates to Index
df.index = pd.to_datetime(df.index)
print(df.index)
✔ Ensures proper time-series analysis
4. Visualizing Stock Prices
Using Matplotlib
and Seaborn
for visualization.
import matplotlib.pyplot as plt
# Plot stock closing price
plt.figure(figsize=(12,6))
plt.plot(df.index, df['Close'], label='Closing Price', color='blue')
plt.title("Apple Stock Closing Price")
plt.xlabel("Date")
plt.ylabel("Price in USD")
plt.legend()
plt.show()
✔ Helps in spotting trends and patterns
5. Calculating Stock Market Indicators
Moving Averages (SMA & EMA)
Moving averages help smooth out price fluctuations.
df['SMA_50'] = df['Close'].rolling(window=50).mean()
df['EMA_50'] = df['Close'].ewm(span=50, adjust=False).mean()
Relative Strength Index (RSI)
RSI helps determine overbought/oversold conditions.
delta = df['Close'].diff()
gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean()
rs = gain / loss
df['RSI'] = 100 - (100 / (1 + rs))
Bollinger Bands
Indicates volatility by measuring price deviations from a moving average.
df['20_SMA'] = df['Close'].rolling(window=20).mean()
df['Upper_Band'] = df['20_SMA'] + (df['Close'].rolling(window=20).std() * 2)
df['Lower_Band'] = df['20_SMA'] - (df['Close'].rolling(window=20).std() * 2)
6. Risk Analysis: Daily Returns & Volatility
Calculating Daily Returns
df['Daily Return'] = df['Close'].pct_change()
Calculating Volatility (Standard Deviation)
volatility = df['Daily Return'].std()
print("Stock Volatility:", volatility)
✔ Higher volatility = higher risk
7. Stock Price Prediction with Machine Learning
We use Linear Regression to predict future stock prices.
Preparing Data
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import numpy as np
# Select features and labels
df['Prediction'] = df['Close'].shift(-30)
X = np.array(df[['Close']][:-30])
y = np.array(df['Prediction'].dropna())
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Training the Model
model = LinearRegression()
model.fit(X_train, y_train)
# Predict stock prices
predictions = model.predict(X_test)
✔ Provides a basic stock price forecast
8. Saving and Exporting Data
Exporting Processed Data to CSV
df.to_csv("stock_analysis.csv")
Saving Model for Future Use
import joblib
# Save the trained model
joblib.dump(model, "stock_prediction_model.pkl")