IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (2024)

Table of Contents
Summary Explore More
  • Today the focus is on risk assessment of top blue chips (NVDA, ORCL, AMD, AAPL, INTC, AMZN, GE, XOM, TSLA, MSFT, WMT, PG, KO, JNJ, BAC, NKE, HON, GS, and ^GSPC) using the IQR-based real-time volatility ranking algorithm and the corresponding Python functions.
  • The objective is to determine market regimes using standard deviation (STD) of log-domain stock prices. It allows us to assess how values in a dataset are distributed around the mean.
  • Let’s set the working directory YOURPATH
import osos.chdir('YOURPATH') # Set working directoryos. getcwd()
import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport matplotlib.patches as mpatchesclass Levels: def run(self): df = self.read_file() df = self.calc_std_dev_levels(df) self.plot_levels(df) def read_file(self): filename = 'data.csv' df = pd.read_csv(filename, names=['receive_timestamp', 'price']) df['receive_timestamp'] = pd.to_datetime(df['receive_timestamp']) df.set_index('receive_timestamp', inplace=True) return df def calc_std_dev_levels(self, df): #log price df['price'] = df['price'].apply(np.log) #price difference df['price_diff'] = df['price'].diff() #std dev df['std_dev'] = df['price_diff'].rolling(window=100).std() #use rolling mean to find 'zones' of volatility df['std_dev_ma'] = df['std_dev'].rolling(3000).mean() #thresholds between levels std_dev_ma_threshold_1 = df['std_dev_ma'].quantile(0.2) std_dev_ma_threshold_2 = df['std_dev_ma'].quantile(0.4) std_dev_ma_threshold_3 = df['std_dev_ma'].quantile(0.6) std_dev_ma_threshold_4 = df['std_dev_ma'].quantile(0.8) # Initialize 'signal' column with zeros df['levels'] = 0 #assign levels based on average std dev df.loc[(df['std_dev_ma'] > std_dev_ma_threshold_1), 'levels'] = 1 df.loc[(df['std_dev_ma'] > std_dev_ma_threshold_2), 'levels'] = 2 df.loc[(df['std_dev_ma'] > std_dev_ma_threshold_3), 'levels'] = 3 df.loc[(df['std_dev_ma'] > std_dev_ma_threshold_4), 'levels'] = 4 return df def plot_levels(self, df): fig, ax = plt.subplots() colors = { 0:'grey', 1:'green', 2: 'blue', 3: 'red', 4: 'black' } scatter = ax.scatter( np.reshape(df.index, -1), np.reshape(df['price'], -1), c=np.reshape(df['levels'].apply(lambda x: colors[x]), -1), s=10, linewidths=1 ) # Create proxy artists for legend legend_labels = [f'Level {level}' for level in colors.keys()] legend_handles = [mpatches.Patch(color=colors[level], label=label) for level, label in enumerate(legend_labels)] # Create a legend ax.legend(handles=legend_handles, title='Levels') plt.title('std dev levels') plt.xlabel('Date') plt.ylabel('Price') plt.grid(True) plt.show()
  • Let’s test this class using the high-volatility segment data.csv
if __name__ == '__main__': v = Levels() v.run()
  • Let’s take a closer look at theoutput
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (1)
  • Clearly, we can create a price chart and mark our levels. The faster the price changes, the higher the volatility and STD. Level 0 will correspond to the lowest volatility, with each subsequent group representing higher volatility. Gray and green zones represent low volatility, while red and black indicate the most volatile segments.
  • Let’s replace the above class with the following two functions
# Importing Libraries# Data Handlingimport pandas as pdimport numpy as np# Financial Data Analysisimport yfinance as yf# Data Visualizationimport plotly.express as pximport plotly.graph_objs as goimport plotly.subplots as spfrom plotly.subplots import make_subplotsimport plotly.figure_factory as ffimport plotly.io as piofrom IPython.display import displayfrom plotly.offline import init_notebook_mode# Statistics & Mathematicsimport scipy.stats as statsimport statsmodels as smfrom scipy.stats import shapiro, skewimport math# Hiding warnings import warningswarnings.filterwarnings("ignore")
def load_and_preprocess(ticker): ''' This function takes in a ticker symbol, which is used to retrieve historical data from Yahoo Finance. The attributes 'Returns', and the Adjusted Low, High, and Open are created. NaNs are filled with 0s ''' df = yf.download(ticker) df['Returns'] = df['Adj Close'].pct_change(1) df['Adj Low'] = df['Low'] - (df['Close'] - df['Adj Close']) df['Adj High'] = df['High'] - (df['Close'] - df['Adj Close']) df['Adj Open'] = df['Open'] - (df['Close'] - df['Adj Close']) df = df.fillna(0) return df
import matplotlib.pyplot as pltimport matplotlib.patches as mpatchesdef plot_levels(df): #log price df['price'] = df['Adj Close'].apply(np.log) #price difference df['price_diff'] = df['price'].diff() #std dev df['std_dev'] = df['price_diff'].rolling(window=100).std() #use rolling mean to find 'zones' of volatility df['std_dev_ma'] = df['std_dev'].rolling(3000).mean() #thresholds between levels std_dev_ma_threshold_1 = df['std_dev_ma'].quantile(0.2) std_dev_ma_threshold_2 = df['std_dev_ma'].quantile(0.4) std_dev_ma_threshold_3 = df['std_dev_ma'].quantile(0.6) std_dev_ma_threshold_4 = df['std_dev_ma'].quantile(0.8) # Initialize 'signal' column with zeros df['levels'] = 0 #assign levels based on average std dev df.loc[(df['std_dev_ma'] > std_dev_ma_threshold_1), 'levels'] = 1 df.loc[(df['std_dev_ma'] > std_dev_ma_threshold_2), 'levels'] = 2 df.loc[(df['std_dev_ma'] > std_dev_ma_threshold_3), 'levels'] = 3 df.loc[(df['std_dev_ma'] > std_dev_ma_threshold_4), 'levels'] = 4 fig, ax = plt.subplots() colors = { 0:'grey', 1:'green', 2: 'blue', 3: 'red', 4: 'black' } scatter = ax.scatter( np.reshape(df.index, -1), np.reshape(df['price'], -1), c=np.reshape(df['levels'].apply(lambda x: colors[x]), -1), s=10, linewidths=1 ) # Create proxy artists for legend legend_labels = [f'Level {level}' for level in colors.keys()] legend_handles = [mpatches.Patch(color=colors[level], label=label) for level, label in enumerate(legend_labels)] # Create a legend ax.legend(handles=legend_handles, title='Levels') plt.title('std dev levels') plt.xlabel('Date') plt.ylabel('Price') plt.grid(True) plt.show()
  • Now, we can examine price := (log Adj Close price) charts of stocks and mark their STD levels as follows
