Saturday, October 22, 2016

Systematic Trading | An R Package for Automated Technical Analysis

This is an R package for automated technical analysis and some ground stuff for some pattern matching algorithm I plan to build. This is available at github - you can directly install it from github or you can fork or download. Currently it has three functionalities - 1) perceptually important points 2) change points for time series with linear deterministic trends and 3) automated technical support/ resistance/ price envelope identification (useful for back-test, but I have not found the time yet). It has also an undocumented module for technical pattern identification, which is in fluid state. Please note the is in early version and features/ data structures may undergo substantial changes in later version. I copy paste the R vignette below.


Techchart: Technical Feature Extraction of Time Series Data The R package techchart is a collection of tools to extract features from time series data for technical analysis and related quantitative applications. While R is not the most suitable platform for carrying out technical analysis with human inputs, this package makes it possible to extract and match technical features and patterns and use them to back-test trading ideas. At present, the package covers four major areas:
  • Perceptually Important Points (PIPs) identification
  • Supports/resistance identification (either based on PIPs or the old-fashioned Fibonacci method)
  • Change point analysis of trends and segmentation of time series based on underlying trend
  • Identification of technical envelopes (like trend channels or triangles) of a time series

Perceptually Important Points

PIPs are an effort to algorithmically derive a set of important points as perceived by a human to describe a time series. This typically can be a set of minima or maxima points or a set of turning points which are important from a feature extraction perspective. Traditional technical analysis - like technical pattern identification - relies heavily on PIPs. In addition, a set of PIPs can be used to compress a time series in a very useful way. This compressed representation then can be used for comparing segments of time series (match finding) or other purposes. In this package, we have implemented the approach detailed here.
spx <- quantmod::getSymbols("^GSPC", auto.assign = FALSE)
spx <- spx["2014::2015"]
imppts <- techchart::find.imppoints(spx,2)
head(imppts)
##            pos sign   value
## 2014-02-03  22   -1 1741.89
## 2014-03-07  45    1 1878.52
## 2014-03-14  50   -1 1841.13
## 2014-04-03  64    1 1891.43
quantmod::chart_Series(spx)
points(as.numeric(imppts$maxima$pos),as.numeric(imppts$maxima$value),bg="green",pch=24,cex=1.25)
points(as.numeric(imppts$minima$pos),as.numeric(imppts$minima$value),bg="red",pch=25,cex=1.25)

The function takes in a time series object (in xts format), and a tolerance level for extreme points identification (can be either a percentage or a multiple of standard deviation). It returns an object which has the list of all PIPs identified, marked by either a -1 (minima) or 1 (maxima), as well as the maxima and minima points separately as xts objects

Supports/ Resistance

Supports and resistance levels are very popular tools for technical analysis. The function find.pivots implements a couple of ways to identify supports and resistance levels for a price series. Using the option FIB will produce a set of Fibonacci levels around the most recent price point. The option SR will run an algorithm to find co-linear points along x-axis (horizontal line) to find levels most tested in recent times. A set of levels as well as xts representation of the lines defined by them are returned
spx <- quantmod::getSymbols("^GSPC", auto.assign = FALSE)
spx <- spx["2014::2015"]
sups <- techchart::find.pivots(spx, type = "FIB")
summary(sups)
## supports and resistance:
## next 3 supports:1982.249 1936.355 1890.461
## next 3 resistance:2130.82
sups <- techchart::find.pivots(spx, type = "SR", strength = 5)
summary(sups)
## supports and resistance:
## next 3 supports:2043.688 1992.551 1895.028
## next 3 resistance:2070.407 2111.588

Price Envelop Identification

Price envelopes features are an integral part of technical analysis. For example technical analysts look for features like trending channel, or ascending triangles etc to identify continuation or breakout from current price actions. The function find.tchannel identifies the most recent such envelopes using an implementation of the popular Hough transform algorithm in image processing, along with some heuristics.
spx <- quantmod::getSymbols("^GSPC", auto.assign = FALSE)
spx <- spx["2016-01-01::2016-09-30"]
tchannel <- techchart::find.tchannel(spx,1.25)
tchannel
## name: channel
## type: neutral
## direction: 0
## threshold: NA
quantmod::chart_Series(spx)

quantmod::add_TA(tchannel$xlines$maxlines[[1]],on=1, lty=3, col="brown")

quantmod::add_TA(tchannel$xlines$minlines[[1]],on=1, lty=3, col="brown")

The function returns an object with parameters of the envelopes found (if any), as well as the xts representation of the envelopes lines

2 comments:

  1. I can't download techchart in r 3.3.2.
    Warning in install.packages :
    package ‘techchart’ is not available (for R version 3.3.2)
    How do I do?

    ReplyDelete
  2. you need to install the devtools package and after loading it, run the install_github("prodipta/techchart") or githubinstall("techchart"). That should work. Please see here for more details: https://cran.r-project.org/web/packages/githubinstall/vignettes/githubinstall.html

    ReplyDelete