Implementing Technical Indicators: BOL

Programming Technical Indicators R BOL


In this part of my series Implementing Technical Indicators I provide you with the R implementation of another technical indicator. Please refer to my article Technical Indicators: An Introduction for an overview of the concept of technical indicators and for details on the particular one presented in this article.

DISCLAIMER: None of the below is intended to be considered as any kind of investment advice. All examples serve as illustrative material only.


For an implementation of the Bollinger bands (BOL) trading strategy please see the R code below. The function BOL calculates both indicator values and resulting trading signals, which may afterwards be plotted with help of the function plotBOL. Note that both function use tools introduced in my article Implementing Technical Indicators: Tools.

# long / short indicator


# indicator calculation
BOL = function(v_values, i_window=20, d_leverage=2)
  checkWindow(v_values, i_window)
  i_length = length(v_values)
  v_ma  = movingAverage(v_values, i_window)
  v_sdv = standardDeviation(v_values, i_window)
  v_upper_band = v_ma + d_leverage * v_sdv
  v_lower_band = v_ma - d_leverage * v_sdv
  v_signals = rep(0, i_length)
  if (v_values[1] >= v_ma[2]) {
    v_signals[1] = 1
  } else {
    v_signals[1] = -1
  for (i in 2 : i_length) {
    if (v_values[i] >= v_upper_band[i]) {
      v_signals[i] = 1
    } else if (v_values[i] <= v_lower_band[i]) {
      v_signals[i] = -1
    } else {
      v_signals[i] = v_signals[i-1]
  df_bol = data.frame(v_signals, v_values, v_ma, v_upper_band, v_lower_band)
  colnames(df_bol) = c("Signal", "Price", "MA", "uBand", "lBand")

# indicator plotting
plotBOL = function(v_date, df_bol, s_path)
  dateMatch(v_date, df_bol)
  v_colors = c("black", "blue", "purple", "purple")
  v_legend = c("Price", "MA", "Upper Band", "Lower Band")
  plotIndicator(v_date, df_bol$Signal, df_bol[, -1], v_colors, v_legend, s_path)