Files
aiomql/docs/core/backtesting/trades_manager.md
T
Ichinga Samuel 2a55f49f78 v4
2024-11-13 04:29:37 +01:00

11 KiB

Table of Contents

trades_manager

TradeManager Objects

class TradeManager(Generic[TradeData])

A generic class to manage trades data during a backtest. It is the parent class of the PositionsManager, OrdersManager, and DealsManager. It implements some dict-like methods to manage the data. It has a private attribute _data to store the data. It exposes the data through the values, keys, and items methods. It also has a to_dict method to convert the data to a dictionary.

Attributes:

  • _data dict[int, TradeData] - The data to store the trades.

Examples:

manager = TradeManager() manager[123456] = TradePosition(ticket=123456, symbol="EURUSD", volume=0.1) manager.update(ticket=123456, symbol="EURUSD", volume=0.1) manager[123456] TradePosition(ticket=123456, symbol='EURUSD', volume=0.1) manager.values() (TradePosition(ticket=123456, symbol='EURUSD', volume=0.1),) manager.keys() (123456,) manager.items() ((123456, TradePosition(ticket=123456, symbol='EURUSD', volume=0.1)),) manager.to_dict()

  • {123456 - {'ticket': 123456, 'symbol': 'EURUSD', 'volume': 0.1}}

    pos = manager.get(123456) pos TradePosition(ticket=123456, symbol='EURUSD', volume=0.1) pos in manager True len(manager) 1 pos in manager False

update

def update(*, ticket: int, **kwargs)

Update the data of a trade. Given the ticket of the trade and the new data to update.

Arguments:

  • ticket int - The ticket of the trade to update.
  • **kwargs - The new data to update.

values

def values() -> tuple[TradeData, ...]

Returns the values of the data.

keys

def keys() -> tuple[int, ...]

Returns the keys of the data.

items

def items() -> tuple[tuple[int, TradeData], ...]

Returns the items of the data.

to_dict

def to_dict()

Convert the data to a dictionary.

PositionsManager Objects

class PositionsManager(TradeManager)

A class to manage the open positions during a backtest. It is a subclass of TradeManager. It has an additional attribute _open_positions to store the open positions. It also has a margins attribute to store the margins of the open positions. It overrides some methods of the TradeManager class to manage the open positions.

Attributes:

  • _open_positions set[int] - The open positions.
  • margins dict[int, float] - The margins of the open positions.

__init__

def __init__(*,
             data: dict = None,
             open_positions: set[int] = None,
             margins: dict = None)

Positions manager manages the open positions during a backtest. It is a subclass of TradeManager. It has an additional attribute _open_positions to store the open positions. It also has a margins attribute to store the margins of the open positions. It overrides some methods of the TradeManager class to manage the open positions.

Arguments:

  • data dict, optional - The data to store the trades. This used for continuation of the backtesting, if it was stopped with some open positions.

  • open_positions set, optional - The open positions. Defaults to None.

  • margins dict, optional - The margins of the open positions. Defaults to None.

margin

@property
def margin()

Returns the total margin of all open positions

close

def close(*, ticket: int) -> bool

Close a position. Given the ticket of the position to close.

Arguments:

  • ticket int - The ticket of the position to close.

get_margin

def get_margin(*, ticket: int) -> float

Get the margin of a position. Given the ticket of the position.

Arguments:

  • ticket int - The ticket of the position.

Returns:

  • float - The margin of the position.

delete_margin

def delete_margin(*, ticket: int)

Delete the margin of a position. Given the ticket of the position.

Arguments:

  • ticket int - The ticket of the position.

set_margin

def set_margin(*, ticket: int, margin: float)

Set the margin of a position. Given the ticket of the position and the margin.

Arguments:

  • ticket int - The ticket of the position.
  • margin float - The margin of the position

positions_get

def positions_get(*,
                  ticket: int = None,
                  symbol: str = None,
                  group: None = None) -> tuple[TradePosition, ...]

Get positions. Given the ticket, symbol, or group of the positions.

Arguments:

  • ticket int - The ticket of the position.
  • symbol str - The symbol of the position.
  • group str - The group

Returns:

tuple[TradePosition, ...]: The positions

positions_total

def positions_total() -> int

Get the total number of open positions.

Returns:

  • int - The total number of open positions.

open_positions

@property
def open_positions() -> tuple[TradePosition, ...]

Returns the open positions.

Arguments:

tuple[TradePosition, ...]: The open positions.

OrdersManager Objects

class OrdersManager(TradeManager)

Managers orders data during a backtest. It is a subclass of TradeManager. It manages access to the historical orders data

get_orders_range

def get_orders_range(*, date_from: float,
                     date_to: float) -> tuple[TradeData, ...]

Get orders within a date range. Given the start and end date of the range.

Arguments:

  • date_from float - The start date of the range.
  • date_to float - The end date of the range.

Returns:

tuple[TradeData, ...]: The orders within the date range.

history_orders_get

def history_orders_get(*,
                       date_from: float | datetime = None,
                       date_to: float | datetime = None,
                       group: str = "",
                       ticket: int = None,
                       position: int = None) -> tuple[TradeOrder, ...]

Get historical orders. Given the start and end date of the range, the group, ticket, or position of the orders.

Arguments:

  • date_from float, datetime - The start date of the range.
  • date_to float, datetime - The end date of the range.
  • group str - The group of the orders.
  • ticket int - The ticket of the order.
  • position int - The position of the order.

Returns:

tuple[TradeOrder, ...]: The historical orders.

history_orders_total

def history_orders_total(*, date_from: datetime | float,
                         date_to: datetime | float) -> int

Get the total number of historical orders. Given the start and end date of the range.

Arguments:

  • date_from datetime, float - The start date of the range.
  • date_to datetime, float - The end date of the range.

DealsManager Objects

class DealsManager(TradeManager)

get_deals_range

def get_deals_range(*, date_from: float,
                    date_to: float) -> tuple[TradeData, ...]

Get deals within a date range. Given the start and end date of the range.

Arguments:

  • date_from float - The start date of the range.
  • date_to float - The end date of the range.

Returns:

tuple[TradeData, ...]: The deals within the date range.

history_deals_get

def history_deals_get(*,
                      date_from: float | datetime = None,
                      date_to: float | datetime = None,
                      group: str = "",
                      ticket: int = None,
                      position: int = None) -> tuple[TradeDeal, ...]

History deals get. Given the start and end date of the range, the group, ticket, or position of the deals.

Arguments:

  • date_from float, datetime - The start date of the range.
  • date_to float, datetime - The end date of the range.
  • group str - The group of the deals.
  • ticket int - The ticket of the deal.
  • position int - The position of the deal.

history_deals_total

def history_deals_total(*, date_from: datetime | float,
                        date_to: datetime | float) -> int

Get the total number of historical deals. Given the start and end date of the range

Arguments:

  • date_from datetime, float - The start date of the range.
  • date_to datetime, float - The end date of the range.

Returns:

  • int - The total number of historical deals.