pybroker.config 模块

包含配置选项。

class StrategyConfig(initial_cash: float = 100000, fee_mode: FeeMode | Callable[[FeeInfo], Decimal] | None = None, fee_amount: float = 0, enable_fractional_shares: bool = False, round_fill_price: bool = True, position_mode: PositionMode = PositionMode.DEFAULT, max_long_positions: int | None = None, max_short_positions: int | None = None, buy_delay: int = 1, sell_delay: int = 1, bootstrap_samples: int = 10000, exit_on_last_bar: bool = False, exit_cover_fill_price: PriceType | Callable[[str, BarData], int | float | Decimal] = PriceType.MIDDLE, exit_sell_fill_price: PriceType | Callable[[str, BarData], int | float | Decimal] = PriceType.MIDDLE, bars_per_year: int | None = None, return_signals: bool = False, return_stops: bool = False, round_test_result: bool = True, leverage: float = 1.0, interest_rate: float = 0.0, record_portfolio_bars: bool = False, record_position_bars: bool = False)[源代码]

基类:object

用于 pybroker.strategy.Strategy 的配置选项。

initial_cash

策略的起始资金。

类型:

float

fee_mode

用于计算经纪商手续费的 pybroker.common.FeeMode。支持以下选项之一:

  • ORDER_PERCENT:手续费为订单金额的百分比。

  • PER_ORDER:手续费为每笔订单的固定金额。

  • PER_SHARE:手续费为订单中每股的固定金额。

  • Callable[[FeeInfo], Decimal]:手续费的计算方式为,通过一个

    接收 pybroker.common.FeeInfo 的自定义 Callable

  • None:禁用手续费(默认值)。

类型:

pybroker.common.FeeMode | Callable[[pybroker.common.FeeInfo], decimal.Decimal] | None

fee_amount

经纪商手续费金额。

类型:

float

enable_fractional_shares

是否启用碎股(fractional shares)交易。加密货币交易请设置为 True。默认为 False

类型:

bool

round_fill_price

是否将成交价格四舍五入到最接近的美分。默认为 True

类型:

bool

position_mode

用于 pybroker.strategy.Strategy 的仓位模式。支持以下选项之一:

  • DEFAULT:多头和空头仓位。

  • LONG_ONLY:仅多头仓位。

  • SHORT_ONLY:仅空头仓位。

类型:

pybroker.common.PositionMode

buy_delay

买入信号触发到下单之间等待的 K 线数量。默认值 1 表示在下一根 K 线下达买单。必须 > 0

类型:

int

sell_delay

卖出信号触发到下单之间等待的 K 线数量。默认值 1 表示在下一根 K 线下达卖单。必须 > 0

类型:

int

bootstrap_samples

用于计算自助法(bootstrap)指标的样本数量。默认为 10_000

类型:

int

exit_on_last_bar

是否在某个品种可用数据的最后一根 K 线上自动平掉所有未平仓头寸。默认为 False

类型:

bool

exit_cover_fill_price

exit_on_last_barTrue 时,用于回补未平仓空头仓位的成交价格。默认为 pybroker.common.PriceType.MIDDLE

类型:

pybroker.common.PriceType | Callable[[str, pybroker.common.BarData], int | float | decimal.Decimal]

exit_sell_fill_price

exit_on_last_barTrue 时,用于卖出未平仓多头仓位的成交价格。默认为 pybroker.common.PriceType.MIDDLE

类型:

pybroker.common.PriceType | Callable[[str, pybroker.common.BarData], int | float | decimal.Decimal]

bars_per_year

用于将评估指标年化的每年观测数量。例如,日线收益率使用 252 来年化夏普比率。该值还决定了 interest_rate 的计提周期,因此在设置 interest_rate 时必须同时设置本参数。

类型:

int | None

return_signals

True 时,K 线数据、指标数据和模型预测结果会随 pybroker.strategy.TestResult 一并返回。信号仅包含基础时间框架的值;仅当绑定中包含 'base' 时,绑定到某个区间的指标或模型才会出现在信号中。默认为 False

类型:

bool

return_stops

True 时,止损值会随 pybroker.strategy.TestResult 一并返回。默认为 False

类型:

bool

round_test_result

True 时,pybroker.strategy.TestResult 中的数值会被四舍五入到最接近的美分。默认为 True

类型:

bool

leverage

多头和空头仓位购买力所使用的账户杠杆倍数。默认值 1.0 表示仅使用现金买入。 2.0 允许持有最多 2 倍权益的仓位。必须 >= 1.0

类型:

float

interest_rate

以百分比表示的年利率,作用于净现金余额(cash - margin_loan)。净现金为负时收取利息,为正时计入利息收益。按每根 K 线 interest_rate / bars_per_year 的比例计提一次,因此设置本参数时必须同时设置 bars_per_year。默认为 0 (禁用)。

类型:

float

record_portfolio_bars

True 时,会在每根 K 线将完整的 pybroker.portfolio.PortfolioBar 快照追加到 pybroker.portfolio.Portfolio.bars。为 False (默认)时,逐 K 线指标会存储在一个紧凑缓冲区中,供 pybroker.strategy.TestResultpybroker.eval.EvalMetrics 使用。

类型:

bool

record_position_bars

True 时,会在每根 K 线将完整的 pybroker.portfolio.PositionBar 快照追加到 pybroker.portfolio.Portfolio.position_bars。为 False (默认)时,pybroker.strategy.TestResult.positions 为空。

类型:

bool