otp.Source.mkt_activity#
- Source.mkt_activity(calendar_name=None, inplace=False)#
Adds a string field named MKT_ACTIVITY to each tick in the input tick stream.
The value of this field is set to the union of session flags that apply for the security at the time of the tick, as specified in the calendar sections of the reference database (see Reference Database Guide).
Session flags may differ between databases, but the following letters have reserved meaning: L - half day, H - holiday, W - weekend, R - regular.
The calendar can either be specified explicitly by name (the CALENDAR sections of the reference database are assumed to contain a calendar with such a name), or default to the security- or exchange-level calendars for the queried symbol (the SYMBOL_CALENDAR or EXCH_CALENDAR sections of the reference database).
The latter case requires a non-zero symbol date to be specified for queried symbols (see parameter
symbol_date
inotp.run
).- Parameters
calendar_name (str,
Column
) –The calendar name to choose for the respective calendar from the CALENDAR sections of the reference database. It can be a string constant or the name of the field with per-tick calendar name.
When this parameter is not specified, default security- or exchange-level calendars configured for the queried database and symbol are used (but symbol date must be specified in this case).
inplace (bool) – The flag controls whether operation should be applied inplace or not. If
inplace=True
, then it returns nothing. Otherwise method returns a new modified object.self (Source) –
- Return type
Source
orNone
Note
When applying
mkt_activity()
to aggregated data, please take into account that session flags may change during the aggregation bucket. The field MKT_ACTIVITY, in this case, will represent the session flags at the time assigned to the bucket, which may be different from the session flags at some times during the bucket.Examples
By default, security- or exchange-level calendars configured for the queried database and symbol are used (but symbol date must be specified in this case):
>>> data = otp.DataSource(...) >>> data = data.mkt_activity() >>> otp.run(data, date=otp.date(2022, 1, 1), symbol_date=otp.date(2022, 1, 1))
Otherwise, parameter
calendar_name
must be specified:>>> data = otp.DataSource(...) >>> data = data.mkt_activity(calendar_name='WNY') >>> otp.run(data, date=otp.date(2022, 1, 1))
Parameter
calendar_name
can also be specified as a column. In this case calendar name can be different for each tick:>>> data = otp.DataSource(...) >>> data = data.mkt_activity(calendar_name=data['CALENDAR_NAME']) >>> otp.run(data, date=otp.date(2022, 1, 1))
In this example you can see how market activity status is changing during the days. We are getting first and last tick of the group each time the type of market activity is changed. You can see regular trades (R) from 9:30 to 16:00, and a holiday (H) on 2018-02-07.
>>> data = otp.DataSource('TRAIN_A_PRL_TRD', tick_type='TRD', symbols='MSFT') >>> data = data.mkt_activity('FRED') >>> data = data[['PRICE', 'SIZE', 'MKT_ACTIVITY']] >>> first = data.first(1, bucket_interval=(data['MKT_ACTIVITY'] != data['MKT_ACTIVITY'][-1])) >>> last = data.last(1, bucket_interval=(data['MKT_ACTIVITY'] != data['MKT_ACTIVITY'][-1])) >>> data = otp.merge([first, last]) >>> df = otp.run(data, ... start=otp.dt(2018, 2, 1), end=otp.dt(2018, 2, 9), ... symbol_date=otp.dt(2018, 2, 1), timezone='EST5EDT') >>> df[['Time', 'MKT_ACTIVITY']] Time MKT_ACTIVITY 0 2018-02-01 01:31:44.466 1 2018-02-01 09:29:59.996 2 2018-02-01 09:30:00.225 R 3 2018-02-01 15:59:58.857 R 4 2018-02-01 16:00:01.858 5 2018-02-02 09:29:50.366 6 2018-02-02 09:30:01.847 R 7 2018-02-02 15:59:59.829 R 8 2018-02-02 16:00:01.782 9 2018-02-05 09:29:43.084 10 2018-02-05 09:30:00.301 R 11 2018-02-05 15:59:59.974 R 12 2018-02-05 16:00:02.438 13 2018-02-06 09:29:27.279 14 2018-02-06 09:30:00.045 R 15 2018-02-06 15:59:59.903 R 16 2018-02-06 16:01:03.524 17 2018-02-07 09:29:56.739 18 2018-02-07 09:30:00.365 H 19 2018-02-07 15:59:59.940 H 20 2018-02-07 16:00:00.187 21 2018-02-08 09:29:28.446 22 2018-02-08 09:30:00.658 F 23 2018-02-08 15:59:59.564 F 24 2018-02-08 16:00:02.355 25 2018-02-08 19:59:57.061
See also
MKT_ACTIVITY OneTick event processor