Introduction to the Financial Analytics with Python
Dr. Fatma Ben Mesmia Chaabouni
PhD Student in AI @EnetCom| Assistant Professor in Computer Science @ CU Ulster University, Qatar |Ph.D. in CS | MSc_B.Sc. in CS| NLP-AI and Data Analytics- Blockchain researcher | MBA mentor| Tunisian AI Society Member
Finance is complex, and Python has become a versatile tool for analyzing large financial datasets. This tutorial demonstrates how to use Python for financial analytics, including time series analysis, financial plotting, and regression analysis.
The objective of the Tutorial:
You can download the datasets used in this tutorial from this Google Drive link.
Step 1: Import Required Libraries
# Importing essential libraries for data manipulation and visualization
import pandas as pd # Used for data handling and manipulation
import numpy as np # Provides support for numerical operations and arrays
import matplotlib.pyplot as plt # Library for creating static plots
import cufflinks as cf # Library for interactive financial plots
from plotly.offline import iplot # Enables offline plotting with Plotly
Complexity: This step just loads the necessary libraries. There is no significant computational complexity involved (O(1)).
Step 2: Load Financial Data
# Load the dataset from a CSV file, parsing dates, and setting index as the first column
raw = pd.read_csv('Dataset1.csv', index_col=0, parse_dates=True)
# Show the first few rows of the dataset to understand its structure
raw.head() # Displaying the first 5 rows of the dataset
Step 3: Analyze Financial Data (SPX Index)
# Extract the S&P 500 (SPX) data from the raw dataset
spx = pd.DataFrame(raw['.SPX']) # Creating a DataFrame for SPX index
# Rename the column for better readability
spx.columns = ['Close'] # Renaming the column to 'Close'
# Plot the historical SPX index with a grid and a title
spx['Close'].plot(figsize=(8, 5), grid=True, title="Historical SPX Index")
# Display the plot
plt.show() # Show the plot for the SPX index
Step 4: Compute Daily Returns
# Calculate the daily logarithmic returns for the SPX index
spx['Return'] = np.log(spx['Close'] / spx['Close'].shift(1)) # Logarithmic returns calculation
# Plot both the SPX index and its returns
spx[['Close', 'Return']].plot(subplots=True, style='b', figsize=(8, 5), grid=True)
领英推荐
Step 5: Moving Averages (42 Days and 252 Days)
# Calculate the 42-day moving average
spx['42d'] = spx['Close'].rolling(window=42).mean() # Rolling window for 42-day average
# Calculate the 252-day moving average
spx['252d'] = spx['Close'].rolling(window=252).mean() # Rolling window for 252-day average
# Plot the SPX index along with 42-day and 252-day moving averages
spx[['Close', '42d', '252d']].plot(figsize=(8, 5), grid=True, title="SPX Index with Moving Averages")
Step 6: Volatility Calculation
# Calculate the annualized volatility using a 252-day rolling window (number of trading days in a year)
spx['Volatility'] = spx['Return'].rolling(window=252).std() * np.sqrt(252) # Annualized volatility calculation
# Plot the SPX index and its volatility in separate subplots
spx[['Close', 'Volatility']].plot(subplots=True, figsize=(8, 7), grid=True)
Step 7: Regression Analysis between SPX and VIX
# Load the VIX (Volatility Index) data from the raw dataset
vix = pd.DataFrame(raw['.VIX']) # Creating a DataFrame for VIX
# Join the SPX and VIX data on their common index
data = spx.join(vix, how='inner') # Joining SPX and VIX data
# Calculate logarithmic returns for both SPX and VIX
rets = np.log(data / data.shift(1)) # Logarithmic returns calculation for both indices
# Remove any missing values from the returns data
rets.dropna(inplace=True) # Drop rows with NaN values
# Extract SPX returns and VIX returns as numpy arrays for regression
xdat = rets['Close'].values # SPX returns
ydat = rets['.VIX'].values # VIX returns
# Perform a linear regression between SPX and VIX returns
reg = np.polyfit(xdat, ydat, deg=1) # Fitting a linear regression
# Plot the SPX vs VIX returns scatter plot
plt.plot(xdat, ydat, 'r.') # Plotting returns as red dots
# Plot the regression line
plt.plot(np.linspace(min(xdat), max(xdat)), np.polyval(reg, np.linspace(min(xdat), max(xdat))), 'b') # Regression line in blue
# Adding labels, grid, and title to the plot
plt.grid(True) # Displaying grid
plt.xlabel('SPX Returns') # Label for X-axis
plt.ylabel('VIX Returns') # Label for Y-axis
plt.title('Regression of SPX vs VIX Returns') # Plot title
Step 8: Interactive Financial Plots with Cufflinks
# Enable offline plotting for Plotly
cf.set_config_file(offline=True)
# Create an interactive QuantFig for the SPX index with the last 100 data points
qf = cf.QuantFig(spx[-100:], title="SPX Index", name="SPX")
# Add Bollinger Bands to the QuantFig
qf.add_bollinger_bands(periods=15, boll_std=2) # Bollinger Bands
# Display the interactive plot
iplot(qf.iplot(asFigure=True)) # Plotting interactively with Plotly
#FinancialAnalytics #PythonForFinance #DataScience #FinancialModeling #DataVisualization #Pandas #Numpy #Matplotlib #Cufflinks #PythonInFinance
Thank you for your valuable time! If you have any questions, feel free to post them in the comments. I'm happy to help! ??