Since Plotly made their interactive graphing platform available for R, I’ve been trying to incorporate it into more of my projects. Recently I was working on a stock graph that included multiple moving averages and thought it could be useful to use vertical lines to help define buy and sell points. While ggplot2 would allow me to pass a single vector of values into the
geom_vline() argument, Plotly appears to require a separate list of arguments for each one (presumably because of the complete control they give you over the positioning of your shapes). Below is a snippect of the data I used to build the graph. Full data available here.
|Date||GE Adj Price||48.5 day EMA||13 day EMA||48.5d greater||Lag||Signal|
Below is an example of arguments necessary to build a vertical line from the x axis to the adjusted price for GE on 2-11-1997.
Obviously drawing more than a couple lines in this manner would be very clumsy. My goal was to point out crossover points between the 2 EMAs, which could mean many vertical lines would need to be drawn. To make this task easier, I created a simple
for loop to identify crossovers indicating a “buy” (in green) or a “sell” (in red).
First I filtered out all the days where the EMAs did not cross:
Then I created a list of lists, with each of the nested lists containing the arguments for one line. To make it easier for the end user to distinguish between “buy” and “sell” days, I used an
ifelse statement to alter the line color for each iteration depending on the signal.