当一个EMA穿过另一个EMA时,在该点绘制一个箭头

时间:2016-09-22 01:13:56

标签: r

#  Make a new environment for quantmod to store data in
stockData <- new.env()

#  prices are for the first 6 months of 2016
startDate = as.Date("2016-01-01") 
endDate = as.Date("2016-06-01")

#  Define the ticker we are interested in
tickers <- c("LGIH") 

# Download the stock history (for all tickers). Here the prices are   downloaded from
# google finance
getSymbols(tickers, env = stockData, src = "google", from = startDate, to =   endDate)


#  Lets plot the data
chartSeries(stockData$LGIH)

addEMA(n=8, on=1, with.col=Cl, overlay=TRUE, col="yellow")
addEMA(n=21, on=1, with.col=Cl, overlay=TRUE, col="orange")

stockData$LGIH$EMA_1<-EMA(na.locf(Cl(stockData$LGIH)),8) # 8
stockData$LGIH$EMA_2<-EMA(na.locf(Cl(stockData$LGIH)),25) # 21


for( i in 1:nrow(stockData$LGIH) )
{
  CurrentDate <- time(stockData$LGIH)[i]

 EMA1 <- as.numeric(stockData$LGIH[i,'EMA_1'])
 EMA2 <- as.numeric(stockData$LGIH[i,'EMA_2'])

 if( !is.na(EMA1)  & # if the moving average has begun
     !is.na(EMA2))# if the moving average has begun # 
 {
    if( EMA1 > EMA2) {

      arrow.plot( as.Date(CurrentDate),EMA1,arrow.ex=.8, length=.1,       col='green', lwd=2)
      break

    } else { 
      if( EMA1 < EMA2) {

        arrow.plot( as.Date(CurrentDate),EMA2,arrow.ex=.8, length=.5,    col='blue', lwd=2)
       break

      } 
    }
  }

}

我无法在8EMA穿过21EMA的地块上得到箭头 任何帮助将不胜感激

情节我没有箭头

enter image description here

0 个答案:

没有答案