Source code#

BackTester#

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

Bases: PORTFOLIO

plot_positions(axes: Axes, max_positions: int | float = inf) 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:
  • axes (matplotlib.axes.Axes, optional) – Axes to draw on. If None, a new figure+axes are created.

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

Extra Parameters#

showbool

Whether to display the plot after creation.

save_asstr, optional

If provided, save the figure to this path.

figure_sizetuple, optional

Size of the figure in inches.

tight_layoutbool, default=True

Whether to use tight layout for the figure.

axesmatplotlib.axes.Axes, optional

Axes to draw on. If None, a new figure+axes are created.

xscalestr, optional

X-axis scale type (e.g., ‘linear’, ‘log’).

yscalestr, optional

Y-axis scale type (e.g., ‘linear’, ‘log’).

xlimtuple, optional

X-axis limits as (min, max).

ylimtuple, optional

Y-axis limits as (min, max).

stylestr, default=plot_style

Matplotlib style to use for the plot.

**kwargs

Additional keyword arguments to pass to the plotting function.

returns:

axes – The axes with the market prices plot.

rtype:

matplotlib.axes.Axes

plot_equity(axes: Axes) None[source]#

Plot the portfolio’s equity over time.

Parameters:

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

Extra Parameters#

showbool

Whether to display the plot after creation.

save_asstr, optional

If provided, save the figure to this path.

figure_sizetuple, optional

Size of the figure in inches.

tight_layoutbool, default=True

Whether to use tight layout for the figure.

axesmatplotlib.axes.Axes, optional

Axes to draw on. If None, a new figure+axes are created.

xscalestr, optional

X-axis scale type (e.g., ‘linear’, ‘log’).

yscalestr, optional

Y-axis scale type (e.g., ‘linear’, ‘log’).

xlimtuple, optional

X-axis limits as (min, max).

ylimtuple, optional

Y-axis limits as (min, max).

stylestr, default=plot_style

Matplotlib style to use for the plot.

**kwargs

Additional keyword arguments to pass to the plotting function.

returns:

axes – The axes with the market prices plot.

rtype:

matplotlib.axes.Axes

plot_capital_at_risk(axes: Axes) None[source]#

Plot the capital at risk over time.

Parameters:

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

Extra Parameters#

showbool

Whether to display the plot after creation.

save_asstr, optional

If provided, save the figure to this path.

figure_sizetuple, optional

Size of the figure in inches.

tight_layoutbool, default=True

Whether to use tight layout for the figure.

axesmatplotlib.axes.Axes, optional

Axes to draw on. If None, a new figure+axes are created.

xscalestr, optional

X-axis scale type (e.g., ‘linear’, ‘log’).

yscalestr, optional

Y-axis scale type (e.g., ‘linear’, ‘log’).

xlimtuple, optional

X-axis limits as (min, max).

ylimtuple, optional

Y-axis limits as (min, max).

stylestr, default=plot_style

Matplotlib style to use for the plot.

**kwargs

Additional keyword arguments to pass to the plotting function.

returns:

axes – The axes with the market prices plot.

rtype:

matplotlib.axes.Axes

plot_capital(axes: Axes) None[source]#

Plot the capital over time.

Parameters:

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

Extra Parameters#

showbool

Whether to display the plot after creation.

save_asstr, optional

If provided, save the figure to this path.

figure_sizetuple, optional

Size of the figure in inches.

tight_layoutbool, default=True

Whether to use tight layout for the figure.

axesmatplotlib.axes.Axes, optional

Axes to draw on. If None, a new figure+axes are created.

xscalestr, optional

X-axis scale type (e.g., ‘linear’, ‘log’).

yscalestr, optional

Y-axis scale type (e.g., ‘linear’, ‘log’).

xlimtuple, optional

X-axis limits as (min, max).

ylimtuple, optional

Y-axis limits as (min, max).

stylestr, default=plot_style

Matplotlib style to use for the plot.

**kwargs

Additional keyword arguments to pass to the plotting function.

returns:

axes – The axes with the market prices plot.

rtype:

matplotlib.axes.Axes

plot_number_of_positions(axes: Axes) None[source]#

Plot the number of open positions over time.

Parameters:

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

Extra Parameters#

showbool

Whether to display the plot after creation.

save_asstr, optional

If provided, save the figure to this path.

figure_sizetuple, optional

Size of the figure in inches.

tight_layoutbool, default=True

Whether to use tight layout for the figure.

axesmatplotlib.axes.Axes, optional

Axes to draw on. If None, a new figure+axes are created.

xscalestr, optional

X-axis scale type (e.g., ‘linear’, ‘log’).

yscalestr, optional

Y-axis scale type (e.g., ‘linear’, ‘log’).

xlimtuple, optional

X-axis limits as (min, max).

ylimtuple, optional

Y-axis limits as (min, max).

stylestr, default=plot_style

Matplotlib style to use for the plot.

**kwargs

Additional keyword arguments to pass to the plotting function.

returns:

axes – The axes with the market prices plot.

rtype:

matplotlib.axes.Axes

plot_prices(axes: Axes) None[source]#

Plot the market bid and ask prices over time.

Parameters:

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

Extra Parameters#

showbool

Whether to display the plot after creation.

save_asstr, optional

If provided, save the figure to this path.

figure_sizetuple, optional

Size of the figure in inches.

tight_layoutbool, default=True

Whether to use tight layout for the figure.

axesmatplotlib.axes.Axes, optional

Axes to draw on. If None, a new figure+axes are created.

xscalestr, optional

X-axis scale type (e.g., ‘linear’, ‘log’).

yscalestr, optional

Y-axis scale type (e.g., ‘linear’, ‘log’).

xlimtuple, optional

X-axis limits as (min, max).

ylimtuple, optional

Y-axis limits as (min, max).

stylestr, default=plot_style

Matplotlib style to use for the plot.

**kwargs

Additional keyword arguments to pass to the plotting function.

returns:

axes – The axes with the market prices plot.

rtype:

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: SupportsInt = 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.