Some Mathematica tidbits

Mike Honeychurch, April 2010.

Tooltips for DateListPlots

The problem: if you right click and select Get Coordinates from a DateListPlot the x values are in AbsoluteTime. This is inconvenient if you want to quickly read some points. Here is a solution.

Get some data:

Make some tooltip data:

Plot the data. When you position the cursor over a point you will get the coordinates in terms of date and value.

DateListPlot[aaplTooltip,Joined->True,PlotMarkers->•]

Make a function to do this:

Get some new data:

Plot the data:

DateListPlot[showDatum[c],Joined->True,PlotMarkers->Style[•,Red]]

Finding files

Try this to locate files you want to import, in this case an Excel file:

SystemDialogInput was new in version 6 but the function name is probably not as intuitive as other functions, i.e. with most of our functions you know what they will do from the name. If you are having problems due to the file structure on different platforms, try and design your notebooks with directory functions:

?*Directory*

For example, if I want to load data from a file on my desktop instead of using this path

Use something like this to construct the path:

By experimenting with the various directory functions you should be able to come up with paths that work across various platforms and installations without having to manually re-configure file paths each time.

Preserving/saving Manipulate values

If you have Manipulates you may want to save settings for later use.

Manipulate[Plot[Sin[a x+b],{x,0,6}],{{a,2,"Multiplier"},1,4},{{b,0,"Phase Parameter"},0,10}]

When you find some settings that you want to save click on the button in the top right hand corner of the Manipulate and from the drop down menu select Add to Bookmarks…

You can then enter a name for the bookmark:

Now the bookmark will appear in the drop down menu. You can select this bookmark to retrieve the settings you saved.

TableView[]

TableView is an experimental undocumented function which was the subject of discussion on this Google groups Mathgroup thread. As an experimental function it is subject to change, so some caution is generally advised about building functions that rely on particular syntax and other features. Notwithstanding this, it may be useful for users (with version 7+) to test.

TableView[]

TableView[RandomReal[{0,5},{10,4}]]

Right clicking on the data will produce a contextual menu that will allow you to sort rows and columns.

Scheduling

How do you schedule a task to run as a certain time in the future? I am a Mac user so I have placed an example of scheduling on a Mac in the library archive:
http://library.wolfram.com/infocenter/MathSource/7590/

When I get some information about scheduling on Linux and Windows I'll update this. I believe that on Linux you would use cron instead of launchd.

Accessing data using a provider API — example: FRED® data

A tutorial on building a function to access FRED® data is available in the library archive: http://library.wolfram.com/infocenter/MathSource/7583/

Accessing data without a provider API — example: (Subscript[Yahoo!, ®]) finance

This is a short example of how to go about building a function to access data in circumstances where an API to the data does not exists.

Daily data

There are several descriptions of how to access Yahoo! finance on the web. In a nutshell you can access data by adding certain query tags to a URL.

The tag list from this website exists as a six element list.

tags=Flatten@Cases[tmp,x_List/;Length[x]==6,{1,5}];
tags=Partition[StringTrim@tags,2];
TableForm@tags

Here is a usage examples where the information we want returned is the company name (tag = n), previous close (p), volume (v), stock exchange (x), ask (a), and bid (b). The tags are concatenated: "npvxab":

Import["http://quote.yahoo.com/d/quotes.csv?s=GE&f=npvxab"]

We can see that the format is s = the stock symbol, and f = the concatenated tags. For multiple stocks just add the symbols with a "+":

tmp2=Import["http://quote.yahoo.com/d/quotes.csv?s=XOM+EK+JNJ+MSFT&f=npvxab"]

Imagine making a function where the user enters the names of companies in a list. They may or may not enter these symbols as strings but the symbols need to be converted to strings.

ToString/@{AAPL,C,MSFT}

To enter multiple stock symbols into a Yahoo! query string you need to add a "+" between them. Use Riffle to do this.

StringJoin@@Riffle[ToString/@{XOM,EK,JNJ,MSFT},"+"]//FullForm

