-1
$\begingroup$
f[today_, yesterday_] := 
\[Piecewise]{
   {+today, (Lo < today < VarBN) && (yesterday < today)},
   {+today, (VarBP < today < VarMN) && (yesterday < today)},
   {+today, (VarMP < today < VarAN) && (yesterday < today)},
   {+today, (VarAP < today < Hi) && (yesterday < today)},
   {+today, VarBN <= today <= VarBP},
   {+today, VarMN <= today <= VarMP},
   {+today, VarAN <= today <= VarAP},
   {-today, (Lo < today < VarBN) && (yesterday > today)},
   {-today, (VarBP < today < VarMN) && (yesterday > today)},
   {-today, (VarMP < today < VarAN) && (yesterday > today)},
   {-today, (VarAP < today < Hi) && (yesterday > today)}}

For[n = 2; H[[n]], n <= 252, n++, f[n, n - 1]; Print[f[n]]]

What I am trying to do is take a Table of 252 elements (H) and have this loop take each element in the list (each element labeled as "today" and previous element is "yesterday") and perform the above piecewise function and then print out that piecewise value for each of the 252 elements. My question is whether or not its a syntax problem or an actual programming issue?

$\endgroup$
  • $\begingroup$ Please give the form of table H (maybe the first 10 lines of H). $\endgroup$ – demm Apr 16 '16 at 19:06
  • $\begingroup$ Try For[n=2, n<=252, n++, Print[f[n, n-1]]] Then try For[n=2, n<=252, n++, Print[f[H[[n]], H[[n]]-1]]] $\endgroup$ – Bill Apr 16 '16 at 19:13
  • $\begingroup$ F = FinancialData["HD", {"Jan. 1, 2015", "Jan. 1, 2016"}] H = Table[F[[n, 2]], {n, 1, 252}] $\endgroup$ – L. J. Apr 16 '16 at 19:45
1
$\begingroup$

You can use MovingMap.

First it is faster to get h with Part rather than with Table. Also it is a bad idea to create variables that begin with a capital letter as you could conflict with built-in symbols.

dat = FinancialData["HD", {"Jan. 1, 2015", "Jan. 1, 2016"}];
h = dat[[All, 2]];

You have not provided the definitions for the variables in the Piecewise but the following will create a moving window of 2 items and pass them to your f. You don't need to use Print. Put the following in its own cell in the notebook.

MovingMap[f[Sequence @@ #] &, h, 1]

Note there is no ending semicolon (;) so the results will display in the notebook.

Hope this helps.

$\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.