MATLAB Examples

# Retrieve Current and Historical Data Using Bloomberg

This example shows how to connect to Bloomberg® and retrieve current and historical Bloomberg® market data. For details about Bloomberg® connection requirements, see docid:datafeed_ug.bq4htf5. To ensure a successful Bloomberg connection, perform the required steps before executing a connection function. For details, see docid:datafeed_ug.bu78as5.

## Connect to Bloomberg®

Create a Bloomberg® Desktop connection.

```c = blp; ```

Alternatively, you can connect to the Bloomberg® Server using blpsrv or Bloomberg® B-PIPE® using bpipe.

## Retrieve Current Data

Format MATLAB® data display for currency.

```format bank ```

Retrieve closing and open prices for Microsoft®.

```sec = 'MSFT US Equity'; fields = {'LAST_PRICE';'OPEN'}; % closing and open prices [d,sec] = getdata(c,sec,fields) ```
```d = struct with fields: LAST_PRICE: 62.32 OPEN: 62.48 sec = cell 'MSFT US Equity' ```

d contains the Bloomberg® closing and open prices. sec contains the Bloomberg® security name for Microsoft®.

## Retrieve Historical Data

Retrieve monthly closing and open price data from January 1, 2012 through December 31, 2012 for Microsoft®.

```fromdate = '1/01/2012'; % beginning of date range for historical data todate = '12/31/2012'; % ending of date range for historical data period = 'monthly'; % retrieve monthly data [d,sec] = history(c,sec,fields,fromdate,todate,period) ```
```d = 734899.00 29.53 26.55 734928.00 31.74 29.79 734959.00 32.26 31.93 734989.00 32.02 32.22 735020.00 29.19 32.05 735050.00 30.59 28.76 735081.00 29.47 30.62 735112.00 30.82 29.59 735142.00 29.76 30.45 735173.00 28.54 29.81 735203.00 26.61 28.84 735234.00 26.71 26.78 sec = cell 'MSFT US Equity' ```

d contains the numeric representation of the date in the first column, closing price in the second column, and open price in the third column. Each row represents data for one month in the date range. sec contains the Bloomberg® security name for Microsoft®.

## Find Maximum Open Price in Date Range

Calculate the maximum open price for the year 2012.

```openprices = d(:,3); % retrieve all open prices in date range max(openprices) % calculate maximum open price ```
```ans = 32.22 ```

## Close Bloomberg® Connection

```close(c) ```