Rblp / Rblpapi

R package interfacing the Bloomberg API from https://www.bloomberglabs.com/api/
Other
167 stars 75 forks source link

Rblpapi - how to return condition codes from getTicks #117

Open cryptobiotic opened 8 years ago

cryptobiotic commented 8 years ago

Hi, snippet below, seems like it should work and return valid condition codes along with price and quantity, but nothing is returned.

require(Rblpapi) blpConnect() symbol <- "WNH6 Comdty" local.times <- as.POSIXct(c("2016-01-07 09:00:00", "2016-01-08 10:00:00"), format="%Y-%m-%d %H:%M:%S")

works

ticks <- getTicks(symbol, eventType = "TRADE", startTime = local.times[1], endTime = local.times[2],verbose = FALSE)

                times    value size

32658 2016-01-08 15:59:59 160.8125 1 32659 2016-01-08 15:59:59 160.8125 1 32660 2016-01-08 15:59:59 160.8125 2 32661 2016-01-08 15:59:59 160.8125 2 32662 2016-01-08 15:59:59 160.8125 1 32663 2016-01-08 16:00:00 160.8125 3

fails

ticks <- getTicks(symbol, eventType = "TRADE", startTime = local.times[1], endTime = local.times[2],verbose = FALSE, setCondCodes = TRUE)

Error in getTicks(symbol, eventType = "TRADE", startTime = local.times[1], : unused argument (setCondCodes = TRUE)

Is the option named something different?

Thanks!

eddelbuettel commented 8 years ago
  1. Please don't crosspost to r-sig-finance and here. Here is preferred.
  2. See previous discussions of getTicks().
cryptobiotic commented 8 years ago

Sorry. I did do quite a bit of digging into the issue before firing out the question. It seems that setCondCodes = TRUE should be returning the valid condition codes but is not. And based on my searching for other questions regarding this, it seemed that setCondCodes = TRUE was correct.

eddelbuettel commented 8 years ago

Yes, in essence there are uses / users who want non-numeric returns. We either need to extend getTicks() or write a new "broader" one... The one we have now works with numeric-only data suitable for xts types. See the other open issues.

cryptobiotic commented 8 years ago

Understood. I was trying the example if the getTicks() update, getTicks(con, "XUA Index", eventType = c("BEST_ASK", "BEST_BID"), startDateTime = as.POSIXct("2015-07-07 10:00"), endDateTime = as.POSIXct("2015-07-07 12:00"), setCondCodes = TRUE)

and no codes were returned. Hopefully this may be updated in the future for the condition code string.

Awesome package, thank you very much for all of your efforts.

eddelbuettel commented 8 years ago

Truth as always in the code; right now it simply isn't returned for the 'numeric' only reason.

eddelbuettel commented 8 years ago

Hopefully this may be updated in the future

There are no fairies. Somone needs to sit down and do it. Could that be you?

joel23888 commented 8 years ago

I may be able to help. I was talking about this in the comments of cd411371a7729c789bae27a168561f3a14f42cbb. I can probably look at this towards the end of the month.

In terms of spec, one point I would add to my comments there is it could potentially return a list in the case where a numeric-only object and condition codes were requested: first element would be e.g. a matrix and second element would be a vector of the condition codes which can be referred to if needed. Would be interested to know your thoughts on my prior comment and this @eddelbuettel

eddelbuettel commented 8 years ago

Howdy, and thanks for following up and offering actual tangible coding help.

We can definitely expand the result set. We may want to prefer to keep the existing interface -- maybe with a hint that it will be deprecated in the future -- but could just add a new, similar function to try this out.

joel23888 commented 8 years ago

Thanks, will adopt that approach. Out of interest, can you give me an example security code where condition codes are generally not required. For the asset classes I focus on they are a necessity but I would like a working example (for reference) to the contrary.

eddelbuettel commented 8 years ago

From the top of my head (and untested) SPY US Equity or ES1 Index. Or maybe they have it but I ignore it :) Gotta check if I have the Bbg example binaries built somewhere....

