Market timing using VIX movements and Portfolio rotation

Collins Hackman

# Function List
Number_of_Round_Trip_fun <-
  function(data,
           condition_pecentage_change_in_decimal,
           holding_period) {
    round_trip = 0
    day_count = 0
    holding_period = holding_period
    on_position = FALSE
    
    # In the case that if "condition_pecentage_change_in_decimal" is positive
    if (condition_pecentage_change_in_decimal > 0) {
      for (vix in data) {
        # If the number > "condition_pecentage_change_in_decimal", get on a position
        if (on_position == FALSE &
            vix >= condition_pecentage_change_in_decimal)
        {
          on_position = TRUE
        }
        # For example, if the vix went above 10%, and the holding period is 2 days, if the following 2 days has vix below 10%, we don't sell our holdings.
        else if (on_position == TRUE &
                 vix >= condition_pecentage_change_in_decimal)
        {
          day_count = 0
        }
        # If position is opened, but the VIX went below the assigned threshold, hold for the assigned period, if the vix did not went back up, close the position.
        else if (on_position == TRUE &
                 vix <= condition_pecentage_change_in_decimal)
        {
          day_count = day_count + 1
          if (day_count == holding_period)
          {
            on_position = FALSE
            round_trip = round_trip + 1
            day_count = 0
          }
        }
      }
      return(round_trip)
    }
    # In the case that if "condition_pecentage_change_in_decimal" is negative
    else if (condition_pecentage_change_in_decimal < 0) {
      for (vix in data) {
        # If the number < "condition_pecentage_change_in_decimal", get on a position
        if (on_position == FALSE &
            vix <= condition_pecentage_change_in_decimal)
        {
          on_position = TRUE
        }
        # For example, if the vix went below -10%, and the holding period is 2 days, if the following 2 days has vix above -10%, we don't sell our holdings.
        else if (on_position == TRUE &
                 vix <= condition_pecentage_change_in_decimal)
        {
          day_count = 0
        }
        # If position is opened, but the VIX went above the assigned threshold, hold for the assigned period, if the vix did not went back down, close the position.
        else if (on_position == TRUE &
                 vix >= condition_pecentage_change_in_decimal)
        {
          day_count = day_count + 1
          if (day_count == holding_period)
          {
            on_position = FALSE
            round_trip = round_trip + 1
            day_count = 0
          }
        }
      }
      return(round_trip)
      
    }
    
  }

Number_of_Days_fun <-
  function(data,
           condition_pecentage_change_in_decimal,
           holding_period) {
    day_count = 0
    holding_period = holding_period
    on_position = FALSE
    number_of_days = 0
    
    
    # In the case that if "condition_pecentage_change_in_decimal" is positive
    if (condition_pecentage_change_in_decimal > 0) {
      for (vix in data) {
        # If the number > "condition_pecentage_change_in_decimal", get on a position
        if (on_position == FALSE &
            vix >= condition_pecentage_change_in_decimal)
        {
          on_position = TRUE
        }
        # For example, if the vix went above 10%, and the holding period is 2 days, if the following 2 days has vix below 10%, we don't sell our holdings.
        else if (on_position == TRUE &
                 vix >= condition_pecentage_change_in_decimal)
        {
          day_count = 0
        }
        # If position is opened, but the VIX went below the assigned threshold, hold for the assigned period, if the vix did not went back up, close the position.
        else if (on_position == TRUE &
                 vix <= condition_pecentage_change_in_decimal)
        {
          day_count = day_count + 1
          if (day_count == holding_period)
          {
            number_of_days = number_of_days + 1
            on_position = FALSE
            day_count = 0
          }
        }
        if (on_position == TRUE)
        {
          number_of_days = number_of_days + 1
        }
      }
      return(number_of_days)
    }
    
    # In the case that if "condition_pecentage_change_in_decimal" is negative
    else if (condition_pecentage_change_in_decimal < 0) {
      for (vix in data) {
        # If the number < "condition_pecentage_change_in_decimal", get on a position
        if (on_position == FALSE &
            vix <= condition_pecentage_change_in_decimal)
        {
          on_position = TRUE
        }
        # For example, if the vix went below -10%, and the holding period is 2 days, if the following 2 days has vix above -10%, we don't sell our holdings.
        else if (on_position == TRUE &
                 vix <= condition_pecentage_change_in_decimal)
        {
          day_count = 0
        }
        # If position is opened, but the VIX went above the assigned threshold, hold for the assigned period, if the vix did not went back down, close the position.
        else if (on_position == TRUE &
                 vix >= condition_pecentage_change_in_decimal)
        {
          day_count = day_count + 1
          if (day_count == holding_period)
          {
            number_of_days = number_of_days + 1
            on_position = FALSE
            day_count = 0
          }
        }
        if (on_position == TRUE)
        {
          number_of_days = number_of_days + 1
        }
      }
      return(number_of_days)
      
      
      
    }
    
  }

