pybroker.scope module

Contains scopes that store data and object references used to execute a pybroker.strategy.Strategy.

class ColumnScope(df: DataFrame)[源代码]

基类:object

Caches and retrieves column data queried from pandas.DataFrame.

参数:

df -- pandas.DataFrame containing the column data.

bar_data_from_data_columns(symbol: str, end_index: int) BarData[源代码]

Returns a new pybroker.common.BarData instance containing column data of default and custom data columns registered with StaticScope.

参数:
  • 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[tuple[int, ...], dtype[_ScalarType_co]] | None[源代码]

Fetches a numpy.ndarray of column data for symbol.

参数:
  • 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.

返回:

numpy.ndarray of column data for every bar until end_index (when specified).

fetch_dict(symbol: str, names: Iterable[str], end_index: int | None = None) dict[str, ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None][源代码]

Fetches a dict of column data for symbol.

参数:
  • 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.

返回:

dict mapping column names to numpy.ndarrays of column values.

class IndicatorScope(indicator_data: Mapping[IndicatorSymbol, Series], filter_dates: Sequence[datetime64])[源代码]

基类:object

Caches and retrieves pybroker.indicator.Indicator data.

参数:
fetch(symbol: str, name: str, end_index: int | None = None) ndarray[tuple[int, ...], dtype[float64]][源代码]

Fetches pybroker.indicator.Indicator data.

参数:
返回:

numpy.ndarray of pybroker.indicator.Indicator data for every bar until end_index (when specified).

class ModelInputScope(col_scope: ColumnScope, ind_scope: IndicatorScope, models: Mapping[ModelSymbol, TrainedModel])[源代码]

基类:object

Caches and retrieves model input data.

参数:
fetch(symbol: str, name: str, end_index: int | None = None) DataFrame[源代码]

Fetches model input data.

参数:
  • 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.

返回:

numpy.ndarray of model input data for every bar until end_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])[源代码]

基类:NamedTuple

Holds data for a pending order.

id

Unique ID.

Type:

int

type

Type of order, either buy or sell.

Type:

Literal['buy', 'sell']

symbol

Ticker symbol of the order.

Type:

str

created

Date the order was created.

Type:

numpy.datetime64

exec_date

Date the order will be executed.

Type:

numpy.datetime64

shares

Number of shares to be bought or sold.

Type:

decimal.Decimal

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[源代码]

基类:object

Stores PendingOrders

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[源代码]

Creates a PendingOrder.

参数:
  • type -- Type of order, either buy or sell.

  • 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.

返回:

ID of the PendingOrder.

contains(order_id: int) bool[源代码]

Returns whether a PendingOrder exists with order_id.

orders(symbol: str | None = None) Iterable[PendingOrder][源代码]

Returns an Iterable of PendingOrders.

remove(order_id: int) bool[源代码]

Removes a PendingOrder with order_id`.

remove_all(symbol: str | None = None)[源代码]

Removes all PendingOrders.

class PredictionScope(models: Mapping[ModelSymbol, TrainedModel], input_scope: ModelInputScope)[源代码]

基类:object

Caches and retrieves model predictions.

参数:
fetch(symbol: str, name: str, end_index: int | None = None) ndarray[tuple[int, ...], dtype[_ScalarType_co]][源代码]

Fetches model predictions.

参数:
  • 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.

返回:

numpy.ndarray of model predictions for every bar until end_index (when specified).

class PriceScope(col_scope: ColumnScope, sym_end_index: Mapping[str, int], round_fill_price: bool)[源代码]

基类:object

Retrieves most recent prices.

fetch(symbol: str, price: int | float | floating | Decimal | PriceType | Callable[[str, BarData], int | float | Decimal]) Decimal[源代码]
class StaticScope[源代码]

基类:object

A static registry of data and object references.

logger

pybroker.log.Logger

data_source_cache

diskcache.Cache that stores data retrieved from pybroker.data.DataSource.

data_source_cache_ns

Namespace set for data_source_cache.

indicator_cache

diskcache.Cache that stores pybroker.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 a pybroker.data.DataSource.

custom_data_cols

User-defined data columns in pandas.DataFrame retrieved from a pybroker.data.DataSource.

property all_data_cols: frozenset[str]

All registered data column names.

freeze_data_cols()[源代码]

Prevents additional data columns from being registered.

get_indicator(name: str)[源代码]

Retrieves a pybroker.indicator.Indicator from static scope.

get_indicator_names(model_name: str) tuple[str][源代码]

Returns a tuple[str] of all pybroker.indicator.Indicator names that are registered with pybroker.model.ModelSource having model_name.

get_model_source(name: str)[源代码]

Retrieves a pybroker.model.ModelSource from static scope.

has_indicator(name: str) bool[源代码]

Whether pybroker.indicator.Indicator is stored in static scope.

has_model_source(name: str) bool[源代码]

Whether pybroker.model.ModelSource is stored in static scope.

classmethod instance() StaticScope[源代码]

Returns singleton instance.

param(name: str, value: ~typing.Any | None = <object object>) Any | None[源代码]

Get or set a global parameter.

register_custom_cols(names: str | Iterable[str], *args)[源代码]

Registers user-defined column names.

set_indicator(indicator)[源代码]

Stores pybroker.indicator.Indicator in static scope.

set_model_source(source)[源代码]

Stores pybroker.model.ModelSource in static scope.

unfreeze_data_cols()[源代码]

Allows additional data columns to be registered if pybroker.scope.StaticScope.freeze_data_cols() was called.

unregister_custom_cols(names: str | Iterable[str], *args)[源代码]

Unregisters user-defined column names.

disable_logging()[源代码]

Disables event logging.

disable_progress_bar()[源代码]

Disables logging a progress bar.

enable_logging()[源代码]

Enables event logging.

enable_progress_bar()[源代码]

Enables logging a progress bar.

get_signals(symbols: Iterable[str], col_scope: ColumnScope, ind_scope: IndicatorScope, pred_scope: PredictionScope) dict[str, DataFrame][源代码]

Retrieves dictionary of pandas.DataFrames containing bar data, indicator data, and model predictions for each symbol.

param(name: str, value: ~typing.Any | None = <object object>) Any | None[源代码]

Get or set a global parameter.

register_columns(names: str | Iterable[str], *args)[源代码]

Registers names of user-defined data columns.

unregister_columns(names: str | Iterable[str], *args)[源代码]

Unregisters names of user-defined data columns.