Skip to contents

Get Stock Market Data

Usage

get_stock_data(
  symbols = NULL,
  start_date = "2007-01-01",
  end_date = "2023-12-31",
  data_source = "yahoo",
  file_path = NULL,
  custom_data = NULL,
  date_col = "Date",
  indices = NULL,
  return_prices = FALSE
)

Arguments

symbols

Character vector of stock symbols (only used when fetching from Yahoo Finance)

start_date

Start date for data retrieval (YYYY-MM-DD)

end_date

End date for data retrieval (YYYY-MM-DD)

data_source

Source of data. Options: "yahoo" (default): Fetch from Yahoo Finance "builtin": Use built-in market indices dataset "file": Load from a local CSV file (specify file_path) "custom": Use a pre-loaded data frame (specify custom_data)

file_path

Path to a local CSV file when data_source="file"

custom_data

A data frame when data_source="custom"

date_col

Name of the date column (default: "Date")

indices

Character vector of columns to retrieve. If NULL (default), returns all available columns.

return_prices

Logical, whether to return prices instead of returns (default: FALSE)

Value

An xts object containing log returns or prices

Examples

# Method 1: Using built-in dataset
data <- get_stock_data(data_source = "builtin")
#> Warning: cannot open URL 'https://raw.githubusercontent.com/avishekb9/WaveQTE/master/market_indices.csv': HTTP status was '404 Not Found'
#> Error in value[[3L]](cond): Could not download built-in dataset. Please check your internet connection.
head(data)
#>                                                                             
#> 1 function (..., list = character(), package = NULL, lib.loc = NULL,        
#> 2     verbose = getOption("verbose"), envir = .GlobalEnv, overwrite = TRUE) 
#> 3 {                                                                         
#> 4     fileExt <- function(x) {                                              
#> 5         db <- grepl("\\\\.[^.]+\\\\.(gz|bz2|xz)$", x)                     
#> 6         ans <- sub(".*\\\\.", "", x)                                      

# Method 2: Using built-in dataset with selected indices
developed_markets <- get_stock_data(data_source = "builtin", 
                                  indices = c("GSPC", "N225", "FTSE"))
#> Warning: cannot open URL 'https://raw.githubusercontent.com/avishekb9/WaveQTE/master/market_indices.csv': HTTP status was '404 Not Found'
#> Error in value[[3L]](cond): Could not download built-in dataset. Please check your internet connection.
head(developed_markets)
#> Error: object 'developed_markets' not found

# Method 3: Using a local CSV file
if (FALSE) { # \dontrun{
  data <- get_stock_data(data_source = "file", 
                        file_path = "path/to/your/data.csv")
  head(data)
} # }

# Method 4: Using a pre-loaded custom data frame
if (FALSE) { # \dontrun{
  my_data <- read.csv("my_stocks.csv")
  data <- get_stock_data(data_source = "custom", 
                        custom_data = my_data,
                        date_col = "MyDateColumn")
  head(data)
} # }

# Method 5: Fetch data from Yahoo Finance (requires internet)
if (FALSE) { # \dontrun{
  data <- get_stock_data(c("AAPL", "MSFT"), 
                        start_date = "2020-01-01", 
                        end_date = "2020-12-31")
  head(data)
} # }