Culmulative_Ret_fun <-
  function(portfolio_long,
           portfolio_short,
           VIXMA_Change_data,
           condition_pecentage_change_in_decimal,
           holding_period) {
    capital = 100  # Assume the initial capital is 100
    day_count = 0
    holding_period = holding_period
    number_of_days = 0
    on_position = FALSE
    
    long_in = 0
    long_out = 0
    short_in = 0
    short_out = 0
    
    # In the case that if "condition_pecentage_change_in_decimal" is positive, we long the longs, short the shorts (For example, Long Large, Short Small)
    if (condition_pecentage_change_in_decimal > 0) {
      for (index in 1:length(VIXMA_Change_data)) {
        # If the number > "condition_pecentage_change_in_decimal", get on a position
        if (on_position == FALSE &
            VIXMA_Change_data[index] >= condition_pecentage_change_in_decimal)
        {
          on_position = TRUE
          
          # Getting into a position
          long_in = portfolio_long[index]  # Long Large
          short_in = portfolio_short[index]  # Short Small
        }
        
        # For example, if the vix went above 10%, and the holding period is 2 days, if the following 2 days has vix below 10%, we don't sell our holdings.
        else if (on_position == TRUE &
                 VIXMA_Change_data[index] >= condition_pecentage_change_in_decimal)
        {
          day_count = 0
        }
        
        # If position is opened, but the VIX went below the assigned threshold, hold for the assigned period, if the vix did not went back up, close the position.
        else if (on_position == TRUE &
                 VIXMA_Change_data[index] <= condition_pecentage_change_in_decimal)
        {
          day_count = day_count + 1
          
          if (day_count == holding_period)
          {
            number_of_days = number_of_days + 1
            on_position = FALSE
            day_count = 0
            
            # Getting out of position
            long_out = portfolio_long[index]  # Long Large
            short_out = portfolio_short[index]  # Short Small
            
            # Calculate my round_trip return
            capital = capital * (1 + (long_out / long_in - 1))  # Long Return
            capital = capital * (1 + (-(short_out / short_in - 1)))  # Short Return
          }
        }
        
        if (on_position == TRUE)
        {
          number_of_days = number_of_days + 1
        }
        
      }
      
      # Cumulative return
      return (capital / 100 - 1)
    }
    
    # In the case that if "condition_pecentage_change_in_decimal" is negative, we long the shorts, short the longs (For example, Long Small, Short Large)
    else if (condition_pecentage_change_in_decimal < 0) {
      for (index in 1:length(VIXMA_Change_data)) {
        # If the number < "condition_pecentage_change_in_decimal", get on a position
        if (on_position == FALSE &
            VIXMA_Change_data[index] <= condition_pecentage_change_in_decimal)
        {
          on_position = TRUE
          
          # Getting into a position
          long_in = portfolio_long[index]  # Long Large
          short_in = portfolio_short[index]  # Short Small
        }
        
        # For example, if the vix went below -10%, and the holding period is 2 days, if the following 2 days has vix above -10%, we don't sell our holdings.
        else if (on_position == TRUE &
                 VIXMA_Change_data[index] <= condition_pecentage_change_in_decimal)
        {
          day_count = 0
        }
        
        # If position is opened, but the VIX went above the assigned threshold, hold for the assigned period, if the vix did not went back down, close the position.
        else if (on_position == TRUE &
                 VIXMA_Change_data[index] >= condition_pecentage_change_in_decimal)
        {
          day_count = day_count + 1
          
          if (day_count == holding_period)
          {
            number_of_days = number_of_days + 1
            on_position = FALSE
            day_count = 0
            
            # Getting out of position
            long_out = portfolio_long[index]
            short_out = portfolio_short[index]
            
            # Calculate my round_trip return
            capital = capital * (1 + (short_out / short_in - 1))  # Long Return
            capital = capital * (1 + (-(long_out / long_in - 1)))  # Short Return
          }
        }
        
        if (on_position == TRUE)
        {
          number_of_days = number_of_days + 1
        }
        
      }
      
      # Cumulative return
      return (capital / 100 - 1)
    }
    
  }

