R-Notebook and Technical Analysis of Stocks

Data Science is about discovery rather than a clear cut journey to a well-defined goal, or perhaps more appropriately a hypothesis. Sometimes the purpose of the exercise is to find some pattern of events that leads to smarter business decisions. The nature of discovery is iterative. R-Notebook provides a scratchpad feature which is ideal for this type of data analysis. In this article we apply these ideas to stock movement analysis. The purpose is to show how quickly one can apply various types of measures of technical analysis to analyse stock movement over a period. This is discovery through visualisation of the measures applied to the selected stock.

STEP-0 

The following software will be required for this example. RStudio preview edition incorporating the R-Notebook feature, currently 1.0.44 is available for download. The following R packages will also need to be installed, quantmod, xts, and RMySQL.

STEP-1

To proceed with this example one needs to have market data for the selected stock. The recommendation (followed here) is to get historical end-of-day price data from yahoo finance (currently free) and set up a table to load the data. What you get is the date, open, high, low, close, and volume of the selected stock. Such a table can be updated on a daily or as frequently as feasible.

STEP-2

 

chunk-1

The above chunk of code in the R-Notebook when executed creates a data frame of the required data from the table. The code is executed by ‘clicking’ on the green arrow on the top right hand of the figure.

STEP-3

chunk-2

 

This second chunk of code in the R-Notebook uses that data read form the table to create the first price chart. It converts the data frame to a time series object via the xts library. This is not strictly necessary, but this technique is used for other future analysis. The chartSeries function of the quantmod is used to visualise the first set of stock technical analysis.

Using the chartSeries function we have added the MACD (Moving Average Convergence Divergence), RSI (Relative Strength Index), ROC (Rate of Change) indicators. The candle chart is overlaid with the exponential moving average. The chart is also restricted to a subset of the period that is of investigative interest.

response-2

Step-4

chunk-3 This chunk of code in the scratch pad (R-Notebook) visualises the same time series data but with an overlay of the Bollinger Bands.

response-3

SUMMARY

  • R-Notebook is an excellent tool for this type of data visualisation for technical stock analysis. As a scratch pad you can change and alter the visualisation, and add many different types of technical analytics provided by the quantmod library.
  • R-Notebook can provide a pdf of the completed analysis notebook.
  • R-Notebook provides a HTML page of the analysis notebook, which can be viewed on a browser. Since the analysis done in this notebook is using a MySQL table; sharing this HTML page is not possible. If instead the getSymbol function of quantmod was used, then sharing would be feasible.
  • The charts can be of better clarity, but for the purpose of this exercise the visualisation is fine.
  • There are many technical measures that can be used, but we focused on those that we are acquainted with, and those that are most often found being used in the literature. The purpose was not to astound the reads with exotic technical measures, of which there are plentiful.
  • In a future article we will try and do the same (and more) with options data for an underlying stock.

About KM Mukku

Kick-start, build and manage teams in product development (particularly in the financial domain), and enjoy all in adaptive case management, business process design and business process improvement. Currently holding the position of CTO at coMakeIT.
This entry was posted in QUANT, R-Notebook, Technical Analysis. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s