**R programming**, and kindly contributed to R-bloggers)

“The

quantmodpackage for R is designed to assist the quantitative trader in the development, testing, and deployment of statistically based trading models.”

It is a rapid prototyping environment where enthusiasts can explore various technical indicators with minimum effort. It offers charting facilities that is not available elsewhere in R. Quantmod package makes modeling easier and analysis simple. This article is intended to present some functions of quantmod using sample market data. The features of quantmod are presented in three sections, downloading data, charting, technical indicators and other functions.

Without much ado, we will see the usage of quantmod package.

### Downloading data

Once the quantmod package is installed and library is loaded, run the following command to get the data of apple stock into thr R console.

getSymbols(‘AAPL’)

To see the starting point of the data, type the following command.

head(AAPL) # You should see the following result.

AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted 2007-01-03 86.29 86.58 81.90 83.80 309579900 11.19449 2007-01-04 84.05 85.95 83.82 85.66 211815100 11.44295 2007-01-05 85.77 86.20 84.40 85.05 208685400 11.36147 2007-01-08 85.96 86.53 85.28 85.47 199276700 11.41757 2007-01-09 86.45 92.98 85.15 92.57 837324600 12.36603 2007-01-10 94.75 97.80 93.45 97.00 738220000 12.95782

### Visualize the charts

The beauty of quantmod lies in its ability to visualize the charts. Type the following command.

chartSeries(AAPL, TA=NULL) # this should produce the following chart.

As one can see, the x axis shows the time period, y axis shows price range for apple stock. In the command above we set TA=”Null”. It means do not include any Technical Analysis parameter. The following command produces the same graph along with volume parameter.

barChart(AAPL)

The noting difference between this graph and the previous one is the representation of volume of the apple stock traded. Rest of the syntax is meant to decorate the chart appearance.

We shall choose closing price for reference and calculate various technical indicators based on it. Following command selects the closing price of apple.

Apple_closeprice = Cl(AAPL) # We assign the closing price to a new variable called Apple_closeprice. plot(Apple_closeprice) # Plotting the close price

Plotting histogram is simple.

hist(AAPL[,4]) #This command plots the histogram of closing price of apple stock.

hist(NSEI[,4], main = "Apple Close") #The histogram of closing price of apple stock with the heading “Apple Close”

hist(NSEI[,4], main = "Apple Close", breaks =25) # Introducing more price ranges.

### Technical indicators

chartSeries(AAPL) addMACD() # adds moving average convergence divergence signals to the apple stock price

addBBands() # Adds Bollinger bands to the apple stock price.

addCCI() # Add Commodity channel index.

addADX() #Add Directional Movement Indicator

addCMF() #Add Chaiken Money Flow

Similarly, other technical indicators can be calculated. Following is the list of technical indicators that quantmod supports.

addCMO # Add Chaiken Money Flow addDEMA # Add Double Exponential Moving Average addDPO # Add Detrended Price Oscillator addEMA # Add Exponential Moving Average addEnvelope # Add Price Envelope addEVWMA # Add Exponential Volume Weigthed Moving Average addMACD # Add Moving Average Convergence Divergence addMomentum # Add Momentum addROC # Add Rate of Change addRSI # Add Relative Strength Indicator addSAR # Add Parabolic Stop and Reverse addSMA # Add Simple Moving Average addSMI # Add Stocastic Momentum Index addTRIX # Add Triple Smoothed Exponential Oscillator addVo # Add Volume addWMA # Add Weighted Moving Average

We will have a look at the data handling features of quantmod. We saw earlier that the apple data downloaded has the following structure.

AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted 2007-01-03 86.29 86.58 81.90 83.80 309579900 11.19449 2007-01-04 84.05 85.95 83.82 85.66 211815100 11.44295 2007-01-05 85.77 86.20 84.40 85.05 208685400 11.36147 2007-01-08 85.96 86.53 85.28 85.47 199276700 11.41757 2007-01-09 86.45 92.98 85.15 92.57 837324600 12.36603 2007-01-10 94.75 97.80 93.45 97.00 738220000 12.95782

### Useful functions

Quantmod provides functions to explore features of the data frame. The following command shows that the object type holding apple data is xts and zoo.

class(AAPL)

One would want to explore whether the data extracted has open price, volume etc. Have a look at the following commands.

is.OHLC(AAPL) # Checks whether the xts data object has the open,high, low and close price?

Output is TRUE implying that the data object contains open, high, low and close.

has.Vo(AAPL) # Checks whether the data object has volume seriesHi(AAPL) # To check the highest point of price. Lag(Cl(AAPL)) #One period lag of the closing price Next(OpCl(AAPL)) #The next periods open to close - today! AAPL ['2007'] #Fetches all Apple’s 2007 OHLC AAPL ['2008::'] # Apple data, from 2008 onward dailyReturn(AAPL) # Returns by day weeklyReturn(AAPL) # Returns by week monthlyReturn(AAPL) # month, indexed by yearmon daily,weekly,monthly,quarterly, and yearly allReturns(AAPL) # note the plural

### Next Step

If you’re new to this and not able to grab all the technical aspects of this article, you may like to take a look at a few articles explaining basic concepts like, designing a quant trading strategy in R. You can also take a look at a basic example of trading strategy coded in R.

Once you’ve successfully learned these basics you can test your skills at our interactive self-paced 10 hours long datacamp course ‘Model a Quantitative Trading Strategy in R‘

The post A Guide on R quantmod Package: How to Get Started? appeared first on .

**leave a comment**for the author, please follow the link and comment on their blog:

**R programming**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...