• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Finance Train

Finance Train

High Quality tutorials for finance, risk, data science

  • Home
  • Data Science
  • CFA® Exam
  • PRM Exam
  • Tutorials
  • Careers
  • Products
  • Login

Creating Charts with Quantmod

Data Science, Quantitative Finance

This lesson is part 15 of 21 in the course Quantitative Trading Strategies in R

Apart from loading data from external and local sources, Quantmod is also suitable for making beautiful charts. There are three types of charts: lines, bars and candlestick. We will learn how to create these charts to show historical data for SPY index. In Quantmod, the function to create charts is called chartSeries. With the type parameter we can change the chart type.

# Use the chartSeries() function to create charts. With the type parameter we can change the chart type.

chartSeries(SPY, type="line")

We can also subset the graph for a certain period using the subset parameter. The following command produces a candlestick chart only for the 2019 data.

chartSeries(SPY,  type="candlestick", subset='2019')

Adding Technical Indicators with Quantmod

The Quantmod package provides built in technical indicators that can be included in the charts. We will now see how to calculate technical indicators with Quantmod for the SPY index and finally, create an R dataframe with the close prices and the technical indicators as columns.

#### Calculates Indicators: EMA,RSI, ADX, STOCHASTIC, ATR
 
EMA <- round(EMA(Cl(SPY),n=14),3)
RSI <- round(RSI(Cl(SPY),n=14),3)
ADX <- round(ADX(HLC(SPY),n=14),3)
ATR <- round(ATR(HLC(SPY),n=14),3) 
STOCH <- round(stoch(HLC(SPY)),3) * 100
 
# At this point it will be a good idea to explore these objects and view the data contained.
# You can use commands such as dim(), head(), and tail() to do this.

# Combine the required indicators into a single data table.
 
spyIndicators <- data.frame(cbind(Cl(SPY),RSI,ADX$ADX,ATR$atr,STOCH$fastK,STOCH$fastD))
 
# Add custom column names for each column
 
colnames(spyIndicators) <- c('SPY','RSI','ADX','ATR','KLine','DLine')
 
# View last 15 rows of the data to get a feel of the data
tail(spyIndicators,15) # shows the last 15 rows of the data
 
              SPY    RSI    ADX   ATR KLine DLine
2019-05-15 285.06 44.151 26.221 3.617  34.2  21.1
2019-05-16 287.70 49.858 24.589 3.655  51.7  36.3
2019-05-17 285.84 46.270 22.850 3.642  39.3  41.7
2019-05-20 283.95 42.893 21.863 3.585  26.8  39.3
2019-05-21 286.51 48.388 20.588 3.542  45.7  37.3
2019-05-22 285.63 46.724 19.558 3.402  39.6  37.3
2019-05-23 282.14 40.739 19.917 3.521  16.5  33.9
2019-05-24 282.78 42.201 19.857 3.420  26.2  27.4
2019-05-28 280.15 38.047 20.298 3.463   2.3  15.0
2019-05-29 278.27 35.367 21.444 3.461  12.5  13.7
2019-05-30 279.03 37.290 22.225 3.373  18.6  11.1
2019-05-31 275.27 32.187 23.460 3.403   0.2  10.4
2019-06-03 274.57 31.328 24.981 3.407   9.2   9.3
2019-06-04 280.53 44.834 24.821 3.600  46.2  18.5
2019-06-05 282.96 49.220 23.937 3.534  61.2  38.9

Add Indicators to Charts 

In order to add technical indicators in a chart, Quantmod provides the add{IndicatorName)() function. We will use this function for plotting the RSI and the MACD of SPY prices. Both indicators are plotted with the default parameters which are specified below:

### Charting with Technical Indicators 

chartSeries(SPY,type="line")
addRSI(n=14,maType="EMA")
addMACD(fast=12,slow=26,signal=9,type='EMA',histogram = TRUE)
Previous Lesson

‹ Downloading Data Using Quantmod Package in R

Next Lesson

Data Analysis with Quantmod in R ›

Join Our Facebook Group - Finance, Risk and Data Science

Posts You May Like

How to Improve your Financial Health

CFA® Exam Overview and Guidelines (Updated for 2021)

Changing Themes (Look and Feel) in ggplot2 in R

Coordinates in ggplot2 in R

Facets for ggplot2 Charts in R (Faceting Layer)

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

In this Course

  • Introduction to Quantitative Trading
  • Quantitative Trading – Advantages and Disadvantages
  • Types of Quantitative Trading Strategies
  • Momentum Strategies
  • Mean Reversion Strategies
  • Market Making Strategies and Day Trading Strategies
  • How to Generate Trading Ideas
  • Designing A Trading Strategy For Profit
  • Backtesting a Trading Strategy – Considerations
  • Risk Management of a Trading Strategy
  • Risk Indicators – VIX Index and TED Spread
  • Plotting the VIX Index and TED Spread in R
  • Introduction to Quantmod in R
  • Downloading Data Using Quantmod Package in R
  • Creating Charts with Quantmod
  • Data Analysis with Quantmod in R
  • Measuring Overall ETFs Performance
  • Quantstrat Example in R – EMA Crossover Strategy
  • Quantstrat – EMA Crossover Strategy – Performance and Risk Metrics
  • Quantstrat Example in R – RSI Strategy
  • Quantstrat Case Study – Multiple Symbol Portfolio

Latest Tutorials

    • Data Visualization with R
    • Derivatives with R
    • Machine Learning in Finance Using Python
    • Credit Risk Modelling in R
    • Quantitative Trading Strategies in R
    • Financial Time Series Analysis in R
    • VaR Mapping
    • Option Valuation
    • Financial Reporting Standards
    • Fraud
Facebook Group

Membership

Unlock full access to Finance Train and see the entire library of member-only content and resources.

Subscribe

Footer

Recent Posts

  • How to Improve your Financial Health
  • CFA® Exam Overview and Guidelines (Updated for 2021)
  • Changing Themes (Look and Feel) in ggplot2 in R
  • Coordinates in ggplot2 in R
  • Facets for ggplot2 Charts in R (Faceting Layer)

Products

  • Level I Authority for CFA® Exam
  • CFA Level I Practice Questions
  • CFA Level I Mock Exam
  • Level II Question Bank for CFA® Exam
  • PRM Exam 1 Practice Question Bank
  • All Products

Quick Links

  • Privacy Policy
  • Contact Us

CFA Institute does not endorse, promote or warrant the accuracy or quality of Finance Train. CFA® and Chartered Financial Analyst® are registered trademarks owned by CFA Institute.

Copyright © 2021 Finance Train. All rights reserved.

  • About Us
  • Privacy Policy
  • Contact Us