2

I want to add Elders Ray bars to the graph which is created using chartSeries. Sample data & expected output are given below.

> head(d1,10)
             Open   High    Low  Close      SMA     elderH     elderL
2016-01-13 201.35 202.40 194.00 200.90 211.8000  -9.400000 -17.800000
2016-01-14 197.00 201.50 194.30 196.05 208.2722  -6.772222 -13.972222
2016-01-15 197.80 197.80 181.00 184.30 204.2278  -6.427778 -23.227778
2016-01-18 184.90 188.15 179.15 181.00 200.1444 -11.994444 -20.994444
2016-01-19 182.15 187.80 181.50 182.95 196.3778  -8.577778 -14.877778
2016-01-20 180.65 180.65 171.50 173.65 192.3889 -11.738889 -20.888889
2016-01-21 177.00 178.65 172.05 175.60 188.6833 -10.033333 -16.633333
2016-01-22 179.90 185.40 178.30 184.60 186.4944  -1.094444  -8.194444
2016-01-25 187.90 188.80 182.50 182.95 184.6667   4.133333  -2.166667
2016-01-27 184.40 186.20 181.05 184.40 182.8333   3.366667  -1.783333
> 

Expected Output

enter image description here

I searched all possible help, but ended up in cloning and editing the 'quantmod:::ChartVo' function to draw the rectangles. Though I was able to achieve the required output, I think the approach is not correct one. Can any one help with better approach.

PS: I didn't pay attention to the labels, bg & fg colours. I am more interested in the approach in drawing the rectangles (both small ones & big ones) for eldersRay like shown in the picture. Changes to the Code in quantmod:::chartVo function.

if(x@params$thin) {
  # plot thin volume bars if appropriate
  segments(x.pos,0,x.pos,Volumes,col=bar.col)
} else {
  #Siva - commented the rect function and added larger rectangles first followed by smaller ones
  #rect(x.pos-spacing/3,0,x.pos+spacing/3,Volumes,col=bar.col,border=border.col)

  up.H <- ifelse(Volumes$elderL>0,Volumes$elderH,0)
  up.L <- ifelse(Volumes$elderL>0,Volumes$elderL,0)

  dn.H <- ifelse(Volumes$elderL<0,Volumes$elderH,0)
  dn.L <- ifelse(Volumes$elderL<0,Volumes$elderL,0) 
  rect(x.pos-spacing/3,0,x.pos+spacing/3,up.H,col=x@params$colors$up.col,border=border.col)
  rect(x.pos-spacing/3,0,x.pos+spacing/3,dn.L,col=x@params$colors$dn.col,border=border.col)

  rect(x.pos-spacing/3,0,x.pos+spacing/3,up.L,col=x@params$colors$dn.col,border=border.col)
  rect(x.pos-spacing/3,0,x.pos+spacing/3,dn.H,col=x@params$colors$up.col,border=border.col)
}

Can anyone help me with better approach?

Kevin Arseneau
  • 6,186
  • 1
  • 21
  • 40

0 Answers0