![]() |
VOOZH | about |
The quantmod package (Quantitative Financial Modelling Framework) is designed to assist quantitative traders and analysts in modeling, testing, and visualizing financial data. It provides tools to easily retrieve stock data from online sources like Yahoo Finance and includes functions for charting, calculating technical indicators, and managing time series data.
We start by loading the quantmod package, which allows users to retrieve and analyze financial data from public sources like Yahoo Finance.
We retrieve stock data for a company (Example: Zomato as ZOMATO.NS) using the getSymbols() function. By default, data is pulled from Yahoo Finance, which contains columns such as Open, High, Low, Close, Volume, and Adjusted prices.
Output:
'ZOMATO.NS'
We can visualize the price movement over time using the chartSeries() function. This creates a basic line chart of the historical adjusted closing prices.
Output:
We can analyze trends by adding moving averages. Moving averages smooth out short-term price fluctuations and help identify trend direction. The addSMA() function adds Simple Moving Averages (SMA) to the price chart. A 20-day SMA captures short-term trends, while a 50-day SMA reflects longer-term movements. When the short-term average crosses above the long-term average, it may indicate a bullish trend, and vice versa.
Output:
We can view the latest records of stock data to observe recent performance. using the head() and tail() functions. These functions display the first and last few rows of the data, helping us understand the historical and recent trends.
Output:
We compute daily returns to analyze how the stock price has changed over time. Here, we extract the closing prices with Cl(ZOMATAO.NS) and calculate daily returns using dailyReturn(). Plotting these returns helps visualize volatility.
Output:
From the analysis, we can conclude the following:
This article demonstrated how to perform stock data analysis in R using the quantmod package. We retrieved historical data, visualized stock prices, added moving averages for trend analysis, and examined daily returns. These steps form the basis of a simple, yet structured, stock price analysis workflow in R.