Wavelet transformation
In the first tutorial, we evaluated basic properties of Stock data called Efficient Market Hypothesis. In the second tutorial, we looked into the geometry and applied some tests for identifying Fractal geometry patterns on Stock Data. In this tutorial, we will focus in the Spectrum analysis. Particularly Wavelet Transformation.
It is possible to use wavelet transformation on stock price data to decompose the time series into different frequency bands and potentially extract useful information about the underlying patterns and trends.
Here is an example of how you can use wavelet transformation to decompose the stock price data of Apple and plot the decomposed output in Python:
import pywt
import matplotlib.pyplot as plt
import pandas as pd
# Load the stock data
stock_data = pd.read_csv("AAPL.csv")
# Extract the stock price series
stock_price = stock_data["Close"]
# Decompose the stock price series using the Haar wavelet
coefficients = pywt.wavedec(stock_price, 'haar', level=5)
# Extract the detail coefficients at each level
cA5, cD5, cD4, cD3, cD2, cD1 = coefficients
# Plot the decomposed output
fig, ax = plt.subplots(nrows=6, ncols=1, figsize=(10, 6))
ax[0].plot(cA5, label="cA5")
ax[1].plot(cD5, label="cD5")
ax[2].plot(cD4, label="cD4")
ax[3].plot(cD3, label="cD3")
ax[4].plot(cD2, label="cD2")
ax[5].plot(cD1, label="cD1")
plt.tight_layout()
plt.show()
Let me explain the code meaning: this code assumes that you have a CSV file called “AAPL.csv“ that contains the stock data for Apple, with a column called “Close”' that contains the closing price of the stock on each date. it also assumes that you have the pywt library installed, which provides functions for wavelet transformation in Python.
the resulting plot will show the decomposed output of the stock price data at each level of the wavelet transformation. the cA coefficients represent the low-frequency components of the time series, while cD coefficients represent the high-frequency components. By analyzing the cA and cD coefficients different levels, you can potentially identity patterns and trend in stock price data at different scales and frequencies.
This particular example assumes that we are decomposing the stock data in to different frequency components, wavelet transformation are seen to work really well to smoothen the data as compared to rolling mean or moving average method. we will use wavelet smoothening in our model building in the future.
How to calculate volatility of a stock price data?
There are several ways to calculate the volatility of an intraday stock price.
One common approach is to use the standard deviation of the log returns of stock price over a given time period. This can be calculated using the following Python Code:
import numpy as np
def calculate_volatility(stock_price, time_period):
"""Calculate the volatility of an intraday stock price"""
# Calculate the log returns of the stock price
log_returns = np.log(stock_price) - np.log(stock_price.shift(1))
# Calculate the standard deviation of the log returns over the given time period
volatility = log_returns.rolling(time_period).std()
return volatility
# Load the stock data
stock_data = [your stock data]
# Extract the stock price series
stock_price = stock_data["Close"]
# Calculate the volatility of the stock price over a time period of 20 minutes
volatility = calculate_volatility(stock_price, 20)
# Print the volatility
print(volatility)
This code assumes that you have a time series of stock data stored in the stock_data variable, with a column called “Close” that contains the closing price of the stock on each date. it also assumes that you have specified the time period over which to calculate the volatility, in case 20 minutes.