pybroker.common module
Contains common classes and utilities.
- class BarData(date: ndarray[tuple[int, ...], dtype[datetime64]], open: ndarray[tuple[int, ...], dtype[float64]], high: ndarray[tuple[int, ...], dtype[float64]], low: ndarray[tuple[int, ...], dtype[float64]], close: ndarray[tuple[int, ...], dtype[float64]], volume: ndarray[tuple[int, ...], dtype[float64]] | None, vwap: ndarray[tuple[int, ...], dtype[float64]] | None, **kwargs)[source]
Bases:
objectContains data for a series of bars. Each field is a
numpy.ndarraythat contains bar values in the series. The values are sorted in ascending chronological order.- Parameters:
date – Timestamps of each bar.
open – Open prices.
high – High prices.
low – Low prices.
close – Close prices.
volume – Trading volumes.
vwap – Volume-weighted average prices (VWAP).
**kwargs – Custom data fields.
- class DataCol(value)[source]
Bases:
EnumDefault data column names.
- CLOSE = 'close'
- DATE = 'date'
- HIGH = 'high'
- LOW = 'low'
- OPEN = 'open'
- SYMBOL = 'symbol'
- VOLUME = 'volume'
- VWAP = 'vwap'
- class Day(value)[source]
Bases:
EnumEnumeration of days.
- FRI = 4
- MON = 0
- SAT = 5
- SUN = 6
- THURS = 3
- TUES = 1
- WEDS = 2
- class FeeInfo(symbol: str, shares: Decimal, fill_price: Decimal, order_type: Literal['buy', 'sell'])[source]
Bases:
NamedTupleContains info for custom fee calculations.
Number of shares in order.
- Type:
- fill_price
Fill price of order.
- Type:
- order_type
Type of order, either “buy” or “sell”.
- Type:
Literal[‘buy’, ‘sell’]
- class FeeMode(value)[source]
Bases:
EnumBrokerage fee mode to use for backtesting.
- ORDER_PERCENT
Fee is a percentage of order amount, where order amount is fill_price * shares.
- PER_ORDER
Fee is a constant amount per order.
- PER_SHARE
Fee is a constant amount per share in order.
- ORDER_PERCENT = 'order_percent'
- PER_ORDER = 'per_order'
- PER_SHARE = 'per_share'
- class IndicatorSymbol(ind_name: str, symbol: str)[source]
Bases:
NamedTuplepybroker.indicator.Indicator/symbol identifier.
- class ModelSymbol(model_name: str, symbol: str)[source]
Bases:
NamedTuplepybroker.model.ModelSource/symbol identifier.
- class PositionMode(value)[source]
Bases:
EnumPosition mode for backtesting.
- DEFAULT
Long and short positions.
- LONG_ONLY
Long-only positions.
- SHORT_ONLY
Short-only positions.
- DEFAULT = 'default'
- LONG_ONLY = 'long_only'
- SHORT_ONLY = 'short_only'
- class PriceType(value)[source]
Bases:
EnumEnumeration of price types used to specify fill price with
pybroker.context.ExecContext.- OPEN
Open price of the current bar.
- LOW
Low price of the current bar.
- HIGH
High price of the current bar.
- CLOSE
Close price of the current bar.
- MIDDLE
Midpoint between low price and high price of the current bar.
- AVERAGE
Average of open, low, high, and close prices of the current bar.
- AVERAGE = 'average'
- CLOSE = 'close'
- HIGH = 'high'
- LOW = 'low'
- MIDDLE = 'middle'
- OPEN = 'open'
- class StopType(value)[source]
Bases:
EnumStop types.
- BAR
Stop that triggers after n bars.
- LOSS
Stop loss.
- PROFIT
Take profit.
- TRAILING
Trailing stop loss.
- BAR = 'bar'
- LOSS = 'loss'
- PROFIT = 'profit'
- TRAILING = 'trailing'
- class TrainedModel(name: str, instance: Any, predict_fn: Callable[[Any, DataFrame], ndarray[tuple[int, ...], dtype[_ScalarType_co]]] | None, input_cols: tuple[str] | None)[source]
Bases:
NamedTupleTrained model/symbol identifier.
- instance
Trained model instance.
- Type:
Any
- predict_fn
Callablethat overrides calling the model’s defaultpredictfunction.- Type:
Callable[[Any, pandas.core.frame.DataFrame], numpy.ndarray[tuple[int, …], numpy.dtype[numpy._typing._array_like._ScalarType_co]]] | None
- input_cols
Names of the columns to be used as input for the model when making predictions.
- default_parallel() Parallel[source]
Returns a
joblib.Parallelinstance withn_jobsequal to the number of CPUs on the host machine.
- get_unique_sorted_dates(col: Series) Sequence[datetime64][source]
Returns sorted unique values from a DataFrame column of dates. Guarantees compatability between Pandas 1 and 2.
- parse_timeframe(timeframe: str) list[tuple[int, str]][source]
Parses timeframe string with the following units:
"s"/"sec": seconds"m"/"min": minutes"h"/"hour": hours"d"/"day": days"w"/"week": weeks
An example timeframe string is
1h 30m.- Returns:
listoftuple[int, str], where each tuple contains anintvalue andstrunit of one of the following:sec,min,hour,day,week.
- quantize(df: DataFrame, col: str, round: bool) Series[source]
Quantizes a
pandas.DataFramecolumn by rounding values to the nearest cent.- Returns:
The quantized column converted to
floatvalues.
- to_datetime(date: str | datetime | datetime64 | Timestamp) datetime[source]
Converts
datetodatetime.
- to_seconds(timeframe: str | None) int[source]
Converts a timeframe string to seconds, where
timeframesupports the following units:"s"/"sec": seconds"m"/"min": minutes"h"/"hour": hours"d"/"day": days"w"/"week": weeks
An example timeframe string is
1h 30m.- Returns:
The converted number of seconds.
- verify_data_source_columns(df: DataFrame)[source]
Verifies that a
pandas.DataFramecontains all of the columns required by apybroker.data.DataSource.