Skip to content

Strategy — Trading Strategy Definition

The Strategy class defines a complete trading strategy, including entry and exit conditions, position type, optional stop loss and take profit logic, and other execution parameters. It integrates with the trading engine to simulate and evaluate strategies over historical data.


Class: Strategy

Strategy(
    ticker: str,
    position_type: PositionTypeEnum,
    buy_condition: Condition,
    sell_condition: Condition,
    stop_loss: Optional[StopLoss] = None,
    take_profit: Optional[TakeProfit] = None,
    start_date: datetime = None,
    end_date: datetime = None,
    interval: Interval = Interval.ONE_DAY,
    period: Period = Period.NOT_PASSED,
    initial_capital: float = 1_000_000,
    order_size: OrderSize = Contracts(value=1),
    trade_commissions: TradeCommissions = MoneyCommissions(0.0)
)

Parameters

  • ticker: The symbol of the financial instrument to trade (e.g., "AAPL"). It is used for downloading historical data. It supports all symbols that are supported in yfinance.
  • position_type: Defines the type of position to take when trading. Supported types of trades are linked here.
  • buy_condition: The condition under which the strategy should enter a trade.
  • sell_condition: The condition under which the strategy should exit a trade. To learn more about conditions, check the conditions module.
  • stop_loss: Optional stop-loss logic for risk management. See the stop loss module for more details.
  • take_profit: Optional profit-taking logic. See the take profit module for more details.
  • start_date, end_date: Range of time over which to test the strategy (ignored if period is used).
  • interval: Data resolution. Supported intervals are linked here.
  • period: Time period on which the strategy should be tested. Supported periods are linked here. This has higher priority than start_date and end_date.
  • initial_capital: Starting balance for trade simulation.
  • order_size: Defines how much to allocate per trade. OrderSize module is defined here.
  • trade_commissions: Commission model to apply to trades. Supported commission models are linked here.

Methods

execute() -> pd.DataFrame

Executes the trading strategy by generating trades based on buy/sell conditions, order size, and available capital. Applies stop loss / take profit logic where defined. It also calculates the performance metrics of the strategy and creates graphs for visualizing the results.

Returns a DataFrame in which there are historical price data for the ticker, used technical indicators calculated by the strategy, and signals generated by the strategy.


Then there is a series of three methods that are used to get data from the strategy. Those are

get_trades() -> dict, get_graphs() -> dict, and get_statistics() -> dict

These methods return the trades, graphs, and statistics generated by the strategy execution. Each method returns a dictionary containing relevant data.


to_dict() -> dict

Lastly there is a serialization method that converts the strategy object into a dictionary format. This is used for testing and comparing strategies generated by the LLM.