R code: package tidyquant
Let’s talk about package tidyquant. The package is concise, easy to understand, convenient and practical, and is an important tool for financial testing.
load the packages first
Then, we get information on S&P 500 and Nasdaq-listed companies.
The S&P 500 consists of the 500 largest companies listed on U.S. stock exchanges, calculated using a market capitalization-weighted method. Because some companies issue two classes of shares, the index actually includes 505 constituents. For example, Alphabet owns both Class A shares (GOOGL) and Class C shares (GOOG). Since 2005, the index has been calculated using a free-float market capitalization-weighted method. This means that only publicly traded stocks are considered when calculating the market value of each company. Index constituents are listed on the New York Stock Exchange or Nasdaq and trade from 9:30 a.m. to 4 p.m.
We get S&P 500 information directly in R:
As you can see, the displayed table is a tibble, including the company code, company name, percentage of the stock in the index, company type, and stock holdings.
Nasdaq is characterized by the collection and publication of broker quotes for unlisted stocks traded over the counter. It is now one of the largest securities exchanges in the world. There are currently more than 5,200 listed companies. Nasdaq is also the first stock market in the world to adopt electronic trading. It has more than 260,000 computer sales terminals in 55 countries and regions.
We get Nasdaq information directly in R:
The tibble contains company code, company name, current transaction price, market value, year of issue, classification and industry type information.
These data can provide valuable reference for investors to understand the market at the macro level. These two functions can also obtain other data sources.
In addition to obtaining market information, tidyquant can also collect individual stock quotes. Take Google as an example:
tq_get("GOG", get = "stock.prices", from = " 2010-01-01")
As long as you enter the stock code GOG, and indicate that you need to obtain the “stock.prices” stock price information, you can get the change in Apple’s stock price since January 1, 2010(customizable adjustment). We can see the date, opening price, high price, low price, closing price, volume and adjusted closing index (adjusted) in the result. Many metrics are well understood, but for adjusted we need further explanation.
If you’re a new investor, you may have heard the terms “closing price” and “adjusted closing price.” These two terms refer to slightly different ways of valuing stocks. The closing price of a stock is the price of that stock at the close of the trading day. The adjusted closing price is a more complex analysis that uses the closing price as a starting point, but it takes into account factors such as dividends, stock splits and new stock offerings to determine a value. The adjusted closing price represents a more accurate reflection of a stock’s value, since distributions and new offerings can alter the closing price. In other words, the corrected closing price is the change in the price of the stock at the close after taking into account dividends, splits, and additions.
tidyquant also supports querying dividend and share information, Take Apple as an example:
tq_get("AAPL", get = "dividends", from = "2000-01-01")tq_get("AAPL", get = "splits", from = "2000-01-01")
We can see that, since 1990, Apple has split shares in 2000, 2005, 2014 and 2020 to increase its liquidity. Meanwhile, Apple paid 41 dividends during this time period.
In addition to stock market data, tidyquant also supports open access to macroeconomic data, to give a simple example:
tq_get("DCOILWTICO", get = "economic.data")
The data source for this data is: Federal Reserve Economic Data. This API is called whenever the get parameter is set to “economic.data”. But what kind of data should be obtained, you need to see the list and detailed introduction on the official website. As an example, go to one of the sites(https://fred.stlouisfed.org/series/DCOILWTICO)
We can see DCOILWTICO is the code name of this data.
WTI crude oil has become one of the three major benchmark oils in the world due to its good liquidity, high transparency of quotations, and the great influence of the United States in global politics and economy. It not only has high utilization rate, but also has high investment value. As one of the most important investment products in the international crude oil market, WTI crude oil has always been in the leading position in the global trading volume. We often see how much oil prices soar or plummet in some media, mainly referring to the price of WTI crude oil.
The international crude oil market pricing is based on the standard oil of major oil-producing regions in the world. WTI crude oil is the subject of oil futures contracts on the New York Mercantile Exchange. All crude oil spot transactions in the United States and North America mainly refer to the WTI futures price as the pricing benchmark and are linked with it.