Indicator structure review

Financial Trading in R

Ilya Kipnis

Professional Quantitative Analyst and R programmer

Review: using add.indicator()

add.indicator(strategy = strategy.st, name = “SMA",
              arguments = list(x = quote(Cl(mktdata)), 
                               n = 200),
              label = "SMA200")
Financial Trading in R

Naming indicators

  • Provide indicators with descriptive names

    • Ex. Name your 200 day simple moving average “SMA200”, not just “SMA”
  • Keep indicator names simple

Financial Trading in R

applyIndicators()

  • creates intermediate dataset containing market data and indicators
test <- applyIndicators(strategy = strategy.st, 
                          mktdata = OHLC(LQD))
head(test, n = 3)
           LQD.Open LQD.High  LQD.Low LQD.Close SMA.SMA200 
SMA.SMA50 DVO.DVO_2_126           
2003-01-02 58.37216 58.37216 57.32224  57.49366         NA    
 NA            NA
2003-01-03 57.63829 57.82042 57.45616  57.82042         NA 
 NA            NA
2003-01-06 57.71864 57.79363 57.39724  57.79363         NA        
 NA            NA
tail(test, n = 3)
           LQD.Open LQD.High  LQD.Low LQD.Close SMA.SMA200 SMA.SMA50 DVO.DVO_2_126
2015-12-23 113.9586 114.1979 113.8888   114.178   115.1378  115.0177     65.873016
2015-12-24 114.3400 114.5500 114.2000   114.550   115.1258  114.9885     92.857143
2015-12-28 114.3600 114.5600 114.2100   114.410   115.1147  114.9575     80.952381
  • In quantstrat, indicator labels take the form of the original name, a dot and your label
Financial Trading in R

applyIndicators() cont.

tail(test, n = 3)
           LQD.Open LQD.High  LQD.Low LQD.Close SMA.SMA200 
SMA.SMA50 DVO.DVO_2_126
2015-12-23 113.9586 114.1979 113.8888   114.178   115.1378  
115.0177     65.873016
2015-12-24 114.3400 114.5500 114.2000   114.550   115.1258  
114.9885     92.857143
2015-12-28 114.3600 114.5600 114.2100   114.410   115.1147  
114.9575     80.952381
  • In quantstrat, indicator labels take the form of the original name, a dot and your label
Financial Trading in R

Further indicator mechanics

  • HLC() returns the high, low, and close as a xts object
head(HLC(LQD))
           LQD.High  LQD.Low LQD.Close
2002-07-30 52.35639 51.97142  52.03302
2002-07-31 52.48472 52.12541  52.35126
2002-08-01 52.92102 52.51038  52.86456
2002-08-02 53.02368 52.58738  52.97235
2002-08-05 53.20334 52.61818  52.84402
2002-08-06 52.69004 52.40772  52.66437
Financial Trading in R

Further indicator mechanics

  • Use object[date/date] with HLC() to subset xts objects
HLC(LQD["2012-01-01/2012-01-07"])
           LQD.High  LQD.Low LQD.Close
2012-01-03 97.05994 96.63424  96.77897
2012-01-04 97.01737 96.58316  96.85560
2012-01-05 96.85560 96.37881  96.43841
2012-01-06 96.90669 96.54058  96.81303
Financial Trading in R

Let's practice!

Financial Trading in R

Preparing Video For Download...