Category Archives: financial markets

New Bloomberg-to-Mathematica interface for Mathematica 10

I have added a new interface notebook for those using Mathematica 10. It fixes some bugs, increases flexibility, and most importantly, takes input and produces output using Mathematica’s new date and temporal data types, and using an Association rather than a list. It’s cleaner and more robust than the old method (though the old notebook will continue to work for those afraid of change.) If you upgrade from the old interface to the new one, note that all the functions have changed names, and their default installation path has changed as well. It’s all documented in the new notebook in the zip file at

http://www.wheels.org/monkeywrench/wp-content/uploads/2013/02/bloomberg_to_mathematica.zip

Sample usage of the new version:

aaplNF2 = msBBGetNF2["AAPL Equity", "Px_Last", DateObject["20070901"], DateObject["20081020"], "day"]; ibmNF2 = msBBGetNF2["IBM US Equity", "Px_Last", DateObject["20070901"], DateObject["20081020"], "day"];
DateListPlot[{aaplNF2[["data"]], ibmNF2[["data"]]}, Frame -> {True, True, False, False},
PlotLabel -> "IBM and Apple in 2007 and 2008", Epilog -> {(Text["apple", {{2008, 8, 1}, 18}]), (Text["ibm", {{2008, 8, 1}, 110}])}]

ex50

[ March 4, 2016 Update coming. See http://www.wheels.org/monkeywrench/?p=834 ]
[ March 7, 2016: the new source code and executables are available on Github, at https://github.com/MichaelSternNYC The Win32 binaries and instructions for use can be found at https://github.com/MichaelSternNYC/bloomberg-to-mathematica]

Bloomberg to Mathematica

Several years ago, my team and I created a tool for importing data via the Bloomberg API directly into Mathematica. It works perfectly and we made both the code and the executable interface available via the Wolfram library. Apparently, Wolfram has removed it, perhaps because it competes with identical functionality in the Wolfram Finance Platform. But the Bloomberg to Mathematica link is free, and a Wolfram Finance Platform license costs $10,000, so people have asked me to put this online somewhere.

So here it is.
[2016 update — I’ve removed the old link as the executables and source code there were outdated. Get the current version at GitHub]

You need to have a Bloomberg Professional license, and as the Bloomberg data API works only on Windows, you need to be running Bloomberg and Mathematica on Windows (I’ve tested this in Windows XP through 8, and with versions of Mathematica through 9.0. Let me know if you run into problems with other setups).

The source code is included here; feel free to make modifications if you like. If you do anything interesting please share it back, it would be nice to see Mathematica used more widely in finance, and free sharing of tools like this would have to help.

Update, 2015:
I have added a new interface notebook for those using Mathematica 10. It fixes some bugs, increases flexibility, and most importantly, takes input and produces output using Mathematica’s new date and temporal data types, and using an Association rather than a list. It’s cleaner and more robust than the old method (though the old notebook will continue to work for those afraid of change.) If you upgrade from the old interface to the new one, note that all the functions have changed names, and their default installation path has as well. It’s all documented in the new notebook in the zip file above.

(Sample use of the old code shown below)

cpi2 = bbg2Mdates[
BBGetNF1["JNCPIYOY Index", "Px_Last", "19820101", "", "monthly"]];

tpxNorm2 =
SternChangeCharNF1[
bbg2Mdates[
SternNormalizeNF1[
BBGetNF1["TPX Index", "Px_Last", "19820101", "", "monthly"]]],
name -> "TPX"];

Show[DateListPlot[cpi2[[2]], Frame -> {True, True, False, False},
Joined -> True, Filling -> 0, FillingStyle -> {Blue, White},
GridLines -> False, PlotRange -> All, PlotStyle -> Purple],
DateListPlot[tpxNorm2[[2]], Frame -> {True, True, False, False},
Joined -> True, GridLines -> False, PlotStyle -> Pink],
PlotLabel -> "日本 inflation has been low since \!\(\*
StyleBox[\"before\",\nFontSlant->\"Italic\"]\) the market crashed",
Axes -> {True, False}, PlotRange -> All,
Epilog -> {(Text[
Style["stock market", FontFamily -> "Times New Roman",
FontSize -> 10, Pink], {AbsoluteTime["6/1/1986"],
4.4(*vertical position*)}, {-.01, 0}(*
how close to the end *)]), (Text[
Style["CPI", FontFamily -> "Times New Roman", FontSize -> 10,
Purple], {AbsoluteTime["1/1/1983"],
3.6(*vertical position*)}, {-.01, 0}(* how close to the end *)])}
]

japan inflation

A demonstration of the new code can be found at http://www.wheels.org/monkeywrench/?p=817

[ Update March 7, 2016: New source code and executables are available on Github, at https://github.com/MichaelSternNYC The Win32 binaries and instructions for use can be found at https://github.com/MichaelSternNYC/bloomberg-to-mathematica

If you want to load Bloomberg data into a version of Mathematica older than 10.0, contact me and I can send you an older release. ]

Futures conversion factors in Mathematica

Sovereign debt futures in the U.S. and elsewhere are designed to accept multiple different bonds for delivery. The exchange specifies a delivery factor algorithm to normalize the prices of these bonds (or notes) to make them roughly of equal value upon delivery. Since they’re not precisely of equal value, traders can make money trading around the cheapest to deliver bond.

The conversion factor in the U.S. is designed to convert the yield on any bond or note to 6%. Considering the semi-annual or monthly coupon payments on bonds and notes, the formula to do this looks as follows.

Options[conversionfactor] = {type -> "bond"};
(* versus "10yr note", "5yr note", or "2yr note" *)

conversionfactor[coupon_Real, wholeYearsToMaturity_Integer,
stubMonthsToMatury_Integer, OptionsPattern[]] :=
Module[{v, a, b, c, d},
v = If[stubMonthsToMatury < 7, stubMonthsToMatury, If[OptionValue[type] == "bond" || OptionValue[type] == "10yr note", 3, stubMonthsToMatury - 6]](* for 10 year, other options possible for other instruments *); a = 1/1.03^(v/6); b = coupon/2*(6 - v)/6; c = If[stubMonthsToMatury < 7, 1/1.03^(2*wholeYearsToMaturity), 1/ 1.03^(2*wholeYearsToMaturity + 1)]; d = coupon/.06*(1 - c); a*(coupon/2 + c + d) - b]

wholeYearsToMaturity represents the number of whole years from the first day of the delivery month to the maturity (or call) date of the bond or note.

stubMonthsToMatury represents the number of whole months between wholeYearsToMaturity and the maturity (or call) date rounded down to the nearest quarter for Treasury Bonds and 10 Year Note futures, or to the nearest month for 5-year and 2-year note futures.

For a 2 year note with 1 year, 10 months remaining and a coupon of .015,

In[]:= conversionfactor[.015, 1, 10, type -> "2yr note"]
Out[]= 0.922939

For a 5 year note with 4 years, 10 months remaining and a coupon of .0275

In[]:= conversionfactor[.0275, 4, 10, type -> "5yr note"]
Out[]= 0.86533