Module engine

Classes

class CashControllerBase

Children of this class are used to control the money flow in and out of the account during the backtest. Each method are called at the beginning of the time period (day, week, month, year) and the user can decide to deposit or withdraw money from the account. For example, If I want to deposit 1000$ every month, I would do it in the every_month method and 1000$ would be deposited at the beginning of every month. To access the account, just use the self.account attribute. However, you must not modify the account in any way. To add or remove money from the account, make your overriden method return the amount of money added(Positive) or removed(Negative) from the account. For example, if you want to deposit 1000$ every month, you would do: override the every_month method and return 1000. If you want to withdraw 1000$ every month, you would do the same thing but return -1000 instead. If you want to do your deposit/withdrawal based on information from your portfolio or your broker, use the attributes: self.broker.portfolio or self.broker.

You also need to return a comment on the transaction that can be None. Check the example below for more information.

Again, if you use the broker, the account or the strategy, Make sure that they aren't modified in any way!

DO NOT override the 'init' method. Instead, override the init method if you need to initialize some variables.

Attributes

broker
The broker object
account
The account object
strategy
The strategy object
transactions
A list of all transactions made by the cash controller
total_deposited
The total amount of money deposited in the account by the cash controller

Example

>>> from backtest.engine import CashControllerBase
>>> # Deposit 1000$ in the account if the available cash is below 1000$ (Check every month)
>>> class MyCashController(CashControllerBase):
...     def every_month(self, timestamp: datetime) -> Tuple[float, Optional[str]]:
...         if self.account.available_cash < 1000:
...             return 1000, "Fund deposit"
...

Parent

  • abc.ABC

Methods

def every_day(self, timestamp: datetime.datetime) ‑> Tuple[float, Optional[str]]

This method is called every day during the backtest.

def every_month(self, timestamp: datetime.datetime) ‑> Tuple[float, Optional[str]]

This method is called every month during the backtest.

def every_week(self, timestamp: datetime.datetime) ‑> Tuple[float, Optional[str]]

This method is called every week during the backtest.

def every_year(self, timestamp: datetime.datetime) ‑> Tuple[float, Optional[str]]

This method is called every year during the backtest.

def init(self, account: Account, broker: Broker, strategy: Strategy)

Args:

account: The account
broker: The broker [Shoudl not be modified!!]

class SimpleCashController (every_day: float = 0.0, every_week: float = 0.0, every_month: float = 0.0, every_year: float = 0.0)

This class is a simple cash controller that will deposit ow withdraw a fixed amount of money at the beginning of every period. If a period's amount is 0 (Default), it won't deposit or withdraw funds.

Parent