Rblpapi
Rblpapi copied to clipboard
Add Side to getTicks/getMultipleTicks
I'm using getTicks
to get executed trade data from Bloomberg, for example as follows:
getTicks("3137G0EZ8@TRAC MTGE", eventType = "TRADE", startTime = as.POSIXct(as.Date("20151201", "%Y%m%d")), endTime = as.POSIXct(as.Date("20151231", "%Y%m%d")), tz = Sys.timezone())
This returns a matrix with trade data, similar to public TRACE data that can be found here: http://finra-markets.morningstar.com/BondCenter/BondTradeActivitySearchResult.jsp?ticker=FFMCC4239504&startdate=12/01/2015&enddate=12/31/2015
The matrix actually has both sides of one trade. On the website above, a column indicates which row corresponds to Buy, and which to Sell. (This detail is also available on the QR screen in Bloomberg, for users with sufficient access).
Would it be possible to add a column to the output containing the side? Or add an argument to the eventType
for "BUY" and "SELL"?
Thank you for all your efforts!
Here is my (very personal) take:
R> library(Rblpapi)
R> X <- getTicks("3137G0EZ8@TRAC MTGE", eventType = "TRADE", startTime = as.POSIXct(as.Date("20151201", "%Y%m%d")), endTime = as.POSIXct(as.Date("20151231", "%Y%m%d")), tz = Sys.timezone())
R> head(X)
value size
2015-12-07 15:31:40 113.500 4000
2015-12-07 15:31:40 113.516 4000
2015-12-07 16:20:32 113.500 0
R> class(X)
[1] "xts" "zoo"
R>
I prefer xts
objects, and those do not allow numeric and textual data to be mixed. Neither do matrices.
There are ways to deal with this:
- we could use factors and just send the levels (but without label maybe less useful)
- we could drop columns when the type is
xts
but let them out in thedata.frame
sense - require a
data.frame
when certain columns are use (messy) - write a different tick getter that always returns
data.frame
and allow mixing
Thoughts?
Thoughts from a humble end-user, in the same order:
- returning levels might be sufficient. My only concern would be whether those levels are the same for arbitrary securities. For example, would "Buy" always have a level of 1? If not, then this solution would become much less useful.
- dropping/retaining columns depending on the object type sounds sufficient. Would this then depend on the
returnAs
argument? - don't put time and effort into branches you foresee being messy.
- I'm not sure what would be entailed when writing a different tick getter, so I don't think I can provide meaningful feedback.
Yes, returning buy/sales as 1 or 2 if it is matrix
or xts
is probably easiest.
I had a quick look but I do not yet see where to get the side information from.
Just in case, the API docs are available.
Somewhere around page 172? The below screenshot is from section A.2.6 IntradayTickRequest: Sequence:
Not sure if this is relevant. I haven't yet familiarized myself with your C++ code for these functions.
P.S. - returning buys/sells as 1 and 2, respectively would certainly work for me.
Nice catch, but what complicates this is that this is now conditional on having set includeRpsCodes
.
Apologies, but I'll have to defer to you on best practices. However, I'm happy to provide feedback or help if I can.
I'll see if I can come up with something (time permitting). Currently en route...
Hi Dirk,
- I'm using getTicks all the time for various purposes, and the return value being restricted to times/values/sizes continues to be an issue.
- I'd like to help contribute to this repository/function (or create a new function?)
- My background is in quant finance, including a few courses in C++. I'd say I've written a few dozen projects in C++, and am generally comfortable coding in C++, although I'm no expert.
- I've never used the Bloomberg API in C++.
- I'd like to make sure my contributions and your current development on this function are not redundant.
- Given some of your comments on this issue, I'm concerned about possibly pushing code that may ultimately be problematic. In particular, I'm finding it more necessary to mix data types (numeric and character), which you'd like to avoid.
Any advice with how I can help contribute? Shall we go into more detail?
Thanks.
Sounds like you can definitely contribute.
We prefer contributions as GitHub pull requests -- fork our repo, make your changes locally and test them carefully, and then submit a pull request. We prefer changes that are incremental and leave existing functionality. It also helps to first discuss what you want to change.
In this case, what we'd need / want is an option to not return an xts object but rather a data.frame which may then contain columns of different types (which an xts cannot as it is based on a matrix type).
Is it worth implementing includeRpsCodes
as part of #147?
Yes, maybe. Do you want to take a look? I had not yet gotten around to it.
Hi all...I'm looking to pull in bond trades using getTicks(). As of now there is no functionality for pulling in the RPS (reporting party side) code (discussed above). I'm just wondering if anyone has worked on developing this function recently? Thx
There was a design error on my part. I generally preferred xts
, but it only carries numeric values. So that didn't work.
Switching to data.table
makes it much better. So if you can tell the back end to get you the information I think we should be able to pass it through. Run with debug
and verbose
on to see what happens.
I switched to data.table and tried to pass includeRpscodes a few different ways. I couldn't get it to work (however, i'm a novice and don't have much experience with Rblpapi)...
has anyone else with more programming/Rblpapi experience had any luck and gotten it to work (^^)?
@benrothman93 - are you able to make this work in some other environment, e.g. Excel?
yes (for the most part). But, for the task i'm trying to accomplish I would need the RPS code thru R. In excel, the code is passed by RPScode = TRUE, rather than includeRpsCodes. Maybe that has something to do with that?
i chatted with bbg help desk. They dont support R but he said he was able to pull everything using Python. I've included his python code:
import blpapi
import datetime
# Create a Session
session = blpapi.Session()
# Start a Session
if not session.start():
print "Failed to start session."
if not session.openService("//blp/refdata"):
print "Failed to open //blp/refdata"
refDataService = session.getService("//blp/refdata")
request = refDataService.createRequest("IntradayTickRequest")
request.set("security", "44052WAA2 Corp")
request.getElement("eventTypes").appendValue("TRADE")
request.getElement("eventTypes").appendValue("AT_TRADE")
request.set("includeConditionCodes", True)
**request.set("includeRpsCodes", True)**
# Date format (YYYY, M, D), Time format (H, M, S)
StartDate = datetime.date(2017,2,6)
StartTime = datetime.time(14,30,0)
Start = datetime.datetime.combine(StartDate,StartTime)
request.set("startDateTime", Start)
EndDate = datetime.date(2017,2,6)
EndTime = datetime.time(19,30,0)
End = datetime.datetime.combine(EndDate,EndTime)
request.set("endDateTime", End)
print "Sending Request:", request
session.sendRequest(request)
endReached = False
while endReached == False:
ev = session.nextEvent()
if ev.eventType() == blpapi.Event.RESPONSE or ev.eventType() == blpapi.Event.PARTIAL_RESPONSE:
for msg in ev:
print msg
if ev.eventType() == blpapi.Event.RESPONSE:
endReached = True
that makes me want to vomit. do you really have to do all that low level fetching in their python package? by the look of it, it should be pretty easy to get those columns on the c++ side. I'm not sure how the data.frame vs xts return type is done. It's been a while since I looked at this function.
@benrothman93 changing the returnAs
parameter in getTicks
will not return RPS codes because this is not included in the BLPAPI request at the C++ level in getTicks_Impl
.
You would first need to add support for RPS codes at the C++ level. This is referred to in the BLPAPI Developer's guide (https://data.bloomberglp.com/labs/sites/2/2014/07/blpapi-developers-guide-2.54.pdf), on page 172. There would need to be an additional parameter in getTicks_Impl
and other necessary adjustments throughout the code in https://github.com/Rblp/Rblpapi/blob/master/src/getTicks.cpp. This should only be a minor modification.
Then at the R level, getTicks
would need to support including RPS in the call to getTicks_Impl
. You might initially adopt the same approach used for condition codes i.e. only request to include RPS if returnAs %in% c("data.frame", "data.table")
.
As for return types other than data.frame
, it seems this is handled at the R level in getTicks
. Once the above changes were made, additional columns (RPS codes in this case) would automatically be returned in the data.table
or data.frame
.
I’ve never programmed in C++ and I’m not quite sure how the pull requests procedure works.
As an academic exercise would anyone be willing to take a stab at the procedure for both the C++ package and the R package for the public good? @joel23888 indicated that this would be a routine addition and wouldn't take much time/effort.
I believe this would be a nice addition to the community at large and while I’d love to make that contribution, I don’t have the technical expertise.
while I’d love to make that contribution, I don’t have the technical expertise
Not to worry, but everybody else is busy too.
Those with an itch to scratch ... are usually the ones doing the work. Here it is your itch.
I would help whenever I have the time, but I cannot push to GitHub from where I would normally need to work on this (so I can test against BBG). I guess I could just make some changes in an environment where I could push to GitHub, but without testing them against BBG. Would someone else mind doing the testing in that case? (Not sure if this makes a lot of sense, just trying to help.)
@joel23888 I really appreciate your help. With some instruction, I can help you test it. I'm not exactly sure how I would go about doing that for you (I have bbg access and if you give me instructions as to how to load the "dev code/package," I'd be glad to try and pull everything with RPS codes). Thx again!
@joel23888 - I would also be very happy to help you in testing with my Bloomberg to get the RPS codes via Rblpapi.