Im trying to extract yahoo finance data like this
head(NQdata_one_minute)
# A tibble: 6 × 6
timestamp open high low close volume
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 5/19/2024 18:00 18634. 18647 18633 18644. 357
2 5/19/2024 18:01 18644 18645. 18638. 18640. 191
3 5/19/2024 18:02 18640 18644. 18640 18643 65
4 5/19/2024 18:03 18644. 18650 18643. 18650. 87
5 5/19/2024 18:04 18649. 18650 18647. 18650. 101
6 5/19/2024 18:05 18649. 18654. 18649. 18652. 147
for a certain period up to todays date with 1 minute timestamp. I subscribed to yahoo for free and got my api key which I do not post here and I use the code below
# Load required libraries
install.packages("httr")
install.packages("jsonlite")
install.packages("tibble")
install.packages("dplyr")
library(httr)
library(jsonlite)
library(tibble)
library(dplyr)
# Define your RapidAPI key
rapidapi_key <- "3820ecec24msheb4fc07be883ed8p106355jsn32ebbf92dfaa"
# Define the URL and payload for the POST request
url <- "https://yahoo-finance160.p.rapidapi.com/history"
payload <- "{"stock":"TSLA","period":"1mo"}"
# Send the POST request
response <- VERB("POST", url, body = payload, add_headers(
'x-rapidapi-key' = rapidapi_key,
'x-rapidapi-host' = 'yahoo-finance160.p.rapidapi.com',
'Content-Type' = 'application/json'
), encode = "json")
# Check if the request was successful
if (status_code(response) == 200) {
# Parse the response as JSON
data <- fromJSON(content(response, "text"), flatten = TRUE)
# Extract the necessary fields and convert to a tibble
historical_data <- data$prices
stock_data <- tibble(
timestamp = as.POSIXct(historical_data$formatted_date, format="%Y-%m-%d"),
open = historical_data$open,
high = historical_data$high,
low = historical_data$low,
close = historical_data$close,
volume = historical_data$volume
)
# Print the tibble
print(stock_data)
} else {
stop("Failed to fetch data: ", status_code(response))
}
but I get an empty tibble
head(stock_data)
# A tibble: 0 × 1
# ℹ 1 variable: timestamp <dttm>
I also tried this but got data up to 2020 and with a data not a 1min timestamp.