Using Java to get Bloomberg fields over specific dates - java

I'm trying to pull in specific data fields from the Bloomberg Java API. I see from the developers guide that I can pull in some fields with:
Request request = refDataSvc.createRequest("ReferenceDataRequest");
request.getElement("securities").appendValue("AAPL US Equity");
request.getElement("securities").appendValue("IBM US Equity");
request.getElement("fields").appendValue("PX_LAST"); // Last Price
request.getElement("fields").appendValue("DS002"); // Description
request.getElement("fields").appendValue("VWAP_VOLUME");
session.sendRequest(request, new CorrelationID(1));
How can I make a call like this while getting some of the fields over a specific date range? For example, I would like to get the: last trade price, last trade volume, the opening price for August 27th, 2012, and the VWAP volume for August 26th between 9AM and 11AM.

You need to create a "HistoricalDataRequest" request:
Request request = refDataSvc.createRequest("HistoricalDataRequest");
You can then specify the start date and end date:
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyyMMdd");
request.set("startDate", startDate.toString(fmt));
request.set("endDate", endDate.toString(fmt));
In your case, startdate and enddate will be 27-Aug for the first request, and 26-Aug for the second.
However, I'm not sure how you can override fields (VWAP_START_TIME and VWAP_END_TIME) to restrict your VWAP to 9-11AM in a historical request, for reference data you would do smoething like the code below - it might work for historical requests too:
Element overridesElt = request.getElement("overrides");
Element override = overridesElt.appendElement();
override.setElement("fieldId", "VWAP_START_TIME");
override.setElement("value", "09:00:00");
override = overridesElt.appendElement();
override.setElement("fieldId", "VWAP_END_TIME");
override.setElement("value", "11:00:00");

Related

How do I get a timestamp not from the system, but from data from the Internet in Java/Kotlin? [duplicate]

This question already has answers here:
How to use an Internet time server to get the time?
(5 answers)
Closed 1 year ago.
I have variable curTime, I want to get the current time of the form 1616572689, there are 2 familiar methods
val curTime: Long = Date().getTime()
or
val curTime = System.currentTimeMillis()
But here's the problem, if I change the time on the phone for example to 2007, then the value of the variable will be incorrect! It will show the year 2007. How do I make sure that the data is taken not from the system, but from the Internet? some site, for example https://timestamp.online/
Please help, couldn't find anything about this problem.
You find an online server with a public API for retrieving the current time, e.g. World Time API. That's just an example, there are other servers, and you should do your own research to find which server best fits your needs and licensing restrictions.
You then perform an HTTP GET request and parse the returned JSON to get the value you seek.
E.g. if you want the current time for US Eastern time zone, you do and HTTP GET from http://worldtimeapi.org/api/timezone/America/New_York, which will respond with something like this:
{
"abbreviation": "EDT",
"client_ip": "73.106.239.191",
"datetime": "2021-03-28T06:37:10.320418-04:00",
"day_of_week": 0,
"day_of_year": 87,
"dst": true,
"dst_from": "2021-03-14T07:00:00+00:00",
"dst_offset": 3600,
"dst_until": "2021-11-07T06:00:00+00:00",
"raw_offset": -18000,
"timezone": "America/New_York",
"unixtime": 1616927830,
"utc_datetime": "2021-03-28T10:37:10.320418+00:00",
"utc_offset": "-04:00",
"week_number": 12
}
The value you're looking for is the unixtime field.

versionone: get defects with project and change date filter from version one java sdk

I am looking to retrieve list of all defects that belongs to project A and that have modified in last 24 hours. I used the given below code to retrieve list of all defects that belongs to project(project is a string variable containing name of the project)
scope=defectType.getAttributeDefinition("Scope.Name");
FilterTerm toDoTerm = Query.term(scope);
toDoTerm.Equal(project);
Now i want to add one more condition, list only those defects that have been changed in last 24 hours.
I checked the versionone api but could not find any relevant java source code/example or function name.
How can i add conditions like "and" or "or" ?
You can use the GroupFilterTerm to concatenate filters (And or Or) like this:
FilterTerm toDoTerm = new FilterTerm(nameAttribute);
toDoTerm.equal(project);
FilterTerm dateFilter = new FilterTerm(dateAttribute);
Calendar c = Calendar.getInstance();
dateFilter.lessOrEqual(c.getTime());
GroupFilterTerm groupFilter = new AndFilterTerm(toDoTerm, dateFilter);
Collection<IAttributeDefinition> attributesToQuery = new LinkedList<IAttributeDefinition>();
attributesToQuery.add(nameAttribute);
attributesToQuery.add(dateAttribute);
query = new Query(storyAsset.getOid().getMomentless(), true);
query.getSelection().addAll(attributesToQuery);
query.setFilter(groupFilter);
In this example I am querying Defects that are in a specific project, and where the Defect ChangeDate (dateAttribute) value is less than or equal to today.
You´ll need to take into account how Java handles Date variables when you do the comparison.

