This R script fetches financial data, such as stock prices, using the Alpha Vantage API. It provides a simple way to retrieve the latest close price for a given stock symbol.
Before running the script, you need to obtain an API key from Alpha Vantage. You can sign up for a free account and obtain the key here.
Make sure you have the required R packages installed. You can install them using the following commands or download Required_Packages file:
-
In R Studio:
-
install.packages("httr")
-
install.packages("jsonlite")
-
- Replace the placeholder
YOUR_ALPHA_VANTAGE_API_KEY
in the script with your actual Alpha Vantage API key.
Copy code
- Alpha Vantage API key (replace with your key)
- api_key <- "YOUR_ALPHA_VANTAGE_API_KEY"
- Run the R script
financial_script.R
in your R environment. You can do this by opening the script in RStudio or running it in the R console. - The script provides an example of fetching stock data for a specific symbol (AAPL for Apple Inc.). You can customize the script to fetch data for different stock symbols dynamically.
Copy code
#Example: Get stock data for a specific symbol (e.g., Apple Inc.)
get_stock_data("AAPL")
Feel free to customize and expand the project according to your needs:
- Allow dynamic input for stock symbols.
- Enhance error handling and provide user-friendly messages.
- Fetch historical stock prices and perform data analysis.
- Create visualizations using ggplot2 or other plotting libraries.
- Build a Shiny app for a more interactive experience.
- This project is licensed under the MIT License.
- Alpha Vantage for providing the API to access financial data.
- The R community for the development of useful packages.