R/Buy_Table.R

Defines functions Buy_Table

#' @title Buy Signal Table for Designated Stock Ticker
#' @description Buy Signal Table for Designated Stock Ticker
#' @param
#' @return
#' @examples Buy_Table()
#' @export Buy_Table
#'
#' # Define function
Buy_Table <- function(x,r_day_plot,end_day_plot,c,height,past.n.days,test.new.price = 0){
  if (test.new.price == 0) {
    x = x
  } else {
    intra.day.test <- matrix(c(0,0,0,test.new.price,0,0), nrow = 1)
    rownames(intra.day.test) <- as.character(Sys.Date())
    x = data.frame(rbind(x, intra.day.test))
  }
  Close<-x[,4] # Define Close as adjusted closing price
  # A new function needs redefine data from above:
  # Create SMA for multiple periods
  SMA10<-SMA(Close,n=10)
  SMA20<-SMA(Close,n=20)
  SMA30<-SMA(Close,n=30)
  SMA50<-SMA(Close,n=50)
  SMA200<-SMA(Close,n=200)
  SMA250<-SMA(Close,n=250)

  # Create RSI for multiple periods
  RSI10 <- (RSI(Close,n=10)-50)*height*5
  RSI20 <- (RSI(Close,n=20)-50)*height*5
  RSI30 <- (RSI(Close,n=30)-50)*height*5
  RSI50 <- (RSI(Close,n=50)-50)*height*5
  RSI200 <- (RSI(Close,n=200)-50)*height*5
  RSI250 <- (RSI(Close,n=250)-50)*height*5

  # Create computable dataset: Close/SMA_i-1
  ratio_10<-(Close/SMA10-1)
  ratio_20<-(Close/SMA20-1)
  ratio_30<-(Close/SMA30-1)
  ratio_50<-(Close/SMA50-1)
  ratio_200<-(Close/SMA200-1)
  ratio_250<-(Close/SMA250-1)
  all_data_ratio <- cbind.data.frame(
    ratio_10,
    ratio_20,
    ratio_30,
    ratio_50,
    ratio_200,
    ratio_250
  )
  # Here we want to create signal for each column
  # Then we add them all together
  all_data_ratio[is.na(all_data_ratio)] <- 0 # Get rid of NAs
  sd(all_data_ratio[,1])
  sd(all_data_ratio[,2])
  sd(all_data_ratio[,3])
  sd(all_data_ratio[,4])
  sd(all_data_ratio[,5])
  sd(all_data_ratio[,6])
  coef<-c
  m<-height*mean(Close)
  all_data_ratio$Sig1<-ifelse(
    all_data_ratio[,1] <= coef*sd(all_data_ratio[,1]),
    m, "0")
  all_data_ratio$Sig2<-ifelse(
    all_data_ratio[,2] <= coef*sd(all_data_ratio[,2]),
    m, "0")
  all_data_ratio$Sig3<-ifelse(
    all_data_ratio[,3] <= coef*sd(all_data_ratio[,3]),
    m, "0")
  all_data_ratio$Sig4<-ifelse(
    all_data_ratio[,4] <= coef*sd(all_data_ratio[,4]),
    m, "0")
  all_data_ratio$Sig5<-ifelse(
    all_data_ratio[,5] <= coef*sd(all_data_ratio[,5]),
    m, "0")
  all_data_ratio$Sig6<-ifelse(
    all_data_ratio[,6] <= coef*sd(all_data_ratio[,6]),
    m, "0")

  all_data_ratio$Signal <- (
    as.numeric(all_data_ratio[,7])
    + as.numeric(all_data_ratio[,8])
    + as.numeric(all_data_ratio[,9])
    + as.numeric(all_data_ratio[,10])
    + as.numeric(all_data_ratio[,11])
    + as.numeric(all_data_ratio[,12])
  )

  all_data_signal <- cbind.data.frame(Close, all_data_ratio$Signal)

  return(
    #tail(all_data_signal)
    all_data_signal[(nrow(all_data_signal)-past.n.days):nrow(all_data_signal),]
  )
} # End of function # End of function # End of function
yiqiao-yin/YinsCapital documentation built on March 15, 2020, 2:35 a.m.