How to unescape tags without unescaping content

How can I unescape only the tags but not the content? Let me explain on an example...
This is the original raw response:
<GetWhoISResponse xmlns="http://www.webservicex.net">
<GetWhoISResult>Whois Server Version 2.0
To single out one record, look it up with "xxx", where xxx is one of the
of the records displayed above. If the records are the same, look them up
with "=xxx" to receive a full display for each record.
>>> Last update of whois database: Mon, 30 Dec 2013 08:20:00 UTC <<<
NOTICE: The expiration date displayed in this record is the date the
registrar&apos;s sponsorship of the domain name registration in the registry is
currently set to expire. This date does not necessarily reflect the expiration
date of the domain name registrant&apos;s agreement with the sponsoring
registrar. Users may consult the sponsoring registrar&apos;s Whois database to
view the registrar&apos;s reported date of expiration for this registration.
</GetWhoISResult>
</GetWhoISResponse>
If I use StringEscapeUtils and unescape text (unescapeXml):
<GetWhoISResponse xmlns="http://www.webservicex.net">
<GetWhoISResult>Whois Server Version 2.0
To single out one record, look it up with "xxx", where xxx is one of the
of the records displayed above. If the records are the same, look them up
with "=xxx" to receive a full display for each record.
>>> Last update of whois database: Mon, 30 Dec 2013 08:20:00 UTC <<<
NOTICE: The expiration date displayed in this record is the date the
registrar's sponsorship of the domain name registration in the registry is
currently set to expire. This date does not necessarily reflect the expiration
date of the domain name registrant's agreement with the sponsoring
registrar. Users may consult the sponsoring registrar's Whois database to
view the registrar's reported date of expiration for this registration.
</GetWhoISResult>
</GetWhoISResponse>
The problem is in the middle, in the line where < and > are escaped. I need this, because I would like to convert this to JSON, but now I get parsing error.
It is an interesting problem and I tried using forgiving xml parsers but they don't seem to parse a broken xml. The next best bet was regex and I managed to parse the given xml through it with a big caveat that the lesser and greater signs should not form a pattern of a tag, like:
< some random text here and >
After some research I finalized the 2 regex patterns for the given xml (can be used in a generalized format too):
public static final String LESSER_STRING = "<(.[^>]*)(<)+";
public static final String GREATER_STRING = ">[^<](.[^<]*)(>)+";
These strings are used to establish the regex pattern for the matcher to scan through the sequences.
Here is the working code with the output:
public static final String LESSER_STRING = "<(.[^>]*)(<)+";
public static final String GREATER_STRING = ">[^<](.[^<]*)(>)+";
public static final String ESCAPED_XML = "<GetWhoISResponse xmlns="http://www.webservicex.net"><GetWhoISResult>Whois Server Version 2.0 To single out one record, look it up with "xxx", where xxx is one of the of the records displayed above. If the records are the same, look them up with "=xxx" to receive a full display for each record. >>> Last update of whois database: Mon, 30 Dec 2013 08:20:00 UTC <<< NOTICE: The expiration date displayed in this record is the date the registrar&apos;s sponsorship of the domain name registration in the registry is currently set to expire. This date does not necessarily reflect the expiration date of the domain name registrant&apos;s agreement with the sponsoring registrar. Users may consult the sponsoring registrar&apos;s Whois database to view the registrar&apos;s reported date of expiration for this registration.</GetWhoISResult></GetWhoISResponse>";
private static Matcher matcher;
private static Pattern pattern;
private static String alter;
private static StringBuffer str = new StringBuffer();
private static StringBuffer jsonString = new StringBuffer();
public static void main(String[] args) {
String xml = StringEscapeUtils.unescapeXml(ESCAPED_XML);
pattern = Pattern.compile(GREATER_STRING);
matcher = pattern.matcher(xml);
while (matcher.find()) {
System.out.println(matcher.group(0));
System.out.println(matcher.group(0).substring(1));
// Find the first encountered greater than sing assuming greater
// than and less than do not form a 'tag' pattern
// Picks the first value after the 'last opened tag' including the
// greater sign - take substring 1
alter = ">" + matcher.group(0).substring(1).replaceAll(">", ">");
matcher.appendReplacement(str, alter);
}
matcher.appendTail(str);
pattern = Pattern.compile(LESSER_STRING);
matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(0));
System.out.println(matcher.group(0).substring(0,
matcher.group(0).length() - 1));
// Find the encountered lesser than sign assuming greater
// than and less than do not form a 'tag' pattern
// Picks the content between the lesser tags and the last opened
// tag; including the lesser sign of the tag
// Reduce it by 1 to prevent the last tag getting replaced
alter = matcher.group(0)
.substring(0, matcher.group(0).length() - 1);
// Add the last tag as is without replacing
alter = alter.replaceAll("<", "<") + "<";
matcher.appendReplacement(jsonString, alter);
}
matcher.appendTail(jsonString);
System.out.println(jsonString);
}
Output:
<GetWhoISResponse xmlns="http://www.webservicex.net"><GetWhoISResult>Whois Server Version 2.0 To single out one record, look it up with "xxx", where xxx is one of the of the records displayed above. If the records are the same, look them up with "=xxx" to receive a full display for each record. >>> Last update of whois database: Mon, 30 Dec 2013 08:20:00 UTC <<< NOTICE: The expiration date displayed in this record is the date the registrar's sponsorship of the domain name registration in the registry is currently set to expire. This date does not necessarily reflect the expiration date of the domain name registrant's agreement with the sponsoring registrar. Users may consult the sponsoring registrar's Whois database to view the registrar's reported date of expiration for this registration.</GetWhoISResult></GetWhoISResponse>
you can read the content and replace the "<" and ">" again

