Chapter 35 Technical Analysis for Stocks using Plotly

Ivan Ugalde (du2160); Bernardo Lopez (bl2786)

The goal of this R markdown file is to show how to use Apha Vantage API to download stocks data, organize it and make interactive plots using Plotly that will help to find trends in the market.

35.1 Import all libraries

We will be ussing:

  • plotly: For interactive plots.
  • alphavantager: R package from Alpha Vantage for downloading stocks data.
  • tidyverse

35.2 Download data from Alpha Vantage

Alpha Vantage provides free historical and real time information on stocks via an API.

35.3 Simple plot: 2 traces in same axis

To add traces to the plot we use add_trace. In this example we will use as type “candlestick” and “scatter”.

35.4 Many traces in independent axis but in same plot

Plotly give us interactivity, that allows to zoom the data for the same dates for all graphs simultanously.

By using the parameter yaxis we can plot traces in different Y axis but same X axis.

35.5 Aesthetics: background and margins

We use parameter paper_bgcolor and plot_bgcolor of layout to change the color of the background of the paper and the plot, we decided to use black. Parameter margin is used to change the size of the plot margins.

35.6 More aesthetics: hide legends and hide X-axis slider

To hide the legend we must set the parameter showlegend as false.

For every Y-axis we used parameters titlte to set the title of the axis and titlefont and tickfont to change the colors of the title and the ticks.

35.7 Shortcuts to slice data by pre-fixed date ranges

This last chunk of code will add a rangeselector to the x axis. This buttons will allow us to focus only in the last 3, 6 or 12 months. We can also see all the data or see it year to date (from the begining of the year to the current day). For each button in the rangeselector we have to specify the count, label, step and stepmode.