Holding_Ret_fun <-
  function(portfolio_long,
           portfolio_short,
           holding_period) {
    mylist = c()
    for (index in 1:((length(portfolio_long)) - holding_period)) {
      capital = 100
      long_in = portfolio_long[index]
      short_in = portfolio_short[index]
      long_out = portfolio_long[index + holding_period]
      short_out = portfolio_short[index + holding_period]
      capital = capital * (1 + (long_out / long_in - 1))
      capital = capital * (1 + (-(short_out / short_in - 1)))
      mylist = append(mylist, capital / 100 - 1)
    }
    return(mylist)
  }
VIX_t <- getSymbols('VIXCLS', src = "FRED", auto.assign = FALSE)
Small_t <-
  getSymbols('WILLSMLCAP', src = "FRED", auto.assign = FALSE)
Large_t <-
  getSymbols('WILLLRGCAP', src = "FRED", auto.assign = FALSE)
Value_t <-
  getSymbols('WILL2500INDVAL', src = "FRED", auto.assign = FALSE)
Growth_t <-
  getSymbols('WILL2500INDGR', src = "FRED", auto.assign = FALSE)

mydata_t <- merge(VIX_t, Small_t, Large_t, Value_t, Growth_t) %>%
  window(., start = "2000-01-01", end = "2022-04-25") %>%
  na.omit(.)
names(mydata_t) = c("VIX", "Small", "Large", "Value", "Growth")

Small_r_t <- diff(log(mydata_t$Small))
Large_r_t <- diff(log(mydata_t$Large))
Value_r_t <- diff(log(mydata_t$Value))
Growth_r_t <- diff(log(mydata_t$Growth))
VIX_change_t = diff(mydata_t$VIX)
VIX_percentage_change_t = diff(log(mydata_t$VIX))
MA75_t = SMA(mydata_t$VIX, 75)
VIX_MA_percentage_change_t = (mydata_t$VIX - MA75_t) / MA75_t

# Data set ready to use
mydata <- merge(Small_t, Large_t, Value_t, Growth_t, Small_r_t, Large_r_t, Value_r_t, Growth_r_t, mydata_t$VIX, VIX_change_t, VIX_percentage_change_t, VIX_MA_percentage_change_t) %>% na.omit(.)

names(mydata) <- c("Small", "Large", "Value", "Growth", "Small_r", "Large_r", "Value_r", "Growth_r", "VIX", "VIX_change", "VIX_percentage_change", "VIX_MA_percentage_change")
mydata <- as.data.frame(mydata)
t1_fit <- list()
t1_fit$S_fit <- lm(Small_r ~ VIX_change, data = mydata)
t1_fit$L_fit <- lm(Large_r ~ VIX_change, data = mydata)
t1_fit$V_fit <- lm(Value_r ~ VIX_change, data = mydata)
t1_fit$G_fit <- lm(Growth_r ~ VIX_change, data = mydata)

