| Title: | A Filter System for Selecting Trading Instruments |
|---|---|
| Description: | Enables filtering datasets of tradable instruments by prior specified identifiers which correspond to saved filter expressions. A filter is a named expression bound to a target dataset, stored once in a package level registry, and later applied to select trading codes such as tickers or symbols out of a universe, price or signal dataset. The design follows the `filters` package, replacing the clinical study dataset convention with a trading instrument convention. |
| Authors: | Joe Zhu [aut, cre] |
| Maintainer: | Joe Zhu <[email protected]> |
| License: | Apache License (>= 2.0) |
| Version: | 0.0.1 |
| Built: | 2026-10-02 13:06:38 UTC |
| Source: | https://github.com/shajoezhu/filters.trade |
A filter system for selecting trading instruments. add_filter() and
apply_filter() are the two entry points of the package.
Maintainer: Joe Zhu [email protected]
Authors:
Joe Zhu [email protected]
Add a new filter definition or overwrite an existing one. A filter binds an
unevaluated condition to a target dataset so that it can be applied
later by apply_filter(). Unless character_only = TRUE, condition is
captured with substitute() and therefore written without quotes.
add_filter( id, title, target, condition, character_only = FALSE, overwrite = FALSE )add_filter( id, title, target, condition, character_only = FALSE, overwrite = FALSE )
id |
|
title |
|
target |
|
condition |
The filter condition |
character_only |
|
overwrite |
|
A list of title, target and condition, invisibly
add_filter( id = "LARGECAP", title = "Large Cap", target = "TICKERS", condition = MARKET_CAP >= 1e10 ) add_filter( id = "TECH", title = "Technology Sector", target = "TICKERS", condition = "SECTOR == 'Technology'", character_only = TRUE )add_filter( id = "LARGECAP", title = "Large Cap", target = "TICKERS", condition = MARKET_CAP >= 1e10 ) add_filter( id = "TECH", title = "Technology Sector", target = "TICKERS", condition = "SECTOR == 'Technology'", character_only = TRUE )
Apply a Filter to a Dataset or List of Datasets
apply_filter(data, ...) ## Default S3 method: apply_filter(data, ...) ## S3 method for class 'data.frame' apply_filter(data, id, target = deparse(substitute(data)), verbose = TRUE, ...) ## S3 method for class 'list' apply_filter(data, id, verbose = TRUE, ...)apply_filter(data, ...) ## Default S3 method: apply_filter(data, ...) ## S3 method for class 'data.frame' apply_filter(data, id, target = deparse(substitute(data)), verbose = TRUE, ...) ## S3 method for class 'list' apply_filter(data, id, verbose = TRUE, ...)
data |
|
... |
Not used. |
id |
|
target |
|
verbose |
|
A new data.frame or list of data.frames filtered based upon
the condition defined for id. When the master trading code dataset is
among the filter targets, every other dataset is restricted to the
surviving trading codes.
tickers <- data.frame( SYMBOL = c("AAA", "BBB", "CCC"), SECTOR = c("Technology", "Energy", "Technology"), stringsAsFactors = FALSE ) prices <- data.frame( SYMBOL = c("AAA", "BBB", "CCC"), CLOSE = c(10, 20, 30), stringsAsFactors = FALSE ) add_filter( id = "TECH", title = "Technology Sector", target = "TICKERS", condition = SECTOR == "Technology", overwrite = TRUE ) apply_filter(tickers, "TECH", target = "TICKERS") apply_filter(list(tickers = tickers, prices = prices), "TECH")tickers <- data.frame( SYMBOL = c("AAA", "BBB", "CCC"), SECTOR = c("Technology", "Energy", "Technology"), stringsAsFactors = FALSE ) prices <- data.frame( SYMBOL = c("AAA", "BBB", "CCC"), CLOSE = c(10, 20, 30), stringsAsFactors = FALSE ) add_filter( id = "TECH", title = "Technology Sector", target = "TICKERS", condition = SECTOR == "Technology", overwrite = TRUE ) apply_filter(tickers, "TECH", target = "TICKERS") apply_filter(list(tickers = tickers, prices = prices), "TECH")
Get a Filter Definition
get_filter(id)get_filter(id)
id |
|
A list with elements title, target and condition
add_filter( id = "LARGECAP", title = "Large Cap", target = "TICKERS", condition = MARKET_CAP >= 1e10, overwrite = TRUE ) get_filter("LARGECAP") ## Filter `FOO` does not exist try(get_filter("FOO"))add_filter( id = "LARGECAP", title = "Large Cap", target = "TICKERS", condition = MARKET_CAP >= 1e10, overwrite = TRUE ) get_filter("LARGECAP") ## Filter `FOO` does not exist try(get_filter("FOO"))
Filter IDs are combined into a single string separated by underscores, so
"US_LARGECAP" requests the two filters US and LARGECAP.
get_filters(ids)get_filters(ids)
ids |
|
A named list of filter definitions
add_filter( id = "US", title = "US Listed", target = "TICKERS", condition = EXCHANGE %in% c("NYSE", "NASDAQ"), overwrite = TRUE ) get_filters("US_LARGECAP")add_filter( id = "US", title = "US Listed", target = "TICKERS", condition = EXCHANGE %in% c("NYSE", "NASDAQ"), overwrite = TRUE ) get_filters("US_LARGECAP")
List all filter definitions currently held in the registry.
list_all_filters()list_all_filters()
A data.frame with columns id, title, target and condition
list_all_filters()list_all_filters()
Load filter definitions from a yaml file. Each top level key is a filter id
and maps to the fields title, target and condition.
load_filters(yaml_file, overwrite = FALSE)load_filters(yaml_file, overwrite = FALSE)
yaml_file |
|
overwrite |
|
On success, load_filters() returns TRUE invisibly
filter_definitions <- system.file("filters_eg.yaml", package = "filters.trade") if (interactive()) file.edit(filter_definitions) load_filters(filter_definitions, overwrite = TRUE)filter_definitions <- system.file("filters_eg.yaml", package = "filters.trade") if (interactive()) file.edit(filter_definitions) load_filters(filter_definitions, overwrite = TRUE)
A master list of trading instruments listed on New Zealand's Exchange
(NZX), in the shape filters.trade expects of the master trading-code
dataset. The join key is the SYMBOL column; every other column is an
attribute that a filter condition can reference unquoted.
nz_datanz_data
A data.frame with 168 rows and 7 columns:
Trading code, e.g. "ACE.NZ". The code column.
Company or instrument name.
Exchange code; always "NZX".
Sector classification.
Market capitalisation in NZD.
Dividend yield in percent.
Average daily volume over 20 days, in shares.
SYMBOL and NAME are from
~/homepage-stock/data/nz_list.csv. EXCHANGE, SECTOR, MARKET_CAP,
DIV_YIELD and ADV_20D are synthetic placeholders generated in
data-raw/nz_data.R for example use.