Feeds:
Posts

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

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}];

## Mathematica work-flows. Retrieving, processing and presenting data. #1 Retrieving

### Introduction

The WWW is rich with sources of useful data, some of which are available directly, others require registration and subsequent login. I want to discuss how independent investors, without access to Bloomberg, Reuters and other expensive data sources, can streamline their work-flow by automating data access and processing. You’re busy—you want to analyse your data, not spend all your time collecting it and processing it.

I’m going to start by writing about retrieving the data once you have established regular and reliable sources, then discuss processing and presentation. Those stages will all be done by Mathematica, sometimes with a little help from wget during the retrieval stage. The final stage is automation. I run Mac OS X so that stage will describe how to tie everything together in one automated flow. The objective is to set all this happening to a timer and wake up each morning (assuming the data of interest is daily) and have a chart, or several charts, with your data, presented in a design you prefer, ready for you in your email inbox.

### Data format

The formats you are likely to want to be acquiring are HTML pages, Excel or CSV files, or zipped files—typically zipped Excel or CSV files. One way to process data available from the web in Excel, or other formats, is to download the data and then import it into Mathematica. You’ll want to be using Mathematica’s Import function. Some additional documentation can be found here.

## Creating “highlighting bands” in plots

I often see plots with vertical bands highlighting segments of the plot. Here’s how to make them in Mathematica.

Starting with a data set of prices we want to highlight segments of the price plot where the correlation with stockpiles is strongly positive. The plot of the price data is made using DateListPlot with some user defined plotting options:

DateListPlot[ prices, options]

Let’s assume that I already have a list of the subsets of this data that we want to highlight. I’ll call these subsets subset1,subset2,… . Next step is to add these subset lists to the list of data:

DateListPlot[ {prices,subset1,subset2,…}, options]

You won’t be able to see the subsets when you plot this unless you specifically set a different styling option for the second and subsequent lists of data that you’re plotting:

DateListPlot[ {prices,subset1,subset2,…}, PlotStyle→{colour1,colour2,colour3,…}]

But this is about creating bands and the easy way to do that is to use the Filling option. If we fill from a subset segment down to the axis plus fill to some point above the plot we can create a continuous highlighting band.

DateListPlot[ {prices,subset1,subset2,…}, Filling{{2Bottom}, {2→Top}, {3Bottom}, {3→Top},…}]

What this syntax is telling us is that we want to fill list number 2 to zero and list number 2 to 5000, the same for the 3rd, 4th and subsequent lists (the first list is our list of prices)

An example of a plot with highlight bands

## Making a Wildebeest Correlation plot

What I’ve set out to do with the Wildebeest Correlation Index is to create a rolling correlation of prices and stockpiles. The correlation data then gets plotted with the line changing colour depending on the correlation: green for negatively correlated data and red for positively correlated data. The input is a list with the form {date, price, stockpile} …

From the initial data we create a temporary list of prices and stockpiles:

temp2 = data[[All,{2, 3}]]

This list is then partitioned into subsets of length chosen by the number, num, of points required for the correlation.The partitioned list is mapped on to the Correlation function:

temp = Correlation[#[ [All,1]], #[[All,2]] ]& /@ Partition[temp2, num, 1]

Next step is to add back the dates and make the plot. To make the line change colour we want to define a ColorFunction which sets the colour to be between green and red depending on the y value of the data:

ColorFunction→Function[{x,y}, Blend[{Green,Red},y]]

The Mathematica shortform notation for this function is written as

ColorFunction→(Blend[{Green,Red},#2]&)

An example of a WCI plot