2
$\begingroup$
sp500const = {"IBM", "GLD", "XOM", "AAPL"};
sp500ts = TimeSeries[FinancialData[#, "Close", DatePlus[-SampleSize]]] & /@ sp500const;
alloc = Table[N[1/Length[sp500const]], {i, Length[sp500const]}];
portfolio = TimeSeriesThread[alloc.# &, sp500ts]

There are 1722 data points of data set "portfolio", how can I split the data evenly. e.g 100 days per list and the final list should contain 22 days. Please feel to comment and advise. Thank you.

$\endgroup$
  • $\begingroup$ Would TimeSeriesWindow work for you? See also the examples in Extract Part of a Time Series. $\endgroup$ – MarcoB Feb 22 '16 at 13:34
  • 1
    $\begingroup$ I tried, but the length of data is changing everyday. I would find a method to split the list dynamically. Moreover, would you advise me how to know the date of 100th item. I tried the method DatePlus[portfolio["FirstDate"], 100], unfortunately, the date is not same as the date in the list. $\endgroup$ – madeinQuant Feb 22 '16 at 13:46
2
$\begingroup$

You should be able to use Partition with an UpTo length specification.

Get some data to play with:

series = TimeSeries@FinancialData["GE", {{2015, 01, 01}, {2015, 12, 31}}];
DateListPlot[series]

Mathematica graphics

Use Normal to extract the time series data as a regular list; split the list using Partition into sub-lists of length up to $100$ elements (notice UpTo); then reconstitute each split list into a TimeSeries object:

TimeSeries /@ Partition[Normal@series, UpTo[100]]

Mathematica graphics

You can check that the data represents the same span as the starting set:

DateListPlot@%

Mathematica graphics

$\endgroup$
  • $\begingroup$ Thank you. I tried to use partition and get the error "TemporalData::argt: TemporalData called with 0 arguments; 1 or 2 arguments are expected.". Anyway, thank you very much. $\endgroup$ – madeinQuant Feb 22 '16 at 14:14
  • 1
    $\begingroup$ @NelsonMok Did you use Normal on your series before / within Partition? Partition won't work on a TimeSeries object directly. Take a look at the example code in my answer. $\endgroup$ – MarcoB Feb 22 '16 at 14:16
  • $\begingroup$ @MacroB No, I didn't. I used a lot of list manipulation but they didn't work for timeseries. Thank you very much. $\endgroup$ – madeinQuant Feb 22 '16 at 14:23

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.