t1_names <- c('Small', 'Large', 'Value', 'Growth')
t1_df <-
  data.frame(
    Index = t1_names,
    Intercept = sapply(t1_fit, function(x)
      Intercept = summary(x)$coefficients[1, 1]),
    Intercept_T_Stat = sapply(t1_fit, function(x)
      Intercept_T_Stat = summary(x)$coefficients[1, 3]),
    Slope = sapply(t1_fit, function(x)
      Slope = summary(x)$coefficients[2, 1]),
    Slope_T_Stat = sapply(t1_fit, function(x)
      Slope_T_Stat = summary(x)$coefficients[2, 3]),
    R2 = sapply(t1_fit, function(x)
      R2 = summary(x)$r.squared)
  )
row.names(t1_df) <- NULL
knitr::kable(t1_df, caption = 'Table1. Contemporaneous Relationship between Change in Historical Volatility and Index Return',align = "c") %>% kableExtra:: kable_classic(full_width = F, html_font = "Cambria")
Table1. Contemporaneous Relationship between Change in Historical Volatility and Index Return
Index Intercept Intercept_T_Stat Slope Slope_T_Stat R2
Small 0.0003526 2.639002 -0.0060566 -82.75777 0.5529576
Large 0.0002832 2.910240 -0.0054828 -102.83937 0.6563631
Value 0.0003150 3.184941 -0.0052412 -96.75023 0.6283296
Growth 0.0002627 2.321734 -0.0058301 -94.07354 0.6151341
t2_fit <- list()
t2_fit$V_minus_G_level_fit <- lm(Value_r - Growth_r ~ VIX, data = mydata)
t2_fit$V_minus_G_change_fit <-
  lm(Value_r - Growth_r ~ VIX_change, data = mydata)
t2_fit$V_minus_G_percentage_fit <-
  lm(Value_r - Growth_r ~ VIX_percentage_change, data = mydata)

t2_names <- c('Level of historical variance',
    'Change in level of historical variance',
    'Percentage change in variance')
t2_df <-
  data.frame(
    Variables = t2_names,
    Intercept = sapply(t2_fit, function(x)
      Intercept = summary(x)$coefficients[1, 1]),
    Intercept_T_Stat = sapply(t2_fit, function(x)
      Intercept_T_Stat = summary(x)$coefficients[1, 3]),
    Slope = sapply(t2_fit, function(x)
      Slope = summary(x)$coefficients[2, 1]),
    Slope_T_Stat = sapply(t2_fit, function(x)
      Slope_T_Stat = summary(x)$coefficients[2, 3]),
    R2 = sapply(t2_fit, function(x)
      R2 = summary(x)$r.squared)
  )
row.names(t2_df) <- NULL
knitr::kable(t2_df, caption = 'Table 2. Contemporaneous Relationship between Timing and Style',align = "c") %>% kableExtra:: kable_classic(full_width = F, html_font = "Cambria")
Table 2. Contemporaneous Relationship between Timing and Style
Variables Intercept Intercept_T_Stat Slope Slope_T_Stat R2
Level of historical variance -0.0002471 -1.2363489 0.0000150 1.635554 0.0004829
Change in level of historical variance 0.0000523 0.6600147 0.0005889 13.568094 0.0321780
Percentage change in variance 0.0000523 0.6648245 0.0179164 16.241821 0.0454760
Holding_Ret_VG_1 <- Holding_Ret_fun(as.vector(mydata$Value), as.vector(mydata$Growth), 1)
Holding_Ret_VG_2 <- Holding_Ret_fun(as.vector(mydata$Value), as.vector(mydata$Growth), 2)
Holding_Ret_VG_3 <- Holding_Ret_fun(as.vector(mydata$Value), as.vector(mydata$Growth), 3)
Holding_Ret_VG_4 <- Holding_Ret_fun(as.vector(mydata$Value), as.vector(mydata$Growth), 4)
Holding_Ret_VG_5 <- Holding_Ret_fun(as.vector(mydata$Value), as.vector(mydata$Growth), 5)
Holding_Ret_VG_10 <- Holding_Ret_fun(as.vector(mydata$Value), as.vector(mydata$Growth), 10)
Holding_Ret_VG_20 <- Holding_Ret_fun(as.vector(mydata$Value), as.vector(mydata$Growth), 20)

