Issues in my script to analyze a list of stock ticker symbols

This script is designed to extract basic data about all listed stocks on S&P via wikipedia and then use the get symbol to extract price data over a period of time, then calculate log.return over the same period of time, and subsequently aggregate these log return with mean, sd of return and the number of log return data used. These calculations are then added to the list of traded stocks from wikipedia using the filter function and added to a new table called output.

If i run this script individually on stock ticker symbols, it runs without issue. However, when i try to use the for loop function or the Analyze stocks function, the table gives an error or no data is added to the output table. Please i need help to figure this out!

{
library(quantmod)
library(xts)
library(rvest)
library(tidyverse)
library(stringr)
library(forcats)
library(lubridate)

     #Extracting from Wikipedia a S&P Data
     sp_500 <- read_html("https://en.wikipedia.org/wiki/List_of_S%26P_500_companies") %>%
     html_node("table.wikitable") %>%
     html_table() %>%
     select(`Symbol`, Security, `GICS Sector`, `GICS Sub-Industry`) %>%
     as_tibble()

     #Creating an empty tibble table
     tbl_colnames <- c("ticker.symbol","security","gics.sector",
              "gics.sub.industry", "mean_return", "sd_return", "days")
     output <- tibble::tibble(!!!tbl_colnames, .rows = 0, .name_repair = ~ tbl_colnames)

     #Sample list of Stock symbols
     symbol_list = c("TSLA", "AKAM","ACN")

     #Function to analyze stocks symbols
     analyze_stocks = function(stocks){
     for (i in stocks){ 
     stock_prices_xts <- getSymbols(i, auto.assign = FALSE)
     names(stock_prices_xts) <- c("Open", "High", "Low", "Close", "Volume", "Adjusted")
     stock_prices <- stock_prices_xts %>%
           as_tibble() %>%
           rownames_to_column(var = "Date") %>%
           mutate(Date = index(stock_prices_xts))

    if (!is.xts(stock_prices)) {
    x <- xts(stock_prices[,-1], order.by = stock_prices$Date)
    }
    log_returns_xts <- periodReturn(x$Adjusted, type = 'log', period = 'daily')
    names(log_returns_xts) <- "Log.Returns"
    log_returns <- log_returns_xts %>%
                as_tibble() %>%
                rownames_to_column(var = "Date") %>%
                mutate(Date = index(log_returns_xts))

    stock_prices_xts = merge.xts(stock_prices_xts, log_returns_xts)

    output = rbind(output, sp_500 %>% as_tibble() %>%
               filter(sp_500$Symbol == stocks(i)) %>% 
               mutate(mean_return = mean(log_returns_xts), sd_return = sd(log_returns_xts), days = nrow(log_returns_xts)))
     }
     return(output)
   }
   analyze_stocks(symbol_list)

}

Sometimes I get these errors when i try to use the for loop or the Analyze stock function.
{
Error in filter():
ℹ In argument: sp_500$Symbol == stocks(i).
Caused by error in stocks():
! could not find function “stocks”
}

New contributor

Tbiz Adeniyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật