2
$\begingroup$

I can display various metrics for one stock and want to know if I can do it easily for many stocks. In my example, I used Microsoft and the code works. Is there a way of setting up another list like techstocks={"MSFT","AMZN","CSCO","ORCL"} and then having the code below altered to cycle through each of the elements in techstocks to display each of the values in Stockstats?

Stockstats = {"BookValuePerShare","PriceToBookRatio","EarningsPerShare","PERatio"}
FinancialData["MSFT", #] & /@ Stockstats
$\endgroup$
  • $\begingroup$ Try Function[stock, FinancialData[stock, #]& /@ Stockstats] /@ techstocks $\endgroup$ – MarcoB Feb 16 '17 at 4:17
2
$\begingroup$

Here is one way to do it.

stockstats =
  {"BookValuePerShare", "PriceToBookRatio", "EarningsPerShare"};
techstocks = {"MSFT", "AMZN", "CSCO", "ORCL"};
FinancialData[#1, #2]& @@@ Catenate @ Outer[List, techstocks, stockstats]

{8.9, 7.25, 2.12, 40.43, 20.69, 4.9, 12.62, 2.56, 2.09, 11.81, 3.48, 2.1}

Or if you want labeling.

{#1, #2, FinancialData[#1, #2]} & @@@ Catenate @ Outer[List, techstocks, stockstats]
{{"MSFT", "BookValuePerShare", 8.9}, 
 {"MSFT", "PriceToBookRatio", 7.25}, 
 {"MSFT", "EarningsPerShare", 2.12}, 
 {"AMZN", "BookValuePerShare", 40.43}, 
 {"AMZN", "PriceToBookRatio", 20.69}, 
 {"AMZN", "EarningsPerShare", 4.9}, 
 {"CSCO", "BookValuePerShare", 12.62}, 
 {"CSCO", "PriceToBookRatio", 2.56}, 
 {"CSCO", "EarningsPerShare", 2.09}, 
 {"ORCL", "BookValuePerShare", 11.81}, 
 {"ORCL", "PriceToBookRatio", 3.48}, 
 {"ORCL", "EarningsPerShare", 2.1}}
$\endgroup$
  • $\begingroup$ This worked perfectly, thank you very much. $\endgroup$ – Andrew Tyler Feb 16 '17 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.