t3_fit <- list()
t3_fit$Holding_Ret_VG_1_fit <- lm(Holding_Ret_VG_1 ~ as.vector(head(mydata$VIX_MA_percentage_change, -1)))
t3_fit$Holding_Ret_VG_2_fit <- lm(Holding_Ret_VG_2 ~ as.vector(head(mydata$VIX_MA_percentage_change, -2)))
t3_fit$Holding_Ret_VG_3_fit <- lm(Holding_Ret_VG_3 ~ as.vector(head(mydata$VIX_MA_percentage_change, -3)))
t3_fit$Holding_Ret_VG_4_fit <- lm(Holding_Ret_VG_4 ~ as.vector(head(mydata$VIX_MA_percentage_change, -4)))
t3_fit$Holding_Ret_VG_5_fit <- lm(Holding_Ret_VG_5 ~ as.vector(head(mydata$VIX_MA_percentage_change, -5)))
t3_fit$Holding_Ret_VG_10_fit <- lm(Holding_Ret_VG_10 ~ as.vector(head(mydata$VIX_MA_percentage_change, -10)))
t3_fit$Holding_Ret_VG_20_fit <- lm(Holding_Ret_VG_20 ~ as.vector(head(mydata$VIX_MA_percentage_change, -20)))

t3_names <- c(1, 2, 3, 4, 5, 10, 20)
t3_df <-
  data.frame(
    Holding_Period = t3_names,
    Intercept = sapply(t3_fit, function(x)
      Intercept = summary(x)$coefficients[1, 1]),
    Intercept_T_Stat = sapply(t3_fit, function(x)
      Intercept_T_Stat = summary(x)$coefficients[1, 3]),
    Slope = sapply(t3_fit, function(x)
      Slope = summary(x)$coefficients[2, 1]),
    Slope_T_Stat = sapply(t3_fit, function(x)
      Slope_T_Stat = summary(x)$coefficients[2, 3])
  )
row.names(t3_df) <- NULL
knitr::kable(t3_df, caption = 'Table 3. Value and Growth Index Returns versus Percentage Change in the VIX Today',align = "c") %>% kableExtra:: kable_classic(full_width = F, html_font = "Cambria")
Table 3. Value and Growth Index Returns versus Percentage Change in the VIX Today
Holding_Period Intercept Intercept_T_Stat Slope Slope_T_Stat
1 -0.0000974 -1.202428 -0.0011055 -3.415996
2 -0.0001706 -1.492402 -0.0016127 -3.532563
3 -0.0002424 -1.752161 -0.0022568 -4.085002
4 -0.0003233 -2.051256 -0.0029203 -4.640104
5 -0.0003946 -2.258433 -0.0036071 -5.171035
10 -0.0006914 -2.776967 -0.0057260 -5.762423
20 -0.0015047 -4.163514 -0.0104528 -7.250302
# Table 4 initialized columns
Holding_Period_Days <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10)
Change_in_the_VIX <- c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, -0.1, -0.2, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, -0.1, -0.2, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, -0.1, -0.2, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, -0.1, -0.2)

# Get Number_of_Days & Number_of_Round_Trip Datas, and store them into vector
Number_of_Days <- c()
Number_of_Round_Trip <- c()


for (index in 1:40)
{
  Number_of_Days <-
    append(
      Number_of_Days,
      Number_of_Days_fun(
        mydata$VIX_MA_percentage_change,
        Change_in_the_VIX[index],
        Holding_Period_Days[index]
      )
    )
  Number_of_Round_Trip <-
    append(
      Number_of_Round_Trip,
      Number_of_Round_Trip_fun(
        mydata$VIX_MA_percentage_change,
        Change_in_the_VIX[index],
        Holding_Period_Days[index]
      )
    )
}

# Get Cumulative Return(Long Value & Short Growth while VIX going up)
Culmulative_Ret <- c()

for (x in 1:40)
{
  Culmulative_Ret <-
    append(
      Culmulative_Ret,
      Culmulative_Ret_fun(
        as.vector(mydata$Value),
        as.vector(mydata$Growth),
        mydata$VIX_MA_percentage_change,
        Change_in_the_VIX[x],
        Holding_Period_Days[x]
      )
    )
}

