Module engine
Classes
class Strategy-
This is the base class of strategies. To create a strategy, you should inherit from this class and implement the run method. The run method is called at each time step to compute the strategy. You can also override the init method to add attributes to the strategy. If you do so, you should also override the save and load methods to save the strategy in a different format (by default it is pickle).
Warning
You should not override the init and the call method. The init, and notice it doesn't have the underscores like init, is used to bind the strategy to the backtest object. This is done under the hood. The call method is used to dispatch the data to the run method. Overriding it might ignore some safety checks or statistics calculations.
While implementing the run method:
You SHOULD NOT change the state of the account, the portfolio or the broker with the exception of calling the methods below on the broker object. If you modify the state of one of these objects, you could encounter bugs and false backtest results. The only methods you should call on the broker object are: - broker.buy_long(…) - broker.sell_long(…) - broker.buy_short(…) - broker.sell_short(…)
Attributes
account- The account object
broker- The broker object
available_time_res- The available time resolutions
portfolio- The portfolio object (Alias of broker.portfolio)
Example
>>> class MyStrategy(Strategy): ... def run(self, data, timestep): ... for ticker in data.main.tickers: ... chart = data.main[ticker].chart ... if len(chart) > 2 and F.crossover(chart["MACD"], chart["MACD_SIGNAL"]) and chart["MACD"].iloc[-1] < 0: ... if ticker not in self.broker.portfolio.long: ... self.broker.buy_long(ticker, 500) ... if ticker in self.broker.portfolio.long and F.crossunder(chart["MACD"], chart["MACD_SIGNAL"]): ... self.broker.sell_long(ticker, 500) ...Parent
- abc.ABC
Static methods
def load(path: pathlib.PurePath)-
This method is used to load the state of a strategy from a file. If you would like to save in an other format, you can override this method and the save method.
Args:
path: The path to load the strategy from. (.pkl)
Methods
def init(self, account: Account, broker: Broker, available_time_res: List[datetime.timedelta])-
YOU SHOULD NOT OVERRIDE THIS METHOD
Args:
account: The account object
broker: The broker object
available_time_res: The available time resolutions def run(self, data: RecordsBucket, timestep: datetime.datetime)-
This method is used to compute the strategy at each time step. It is in this method that the strategy logic is implemented.
Args:
data: The data to use for the strategy
timestep: The current time step def save(self, path: pathlib.PurePath)-
This method is used to save the state of the strategy to a file. If you would like to save in another format, you can override this method and the load method.
Note
To avoid saving things twice, we set the account, broker and available_time_res to None before saving. Only the other attributes will be saved. (If any)
Args:
path: The path to save the strategy (.pkl)