Gather some stocks, and reset them from $100, and compare them from 2009-2019.

Do someone know how to plot multiple lines in the same graph, and start from the same value, for example $100?

Then like you can compare Netflix, Amazon, Apple from 2009 to 2019?

Hi JokerMads1,
and welcome to the RStudio Community!

I think that the code below should get you started.

library(BatchGetSymbols)
#> Loading required package: rvest
#> Loading required package: xml2
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
#> 
library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date

# set dates
first.date <- ymd('2009-01-01')
last.date <- ymd('2019-12-12')
freq.data <- 'daily'

# set tickers
tickers <- c('NFLX','AAPL','AMZN')

stock_df <- BatchGetSymbols(tickers = tickers, 
                         first.date = first.date,
                         last.date = last.date) 
#> 
#> Running BatchGetSymbols for:
#> 
#>    tickers =NFLX, AAPL, AMZN
#>    Downloading data for benchmark ticker
#> ^GSPC | yahoo (1|1) | Not Cached | Saving cache
#> NFLX | yahoo (1|3) | Not Cached | Saving cache - Got 100% of valid prices | Youre doing good!
#> AAPL | yahoo (2|3) | Not Cached | Saving cache - Got 100% of valid prices | You got it!
#> AMZN | yahoo (3|3) | Not Cached | Saving cache - Got 100% of valid prices | Good job!

print(stock_df$df.control)                         
#> # A tibble: 3 x 6
#>   ticker src   download.status total.obs perc.benchmark.d… threshold.decis…
#>   <chr>  <chr> <chr>               <int>             <dbl> <chr>           
#> 1 NFLX   yahoo OK                   2730                 1 KEEP            
#> 2 AAPL   yahoo OK                   2730                 1 KEEP            
#> 3 AMZN   yahoo OK                   2730                 1 KEEP

ggplot(stock_df$df.tickers, aes(x=ref.date, y=price.close, color = ticker)) +
  geom_line()

Created on 2019-11-06 by the reprex package (v0.3.0)

1 Like

@phiggins

where in the script did you code $ 100 as starting capital?

I did not, as you can see from the graph.
This sounds a lot like a homework assignment,
and the two of you should be able to figure this out on your own.

@EconomiCurtis

1 Like

Thanks for the help!
WelI I try to learn RStudio as much as possible, everything helps

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.