Skip to content

Strategy

Ben Bell edited this page Nov 15, 2024 · 1 revision

Welcome to the pyBacktest wiki!

Strategy Class Documentation

The Strategy class is an abstract base class for implementing trading strategies in the backtesting framework. It provides a structure for defining the setup and step logic for a strategy, as well as utility methods for managing positions and applying risk management.

Methods

__init__(self) -> None

Initializes the strategy with default values.

initialize(self, backtest: 'Backtest') -> None

Initializes the strategy with the backtest instance and sets up the historical data.

setup(self) -> None

A method to be overridden by subclasses to set up the strategy-specific parameters.

next(self, row: pd.Series) -> None

Deprecated method for processing the next row of data. Use step() instead.

step(self, row: pd.Series) -> None

Abstract method to be implemented by subclasses to define the strategy logic for each step.

get_position(self) -> int

Returns the current position (number of shares) held by the strategy.

get_market_state(self) -> Dict[str, Any]

Returns the current market state, including cash, position, and total value.

applyRiskManagement(self, stop_loss: float, take_profit: float)

Applies risk management by setting stop loss and take profit levels.

rebalance(self, target_allocations: Dict[str, float])

Rebalances the portfolio according to the target allocations.