What is low - Regression Approach

What is low - Regression Approach

This serves as an alternate compass for me to visualize what is low. See post here for another version of compass.

I was inspired by a post written by a local finance blogger. He used a regression line flanked by the intervals to inform whether the prices are low or not. I’m unsure what sort of intervals of intervals he used in his plots, but for my purpose I will use prediction intervals instead. You may visit this link to understand the statistical properties.

Another variation over the blogger’s method is that I used Log of Prices as my Y-axis to normalize data across time. If I use the non-log version, the prices will be too low in the beginning and it will mess up the regression line.

Here’re some quick explanations on the diagrams,

  • The highest red dotted line is +2SD which suggest that market is really overheated.
  • The second highest blue dotted line is +1SD which suggest that market is moderately overheated.
  • Blue line is best fit line. Market is currently at an average trajectory.
  • The second lowest blue dotted line is -1SD which suggest that market is moderately undervalued.
  • The lowest red dotted line is -2SD which suggest that market is really undervalued.

These diagrams below will aid me in understanding if I should deploy more cash or I might even consider leveraging using interactive brokers margin loan (https://www.interactivebrokers.com/en/index.php?f=1595) when prices are < -2SD.

Without further ado, you may find the diagrams below for your reference pls.

Note:

  • I will schedule a cron job to update the diagrams at midnight every day.
  • You may find the code after all the diagrams.

World

/project/img/VT.png

US SnP 500

/project/img/^GSPC.png

Technology (VGT)

/project/img/VGT.png

Technology (IYW)

/project/img/IYW.png

Emerging Markets

/project/img/VWO.png

Frontier markets

/project/img/FM.png

Latin America

/project/img/ILF.png

China

/project/img/MCHI.png

Bank of China

/project/img/BACHY.png

Europe

/project/img/VGK.png

India

/project/img/INDA.png

Singapore

/project/img/^STI.png

Gold

/project/img/GLD.png

Oil (USO)

/project/img/USO.png

TLT (Long Term Bonds)

/project/img/TLT.png

Healthcare (XLV)

/project/img/XLV.png

BNDX (Vanguard Total International Bond ETF)

/project/img/BNDX.png

VWOB (Vanguard Emerging Markets Government Bond ETF)

/project/img/VWOB.png

DBC (Commodities)

/project/img/DBC.png

RWO (Global Real Estate)

/project/img/RWO.png

UK FTSE (included here because of low interest in margin loan)

/project/img/^FTSE.png

Germany

/project/img/EWG.png

Spain

/project/img/EWP.png

France

/project/img/EWQ.png

HK HSI

/project/img/^HSI.png

Korea

/project/img/^KS11.png

Taiwan

/project/img/EWT.png

Turkey

/project/img/TUR.png

South Africa

/project/img/EZA.png

Russia

/project/img/RSX.png

Japan Nikkei

/project/img/^N225.png

Australia (included here because of low interest in margin loan)

/project/img/^AXJO.png

Switzerland (included here because of low interest in margin loan)

/project/img/^SSMI.png

Brazil

/project/img/EWZ.png

Argentina

/project/img/ARGT.png

Malaysia

/project/img/EWM.png

Indonesia

/project/img/EIDO.png

Thailand

/project/img/THD.png

Code used to produce the diagrams

sapply(c("ggplot2", "plotly"), require, character.only = T)

source('util/extract_stock_prices.R')

#define list of indices
indices = c("VT", "GXC", "^STI", "^GSPC", "VWO", "^FTSE", "^N225", "^HSI", "000001.SS", "^JKSE", "^KLSE", "^KS11")

for(i in 1:length(indices)){
  
stock = indices[i]
start_date = "1950-07-01"
end_date = "2030-12-30"

data = df_crawl_time_series(stock, start_date, end_date)
data = subset(data, !is.na(data$Adj.Close))
data$Adj.Close = log(data$Adj.Close)
data$Date = as.Date(data$Date)

model1 <- lm(data$Adj.Close ~ data$Date)

temp_var <- predict(model1, interval = "prediction")
temp_var1 <- predict(model1, interval = "prediction", level = 0.68)
temp_var1 = data.frame(temp_var1); names(temp_var1) = c("fit1", "lwr1", "upr1")

new_df <- cbind(data, temp_var, temp_var1)

p = ggplot(new_df, aes(x = Date, y = Adj.Close))+
  geom_point(size = 0.1) +
  geom_line(aes(y=lwr), color = "red", linetype = "dashed")+
  geom_line(aes(y=upr), color = "red", linetype = "dashed")+
  geom_line(aes(y=lwr1), color = "blue", linetype = "dashed")+
  geom_line(aes(y=upr1), color = "blue", linetype = "dashed")+
  geom_smooth(method=lm, se=TRUE) +
  ylab("Log of Ticker") +
  ggtitle(paste(stock,
                "\n+2 SD", round(exp(new_df$upr[nrow(new_df)]), 0),
                "\n+1 SD", round(exp(new_df$upr1[nrow(new_df)]), 0),
                "\nCurrent", round(exp(new_df$Adj.Close[nrow(new_df)]), 0),
                "\n-1 SD", round(exp(new_df$lwr1[nrow(new_df)]), 0),
                "\n-2 SD", round(exp(new_df$lwr[nrow(new_df)]), 0),
                "\nUpdated on", Sys.Date()
                )
         )
p

p

ggsave(filename = paste0(stock, ".png") , plot = p)

}