joel23888 commented 8 years ago

Thanks, I checked them and they do actually have condition codes that could be returned. Then I thought to check a stock index e.g. NKY Index and there are just prints with no codes in that case. I will probably include this as a test case for a symbol that has no condition codes.

wmorgan85 commented 8 years ago

Equity indices that are calculated but not cash traded should fit this bill. They usually print at regular intervals, though that is index dependent . If you need something more liquid a currency like EUR Curncy will have continuous prints based on trade activity and no condition codes.

On Wed, 3 Feb 2016, 20:56 joel23888 notifications@github.com wrote:

Thanks, I checked them and they do actually have condition codes that could be returned. Then I thought to check a stock index e.g. NKY Index and there are just prints with no codes in that case. I will probably include this as a test case for a symbol that has no condition codes.

— Reply to this email directly or view it on GitHub https://github.com/Rblp/Rblpapi/issues/117#issuecomment-179571753.

auenbauer commented 7 years ago

I am also very interested to get some additional information from the getTicks() function in order to make sense of the data. The area I specialize in is High Yield bonds and the data come from TRACE (Source: TRAC).

I wan't go give an example with one bond: LVLT 5 3/8 01/15/24 (527298BK8 Corp ) of what I think would be nice to have and my suggestion is to keep the additional information numerical so that no huge change is required (maybe my understanding is not correct but from what I did read it looks like that you need to keep the info numerical to be able to put it into XTS).

Not sure if that is of help and if I understand the issue correctly but at last for high yield bonds I think the most useful fields that are missing are rpsCode, rptParty and rptContra and those could be made numerical with a code.

f.i.:

rpsCode shows S, B -> you could make S -1 for sell and B +1 for buy;

rptParty shows D or T -> Dealer (1), Trading System (2)

rptContra is C, D or T -> which could also be make numerical

Please forgive if this suggestion makes no sense but at least I wanted to let you know that I would find it very useful if I could get the background info about the trades.

Here is a screenshot for QR on the terminal of this bond:

image

And a screenshot from the DAPI Demo App

image

Thank you for your great contributions!

PS: not sure if Yield can be derived from getTicks() already (I was not able to get it). But this would also be nice to have if not.

eddelbuettel commented 7 years ago

You already can by specifying data.frame (or data.table) as return type:

R> now <- Sys.time()
R> getTicks("527298BK8 Corp", startTime=now-5*60, endTime=now, returnAs="data.frame")
                 times  type   value size condcode
1  2017-08-23 09:27:14 TRADE 102.576    0         
2  2017-08-23 09:27:44 TRADE 102.576    0         
3  2017-08-23 09:28:14 TRADE 102.576    0         
4  2017-08-23 09:28:44 TRADE 102.586    0         
5  2017-08-23 09:29:14 TRADE 102.586    0         
6  2017-08-23 09:29:44 TRADE 102.586    0         
7  2017-08-23 09:30:14 TRADE 102.586    0         
8  2017-08-23 09:30:44 TRADE 102.586    0         
9  2017-08-23 09:31:14 TRADE 102.586    0         
10 2017-08-23 09:31:44 TRADE 102.586    0         
R> 

If there is more data on the wire and it can be gotten we (as always) greatly welcome pull requests.

eddelbuettel commented 7 years ago

But even for other event types I do not set much more being returned (when using verbose=TRUE) so I am not sure we are actually getting what you desire.

auenbauer commented 7 years ago

Here is how I can download the data into Excel (by using the BDH function). So maybe I could also use bdh with Rblpapi somehow (?) but it looks like that "TRADE" is not accepted as a field...