That code can be used to create a function:

yahooCurrentData1[{AAPL,C,MSFT},"npvxab"]

tmp2=yahooCurrentData1[{"AAPL","C","MSFT"},"npvxab"]

TableView[tmp2]

Here is an alternative implementation in which we add some headers by first assigning an integer value to each tag:

tags2=MapIndexed[Flatten@{#2,#1}&,tags];
TableForm@tags2

This time enter the tags as a list of integers:

tmp2=yahooCurrentData2[{"AAPL","C","MSFT","GS"},{56,59,78,84,1,4}]

Text@TableForm@tmp2

Historical data

Historical data is available for a single stock, daily, weekly, or monthly. For some reason you need to subtract 1 from the month, i.e. March, the third month is entered as "2". To begin, test a function to make the query strings for the start and end dates for the data.

end=StringJoin@@({"&d=",ToString@#[[3]],"&e=",ToString[#[[2]]-1],"&f=",ToString@#[[1]]}&/@{{2010,3,24}})
start=StringJoin@@({"&a=",ToString@#[[3]],"&b=",ToString[#[[2]]-1],"&c=",ToString@#[[1]]}&/@{{1984,7,8}})

a, b, a are the day, month and year for the start of the series, and d, e, and f, are the day, month and year for the end date in the series. The letter "g" is the assignment of the data interval, d for day, w for week, and m for month. Dividends are also available be setting "g=v".

tmp5=yahooHistoricalData1["AAPL",{2001,10,4},DateList[],"w"]

Not bad, but we should make the dates a Mathematica date list. Do do this you want to apply a function to the first element in each list. Map is one way of doing that. Another way is to use Apply at levespec 1. Which is more efficient? I don't know. We can test them both.

Timing[Do[{DateList[{#,{"Year","-","Month","-","Day"}}], ##2}&@@@Rest[tmp5],{50}]]

Timing[Do[{DateList[{First@#,{"Year","-","Month","-","Day"}}], Sequence@@Rest[#]}&/@Rest[tmp5],{50}]]

After numerous tests with different data sizes there is barely a difference. If you're running this you'll notice both alternatives are very slow. Since we know the exact format of the date string there is an alternative, faster way of making a date list.

ToExpression/@StringSplit["2009-05-18","-"]

Timing[Do[{ToExpression/@StringSplit[#1,"-"], ##2}&@@@Rest[tmp5],{50}]]

Timing[Do[{ToExpression/@StringSplit[First@#,"-"], Sequence@@Rest[#]}&/@Rest[tmp5],{50}]]

Hmmm, quite a difference in timing!

tmp6=yahooHistoricalData2["AAPL",{2001,10,4},DateList[],"w"]

Plot the opening prices:

(* using Rest so as to remove the header *)
DateListPlot[Rest@tmp6[[All,{1,2}]]]

Plot the volume:

DateListPlot[Rest@tmp6[[All,{1,6}]]]

Technicals — e.g. candlestick plots

It was announced at the User conference last October that candlestick plots would be included in the next release. In the meantime this package is a bit crude but may suffice …or at least provide the basics of something that can be extended:

http://library.wolfram.com/infocenter/MathSource/7526/

Additional sources of information

Forums:
http://groups.google.com/group/comp.soft-sys.math.mathematica
http://stackoverflow.com/questions/tagged?tagnames=mathematica

There are also some blogs that have interesting Mathematica content from time to time (this list is not exhaustive):
http://www.walkingrandomly.com/?cat=8
http://mathgis.blogspot.com/
http://www.wheels.org/cgi-bin/blosxom.cgi/
http://www.uoregon.edu/~noeckel/MathematicaGraphics.html
http://www.wildebeests.net/category/mathematica/
http://web.mac.com/hrbishop.pmsi/DCSIG.m/Gems.html
http://www.verbeia.com/mathematica/mathecon/mathecon_index.html

And there is also the Wolfram Library Archive and the Wolfram Demonstrations project:
http://library.wolfram.com/
http://demonstrations.wolfram.com/