2

I am currently trying to create a bid ask model on R for 12 companies. I have already figured out how to get stock prices for 12 companies and how to apply the equation to it. Now I need to create a function which will give me the final spread for individual companies from the list of companies i have. For eg: I have stock prices for SBUX, AAPL and GOOG. When I run my function, it shows the answer for all the 3 companies but I want to create a function which will allow me to specify say SBUX for example and it shows me the spread just for SBUX. This is the code I have used for all the companies

library(quantmod)
library(dplyr)
#To get the daily prices of the stocks
sdate <- "2017-11-29"
tickers <- 
 c("SBUX","C","AAPL","AMZN","CAT","DAL","MCD","GS","K","VZ","PEP","CVX")
Stock_Prices = NULL
for(ticker in tickers)
Stock_Prices <- cbind(Stock_Prices, getSymbols(ticker, src = 
"yahoo",from=sdate,auto.assign=F)[,6])
colnames(Stock_Prices) <- tickers
p<-diff(Stock_Prices[1:253])
p[is.na(p)] <- 0
n_days=252
cov1<-cov(p[1:n_days],p[2:(n_days+1)]) 
2*sqrt(cov1) `

So can someone help me implement a function where I can find the diff, cov and the 2 sqrt for individual stocks instead of for all at once?

NSI
  • 21
  • 4

0 Answers0