## Using Mathematica to create a daily list of Trade Weight Index data

October 11, 2009 by wildebeests

Given the weakness (freefall?) of the US Dollar I decided to make another series of LME data, this one with Trade Weight Index corrected pricing. The Fed Trade Weight Index (TWI) is published weekly whereas the LME trades daily and is closed on certain holidays. So I wanted to create a list of the TWI for each day of LME trading this decade. There may be easier ways (?) but this is the approach I took:

1. Convert all dates to absolute time.

2. The first LME trading day this decade was on 4th January 2000. Remove TWI data prior to that:

**time = AbsoluteTime[{2000, 1, 4, 0, 0, 0.}];**

TWIData = DeleteCases[TWIData, x_ /; AbsoluteTime[First@x] < time];

3. Collect the LME trading days for each weekly TWI datum. I figured the easiest way to do this was to use **BinLists**. Each week has 604800 seconds so this will be the bin width.

**bins = BinLists[LMEdates, {start, finish, steps}];**

4. Now you’ve got the LME trading days partitioned into lists with each list corresponding to a TWI datum. You want to thread the TWI data through each of the partitioned LME dates and then **Flatten** at the first level.

**result = Flatten[MapThread[Thread[{#1, #2}] &, {bins, TWIData[[All, 2]]}], 1];**

Finally, if you want, you can convert the dates back to a standard format from absolute time. Here is the function that does all those steps:

**FullTWIdata[twi_, lme : {{_, _, _, _, _, _} ..}] := Module[{tmp1, tmp2, tmp3, start, finish, bins},**

tmp1 = AbsoluteTime /@ twi[[All, 1]];

tmp2 = AbsoluteTime /@ lme;

start = First[tmp1] – 604800;

finish = Last[tmp1];

bins = BinLists[tmp2, {start, finish, 604800}];

tmp3 = Flatten[MapThread[Thread[{#1, #2}] &, {bins, twi[[All, 2]]}], 1];

tmp3[[All, 1]] = DateList /@ tmp3[[All, 1]];

tmp3

]

### Like this:

Like Loading...

*Related*

## Leave a Reply