
I’d like some helping adding a text series to a quantmod “chartSeries” bar
chart.  L.A. Little’s recent book “Trend Qualification and Trading” gives
an algorithm for swing point calculation (pp. 32) that I’ve implemented in
a function below (swingPointCalc).   The history for Randgold Corp. (GOLD)
is attempting to replicate the example on pp. 55, which applies text for
Swing Point High (SPH) and Swing Point Low (SPL) at the respective dates.
My function is creating the “xts” object “swingPoints” which  has the
SPL/SPH text that I’d like to have applied to the “chartSeries”.  What’s
the simplest way to display the text “SPL/SPH” right above the
corresponding low, high prices on the “chartSeries” chart?

Thank you,

Jon Noble
jnoble1@mmm.com


#
------------------------------------------------------------------------------------------------------------------------------


# Getting stock data

library(quantmod)
library(TTR)
library(PerformanceAnalytics)

# Parameters
SymbolName="GOLD"
initDate="2010-02-01"
endDate="2010-05-28"

# pull stock data from Yahoo Finance
symbol<-getSymbols(SymbolName,from=initDate,to=endDate)
stock<-xts(get(symbol))

chartSeries(stock)

# --------------------- swingPointCalc function
--------------------------------------------------------------------

swingPointCalc<-function(x){

# Initialize variables
    swingPoints<-character(nrow(x))
    Pot_SPL_Row<-numeric(0)
    Pot_SPH_Row<-numeric(0)
    successiveLows<-1
    successiveHighs<-1
    PotSwingLow<-as.numeric(Lo(x[2]))
    PotSwingHigh<-as.numeric(Hi(x[2]))

    for (i in 3:nrow(x)) {

      # Calc "Swing Point Lows"" (SPL)

      Low<-as.numeric(Lo(x[i]))
      if(successiveLows>6) {
        swingPoints[Pot_SPL_Row]<-'SPL'
        PotSwingLow<-Low
        Pot_SPL_Row<-i
        successiveLows<-1
      }
      if(PotSwingLow>Low) {
         PotSwingLow<-Low
         Pot_SPL_Row<-i
         successiveLows<-1
      }
      if(PotSwingLow<Low) successiveLows <- successiveLows + 1

      # Calc "Swing Point Highs" (SPH)

      high<-as.numeric(Hi(x[i]))
      if(successiveHighs>6) {
        swingPoints[Pot_SPH_Row]<-'SPH'
        PotSwingHigh<-high
        Pot_SPH_Row<-i
        successiveHighs<-1
      }
      if(PotSwingHigh<high) {
         PotSwingHigh<-high
         Pot_SPH_Row<-i
         successiveHighs<-1
      }
      if(PotSwingHigh>high) successiveHighs <- successiveHighs + 1
    }
swingPoints<<-as.xts(data.frame(swingPoints,row.names=index(x)))
}

# --------------- Call function with the GOLD data




	[[alternative HTML version deleted]]

