0

Sorry, very silly newbie question.. regarding FinancialData, how to return multiple specified properties.

eg. if I want Open & Close properties , or perform an operation such as Open-Close

FinancialData["SP500","Open",{2015}]
FinancialData["SP500","Close",{2015}]

return daily Opening and Closing property values but as separate lists.

bbgodfrey
  • 61,439
  • 17
  • 89
  • 156
  • Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. – bbgodfrey Mar 23 '15 at 01:42
  • Tried using 'Union' but it seems a bit long winded to combine the lists.. is that the best? Need output with 3 columns, - date, open, close. – Pavan Lulla Mar 23 '15 at 03:04
  • Thanks a lot! Can I use the MapIndexed & Join to add the Open-Close column too? Wish it was not so cryptic. I expected an SQL like language. – Pavan Lulla Mar 23 '15 at 08:48
  • Easier to use Table[{o[[i, 1]], o[[i, 2]], c[[i, 2]], o[[i, 2]] - c[[i, 2]]}, {i, Length[o]}] to add the Open-Close column. – bbgodfrey Mar 23 '15 at 12:08

1 Answers1

2

Defining, for convenience,

o = FinancialData["SP500", "Open", {2015}]
c = FinancialData["SP500", "Close", {2015}]

the two can be combined into 3 columns, - date, open, close" with

MapIndexed[Join[#, c[[#2, 2]]] &, o]
(* {{{2015, 1, 2}, 2058.9, 2058.2}, {{2015, 1, 5}, 2054.44, 2020.58}, {{2015, 1, 6}, 2022.15, 2002.61}, ...} *)

Other methods include

Cases[MapThread[Join, {o, c}], {l1_, l2_, l3_, l4_} -> {l1, l2, l4}, Infinity]

Table[{o[[i, 1]], o[[i, 2]], c[[i, 2]]}, {i, Length[o]}]
bbgodfrey
  • 61,439
  • 17
  • 89
  • 156