5
Ticker[comp_String] := 
 Interpreter["Company"][comp] /. Entity[_, x_] :> x

ticks = Ticker /@ {"Apple", "Google"}

{"NASDAQ:AAPL", "NASDAQ:GOOGL"}

DateListPlot[{
  FinancialData[ticks[[1]],  "CumulativeFractionalChange", {2010}],
  FinancialData[ticks[[2]],  "CumulativeFractionalChange", {2010}],
  FinancialData["NASDAQ100", "CumulativeFractionalChange", {2010}]
  },
 GridLines -> Automatic,
 PlotLegends -> {ticks[[1]], ticks[[2]], "NASDAQ100"},
 Joined -> True,
 ImageSize -> 500,
 Filling -> Bottom]

enter image description here

I have many questions, but only pose two:

(1) How can I efficiently apply a moving average of , let's say, 200 days to the above lines?

(2) How can I sort the PlotLegends? (NASDAQ100 should appear before NASDAQ:GOOGL)

kglr
  • 394,356
  • 18
  • 477
  • 896
eldo
  • 67,911
  • 5
  • 60
  • 168

2 Answers2

3
fdata = FinancialData[#, "CumulativeFractionalChange", {{2009, 1, 1}, {2014, 11, 1}}] & /@
       {"NASDAQ:AAPL", "NASDAQ:GOOGL", "NASDAQ100"};

ma200 = FinancialIndicator["WildersMovingAverage", 200] /@ fdata[[All, All, 2]];
madata = Transpose[{fdata[[1, All, 1]], Join[ConstantArray[Missing[], 199], #]}] & /@ ma200;

DateListPlot[Join[fdata, madata],  Joined -> {False, False, False, True, True, True}, 
 PlotStyle -> ColorData[1, "ColorList"][[{1, 2, 3, 1, 2, 3}]],
 BaseStyle -> Thick, 
 PlotLegends -> LineLegend[Automatic, {"NASDAQ:AAPL", "NASDAQ:GOOGL", "NASDAQ100"},
   LegendLayout -> (Grid[#[[{1, 3, 2}]]] &)]]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
3

Using mathematica v10

ticks = TemporalData@
 FinancialData[#,"CumulativeFractionalChange", {2010}] & /@ {"AAPL", "^NDX","GOOGL"};
maticks = MovingAverage[#, 200] & /@ ticks;
DateListPlot[{ticks, maticks}, 
 PlotLegends -> {"AAPL", "^NDX", "GOOGL", "MA(200)-AAPL", 
   "MA(200)-^NDX", "MA(200)-GOOGL"}, 
 Filling -> {1 -> Bottom, 2 -> Bottom, 3 -> Bottom}, 
 PlotTheme -> "Detailed", 
 PlotStyle -> {{Blue, Red, 
    Black}, {{Dashed, Blue}, {Dashed, Red}, {Dashed, Black}}}]

enter image description here

Zviovich
  • 9,308
  • 1
  • 30
  • 52