# Form Table4
Table_4 <- data.frame(Holding_Period_Days, paste(Change_in_the_VIX * 100, "%"))
Table_4 <- Table_4 %>%
  rename(Change_in_the_VIX = paste.Change_in_the_VIX...100......) %>%
  mutate(
    Number_of_Days = Number_of_Days, Number_of_Round_Trip = Number_of_Round_Trip,
    Culmulative_Return = paste((round(Culmulative_Ret, 3)) * 100, "%")
  )

# Display Table4
kableExtra::kable(Table_4, caption = "Table 4. Trading Rule Results: Using Changes in the VIX To Switch between Value and Growth Strategies",align = "c") %>% kableExtra:: kable_classic(full_width = F, html_font = "Cambria")
Table 4. Trading Rule Results: Using Changes in the VIX To Switch between Value and Growth Strategies
Holding_Period_Days Change_in_the_VIX Number_of_Days Number_of_Round_Trip Culmulative_Return
1 10 % 1466 199 -8.2 %
1 20 % 949 164 -26.4 %
1 30 % 609 110 -17.7 %
1 40 % 383 74 -15.4 %
1 50 % 268 55 -16.6 %
1 60 % 183 39 -11.7 %
1 70 % 130 26 -18.8 %
1 80 % 98 20 -15.6 %
1 -10 % 2298 296 0.9 %
1 -20 % 697 141 16 %
2 10 % 1618 152 -9.3 %
2 20 % 1063 114 -12.7 %
2 30 % 697 88 -25.5 %
2 40 % 443 60 -25.5 %
2 50 % 314 46 -18.4 %
2 60 % 217 34 -10 %
2 70 % 152 22 -12.4 %
2 80 % 117 19 -11 %
2 -10 % 2507 209 -12.9 %
2 -20 % 802 105 2.5 %
3 10 % 1738 120 0.2 %
3 20 % 1165 102 -19 %
3 30 % 774 77 -26.6 %
3 40 % 497 54 -22.3 %
3 50 % 355 41 -18.9 %
3 60 % 248 31 -18.1 %
3 70 % 171 19 -13.1 %
3 80 % 133 16 -8.6 %
3 -10 % 2671 164 -26.7 %
3 -20 % 886 84 7.4 %
10 10 % 2396 80 5.4 %
10 20 % 1714 66 -7.2 %
10 30 % 1174 53 -24.1 %
10 40 % 769 36 -24.9 %
10 50 % 577 29 -24.1 %
10 60 % 415 22 -20.3 %
10 70 % 291 15 -17.6 %
10 80 % 231 13 -20.9 %
10 -10 % 3339 72 -28.8 %
10 -20 % 1288 51 -0.3 %
t5_fit <- list()
t5_fit$L_minus_S_level_fit <- lm(Large_r - Small_r ~ VIX, data = mydata)
t5_fit$L_minus_S_change_fit <- lm(Large_r - Small_r ~ VIX_change, data = mydata)
t5_fit$L_minus_S_percentage_fit <- lm(Large_r - Small_r ~ VIX_percentage_change, data = mydata)

t5_names <- c('Level of historical variance',
    'Change in level of historical variance',
    'Percentage change in variance')
t5_df <-
  data.frame(
    Variables = t5_names,
    Intercept = sapply(t5_fit, function(x)
      Intercept = summary(x)$coefficients[1, 1]),
    Intercept_T_Stat = sapply(t5_fit, function(x)
      Intercept_T_Stat = summary(x)$coefficients[1, 3]),
    Slope = sapply(t5_fit, function(x)
      Slope = summary(x)$coefficients[2, 1]),
    Slope_T_Stat = sapply(t5_fit, function(x)
      Slope_T_Stat = summary(x)$coefficients[2, 3]),
    R2 = sapply(t5_fit, function(x)
      R2 = summary(x)$r.squared)
  )