youTube data API V3 - search return Error 400 when PublishedAfter is used

I am running a search by keyword from a java project, exactly like in the example -
https://developers.google.com/youtube/v3/code_samples/java#search_by_keyword
It works great, but when I add to the example the PublishedAfter option
search.setPublishedAfter(myDateTime);
it returns error 400 message": "Bad Request"
YouTube.Search.List search = youtube.search().list("id,snippet");
String apiKey = properties.getProperty("youtube.apikey");
search.setKey(apiKey);
search.setQ(queryTerm);
search.setPublishedAfter(myDateTime) // the code works when removing this line
search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)");
search.setMaxResults(NUMBER_OF_VIDEOS_RETURNED);
SearchListResponse searchResponse = search.execute();
List<SearchResult> searchResultList = searchResponse.getItems();
I want to use the setPublishedAfter option
Please Help
Thanks
Youtube api v3 says that "The publishedAfter parameter indicates that the API response should only contain resources created after the specified time. The value is an RFC 3339 formatted date-time value (1970-01-01T00:00:00Z)" Therefore, your problem is most likely to because of your date format. Look at your "myDateTime" object. Mistake should be near to it. Also look at my code :
Calendar cal = Calendar.getInstance();
Date currentDateOfAmerica=cal.getTime();
cal.set(12, cal.get(12)-1);
Date dateOneMinuteBefore=cal.getTime();
DateTime dt1 = new DateTime(dateOneMinuteBefore);
DateTime dt2 = new DateTime(currentDateOfAmerica);
search.setPublishedAfter(dt1);
search.setPublishedBefore(dt2);
This code segment sets searching video which are uploaded between now and one minute ago
You are having a problem with fields part. Most likely publishedafter is not described in fields part you suggested.
Just comment out setFields, should work.

Display tag show user's local time in browser Jsp

I am using display tag for table details i want to change date & time to user's local browser setting, Data in DB is UMT. IF user is from india then it will add +5:30 etc. How can i do this. I am stuck in this. Please help me.
Have you tried like this ??
Locale cLoc=request.getLocale();
Calendar cCal=Calendar.getInstance(cLoc);
TimeZone tz=cCal.getTimeZone();
//......
A bit late to the party, but still answer here in case anyone else needs it.
Display.tag uses MessageFormatColumnDecorator internally to apply MessageFormat formatter to all columns that have "format" parameter set.
So, your options are:
Create your own custom MessageFormatColumnDecorator. Unfortunatelly it is inined in the ColumnTag code and cannot be very easily replaced (decorators are chained), so you will have to compile your own copy of Display.Tag jar, with your custom decorator in it.
In your custom decorator use the following sample code to change MessageFormat's timezone:
TimeZone tz = //get your timezone here
Object [] formats = format.getFormats(); //'format' is MessageFormat instance
for (int i = 0; i < formats.length; i++) {
if (formats[i] instanceof SimpleDateFormat) {
((SimpleDateFormat)formats[i]).setTimeZone(tz);
}
}
Second option is to use the <fmt:formatDate> within Display.tag column tags to format the output.
Sample Code:
<display:table name="dateList" id="object">
<display:column title="Date">
<fmt:formatDate value="${object.date}" type="both" dateStyle="long" timeStyle="long"/>
</display:column>
</display:table>
You can set default request locale for <fmt:formatDate> or wrap them in <fmt:timeZone> tag to set a timezone for them to use.

Categories