pybroker.scope module
Contains scopes that store data and object references used to execute a
pybroker.strategy.Strategy
.
- class ColumnScope(df: DataFrame)[source]
Bases:
object
Caches and retrieves column data queried from
pandas.DataFrame
.- Parameters:
df –
pandas.DataFrame
containing the column data.
- bar_data_from_data_columns(symbol: str, end_index: int) BarData [source]
Returns a new
pybroker.common.BarData
instance containing column data of default and custom data columns registered withStaticScope
.- Parameters:
symbol – Ticker symbol to query.
end_index – Truncates column values (exclusive). If
None
, then column values are not truncated.
- fetch(symbol: str, name: str, end_index: int | None = None) ndarray[Any, dtype[_ScalarType_co]] | None [source]
Fetches a
numpy.ndarray
of column data forsymbol
.- Parameters:
symbol – Ticker symbol to query.
name – Name of column to query.
end_index – Truncates column values (exclusive). If
None
, then column values are not truncated.
- Returns:
numpy.ndarray
of column data for every bar untilend_index
(when specified).
- fetch_dict(symbol: str, names: Iterable[str], end_index: int | None = None) dict[str, ndarray[Any, dtype[_ScalarType_co]] | None] [source]
Fetches a
dict
of column data forsymbol
.- Parameters:
symbol – Ticker symbol to query.
names – Names of columns to query.
end_index – Truncates column values (exclusive). If
None
, then column values are not truncated.
- Returns:
dict
mapping column names tonumpy.ndarray
s of column values.
- class IndicatorScope(indicator_data: Mapping[IndicatorSymbol, Series], filter_dates: Sequence[datetime64])[source]
Bases:
object
Caches and retrieves
pybroker.indicator.Indicator
data.- Parameters:
indicator_data –
Mapping
ofpybroker.common.IndicatorSymbol
pairs topandas.Series
ofpybroker.indicator.Indicator
values.filter_dates – Filters
pybroker.indicator.Indicator
data onSequence
of dates.
- fetch(symbol: str, name: str, end_index: int | None = None) ndarray[Any, dtype[float64]] [source]
Fetches
pybroker.indicator.Indicator
data.- Parameters:
symbol – Ticker symbol to query.
name – Name of
pybroker.indicator.Indicator
to query.end_index – Truncates the array of
pybroker.indicator.Indicator
data returned (exclusive). IfNone
, then indicator data is not truncated.
- Returns:
numpy.ndarray
ofpybroker.indicator.Indicator
data for every bar untilend_index
(when specified).
- class ModelInputScope(col_scope: ColumnScope, ind_scope: IndicatorScope, models: Mapping[ModelSymbol, TrainedModel])[source]
Bases:
object
Caches and retrieves model input data.
- Parameters:
col_scope –
ColumnScope
.ind_scope –
IndicatorScope
.models –
Mapping
ofpybroker.common.ModelSymbol
pairs topybroker.common.TrainedModel
s.
- fetch(symbol: str, name: str, end_index: int | None = None) DataFrame [source]
Fetches model input data.
- Parameters:
symbol – Ticker symbol to query.
name – Name of
pybroker.model.ModelSource
to query input data.end_index – Truncates the array of model input data returned (exclusive). If
None
, then model input data is not truncated.
- Returns:
numpy.ndarray
of model input data for every bar untilend_index
(when specified).
- class PendingOrder(id: int, type: Literal['buy', 'sell'], symbol: str, created: datetime64, exec_date: datetime64, shares: Decimal, limit_price: Decimal | None, fill_price: int | float | floating | Decimal | PriceType | Callable[[str, BarData], int | float | Decimal])[source]
Bases:
NamedTuple
Holds data for a pending order.
- type
Type of order, either
buy
orsell
.- Type:
Literal[‘buy’, ‘sell’]
- created
Date the order was created.
- Type:
- exec_date
Date the order will be executed.
- Type:
Number of shares to be bought or sold.
- Type:
- limit_price
Limit price to use for the order.
- Type:
decimal.Decimal | None
- fill_price
Price that the order will be filled at.
- Type:
int | float | numpy.floating | decimal.Decimal | pybroker.common.PriceType | Callable[[str, pybroker.common.BarData], int | float | decimal.Decimal]
- class PendingOrderScope[source]
Bases:
object
Stores
PendingOrder
s- add(type: Literal['buy', 'sell'], symbol: str, created: datetime64, exec_date: datetime64, shares: Decimal, limit_price: Decimal | None, fill_price: int | float | floating | Decimal | PriceType | Callable[[str, BarData], int | float | Decimal]) int [source]
Creates a
PendingOrder
.- Parameters:
type – Type of order, either
buy
orsell
.symbol – Ticker symbol of the order.
created – Date the order was created.
exec_date – Date the order will be executed.
shares – Number of shares to be bought or sold.
limit_price – Limit price to use for the order.
fill_price – Price that the order will be filled at.
- Returns:
ID of the
PendingOrder
.
- contains(order_id: int) bool [source]
Returns whether a
PendingOrder
exists withorder_id
.
- orders(symbol: str | None = None) Iterable[PendingOrder] [source]
Returns an
Iterable
ofPendingOrder
s.
- remove(order_id: int) bool [source]
Removes a
PendingOrder
withorder_id`
.
- remove_all(symbol: str | None = None)[source]
Removes all
PendingOrder
s.
- class PredictionScope(models: Mapping[ModelSymbol, TrainedModel], input_scope: ModelInputScope)[source]
Bases:
object
Caches and retrieves model predictions.
- Parameters:
models –
Mapping
ofpybroker.common.ModelSymbol
pairs topybroker.common.TrainedModel
s.input_scope –
ModelInputScope
.
- fetch(symbol: str, name: str, end_index: int | None = None) ndarray[Any, dtype[_ScalarType_co]] [source]
Fetches model predictions.
- Parameters:
symbol – Ticker symbol to query.
name – Name of
pybroker.model.ModelSource
that made the predictions.end_index – Truncates the array of predictions returned (exclusive). If
None
, then predictions are not truncated.
- Returns:
numpy.ndarray
of model predictions for every bar untilend_index
(when specified).
- class PriceScope(col_scope: ColumnScope, sym_end_index: Mapping[str, int])[source]
Bases:
object
Retrieves most recent prices.
- class StaticScope[source]
Bases:
object
A static registry of data and object references.
- logger
- data_source_cache
diskcache.Cache
that stores data retrieved frompybroker.data.DataSource
.
- data_source_cache_ns
Namespace set for
data_source_cache
.
- indicator_cache
diskcache.Cache
that storespybroker.indicator.Indicator
data.
- indicator_cache_ns
Namespace set for
indicator_cache
.
- model_cache
diskcache.Cache
that stores trained models.
- model_cache_ns
Namespace set for
model_cache
.
- default_data_cols
Default data columns in
pandas.DataFrame
retrieved from apybroker.data.DataSource
.
- custom_data_cols
User-defined data columns in
pandas.DataFrame
retrieved from apybroker.data.DataSource
.
- get_indicator(name: str)[source]
Retrieves a
pybroker.indicator.Indicator
from static scope.
- get_indicator_names(model_name: str) tuple[str] [source]
Returns a
tuple[str]
of allpybroker.indicator.Indicator
names that are registered withpybroker.model.ModelSource
havingmodel_name
.
- get_model_source(name: str)[source]
Retrieves a
pybroker.model.ModelSource
from static scope.
- has_indicator(name: str) bool [source]
Whether
pybroker.indicator.Indicator
is stored in static scope.
- has_model_source(name: str) bool [source]
Whether
pybroker.model.ModelSource
is stored in static scope.
- classmethod instance() StaticScope [source]
Returns singleton instance.
- param(name: str, value: ~typing.Any | None = <object object>) Any | None [source]
Get or set a global parameter.
- register_custom_cols(names: str | Iterable[str], *args)[source]
Registers user-defined column names.
- set_indicator(indicator)[source]
Stores
pybroker.indicator.Indicator
in static scope.
- set_model_source(source)[source]
Stores
pybroker.model.ModelSource
in static scope.
- unfreeze_data_cols()[source]
Allows additional data columns to be registered if
pybroker.scope.StaticScope.freeze_data_cols()
was called.
- param(name: str, value: ~typing.Any | None = <object object>) Any | None [source]
Get or set a global parameter.