0
$\begingroup$

I have the data of the SPX index weekly

dataweekly=FinancialData["^SPX", "OHLCV", {{2010, 7, 1}, Today, "Week"}]["Path"][[All, 2]];

and I calculate the arithmetic returns:

datapercweekly=100*(dataweekly[[All, 4]] - dataweekly[[All, 1]])/dataweekly[[All, 1]];

Obviously, this corresponds to individual weeks. I am looking for a method to calculate the arithmetic returns for every (rolling) n weeks (eg n=6).

Any help is appreciated!

$\endgroup$
1
  • $\begingroup$ Look at Fold & Table. I'll give this some thought this weekend. You may also want to look in the documentation at FinancialIndicators. Not certain how this set of dressed up pseudo science got into Mathematica. Most of these indicators require extraordinary and completely undisclosed assumptions (e.g. markets revert to a mean and behave like normal distributions) indefensible in the real world. The documentation has no discussion of the underlying/implied assumptions. Ethically, I advise avoiding them, but they exist and can produce pretty graphics. Sorry if this seems like a rant, $\endgroup$ – Jagra Apr 10 at 0:46
2
$\begingroup$
(* Get the weekly closing prices *)
dataweekly = FinancialData["^SPX", "Close", {{2010, 7, 1}, Today, "Week"}];

(* 6 week rolling weekly return *)
rolling = MovingMap[100*(Last[#]/First[#] - 1) &, dataweekly, {6, "Week"}];
DateListPlot[rolling]
$\endgroup$
2
$\begingroup$

Not certain what your really want to do, but maybe the following will spark some ideas.

Manipulate[
 Module[{dw, dpw},
  dw = dataweekly[[All, 4]];
  dpw = datapercweekly;
  Grid[{
    {ListLinePlot[dw, ImageSize -> 350],
     ListLinePlot[dpw, ImageSize -> 350]},
    {ListLinePlot[MovingAverage[dw, n], ImageSize -> 350],
     ListLinePlot[MovingAverage[dpw, n], ImageSize -> 350]}},
   Spacings -> 3]],
 {n, 1, 6, 1}]

enter image description here

$\endgroup$

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.