=BDH("527298BK8 Corp";"TRADE";"2017-02-02";"";"Dir=V";"Dts=S";"Sort=D";"IntrRw=True";"CondCodes=S";"QRM=H";"ExchCode=H";"BrkrCodes=H";"RPSCodes=S";"RPTParty=S";"RPTContra=S";"BICMICCodes=H";"Type=H";"Price=S";"Size=S";"TradeTime=H";"Yield=S";"ActionCodes=S";"IndicatorCodes=H";"UpfrontPrice=H";"Spread=H";"Points=100000";"UseDPDF=Y";"cols=9;rows=585")

image

joel23888 commented 7 years ago

According to the developer guide at p85 (link below), IntradayTickRequest apparently supports BID_YIELD and ASK_YIELD for eventType, although on p95 under the Fields parameter it does not include these in the set of valid element values. So I just tried it out but I could not get any yield data for 527298BK8 Corp:

> head(getTicks("527298BK8 Corp", "TRADE", startTime=now-5*60*24, endTime=now, returnAs="data.frame"))
                times  type   value size condcode
1 2017-08-24 05:51:14 TRADE 102.710    0         
2 2017-08-24 05:51:44 TRADE 102.710    0         
3 2017-08-24 05:52:14 TRADE 102.710    0         
4 2017-08-24 05:52:44 TRADE 102.722    0         
5 2017-08-24 05:53:14 TRADE 102.722    0         
6 2017-08-24 05:53:44 TRADE 102.722    0         
> head(getTicks("527298BK8 Corp", "BID_YIELD", startTime=now-5*60*24, endTime=now, returnAs="data.frame"))
REQUEST FAILED: responseError = { source = "bbdbl3" code = 23 category = "BAD_ARGS" message = "No event types specified [nid:66] " }
[1] times    type     value    size     condcode
<0 rows> (or 0-length row.names)
> head(getTicks("527298BK8 Corp", "ASK_YIELD", startTime=now-5*60*24, endTime=now, returnAs="data.frame"))
REQUEST FAILED: responseError = { source = "bbdbl2" code = 23 category = "BAD_ARGS" message = "No event types specified [nid:66] " }
[1] times    type     value    size     condcode
<0 rows> (or 0-length row.names)

Also, the developer guide at p95 mentions several options for IntradayTickRequest which seem to include the data that @auenbauer is seeking in the response. For example, there is an option includeYield which (I guess) would include the yield alongside price when requesting TRADE. However, getTicks does not currently support an options parameter so there is currently no way for the user to specify this or other options. The only exception to this is for condition codes which are automatically requested if requesting a data.frame or data.table return type.

Developer guide can be found at: https://data.bloomberglp.com/professional/sites/10/2017/03/BLPAPI-Core-Developer-Guide.pdf

auenbauer commented 7 years ago

Page 85 includes request.set("includeConditionCodes", true); in the example.

I wonder if adding the following three lines to the getTicks() would do the job or not.

request.set("includeRpsCodes", true) 
request.set("includeYield ", true) 
request.set("includeActionCodes ", true)
joel23888 commented 7 years ago

No, this would need to be added to the request at the C++ level but getTicks is an R function. You could look at how condition codes support was implemented to see how R and C++ levels work together. However, at first you could just try seeing what happens at the C++ level if you add one or more of these to the request. For example, was my guess above correct in that you would request TRADE ticks and by setting request.set("includeYield ", true) the yield would be added alongside as an additional column?

auenbauer commented 7 years ago

I looked at the C++ code of getTicks but could not find request.set("includeConditionCodes", true); for some reason - I looked out for this because there I thought would be the place to insert the three other arguments

request.set("includeRpsCodes", true) 
request.set("includeYield ", true) 
request.set("includeActionCodes ", true)
joel23888 commented 7 years ago

That is because there is a parameter to the function. See: https://github.com/Rblp/Rblpapi/blob/a58dc457cf93aa237bbcbe9e9a6649a49fb72844/src/getTicks.cpp#L168

auenbauer commented 7 years ago

Ah here it is - THANK YOU!