Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
Stock Data Analysis and Data Visualization with Quantmod in R
Next article icon

Stock Data Analysis and Data Visualization with Quantmod in R

Last Updated : 30 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

1. Loading the Required Package

We start by loading the quantmod package, which allows users to retrieve and analyze financial data from public sources like Yahoo Finance.

R
install.packages("quantmod") library(quantmod) 

2. Downloading Stock Data

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.

R
getSymbols("ZOMATO.NS", src = "yahoo") 

Output:

'ZOMATO.NS'

3. Visualizing Historical Prices

We can visualize the price movement over time using the chartSeries() function. This creates a basic line chart of the historical adjusted closing prices.

R
options(repr.plot.width = 14, repr.plot.height = 8)  chartSeries(ZOMATO.NS, theme = chartTheme("black"), type = "line") 

Output:

zomato_price_chart
Zomato Stock Price Chart

4. Adding Moving Averages

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.

R
options(repr.plot.width = 14, repr.plot.height = 8) chartSeries(ZOMATO.NS, theme = chartTheme("black"), TA ="addSMA(n=20,col='blue') ; addSMA(n=50,col='red')") 

Output:

ma_zomato_price
Moving Averages

5. Summary of Recent Price Data

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.

R
print("Staring price of the stock") head(ZOMATO.NS) print("----------------------------------------------------") print("Latest price of the stock") tail(ZOMATO.NS) 

Output:

head_tail
Historical and Latest prices of the stock

6. Analyzing Price Change

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.

R
daily_returns <- dailyReturn(Cl(ZOMATO.NS)) plot(daily_returns, main = "Daily Returns of ZOMATO.NS") 

Output:

volitality
Volatility of the stock prices

From the analysis, we can conclude the following:

  1. The use of simple moving averages helps in understanding short- and long-term trends in the stock's price.
  2. Historical price plots provide insights into how the stock has performed over time.
  3. Daily returns highlight the stock’s day-to-day volatility, which is essential for risk evaluation.

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.


Next Article
Stock Data Analysis and Data Visualization with Quantmod in R

M

manojjai07nu
Improve
Article Tags :
  • R Language
  • Geeks Premier League
  • Data Visualization
  • Data Analysis
  • AI-ML-DS
  • Geeks Premier League 2023
  • AI-ML-DS With R

Similar Reads

    Getting started with Data Visualization in R
    Data visualization is the practice of representing data through visual elements like graphs, charts, and maps. It helps in understanding large datasets more easily, making it possible to identify patterns and trends that support better decision-making. R is a language designed for statistical analys
    5 min read
    Data visualization with R and ggplot2
    The ggplot2 ( Grammar of Graphics ) is a free, open-source visualization package widely used in R Programming Language. It includes several layers on which it is governed. The layers are as follows:Layers with the grammar of graphicsData: The element is the data set itself.Aesthetics: The data is to
    7 min read
    Multivariate Data Visualization with R
    A method for visualizing data with numerous variables is called multivariate data visualization with R. In this method, graphs and charts are made to show how the various factors relate to one another. The programming language R, which is frequently used for data visualization, provides a number of
    5 min read
    Data visualization with R : A Complete Guide for Beginners to Advance
    In R Programming Language you have a wide range of tools and packages to create stunning visualizations, allowing you to unlock insights and communicate your findings effectively. data visualization with R is like turning raw data into a work of art that helps you understand the story behind the num
    4 min read
    Data Visualization with Highcharter in R
    Highcharter is an R package that provides an interface to the Highcharts JavaScript library, enabling the creation of interactive and customizable charts. It supports a variety of chart types, such as line, bar and scatter charts and offers extensive customization options for appearance and behavior
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences