Importing Stock Data

A Finance Example Using XML

This example uses XMLToday to obtain stock quotes in an XML format using a URL query system.  Mathematica 4.2 provides functions that request XML data from a specified URL.  Once obtained, the data can be analyzed and presented using Mathematica.

First, we need to make use of some graphics functions provided in the Graphics`Graphics` standard package.  Tho following command load the necessary package.

<< Graphics`Graphics`

Next, we perform the URL query to obtain the XML data.  The requested stocks can be modified by changing the values that follow the "?s=" parameter.

xmlquotes = 
        XML`Parser`XMLGet["http://www.xmltoday.com/examples/stockquote/\
getxmlquote.vep?s=ba+lmt+ibm+int+amd+mmm"] ;

Now that we have the data, we need to extract the information and present it in a form that Mathematica can graph.

\(\(GetQuotes[xml_] := 
        Cases[xml, 
          XMLElement["stock_quote", _, {___, 
                XMLElement["symbol", _, {symbol_}], ___, 
                XMLElement["price", {"type" -> "ask", "value" -> v_}, {}], ___}] :> {symbol, ToExpression[v]}, 
          Infinity];\)\)

We can see the results of the formatting as follows.

quotes = GetQuotes[xmlquotes]

We are now ready to plot the data.

BarChart[#[[2]] & /@ quotes, 
        BarLabels -> (#[[1]] & /@ quotes), 
        TextStyle -> {FontFamily -> "Times", FontSize -> 15, 
            FontWeight -> "Bold"}, 
        Frame -> {True, True, False, False}, ImageSize -> 450, 
        PlotLabel -> "Stock Quotes from XMLToday"];