row.names(t5_df) <- NULL
knitr::kable(t5_df, caption = 'Table 5. Contemporaneous Relationship between Timing and Style',align = "c") %>% kableExtra:: kable_classic(full_width = F, html_font = "Cambria")
Table 5. Contemporaneous Relationship between Timing and Style
Variables Intercept Intercept_T_Stat Slope Slope_T_Stat R2
Level of historical variance -0.0007007 -3.5258156 0.0000316 3.470007 0.0021699
Change in level of historical variance -0.0000693 -0.8786305 0.0005738 13.274981 0.0308451
Percentage change in variance -0.0000693 -0.8805118 0.0156021 14.134787 0.0348265
Holding_Ret_LS_1 <- Holding_Ret_fun(as.vector(mydata$Large), as.vector(mydata$Small), 1)
Holding_Ret_LS_2 <- Holding_Ret_fun(as.vector(mydata$Large), as.vector(mydata$Small), 2)
Holding_Ret_lS_3 <- Holding_Ret_fun(as.vector(mydata$Large), as.vector(mydata$Small), 3)
Holding_Ret_LS_4 <- Holding_Ret_fun(as.vector(mydata$Large), as.vector(mydata$Small), 4)
Holding_Ret_LS_5 <- Holding_Ret_fun(as.vector(mydata$Large), as.vector(mydata$Small), 5)
Holding_Ret_LS_10 <- Holding_Ret_fun(as.vector(mydata$Large), as.vector(mydata$Small), 10)
Holding_Ret_LS_20 <- Holding_Ret_fun(as.vector(mydata$Large), as.vector(mydata$Small), 20)

t6_fit <- list()
t6_fit$Holding_Ret_LS_1_fit <- lm(Holding_Ret_LS_1 ~ as.vector(head(mydata$VIX_MA_percentage_change, -1)))
t6_fit$Holding_Ret_LS_2_fit <- lm(Holding_Ret_LS_2 ~ as.vector(head(mydata$VIX_MA_percentage_change, -2)))
t6_fit$Holding_Ret_LS_3_fit <- lm(Holding_Ret_lS_3 ~ as.vector(head(mydata$VIX_MA_percentage_change, -3)))
t6_fit$Holding_Ret_LS_4_fit <- lm(Holding_Ret_LS_4 ~ as.vector(head(mydata$VIX_MA_percentage_change, -4)))
t6_fit$Holding_Ret_LS_5_fit <- lm(Holding_Ret_LS_5 ~ as.vector(head(mydata$VIX_MA_percentage_change, -5)))
t6_fit$Holding_Ret_LS_10_fit <- lm(Holding_Ret_LS_10 ~ as.vector(head(mydata$VIX_MA_percentage_change, -10)))
t6_fit$Holding_Ret_LS_20_fit <- lm(Holding_Ret_LS_20 ~ as.vector(head(mydata$VIX_MA_percentage_change, -20)))

t6_names <- c(1, 2, 3, 4, 5, 10, 20)
t6_df <-
  data.frame(
    Holding_Period = t6_names,
    Intercept = sapply(t6_fit, function(x)
      Intercept = summary(x)$coefficients[1, 1]),
    Intercept_T_Stat = sapply(t6_fit, function(x)
      Intercept_T_Stat = summary(x)$coefficients[1, 3]),
    Slope = sapply(t6_fit, function(x)
      Slope = summary(x)$coefficients[2, 1]),
    Slope_T_Stat = sapply(t6_fit, function(x)
      Slope_T_Stat = summary(x)$coefficients[2, 3])
  )
row.names(t6_df) <- NULL
knitr::kable(t6_df, caption = 'Table 6. Large and Small Index Returns versus Percentage Change in the VIX Today',align = "c") %>% kableExtra:: kable_classic(full_width = F, html_font = "Cambria")
Table 6. Large and Small Index Returns versus Percentage Change in the VIX Today
Holding_Period Intercept Intercept_T_Stat Slope Slope_T_Stat
1 -0.0002721 -3.405647 0.0004965 1.555840
2 -0.0005191 -4.611332 0.0013652 3.036899
3 -0.0007691 -5.639854 0.0011634 2.136528
4 -0.0010244 -6.527865 0.0013656 2.179263
5 -0.0012666 -7.321569 0.0014297 2.069708
10 -0.0023907 -10.188201 0.0037842 4.040583
20 -0.0047474 -13.881327 0.0042688 3.128807
# Table 7 initialized columns
Holding_Period_Days <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10)
Change_in_the_VIX <- c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, -0.1, -0.2, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, -0.1, -0.2, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, -0.1, -0.2, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, -0.1, -0.2)

