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的配置选项。- 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
是否启用碎股(fractional shares)交易。加密货币交易请设置为
True。默认为False。- 类型:
- position_mode
用于
pybroker.strategy.Strategy的仓位模式。支持以下选项之一:DEFAULT:多头和空头仓位。LONG_ONLY:仅多头仓位。SHORT_ONLY:仅空头仓位。
- exit_cover_fill_price
当
exit_on_last_bar为True时,用于回补未平仓空头仓位的成交价格。默认为pybroker.common.PriceType.MIDDLE。- 类型:
pybroker.common.PriceType | Callable[[str, pybroker.common.BarData], int | float | decimal.Decimal]
- exit_sell_fill_price
当
exit_on_last_bar为True时,用于卖出未平仓多头仓位的成交价格。默认为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。- 类型:
- return_stops
为
True时,止损值会随pybroker.strategy.TestResult一并返回。默认为False。- 类型:
- round_test_result
为
True时,pybroker.strategy.TestResult中的数值会被四舍五入到最接近的美分。默认为True。- 类型:
- interest_rate
以百分比表示的年利率,作用于净现金余额(
cash - margin_loan)。净现金为负时收取利息,为正时计入利息收益。按每根 K 线interest_rate / bars_per_year的比例计提一次,因此设置本参数时必须同时设置bars_per_year。默认为0(禁用)。- 类型:
- record_portfolio_bars
为
True时,会在每根 K 线将完整的pybroker.portfolio.PortfolioBar快照追加到pybroker.portfolio.Portfolio.bars。为False(默认)时,逐 K 线指标会存储在一个紧凑缓冲区中,供pybroker.strategy.TestResult和pybroker.eval.EvalMetrics使用。- 类型:
- record_position_bars
为
True时,会在每根 K 线将完整的pybroker.portfolio.PositionBar快照追加到pybroker.portfolio.Portfolio.position_bars。为False(默认)时,pybroker.strategy.TestResult.positions为空。- 类型: