API Reference
Lightweight library for backtesting factor strategies.
pqr.picking
Implemented classic factor strategies and pretransforms for them.
- pqr.picking.filter(factor, *, universe)
Filters factor values based on given universe.
Actually, replaces factor values with nans, where universe is equal to False.
- Parameters
factor (
pd.DataFrame
) – Matrix with factor values.universe (
pd.DataFrame
) – Matrix with True/False, indicating to include factor values or not.
- Returns
Filtered factor values.
- Return type
pd.DataFrame
- pqr.picking.look_back(factor, *, period, agg)
Aggregates factor values column-wise each period by agg.
If agg is not predefined can work very slow. In this case the best decision is to write your own effective realisation of transformation function.
- Parameters
factor (
pd.DataFrame
) – Matrix with factor values.period (
int
) – Period to look back on the data.agg (
{"pct", "mean", "median", "min", "max"}
orcallable
) – Aggregation func to apply on factor values. If callable is given function must be appliable on pd.Series and return float.
- Returns
Aggregated factor values.
- Return type
pd.DataFrame
- pqr.picking.lag(factor, *, period)
Lags factor values for period.
Can be used both forward and backward.
- Parameters
factor (
pd.DataFrame
) – Matrix with factor values.period (
int
) – Period to look back on the data.
- Returns
Lagged factor values.
- Return type
pd.DataFrame
- pqr.picking.hold(factor, *, period)
Spread factor values for period.
Can be used to react on new information every period timestamps.
- Parameters
factor (
pd.DataFrame
) – Matrix with factor values.period (
int
) – Period to look back on the data.
- Returns
Rolling mean of factor values.
- Return type
pd.DataFrame
- pqr.picking.quantiles(factor, *, min_q, max_q)
Picks when factor values are between min_q and max_q quantiles in a period.
- Parameters
factor (
pd.DataFrame
) – Matrix with factor values.min_q (
float
) – Quantile to estimate lower boarder of factor values to pick.max_q (
float
) – Quantile to estimate upper boarder of factor values to pick.
- Returns
Matrix of True/False, indicating whether factor values are between quantile boarders or not.
- Return type
pd.DataFrame
- pqr.picking.top(factor, *, k)
Picks when factor values are at the top k in a period.
- Parameters
factor (
pd.DataFrame
) – Matrix with factor values.k (
int
) – Place to estimate lower boarder of factor values to pick.
- Returns
Matrix of True/False, indicating whether factor values are in the top or not.
- Return type
pd.DataFrame
- pqr.picking.bottom(factor, *, k)
Picks when factor values are in the bottom k in a period.
- Parameters
factor (
pd.DataFrame
) – Matrix with factor values.k (
int
) – Place to estimate upper boarder of factor values to pick.
- Returns
Matrix of True/False, indicating whether factor values are in the bottom or not.
- Return type
pd.DataFrame
- pqr.picking.thresholds(factor, *, min_t, max_t)
Picks factor values between min_t and max_t thresholds.
- Parameters
factor (
pd.DataFrame
) – Matrix with factor values.min_t (
float
) – Lower boarder of factor values to pick.max_t (
float
) – Upper boarder of factor values to pick.
- Returns
Matrix of True/False, indicating whether factor values are between thresholds or not.
- Return type
pd.DataFrame
pqr.allocation
Basic operations to get weighted positions (holdings) from strategy signals.
- pqr.allocation.allocate(signals, *, weights)
Calculates portfolio holdings.
Weighted signals are normalized to 1.
- Parameters
signals (
pd.DataFrame
) – Matrix, consists of True/False, indicating presence of an asset in a portfolio.weights (
pd.DataFrame
) – Matrix with weights (e.g. market capitalization).
- Returns
Matrix of holdings, each row sum equals to 1.
- Return type
pd.DataFrame
- pqr.allocation.ew(signals)
Calculates equally-weighted holdings.
- Parameters
signals (
pd.DataFrame
) – Matrix, consists of True/False, indicating presence of an asset in a portfolio.- Returns
Matrix of holdings, each row sum equals to 1 and all non-zero row values are the same.
- Return type
pd.DataFrame
- pqr.allocation.scale(holdings, *, leverage)
Calculates leveraged portfolio holdings.
- Parameters
holdings (
pd.DataFrame
) – Matrix of weighted positions.leverage (
pd.DataFrame
) – Series with leverage value in each period.
- Returns
Leveraged holdings.
- Return type
pd.DataFrame
- pqr.allocation.limit(holdings, *, min_leverage, max_leverage)
Clips portfolio leverage by min and max allowed leverage in a period.
- Parameters
holdings (
pd.DataFrame
) – Matrix of weighted positions.min_leverage (
float
) – Minimum allowed total leverage in a period.max_leverage (
float
) – Maximum allowed total leverage in a period.
- Returns
Matrix with scaled weights.
- Return type
pd.DataFrame
pqr.evaluation
Functionality to estimate performance of a strategy.
- pqr.evaluation.evaluate(holdings, *, universe_returns)
Calculates portfolio returns.
- Parameters
holdings (
pd.DataFrame
) – Weights of a portfolio.universe_returns (
pd.DataFrame
) – Returns of universe, available to trade for a strategy.
- Returns
Periodic returns of a portfolio.
- Return type
pd.Series
- pqr.evaluation.to_returns(prices)
Calculates universe returns from given prices of assets universe.
1st period returns are set to zero. All incorrect values (nans and infs) are converted to zero.
- Parameters
prices (
pd.DataFrame
) – Matrix with close prices of traded assets and nans for missing assets (e.g. already delisted)- Returns
Returns of assets universe.
- Return type
pd.DataFrame
pqr.utils
Some additional stuff and “sugar” for creating pipelines.
- pqr.utils.align(*args)
Aligns dataframes and series to ake them having the same index and columns.
- Parameters
args (
sequence
ofpd.DataFrame
orpd.Series
) – Dataframes and series to be aligned.- Returns
Aligned dataframes and series.
- Return type
tuple
ofpd.DataFrame
orpd.Series
- pqr.utils.compose(*steps)
Combines functions to pipeline.
- Parameters
steps (
sequence
ofcallable
) – Steps to be composed into pipeline.- Returns
Function, realizing full pipeline.
- Return type
callable
- pqr.utils.freeze
alias of
functools.partial