# Get Number_of_Days & Number_of_Round_Trip Datas, and store them into vector
Number_of_Days <- c()
Number_of_Round_Trip <- c()


for (index in 1:40)
{
  Number_of_Days <-
    append(
      Number_of_Days,
      Number_of_Days_fun(
        mydata$VIX_MA_percentage_change,
        Change_in_the_VIX[index],
        Holding_Period_Days[index]
      )
    )
  Number_of_Round_Trip <-
    append(
      Number_of_Round_Trip,
      Number_of_Round_Trip_fun(
        mydata$VIX_MA_percentage_change,
        Change_in_the_VIX[index],
        Holding_Period_Days[index]
      )
    )
}

# Get Cumulative Return(Long Large & Short Small while VIX going up)
Culmulative_Ret <- c()

for (x in 1:40)
{
  Culmulative_Ret <-
    append(
      Culmulative_Ret,
      Culmulative_Ret_fun(
        as.vector(mydata$Large),
        as.vector(mydata$Small),
        mydata$VIX_MA_percentage_change,
        Change_in_the_VIX[x],
        Holding_Period_Days[x]
      )
    )
}

# Form Table7
Table_7 <- data.frame(Holding_Period_Days, paste(Change_in_the_VIX * 100, "%"))
Table_7 <- Table_7 %>%
  rename(Change_in_the_VIX = paste.Change_in_the_VIX...100......) %>%
  mutate(Number_of_Days = Number_of_Days, Number_of_Round_Trip = Number_of_Round_Trip, Culmulative_Return = paste((round(Culmulative_Ret, 3)) * 100, "%"))

# Display Table7
kableExtra::kable(Table_7, caption = "Table 7. Trading Rule Results: Percentage Changes in the VIX To Switch between Large- and Small-Cap Strategies",align = "c") %>% kableExtra:: kable_classic(full_width = F, html_font = "Cambria")
Table 7. Trading Rule Results: Percentage Changes in the VIX To Switch between Large- and Small-Cap Strategies
Holding_Period_Days Change_in_the_VIX Number_of_Days Number_of_Round_Trip Culmulative_Return
1 10 % 1466 199 26.2 %
1 20 % 949 164 11.8 %
1 30 % 609 110 12.9 %
1 40 % 383 74 10.7 %
1 50 % 268 55 12.9 %
1 60 % 183 39 10 %
1 70 % 130 26 8.7 %
1 80 % 98 20 10 %
1 -10 % 2298 296 29.9 %
1 -20 % 697 141 -15.6 %
2 10 % 1618 152 19.9 %
2 20 % 1063 114 25.5 %
2 30 % 697 88 6.4 %
2 40 % 443 60 3.6 %
2 50 % 314 46 14.8 %
2 60 % 217 34 18.1 %
2 70 % 152 22 3.9 %
2 80 % 117 19 -0.2 %
2 -10 % 2507 209 23 %
2 -20 % 802 105 -10.5 %
3 10 % 1738 120 4.8 %
3 20 % 1165 102 21.8 %
3 30 % 774 77 18.3 %
3 40 % 497 54 9.8 %
3 50 % 355 41 15.1 %
3 60 % 248 31 9.3 %
3 70 % 171 19 3.4 %
3 80 % 133 16 0 %
3 -10 % 2671 164 8.7 %
3 -20 % 886 84 -8.1 %
10 10 % 2396 80 -16.8 %
10 20 % 1714 66 -12.5 %
10 30 % 1174 53 -17.3 %
10 40 % 769 36 -3.1 %
10 50 % 577 29 3.8 %
10 60 % 415 22 11.7 %
10 70 % 291 15 16.1 %
10 80 % 231 13 4 %
10 -10 % 3339 72 0.5 %
10 -20 % 1288 51 6.7 %