Application 3: Stock Analysis Automation
In the financial world, news is a business tool. If you can find a way to compose news stories automatically from wire sources you can save staff time. In addition, building stories targeted to a specific user or client can increase customer satisfaction. XQuery is well suited to both tasks, because it lets you perform targeted full text search even across structured data sources such as stock prices, and can compose the results into a custom format.
Suppose a trader wants to be alerted of specific types of news for a company he's following. He wants to see the effect that positive earnings from a NASDAQ bellwether has on some smaller cap stocks he's tracking. The task involves more than just popping up all AP and Reuter's stories with a stock list; he wants to test for a combination of conditions.
Reliant Corp maintains a news service through which traders can get custom news reports. Reliant uses XML extensively to hold information about exchange-listed companies as well as daily news stories. Traders maintain lists of companies in their portfolio. The trader portal needs to execute a search of daily news for stories that mention traders' portfolio companies in the headlines, and that also mention any of that company's partners within a single paragraph in the story content. You can write an XQuery application that fulfills all these needs.
First, create a simple user-defined function to retrieve all partners of a given company.
define function getPartners($company as xs:string)
let $c := document("company/company.xml")
//company[name = $company]
Next, you need to find news items where a specific trader's portfolio company appears in the headline, and one or more of its partners are mentioned within a single paragraph in the document body. The result should list each news item by its headline and date. The trader is identified by a user id passed to the query shown below as the parameter $user
. The portions of the query below in boldface text apply the required constraints.
for $companyName in
let $foobar_partners :=
for $item in document("News/companyNews.xml")
where some $line in $item//NewsLines/HeadLine
(some $para in
(some $partner in $foobar_partners
The query is specific to each trader because it retrieves only news items that contain company names that also
appear in that trader's user profile, which the query retrieves via the passed-in $user
value. The where
condition restricts the output to news items where the headline contains a company name, and where the data content (the body of the news item) contains the name of one or more partner companies. Here's a sample result:
<HeadLine>KO knocks out Pepsi Co.
<HeadLine>KO releases earning results
for Q1 2003</HeadLine>
<HeadLine>WMT to acquire Ben and Jerry</HeadLine>
The results clearly show that the returned data is specific to one trader and meets the query restrictions.