Strategy

class src.boatwright.Strategy.Strategy(symbol, strategy_id=None, order_handler=None)

Strategies systematically place and manage orders with the broker. This parent class imposes the methods necessary for the rest of package to run backtests and live execution instances.

Parameters:
  • symbol (str) – symbol being to be traded by the strategy, e.g. “AAPL” or “BTC”

  • strategy_id – optional strategy ID, defaults to the class name

  • order_handler – optional order handler to be used by the strategy, defaults to None

Variables:

broker – upon the initialization of a Backtest or LiveExecution instance a Broker is attributed to the strategy and order_handler

abstractmethod calc_prerequisite_data_length()
Returns:

length of dataframe required to generate all signals in calculate_signals()

Return type:

int

abstractmethod calculate_signals(data)

takes a dataframe and returns an updated dataframe with additional columns for any technical inidcator signals required for the strategy

Parameters:

data (DataFrame) – pandas dataframe with columns [datetime, open, high, low, close, volume]

Returns:

(pd.DataFrame) dataframe with additional columns of technical indicators

plot_info()

This optional method is called by Visualization.plot_backtest() for instruction on how to plot signals. :return: (dict) dictionary of information specifying how technical indicators should be plotted.

Note

See Examples.Strategy and Examples.Backtest for an example of how the dict should be structured

set_broker(broker)

set the broker for the strategy

Parameters:

broker (Broker)

abstractmethod step(row)

actions to taken on each ‘bar’ of data in a Backtest or LiveExecution instance. e.g. check buy/sell triggers and create or check orders accordingly.

Parameters:

row – one row of the pd.Dataframe calculated by calculate_signals()

Returns:

None

Note

logic should only be applied to the passed row, and not self.dataframe. avoid accessing other rows such as self.df.iloc[i-10]; rather in ‘calculate signals’ calculate another pd.Series of the delayed data