Source code#

BackTester#

class BackTester(market: DataFrame, strategy: Strategy)[source]#

Bases: object

A class for backtesting forex trading strategies.

The ForexBacktester takes a trading strategy, applies it to historical forex data, and simulates the execution of trades based on the signals generated by the strategy. It tracks the performance of these trades over time, allowing for the evaluation of the strategy’s effectiveness.

signals_to_positions() NoReturn[source]#

Transforms trading signals into positions by interpreting changes in the signals.

This method updates positions based on the following rules: - A positive change in the signal (e.g., from 0 to 1) indicates entering a long position. - A negative change in the signal (e.g., from 1 to 0 or 1 to -1) indicates exiting a long position or entering a short position. - Continuous signals (no change) imply holding the current position. - Direct reversals (e.g., from -1 to 1) are allowed and indicate switching positions.

The ‘positions’ column in the portfolio DataFrame is updated to reflect the current trading position.

backtest(capital_management: LimitedCapital | UnlimitedCapital) DataFrame[source]#

Conducts a backtest on a trading strategy using historical market data, applying either a limited or unlimited capital position strategy.

This method simulates trading operations based on the strategy’s signals within the constraints of the specified position strategy (either LimitedCapital or UnlimitedCapital). It takes into account the initial capital, transaction costs, and the capital allocation per trade, to compute the performance of the trading portfolio over time.

Parameters:

capital_management (LimitedCapital | UnlimitedCapital): An instance of a position strategy, which determines the trading behavior based on capital limitations. The strategy must be one of the following:

  • LimitedCapital: A strategy that enforces a maximum limit on the capital allocated per trade.

  • UnlimitedCapital: A strategy with no limit on the capital allocation per trade, implying the availability of unlimited capital for trading.

Returns:

pandas.DataFrame: A DataFrame that chronicles the performance of the trading portfolio over the course of the backtest. This includes columns for the date, current holdings (units), cash balance, and positions (both long and short), reflecting changes in the portfolio’s value over time.

The method initializes the portfolio with the given initial capital and iterates through market data to apply the trading strategy. Trades are executed based on the strategy’s signals, adjusting the portfolio’s positions and cash balance accordingly. The final DataFrame provides a detailed account of the portfolio’s performance, facilitating analysis of the strategy’s effectiveness.

Note:
  • The specific implementation of position management and trade execution is dependent on the position_strategy parameter, which must be provided when calling this method.

  • This method does not return additional data frames with extra backtest-related data. For detailed trade or market condition analysis, modifications to the method or strategy implementation may be required.

generate_portfolio() NoReturn[source]#
plot(**kwargs) NoReturn[source]#

Generates a visual representation of the backtest results using the PlotTrade class.

This method constructs a figure that includes plots for market data, trading signals, and portfolio metrics, providing a comprehensive overview of the trading strategy’s performance over the backtesting period.

The figure is constructed and displayed immediately, utilizing the market data, portfolio results, and the strategy instance associated with this BackTester.

Note:

The method assumes that the backtest has been run and the portfolio attribute of the BackTester class has been populated with trading data and results.