quantlab.dataset.nbbo.resample.NbboFilterPolicy#

class quantlab.dataset.nbbo.resample.NbboFilterPolicy(drop_crossed=True, drop_locked=False, drop_nonpositive_price=True, keep_qu_cond=None)[source]#

Bases: object

Filter applied to NBBO records before resampling.

A dropped record is ignored completely: the previous valid quote stays in force, and the record neither starts a session (as the seed, the last quote before the open) nor takes part in a tie between records with the same timestamp. A record matching several rules is counted under the first in the order nonpositive price, condition, crossed, locked, so each dropped record is counted exactly once.

Parameters:
drop_crossedbool, default True

Drop crossed quotes, where bid > ask. Only checked when both sides are present.

drop_lockedbool, default False

Drop locked quotes, where bid == ask. Only checked when both sides are present. Locked quotes do occur legitimately, so they are kept by default.

drop_nonpositive_pricebool, default True

Drop a record whose bid or ask, where present, is <= 0. A null side is not a price and is not checked.

keep_qu_condsequence of str, optional

If set, keep only records whose quote condition code qu_cond is in this list; a null condition is in no list. Stored as a tuple.

Examples

>>> NbboFilterPolicy().drop_locked
False
>>> NbboFilterPolicy(drop_locked=True, keep_qu_cond=["R", "C"]).keep_qu_cond
('R', 'C')
classmethod from_config(config)[source]#

Build a policy from the four filter fields of an NbboDatasetConfig.

Parameters:
configNbboDatasetConfig

The dataset config holding drop_crossed, drop_locked, drop_nonpositive_price and keep_qu_cond.

Returns:
NbboFilterPolicy

The policy.

Examples

>>> config = NbboDatasetConfig(
...     raw_data_dir_path="downloads/us_equity/tick/wrds_taq/wrds",
...     zarr_file_path="data/us_equity/tick/wrds_nbbo_1m.zarr",
...     reference_dir="downloads/_reference",
... )
>>> NbboFilterPolicy.from_config(config).drop_crossed
True
reason()[source]#

Return an expression giving each record’s drop reason, or null if it is kept.

The frame must have bid and ask columns, with a missing side already null, and a qu_cond column.

Returns:
pl.Expr

A string expression: "nonpositive_price", "condition", "crossed", "locked" or null.

Examples

>>> policy = NbboFilterPolicy(drop_locked=True)
>>> frame = pl.DataFrame({
...     "bid": [1.0, 2.0, None, 0.0],
...     "ask": [1.0, 1.5, 2.0, 1.0],
...     "qu_cond": ["R", "R", "R", "R"],
... })
>>> frame.with_columns(policy.reason().alias("reason"))
shape: (4, 4)
┌──────┬─────┬─────────┬───────────────────┐
│ bid  ┆ ask ┆ qu_cond ┆ reason            │
│ ---  ┆ --- ┆ ---     ┆ ---               │
│ f64  ┆ f64 ┆ str     ┆ str               │
╞══════╪═════╪═════════╪═══════════════════╡
│ 1.0  ┆ 1.0 ┆ R       ┆ locked            │
│ 2.0  ┆ 1.5 ┆ R       ┆ crossed           │
│ null ┆ 2.0 ┆ R       ┆ null              │
│ 0.0  ┆ 1.0 ┆ R       ┆ nonpositive_price │
└──────┴─────┴─────────┴───────────────────┘