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
• 18,570 points
478 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
• 52,360 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 Oct 14, 2019 in Data Analytics by Cherukuri
• 33,030 points
812 views
0 votes
1 answer

How to Identify comman pattern of the Products that sells more from a sales data base.

Hi, If you share a real database, then ...READ MORE

answered Sep 7, 2020 in Data Analytics by MD
• 95,440 points
573 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
625 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
2,053 views
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
1,993 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
1,605 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

edited Jun 9, 2020 by Gitika 892 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
994 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 5, 2018 in Data Analytics by Kalgi
• 52,360 points
753 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
• 52,360 points
833 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP