I am trying to terminate my old data sourcing process (that used Excel BDH formulas on one sheet), and instead trying to code a very simple C# console application to Bloomberg C# Desktop API.
All i need is to replicate this BDH formula - that basically returns the BAR data for a given time interval:
= BDH(security,fields,startdatetime,enddatetime,"BarTp=T","BarSz=1")
I did my best to replicate the example presented here at bloomberg documentation, but could not handle the error I mentioned below.
I checked openbloomberg.com and of course stackoverflow questions for a relevant example, but, simply put, the closest example I could find returned errors when I replicated the code.
The following code is the overall structure I completed so far:
using System;
using System.Collections.Generic;
using System.Text;
using CorrelationID = Bloomberglp.Blpapi.CorrelationID;
using Element = Bloomberglp.Blpapi.Element;
using Event = Bloomberglp.Blpapi.Event;
using Message = Bloomberglp.Blpapi.Message;
using Request = Bloomberglp.Blpapi.Request;
using Service = Bloomberglp.Blpapi.Service;
using Session = Bloomberglp.Blpapi.Session;
using SessionOptions = Bloomberglp.Blpapi.SessionOptions;
using BDateTime = Bloomberglp.Blpapi.Datetime;
namespace RequestResponseMultiple
{
class RequestResponseMultiple
{
public static Request request;
public static Session session;
static void Main(string[] args)
{
open_one_bb_session();
// Add securities
request.Set("security", "GARAN TI Equity");
request.Set("eventType", "TRADE");
request.Set("interval", 1); // bar interval in minutes
request.Set("startDateTime", new BDateTime(2014, 03, 21, 13, 30, 0, 0));
request.Set("endDateTime", new BDateTime(2014, 03, 21, 15, 30, 0, 0));
// send request and handle response!
while (true)
{
//... SEND THE REQUEST...
session.SendRequest(request, new CorrelationID(1));
//... AND, HANDLE THE RESPONSE!
bool continueToLoop = true;
while (continueToLoop)
{
Event eventObj = session.NextEvent();
switch (eventObj.Type)
{
case Event.EventType.RESPONSE:
continueToLoop = false;
handleResponseEvent(eventObj);
break;
case Event.EventType.PARTIAL_RESPONSE:
handleResponseEvent(eventObj);
break;
default:
handleOtherEvent(eventObj);
break;
}
} // each eventobj
Console.ReadKey();
} // time
} // void
private static void open_one_bb_session()
{
// open session
SessionOptions sessionOptions = new SessionOptions();
sessionOptions.ServerHost = "localhost";
sessionOptions.ServerPort = 8194;
session = new Session(sessionOptions);
if (!session.Start())
{
System.Console.WriteLine("Could not start session.");
System.Environment.Exit(1);
}
// open server from the session
if (!session.OpenService("//blp/refdata"))
{
System.Console.WriteLine("Could not open service " + "//blp/refdata");
System.Environment.Exit(1);
}
// get server from the session
Service refDataSvc = session.GetService("//blp/refdata");
// Create request from this server
// request = refDataSvc.CreateRequest("ReferenceDataRequest");
request = refDataSvc.CreateRequest("IntradayBarRequest");
// create intraday bar request
// request = refDataSvc.CreateRequest("IntradayBarRequest");
}
private static void handleResponseEvent(Event eventObj)
{
foreach (Message message in eventObj.GetMessages())
{
Element ReferenceDataResponse = message.AsElement;
if (ReferenceDataResponse.HasElement("responseError"))
{System.Environment.Exit(1);}
Element securityDataArray =ReferenceDataResponse.GetElement("security");
int numItems = securityDataArray.NumValues;
for (int i = 0; i < numItems; ++i)
{
Element securityData =securityDataArray.GetValueAsElement(i);
String security =securityData.GetElementAsString("security");
int sequenceNumber =securityData.GetElementAsInt32("sequenceNumber");
if (securityData.HasElement("securityError"))
{
Element securityError =securityData.GetElement("securityError");
System.Console.WriteLine("* security =" + security);
securityError =securityData.GetElement("securityError");
securityError.Print(System.Console.Out);
return;
}
else
{
Element Bars = message.GetElement("barData").GetElement("barTickData");
int nBars = Bars.NumValues;
for (int nB = 0; nB < nBars; ++nB)
{
Element Bar = Bars.GetValueAsElement(nB);
BDateTime time = Bar.GetElementAsDate("time");
double open = Bar.GetElementAsFloat64("open");
double high = Bar.GetElementAsFloat64("high");
double low = Bar.GetElementAsFloat64("low");
double close = Bar.GetElementAsFloat64("close");
int numEvents = Bar.GetElementAsInt32("numEvents");
long volume = Bar.GetElementAsInt64("volume");
// Individually output each value
System.Console.WriteLine(
"security =" +security
+ " sequenceNumber =" +sequenceNumber
+ ", " + time
+ ", " + open
+ ", " + high
+ ", " + low
+ ", " + close
+ ", " + volume);
} // each Bar
}
} // num msg items
} // msg
} // void
private static void handleOtherEvent(Event eventObj)
{
System.Console.WriteLine("EventType=" + eventObj.Type);
foreach (Message message in eventObj.GetMessages())
{
System.Console.WriteLine("correlationID=" +message.CorrelationID);
System.Console.WriteLine("messageType=" +message.MessageType);
message.Print(System.Console.Out);
if (Event.EventType.SESSION_STATUS == eventObj.Type && message.MessageType.Equals("SessionTerminated"))
{
System.Console.WriteLine("Terminating: " + message.MessageType);
System.Environment.Exit(1);
}
} // each msg
} // void
} // class
}// n
My question is as follows:
Q1- How to set startdatetime and enddatetime for this kind of tick data requests? the code i provided below returns error:
"An unhandled exception of type 'Bloomberglp.Blpapi.NotFoundException' occurred in Bloomberglp.Blpapi.dll. Additional information: Bloomberglp.Blpapi.NotFoundException: security not found in: IntradayBarResponse;
My hunch is that, the start and end time could not be set properly (and I could not come up with the right way to define start and end days, unfortunately). That is why no result is returned from the server.
Additional sources:
One link that might be related to this question: Hourly Data using Bloomberg .Net API
And another link directly from the bloomberg example is here!:
Thanks in advance for your time, help, support,
Aykut Saribiyik
After a decent search at the bloomberg terminal:
open WAPI
pick DESKTOP API
download the zip file, extract it, and goto examples
pick IntraDayExample.cs
This example is exactly what I required.
Thanks