-
-
Notifications
You must be signed in to change notification settings - Fork 1
Backtest
The Backtest class is responsible for simulating the execution of a trading strategy over historical data. It manages the portfolio, executes trades, and tracks performance metrics.
__init__(self, ticker: str, cash: float | int, strategy: 'Strategy', commision: float | int = 0.0, commisionType: str = "FLAT", timePeriod: str = "1mo", interval: str = "1d", startDate: datetime = datetime(2024, 1, 1), endDate: datetime = datetime(2024, 2, 1)) -> None
Initializes the backtest with the specified parameters.
Returns the nearest valid date in the historical data for the given target date.
Formats the given date to a valid timestamp in the historical data.
Calculates the commission for a trade based on the commission type and rate.
Cancels an order by its index in the pending orders list.
Submits a Good-Till-Canceled (GTC) order.
Calculates the total cost of a trade, including commission.
Advances the backtest to the next time step, executing any pending orders and calling the strategy's step method.
Runs the backtest from the start date to the end date and returns the results.
trade(self, tradeType: TradeType, numShares: int, price: float = None, duration: str = 'DAY') -> Optional[Holding]
Executes a trade of the specified type and returns the resulting holding.
Calculates the total value of the portfolio, including cash and holdings.
Returns the current position (number of shares) held in the portfolio.
Calculates the position size based on the risk per trade and stop loss level.
Applies a stop loss to the current holdings.
Applies a take profit to the current holdings.
Calculates the Value at Risk (VaR) for the portfolio.
Rebalances the portfolio according to the target allocations.