Source code#

BackTester#

class Portfolio(position_collection, debug_mode: bool = False)[source]#

Bases: PORTFOLIO

plot_positions(figure_size: Tuple[int, int] = (12, 4), max_positions: int | float = inf, show: bool = True) Figure[source]#

Plot market bid/ask prices and shade closed positions, using the mps style, consistent naming of ‘position’, and a clear legend with distinct colors.

Parameters:
  • figsize (tuple[int, int], default=(12,4)) – Size of the figure in inches.

  • max_positions (int or float, default=np.inf) – Maximum number of positions to draw (in chronological order).

  • show (bool, default=True) – Whether to display the plot after creation.

  • save_as (str, optional) – If provided, save the figure to this path.

Returns:

plt.Figure

Return type:

Figure and Axes objects for further customization or saving.

plot_equity(ax: Axes) Axes[source]#

Plot the portfolio’s equity over time.

Parameters:
  • show (bool, default=True) – Show the plot after creation.

  • figure_size (tuple, default=(12, 4)) – Size of the figure in inches.

  • ax (matplotlib.axes.Axes, optional) – Axes to draw on. If None, a new figure+axes are created.

  • save_as (str, optional) – If provided, save the figure to this path.

Returns:

ax – The axes with the equity plot.

Return type:

matplotlib.axes.Axes

plot_capital_at_risk(ax: Axes) Axes[source]#

Plot the capital at risk over time.

Parameters:
  • show (bool, default=True) – Show the plot after creation.

  • figure_size (tuple, default=(12, 4)) – Size of the figure in inches.

  • ax (matplotlib.axes.Axes, optional) – Axes to draw on. If None, a new figure+axes are created.

  • save_as (str, optional) – If provided, save the figure to this path.

Returns:

ax – The axes with the capital at risk plot.

Return type:

matplotlib.axes.Axes

plot_capital(ax: Axes) Axes[source]#

Plot the capital over time.

Parameters:
  • show (bool, default=True) – Show the plot after creation.

  • figure_size (tuple, default=(12, 4)) – Size of the figure in inches.

  • ax (matplotlib.axes.Axes, optional) – Axes to draw on. If None, a new figure+axes are created.

  • save_as (str, optional) – If provided, save the figure to this path.

Returns:

ax – The axes with the capital plot.

Return type:

matplotlib.axes.Axes

plot_number_of_positions(ax: Axes | None = None) Axes[source]#

Plot the number of open positions over time.

Parameters:
  • show (bool, default=True) – Show the plot after creation.

  • figure_size (tuple, default=(12, 4)) – Size of the figure in inches.

  • ax (matplotlib.axes.Axes, optional) – Axes to draw on. If None, a new figure+axes are created.

  • save_as (str, optional) – If provided, save the figure to this path.

Returns:

ax – The axes with the number of open positions plot.

Return type:

matplotlib.axes.Axes

plot_prices(ax: Axes | None = None) Axes[source]#

Plot the market bid and ask prices over time.

Parameters:
  • show (bool, default=True) – Show the plot after creation.

  • figure_size (tuple, default=(12, 4)) – Size of the figure in inches.

  • ax (matplotlib.axes.Axes, optional) – Axes to draw on. If None, a new figure+axes are created.

  • save_as (str, optional) – If provided, save the figure to this path.

Returns:

ax – The axes with the market prices plot.

Return type:

matplotlib.axes.Axes

plot(*plot_type) Figure[source]#

Plot the portfolio’s performance, including equity, capital at risk, capital, number of open positions, and market prices.

Return type:

None

property debug_mode#

Enable or disable debug output for development purposes.

display(self: TradeTide.binary.interface_portfolio.PORTFOLIO) None#

Total time elapsed in the simulation, as a timedelta.

get_metrics(self: TradeTide.binary.interface_portfolio.PORTFOLIO) TradeTide.binary.interface_portfolio.Metrics#

Access the Metrics object containing performance metrics.

get_positions(self: TradeTide.binary.interface_portfolio.PORTFOLIO, count: int = 18446744073709551615) list[TradeTide.binary.interface_position.BasePosition]#

Get the list of activated positions up to an optional maximum.

Parameters:

count (int, optional) – Maximum number of positions to return (default = all)

Returns:

Positions selected by the portfolio during simulation.

Return type:

List[BasePosition]

property market#

Access the Market object backing the portfolio.

property record#

Access the Record object containing historical state data.

simulate(self: TradeTide.binary.interface_portfolio.PORTFOLIO, capital_management: TradeTide.binary.interface_capital_management.BaseCapitalManagement) None#

Run the full simulation, opening/closing trades according to constraints.

property state#

Access the current state of the portfolio, including equity and position count.