This commit is contained in:
Ichinga Samuel
2024-11-13 04:29:37 +01:00
parent 91a09a8ab9
commit 2a55f49f78
22 changed files with 3088 additions and 441 deletions
+58
View File
@@ -0,0 +1,58 @@
# Table of Contents
* [backtest\_account](#backtest_account)
* [BackTestAccount](#backtest_account.BackTestAccount)
* [get\_dict](#backtest_account.BackTestAccount.get_dict)
* [asdict](#backtest_account.BackTestAccount.asdict)
* [set\_attrs](#backtest_account.BackTestAccount.set_attrs)
<a id="backtest_account"></a>
# backtest\_account
<a id="backtest_account.BackTestAccount"></a>
## BackTestAccount Objects
```python
@dataclass
class BackTestAccount()
```
Account data for backtesting
<a id="backtest_account.BackTestAccount.get_dict"></a>
#### get\_dict
```python
def get_dict(exclude: set = None, include: set = None)
```
Returns a dictionary of the account data. Using the exclude and include arguments, you can filter the data
**Arguments**:
- `exclude` _set_ - A set of keys to exclude
- `include` _set_ - A set of keys to include
<a id="backtest_account.BackTestAccount.asdict"></a>
#### asdict
```python
def asdict()
```
Returns a dictionary of the account data
<a id="backtest_account.BackTestAccount.set_attrs"></a>
#### set\_attrs
```python
def set_attrs(**kwargs)
```
Se the attributes of the account data to the instance
@@ -0,0 +1,125 @@
# Table of Contents
* [backtest\_controller](#backtest_controller)
* [BackTestController](#backtest_controller.BackTestController)
* [backtest\_engine](#backtest_controller.BackTestController.backtest_engine)
* [add\_tasks](#backtest_controller.BackTestController.add_tasks)
* [set\_parties](#backtest_controller.BackTestController.set_parties)
* [parties](#backtest_controller.BackTestController.parties)
* [control](#backtest_controller.BackTestController.control)
* [stop\_backtesting](#backtest_controller.BackTestController.stop_backtesting)
* [wait](#backtest_controller.BackTestController.wait)
* [abort](#backtest_controller.BackTestController.abort)
<a id="backtest_controller"></a>
# backtest\_controller
<a id="backtest_controller.BackTestController"></a>
## BackTestController Objects
```python
class BackTestController()
```
The controller for the backtesting engine.
It also act's as a synchronizier for running multiple strategies (tasks) using a threading.Barrier primitive.
It handles the updating of open positions and close them when necessary.
It handles the iterator for the backtesting engine and handles it movement in time by moving it to the next time step.
**Attributes**:
- `_instance` _Self_ - The instance of the controller
- `config` _Config_ - The configuration for the backtesting engine
- `tasks` _list[Task]_ - The tasks that are being run
- `barrier` _Barrier_ - The barrier for synchronizing the tasks
<a id="backtest_controller.BackTestController.backtest_engine"></a>
#### backtest\_engine
```python
@property
def backtest_engine()
```
Returns the backtest engine
<a id="backtest_controller.BackTestController.add_tasks"></a>
#### add\_tasks
```python
def add_tasks(*tasks: Task)
```
Adds tasks to the tasks list
<a id="backtest_controller.BackTestController.set_parties"></a>
#### set\_parties
```python
def set_parties(*, parties: int)
```
Sets the number of parties for the barrier. The barrier will wait for the number of parties to reach the barrier.
This has to be done here as it can be impossible to know the eventual number of parties to set the barrier to during initialization.
**Arguments**:
- `parties` _int_ - The number of parties to set the barrier to
<a id="backtest_controller.BackTestController.parties"></a>
#### parties
```python
@property
def parties()
```
Returns the number of parties for the barrier
<a id="backtest_controller.BackTestController.control"></a>
#### control
```python
async def control()
```
The backtest controller. It controls the backtesting engine and the tasks that are being run.
It acts as a synchronizer for the tasks and the backtesting engine.
<a id="backtest_controller.BackTestController.stop_backtesting"></a>
#### stop\_backtesting
```python
def stop_backtesting()
```
Stop the backtester, and shutdown the executor
<a id="backtest_controller.BackTestController.wait"></a>
#### wait
```python
def wait()
```
Called by individual tasks to indicate completion of their cycle
<a id="backtest_controller.BackTestController.abort"></a>
#### abort
```python
def abort()
```
Aborts the barrier
File diff suppressed because it is too large Load Diff
+185
View File
@@ -0,0 +1,185 @@
# Table of Contents
* [get\_data](#get_data)
* [Cursor](#get_data.Cursor)
* [BackTestData](#get_data.BackTestData)
* [set\_attrs](#get_data.BackTestData.set_attrs)
* [fields](#get_data.BackTestData.fields)
* [GetData](#get_data.GetData)
* [\_\_init\_\_](#get_data.GetData.__init__)
* [pickle\_data](#get_data.GetData.pickle_data)
* [load\_data](#get_data.GetData.load_data)
* [save\_data](#get_data.GetData.save_data)
* [get\_data](#get_data.GetData.get_data)
<a id="get_data"></a>
# get\_data
<a id="get_data.Cursor"></a>
## Cursor Objects
```python
class Cursor(NamedTuple)
```
A cursor to iterate over the data. Marks the current position.
<a id="get_data.BackTestData"></a>
## BackTestData Objects
```python
@dataclass
class BackTestData()
```
The data class to store the backtesting data.
**Attributes**:
- `name` _str_ - The name of the backtest data.
- `terminal` _dict_ - The terminal information.
- `version` _tuple_ - The version of the terminal.
- `account` _dict_ - The account information.
- `symbols` _dict_ - The symbols information.
- `ticks` _dict_ - The ticks data.
- `rates` _dict_ - The rates data.
- `span` _range_ - The range of the data.
- `range` _range_ - The range of the data.
- `orders` _dict_ - The orders data.
- `deals` _dict_ - The deals data.
- `positions` _dict_ - The positions data.
- `open_positions` _set_ - The open positions.
- `cursor` _Cursor_ - The cursor to iterate over the data.
- `margins` _dict_ - The margins data.
- `fully_loaded` _bool_ - A flag to indicate if the data is fully loaded
<a id="get_data.BackTestData.set_attrs"></a>
#### set\_attrs
```python
def set_attrs(**kwargs)
```
Set the attributes of the class on the instance.
<a id="get_data.BackTestData.fields"></a>
#### fields
```python
@property
def fields()
```
A list of the fields of the class.
<a id="get_data.GetData"></a>
## GetData Objects
```python
class GetData()
```
A class to get the backtesting data from the MetaTrader5 terminal.
**Attributes**:
- `start` _datetime_ - The start date of the data.
- `end` _datetime_ - The end date of the data.
- `symbols` _Sequence[str]_ - The symbols to get the data for.
- `timeframes` _Sequence[TimeFrame]_ - The timeframes to get the data for.
- `name` _str_ - The name of the backtest data.
- `range` _range_ - The range of the data.
- `span` _range_ - The span of the data.
- `data` _BackTestData_ - The backtesting data.
- `mt5` _MetaTrader_ - The MetaTrader5 instance.
- `task_queue` _TaskQueue_ - The task queue to handle the requests.
<a id="get_data.GetData.__init__"></a>
#### \_\_init\_\_
```python
def __init__(*,
start: datetime,
end: datetime,
symbols: Sequence[str],
timeframes: Sequence[TimeFrame],
name: str = "")
```
Get the backtesting data from the MetaTrader5 terminal.
**Arguments**:
- `start` _datetime_ - The start date of the data.
- `end` _datetime_ - The end date of the data.
- `symbols` _Sequence[str]_ - The symbols to get the data for.
- `timeframes` _Sequence[TimeFrame]_ - The timeframes to get the data for.
- `name` _str_ - The name of the backtest data.
<a id="get_data.GetData.pickle_data"></a>
#### pickle\_data
```python
@classmethod
def pickle_data(cls, *, data: BackTestData, name: str | Path)
```
Pickle the data to a file.
**Arguments**:
- `data` _BackTestData_ - The data to pickle.
- `name` _str | Path_ - The name of the file to pickle the data to.
<a id="get_data.GetData.load_data"></a>
#### load\_data
```python
@classmethod
def load_data(cls, *, name: str | Path) -> BackTestData
```
Load the data from a file.
**Arguments**:
- `name` _str | Path_ - The name of the file to load the data from.
<a id="get_data.GetData.save_data"></a>
#### save\_data
```python
def save_data(*, name: str | Path = "")
```
Save the data to a file.
**Arguments**:
- `name` _str | Path_ - The name of the file to save the data to. If not provided, the name of the data is used.
<a id="get_data.GetData.get_data"></a>
#### get\_data
```python
async def get_data(workers: int = None)
```
Use the task queue to get the data from the MetaTrader5 terminal.
**Arguments**:
- `workers` _int_ - The number of workers to use in the task queue. If not provided, the default number of workers
is used.
+447
View File
@@ -0,0 +1,447 @@
# Table of Contents
* [trades\_manager](#trades_manager)
* [TradeManager](#trades_manager.TradeManager)
* [update](#trades_manager.TradeManager.update)
* [values](#trades_manager.TradeManager.values)
* [keys](#trades_manager.TradeManager.keys)
* [items](#trades_manager.TradeManager.items)
* [to\_dict](#trades_manager.TradeManager.to_dict)
* [PositionsManager](#trades_manager.PositionsManager)
* [\_\_init\_\_](#trades_manager.PositionsManager.__init__)
* [margin](#trades_manager.PositionsManager.margin)
* [close](#trades_manager.PositionsManager.close)
* [get\_margin](#trades_manager.PositionsManager.get_margin)
* [delete\_margin](#trades_manager.PositionsManager.delete_margin)
* [set\_margin](#trades_manager.PositionsManager.set_margin)
* [positions\_get](#trades_manager.PositionsManager.positions_get)
* [positions\_total](#trades_manager.PositionsManager.positions_total)
* [open\_positions](#trades_manager.PositionsManager.open_positions)
* [OrdersManager](#trades_manager.OrdersManager)
* [get\_orders\_range](#trades_manager.OrdersManager.get_orders_range)
* [history\_orders\_get](#trades_manager.OrdersManager.history_orders_get)
* [history\_orders\_total](#trades_manager.OrdersManager.history_orders_total)
* [DealsManager](#trades_manager.DealsManager)
* [get\_deals\_range](#trades_manager.DealsManager.get_deals_range)
* [history\_deals\_get](#trades_manager.DealsManager.history_deals_get)
* [history\_deals\_total](#trades_manager.DealsManager.history_deals_total)
<a id="trades_manager"></a>
# trades\_manager
<a id="trades_manager.TradeManager"></a>
## TradeManager Objects
```python
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
<a id="trades_manager.TradeManager.update"></a>
#### update
```python
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.
<a id="trades_manager.TradeManager.values"></a>
#### values
```python
def values() -> tuple[TradeData, ...]
```
Returns the values of the data.
<a id="trades_manager.TradeManager.keys"></a>
#### keys
```python
def keys() -> tuple[int, ...]
```
Returns the keys of the data.
<a id="trades_manager.TradeManager.items"></a>
#### items
```python
def items() -> tuple[tuple[int, TradeData], ...]
```
Returns the items of the data.
<a id="trades_manager.TradeManager.to_dict"></a>
#### to\_dict
```python
def to_dict()
```
Convert the data to a dictionary.
<a id="trades_manager.PositionsManager"></a>
## PositionsManager Objects
```python
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.
<a id="trades_manager.PositionsManager.__init__"></a>
#### \_\_init\_\_
```python
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.
<a id="trades_manager.PositionsManager.margin"></a>
#### margin
```python
@property
def margin()
```
Returns the total margin of all open positions
<a id="trades_manager.PositionsManager.close"></a>
#### close
```python
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.
<a id="trades_manager.PositionsManager.get_margin"></a>
#### get\_margin
```python
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.
<a id="trades_manager.PositionsManager.delete_margin"></a>
#### delete\_margin
```python
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.
<a id="trades_manager.PositionsManager.set_margin"></a>
#### set\_margin
```python
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
<a id="trades_manager.PositionsManager.positions_get"></a>
#### positions\_get
```python
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
<a id="trades_manager.PositionsManager.positions_total"></a>
#### positions\_total
```python
def positions_total() -> int
```
Get the total number of open positions.
**Returns**:
- `int` - The total number of open positions.
<a id="trades_manager.PositionsManager.open_positions"></a>
#### open\_positions
```python
@property
def open_positions() -> tuple[TradePosition, ...]
```
Returns the open positions.
**Arguments**:
tuple[TradePosition, ...]: The open positions.
<a id="trades_manager.OrdersManager"></a>
## OrdersManager Objects
```python
class OrdersManager(TradeManager)
```
Managers orders data during a backtest. It is a subclass of TradeManager. It manages access to the historical
orders data
<a id="trades_manager.OrdersManager.get_orders_range"></a>
#### get\_orders\_range
```python
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.
<a id="trades_manager.OrdersManager.history_orders_get"></a>
#### history\_orders\_get
```python
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.
<a id="trades_manager.OrdersManager.history_orders_total"></a>
#### history\_orders\_total
```python
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.
<a id="trades_manager.DealsManager"></a>
## DealsManager Objects
```python
class DealsManager(TradeManager)
```
<a id="trades_manager.DealsManager.get_deals_range"></a>
#### get\_deals\_range
```python
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.
<a id="trades_manager.DealsManager.history_deals_get"></a>
#### history\_deals\_get
```python
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.
<a id="trades_manager.DealsManager.history_deals_total"></a>
#### history\_deals\_total
```python
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.