Home > Back-end >  Parse Bloomberg API response in C#
Parse Bloomberg API response in C#

Time:05-03

Good afternoon. When using the bloomberg api (getting historical data), I get the response as a string in this format:

START-OF-FILE
RUNDATE=20220501
# Required headers
FIRMNAME=dl12345
PROGRAMNAME=gethistory
REPLYFILENAME=r20220501030826f95770.bbg

# Scheduling headers
PROGRAMFLAG=daily
TIME=1610

# Runtime options headers
DATERANGE=3
HIST_PERIOD=daily

# Formatting headers
HIST_FORMAT=horizontal
DATEFORMAT=ddmmyyyy
FILETYPE=unix
DISPLAY_PRICING_SRC=no

# Pricing Source headers

# Terminal Identity headers

# Entitlement headers
REGFVHL=YES
REGPRICEUNCERTAINTY=YES
REGTRANSPARENCY=YES

START-OF-FIELDS
PX_BID
PX_ASK
PX_LAST
END-OF-FIELDS

TIMESTARTED=Sun May  1 16:23:55 BST 2022
START-OF-DATA
USDKZT Curncy|0|3|28/04/2022|445.4|446.36|445.89|
USDKZT Curncy|0|3|29/04/2022|441.33|443.63|442.48|
RUBKZT Curncy|0|3|28/04/2022|5.98|6.3062|6.1431|
RUBKZT Curncy|0|3|29/04/2022|5.9895|6.3651|6.1773|
EURKZT Curncy|0|3|28/04/2022|467.8|470.18|468.99|
EURKZT Curncy|0|3|29/04/2022|467.23|469.55|468.39|
CHFKZT Curncy|0|3|28/04/2022|459.5881|460.8167|460.2024|
CHFKZT Curncy|0|3|29/04/2022|453.1108|455.9873|454.5491|
GBPKZT Curncy|0|3|28/04/2022|556.42|558.18|557.3|
GBPKZT Curncy|0|3|29/04/2022|554.85|558.72|556.79|
CNYKZT Curncy|0|3|28/04/2022|67.4989|67.687|67.5929|
CNYKZT Curncy|0|3|29/04/2022|66.908|67.2361|67.0721|
END-OF-DATA
TIMEFINISHED=Sun May  1 16:23:56 BST 2022
#MACRO
END-OF-FILE

The required data is between START-OF-DATA and END-OF-DATA (these are currency quotes for a specific date), I need to convert this data to json or xml. Is there any way to normalize the data in this form, I'm sorry, but I had no experience with this kind of documents. I would appreciate any help, thanks

CodePudding user response:

It's an old school ini with a some main frame data between your START & END points. Simply get a Substring between s.IndexOf("START-OF-DATA") and s.IndexOf("END-OF-DATA") and then Split string[] arr = mainframeData.Split(Environment.Newline); and then in a loop of the arr Split again by pipe item.Split("|"); and add those values to a class, added to a list of classes which you then Serialize to JSON.

Psuedo code:

string fileContents = new File().ReadAllText("bloomberg.dat");
int st = fileContents.IndexOf("START-OF-DATA");
int end = fileContents.IndexOf("END-OF-DATA")
string data = fileContents.SubString(st   13, end - 11);

string[] arr = data.Split(Environment.Newline);
List<ClassWithPropertiesForData> currencyList = new ();
foreach(var item in arr)
{
   currencyList.Add(new ClassWithPropertiesForData(item.Split("|")));
}

return Json.Serialize(currencyList);






public class ClassWithPropertiesForData {
  private DateTime day...

// Ctor
public ClassWithPropertiesForData(paramarray...

}
  • Related