I'm trying to build a financial indicator in mathematica. I import some historical data
Data = FinancialData["^GSPC", "OHLCV", {2009}];
Data[[1;;5]]
{{{2009, 1, 2}, {902.99, 934.73, 899.35, 931.8, 4048270000}},
{{2009, 1, 5}, {929.17, 936.63, 919.53, 927.45, 5413910000}},
{{2009, 1, 6}, {931.17, 943.85, 927.28, 934.7, 5392620000}},
{{2009, 1, 7}, {927.45, 927.45, 902.37, 906.65, 4704940000}},
{{2009, 1, 8}, {905.73, 910., 896.81, 909.73, 4991550000}}}
Now I want to apply a function to a moving window of the data:
MovingMap[foo[#] &, Data, 5]
However, MovingMap does something unexpected it maps over the second entry in each list:
{{3440361600,
foo[{{902.99, 934.73, 899.35, 931.8, 4048270000},
{929.17, 936.63, 919.53, 927.45, 5413910000},
{931.17, 943.85, 927.28, 934.7, 5392620000},
{927.45, 927.45, 902.37, 906.65, 4704940000},
{905.73, 910., 896.81, 909.73, 4991550000}}]},
{3440448000,
foo[{{929.17, 936.63, 919.53, 927.45, 5413910000},
{931.17, 943.85, 927.28, 934.7, 5392620000},
{927.45, 927.45, 902.37, 906.65, 4704940000},
{905.73, 910., 896.81, 909.73, 4991550000},
{909.91, 911.93, 888.31, 890.35, 4716500000}}]}, ...}
I was expecting something like a moving window through the list:
foo[{{{date}, {O, H, L, C, V}},
{{date}, {O, H, L, C, V}},
{{date}, {O, H, L, C, V}},
{{date}, {O, H, L, C, V}}, ...}]
What have I misunderstood or done wrong here?
Just to be thorough
MovingMap[foo &, dowData[[1 ;; 200]], {20, "Day"}]
Produces this output:
{{3441484800, foo},
{3441571200, foo},
{3441657600, foo},...}
EDIT: It seems when given a time series like that which FinancialData returns MovingMap will Map over the data for you. Removing the need to deal with the dates yourself. Poorly documented, but convenient if you know about this behaviour.
However, my indicator is calling FinancialIndicator itself, which seems to need the full date/OHLCV time series. Is there any way to have this work as one might expect?
Example of what I'm trying to do:
intraTotal[ts_] := Total[FinancialIndicator["TrueRange" ][ts]];
periodRange[ts_] := Max[Map[ #[[2, 2]] &, ts]] - Min[Map[ #[[2, 3]] &, ts]];
FracDim[ts_, period_] := intraTotal[Take[ts, -period]]/periodRange[Take[ts, -period]];
MovingMap[foo, Data, {5, "Day"}]
$\endgroup$FracDim
to work similar to theFinancialIndicator
s. Something likefracDim[ts_, period_] := Table[ intraTotal[ts[[s ;; s + period]]]/ periodRange[ts[[s ;; s + period]]], {s, Length@ts - period}];
. $\endgroup$