How to download the historical data from two random picked stocks?

0 votes

I'm trying to randomly select 2 stocks out of the Swiss Market Index, which contains of 30 stocks.

Until now I solved the random pick of the 2 stocks with the following code:

SMI_components <- cbind("ABB (ABBN.VX)", "ADECCO (ADEN.VX)", "ACTELION (ATLN.VX)", "JULIUS BAER GRP (BAER.VX)", "RICHEMONT (CFR.VX)", "CREDIT SUISSE (CSGN.VX)", "GEBERIT (GEBN.VX)", "GIVAUDAN (GIVN.VX)", "HOLCIM (HOLN.VX)", "NESTLE (NESN.VX)", "NOVARTIS (NOVN.VX)", "TRANSOCEAN (RIGN.VX)", "ROCHE HOLDING (ROG.VX)", "SWISSCOM (SCMN.VX)", "SGS (SGSN.VX)", "SWISS RE (SREN.VX)", "SYNGENTA (SYNN.VX)", "UBS (UBSG.VX)", "SWATCH GROUP (UHR.VX)", "ZURICH INSURANCE GROUP (ZURN.VX)")

 for(i in 1:1){
 print(sample(SMI_components, 2))
}

How do I continue my code, if I want to download the historical data from these two random picked stocks?

Nov 5, 2018 in Data Analytics by Hannah
• 15,440 points
40 views

1 answer to this question.

0 votes

assign the output of sample into a variable.

my_picks <- sample(SMI_components, 2)

Extract ticker symbol between parens (courtesy the comment below):

my_picks <- sub(".*\\((.*)\\).*", "\\1", my_picks)

Then you can use lapply, to call a function (yahooSeries) for each value in my_picks.

series_list <- lapply(my_picks, yahooSeries, from = "2005-01-01", to = "2015-07-30", frequency = "daily")

Then you'll get the output in a list. series_list[[1]] will have the output of yahooSeries for the first value of my_picks, and series_list[[2]] for the second

answered Nov 5, 2018 by Kalgi
• 41,660 points

Related Questions In Data Analytics

0 votes
1 answer

How to edit the data source from R?

Use edit function like below and pass ...READ MORE

answered 4 days ago in Data Analytics by Cherukuri
• 28,040 points
15 views
0 votes
1 answer

How to Share the files from local system Mac Book to Edureka VM

Hey Satya, Hope you're doing great. Would you please ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,610 points
55 views
0 votes
1 answer

How to import data from a Json file into R?

Easy and simple: library("rjson") json_file <- "file_name" json_data <- fromJSON(file=json_file)) or ...READ MORE

answered Apr 26, 2018 in Data Analytics by DeepCoder786
• 1,720 points
193 views
0 votes
2 answers
0 votes
1 answer

By using dpylr package sum of multiple columns

Basically here we are making an equation ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
105 views
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
134 views
0 votes
1 answer

In a dpylr pipline how to use sample and seq?

For avoiding rowwise(), I prefer to use ...READ MORE

answered Apr 6, 2018 in Data Analytics by DeepCoder786
• 1,720 points
78 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
84 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 4, 2018 in Data Analytics by Kalgi
• 41,660 points
55 views
0 votes
2 answers

How to arrange a data set in ascending order based on a variable?

In your case it'll be, orderedviews = arrange(movie_views, ...READ MORE

answered Nov 27, 2018 in Data Analytics by Kalgi
• 41,660 points
43 views