Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Allow arbitrary date formats #72

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ file for building docs
- Refactor pdblp to use blpapi.Messages parsed into dicts
- Remove conda package building
- Remove testing for python 3.5 and 3.4

# pdblp 0.1.9

- Allow arbitrary dates formats
59 changes: 39 additions & 20 deletions pdblp/pdblp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

# partial lookup table for events used from blpapi.Event
_EVENT_DICT = {
blpapi.Event.SESSION_STATUS: 'SESSION_STATUS',
blpapi.Event.RESPONSE: 'RESPONSE',
blpapi.Event.PARTIAL_RESPONSE: 'PARTIAL_RESPONSE',
blpapi.Event.SERVICE_STATUS: 'SERVICE_STATUS',
blpapi.Event.TIMEOUT: 'TIMEOUT',
blpapi.Event.REQUEST: 'REQUEST'
blpapi.Event.SESSION_STATUS: 'SESSION_STATUS',
blpapi.Event.RESPONSE: 'RESPONSE',
blpapi.Event.PARTIAL_RESPONSE: 'PARTIAL_RESPONSE',
blpapi.Event.SERVICE_STATUS: 'SERVICE_STATUS',
blpapi.Event.TIMEOUT: 'TIMEOUT',
blpapi.Event.REQUEST: 'REQUEST'
}


Expand Down Expand Up @@ -251,10 +251,8 @@ def bdh(self, tickers, flds, start_date, end_date, elms=None,
String or list of strings corresponding to tickers
flds: {list, string}
String or list of strings corresponding to FLDS
start_date: string
String in format YYYYmmdd
end_date: string
String in format YYYYmmdd
start_date: {string, datetime, date}
end_date: {string, datetime, date}
elms: list of tuples
List of tuples where each tuple corresponds to the other elements
to be set, e.g. [("periodicityAdjustment", "ACTUAL")].
Expand All @@ -266,7 +264,11 @@ def bdh(self, tickers, flds, start_date, end_date, elms=None,
longdata: boolean
Whether data should be returned in long data format or pivoted
"""
ovrds = [] if not ovrds else ovrds
start_date = f"{pd.to_datetime(start_date):%Y%m%d}"
end_date = f"{pd.to_datetime(end_date):%Y%m%d}"

ovrds = [] if not ovrds else [(field, _todate_greedy(date))
for (field, date) in ovrds]
elms = [] if not elms else elms

elms = list(elms)
Expand All @@ -285,6 +287,8 @@ def bdh(self, tickers, flds, start_date, end_date, elms=None,

def _bdh_list(self, tickers, flds, start_date, end_date, elms,
ovrds):
start_date = f"{pd.to_datetime(start_date):%Y%m%d}"
end_date = f"{pd.to_datetime(end_date):%Y%m%d}"
logger = _get_logger(self.debug)
if type(tickers) is not list:
tickers = [tickers]
Expand Down Expand Up @@ -350,7 +354,8 @@ def ref(self, tickers, flds, ovrds=None):
FUT_GEN_MONTH = "FGHJKMNQUVXZ"
}
"""
ovrds = [] if not ovrds else ovrds
ovrds = [] if not ovrds else [(field, _todate_greedy(date))
for (field, date) in ovrds]

logger = _get_logger(self.debug)
if type(tickers) is not list:
Expand Down Expand Up @@ -451,7 +456,8 @@ def bulkref(self, tickers, flds, ovrds=None):
}
}
"""
ovrds = [] if not ovrds else ovrds
ovrds = [] if not ovrds else [(field, _todate_greedy(date))
for (field, date) in ovrds]

logger = _get_logger(self.debug)
if type(tickers) is not list:
Expand Down Expand Up @@ -524,7 +530,7 @@ def ref_hist(self, tickers, flds, dates, ovrds=None,
flds: {list, string}
String or list of strings corresponding to FLDS
dates: list
list of date strings in the format YYYYmmdd
list of dates
ovrds: list of tuples
List of tuples where each tuple corresponds to the override
field and value. This should not include the date_field which will
Expand All @@ -542,7 +548,9 @@ def ref_hist(self, tickers, flds, dates, ovrds=None,
>>> con.ref_hist("AUD1M CMPN Curncy", "SETTLE_DT", dates)

"""
ovrds = [] if not ovrds else ovrds
dates = [f"{date:%Y%m%d}" for date in pd.to_datetime(dates)]
ovrds = [] if not ovrds else [(field, _todate_greedy(date))
for (field, date) in ovrds]

if type(tickers) is not list:
tickers = [tickers]
Expand Down Expand Up @@ -572,7 +580,7 @@ def bulkref_hist(self, tickers, flds, dates, ovrds=None,
flds: {list, string}
String or list of strings corresponding to FLDS
dates: list
list of date strings in the format YYYYmmdd
list of dates
ovrds: list of tuples
List of tuples where each tuple corresponds to the override
field and value. This should not include the date_field which will
Expand All @@ -591,6 +599,7 @@ def bulkref_hist(self, tickers, flds, dates, ovrds=None,
... date_field="CURVE_DATE")

"""
dates = [f"{date:%Y%m%d}" for date in pd.to_datetime(dates)]
ovrds = [] if not ovrds else ovrds

if type(tickers) is not list:
Expand All @@ -608,6 +617,7 @@ def bulkref_hist(self, tickers, flds, dates, ovrds=None,
return data

def _send_hist(self, tickers, flds, dates, date_field, ovrds):
dates = [f"{date:%Y%m%d}" for date in pd.to_datetime(dates)]
logger = _get_logger(self.debug)
setvals = []
request = self._create_req('ReferenceDataRequest', tickers, flds,
Expand Down Expand Up @@ -637,10 +647,10 @@ def bdib(self, ticker, start_datetime, end_datetime, event_type, interval,
----------
ticker: string
String corresponding to ticker
start_datetime: string
UTC datetime in format YYYY-mm-ddTHH:MM:SS
end_datetime: string
UTC datetime in format YYYY-mm-ddTHH:MM:SS
start_datetime: {string, datetime}
UTC datetime
end_datetime: {string, datetime}
UTC datetime
event_type: string {TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID,
BEST_ASK}
Requested data event type
Expand All @@ -651,6 +661,8 @@ def bdib(self, ticker, start_datetime, end_datetime, event_type, interval,
to be set. Refer to the IntradayBarRequest section in the
'Services & schemas reference guide' for more info on these values
"""
start_datetime = f"{pd.to_datetime(start_datetime):%Y-%m-%dT%H:%M:%S}"
end_datetime = f"{pd.to_datetime(end_datetime):%Y-%m-%dT%H:%M:%S}"
elms = [] if not elms else elms

# flush event queue in case previous call errored out
Expand Down Expand Up @@ -746,3 +758,10 @@ def message_to_dict(msg):
'topicName': msg.topicName(),
'element': _element_to_dict(msg.asElement())
}


def _todate_greedy(date):
try:
return f"{pd.to_datetime(date):%Y%m%d}"
except (ValueError, TypeError, OverflowError):
return date
Loading