ticker = 'NVDA'df = load_and_preprocess(ticker)plot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (2)
ticker = 'ORCL'df = load_and_preprocess(ticker)plot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (3)
ticker = 'AMD'df = load_and_preprocess(ticker)plot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (4)
ticker = 'XOM'df = load_and_preprocess(ticker)plot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (5)
ticker = 'AAPL'df = load_and_preprocess(ticker)plot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (6)
ticker = 'INTC'df = load_and_preprocess(ticker) plot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (7)
ticker = 'AMZN'df = load_and_preprocess(ticker) # Loading and Transforming Dataframeplot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (8)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (9)
ticker = 'TSLA'df = load_and_preprocess(ticker) plot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (10)
ticker = 'MSFT'df = load_and_preprocess(ticker) plot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (11)
ticker = 'WMT'df = load_and_preprocess(ticker) plot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (12)
ticker = 'PG'df = load_and_preprocess(ticker) plot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (13)
ticker = 'JNJ'df = load_and_preprocess(ticker) plot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (14)
ticker = 'BAC'df = load_and_preprocess(ticker) plot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (15)
ticker = '^GSPC'df = load_and_preprocess(ticker) plot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (16)
ticker = 'NKE'df = load_and_preprocess(ticker) plot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (17)
ticker = 'HON'df = load_and_preprocess(ticker) plot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (18)
ticker = 'GS'df = load_and_preprocess(ticker) plot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (19)
ticker = 'KO'df = load_and_preprocess(ticker) plot_levels(df)
IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (20)

Summary

  • These plots can be interpreted in various ways: (1) you can enable or disable your trading/investment strategies using the proposed IQR-based market’s volatility levels 0-4; (2) traders or investors can determine the distance from the mean price at which they place their orders.
  • On a risk-adjusted basis, low-to-moderate volatility stocks (NVDA, ORCL, INTC, AMZN, AAPL, PG, HON, KO, and JNJ) can be superior investments in 2023 and beyond.
  • Highly volatile stocks (XOM, GE, BAC, TSLA, and ^GSPC) offer the most profit potential but are equally susceptible to losses in Q4’23: traders can take advantage of short-term strategies to trade the momentum. Highly volatile stocks canearn profits for a short lock-in period.
  • Taking a long-term investment view well beyond 2023 is important for stocks with moderate volatility levels such as AMD, MSFT, WMT, NKE, and GS.

Explore More

  • Blue-Chip Stock Portfolios for Quant Traders
  • Multiple-Criteria Technical Analysis of Blue Chips in Python
  • Are Blue Chips Perfect for This Bear Market?
  • Advanced Integrated Data Visualization (AIDV) in Python – 1. Stock Technical Indicators
  • Applying a Risk-Aware Portfolio Rebalancing Strategy to ETF, Energy, Pharma, and Aerospace/Defense Stocks in 2023
  • 360-Deg Revision of Risk Aware Investing after SVB Collapse – 1. The Financial Sector
  • Portfolio max(Return/Risk) Stochastic Optimization of 20 Dividend Growth Stocks
  • The Zacks Market Outlook Nov ’22 – Energy
  • The CodeX-Aroon Auto-Trading Approach – the AAPL Use Case
  • Bear vs. Bull Portfolio Risk/Return Optimization QC Analysis
  • Inflation-Resistant Stocks to Buy
  • Risk-Return Analysis and LSTM Price Predictions of 4 Major Tech Stocks in 2023

One-Time

Monthly

Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

€5.00

€15.00

€100.00

€5.00

€15.00

€100.00

€5.00

€15.00

€100.00

Or enter a custom amount

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

IQR-Based Log Price Volatility Ranking of Top 19 Blue Chips (2024)
Top Articles
Latest Posts
Article information

Author: Kimberely Baumbach CPA

Last Updated:

Views: 5856

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Kimberely Baumbach CPA

Birthday: 1996-01-14

Address: 8381 Boyce Course, Imeldachester, ND 74681

Phone: +3571286597580

Job: Product Banking Analyst

Hobby: Cosplaying, Inline skating, Amateur radio, Baton twirling, Mountaineering, Flying, Archery

Introduction: My name is Kimberely Baumbach CPA, I am a gorgeous, bright, charming, encouraging, zealous, lively, good person who loves writing and wants to share my knowledge and understanding with you.