9. Trading Strategy Class

This page is the autodoc entry for core Strategy classes. For design notes and tutorials, see:

  • Design: design/10-live-trading-s1.3-architecture.md

  • Strategy management: manage_strategies/*

  • Hands-on tutorials: tutorials/*

BaseStrategy

class qteasy.BaseStrategy(*, name: str = '', description: str = '', stg_type: str = 'BASE', pars: Optional[Union[Parameter, List[Parameter], Dict[str, Parameter]]] = None, data_types: Optional[Union[DataType, List[DataType], Dict[str, DataType]]] = None, use_latest_data_cycle: Union[bool, List[bool], Dict[str, bool]] = False, window_length: Union[int, List[int], Dict[str, int]] = 270, opt_tag: int = 0, par_values: Optional[Union[Tuple[Any], List[Any]]] = None)[source]

Bases: object

Abstract base class for quantitative strategies; concrete strategies inherit from it and implement signal generation logic.

A complete strategy usually has three parts: tunable parameters (pars), declared historical data (data_types, window_length, etc.), and realize() logic that turns data into signals. During runs, use get_pars() and get_data(); the Operator parses numeric signals as PT/PS/VS trade instructions. See the “Trading Strategies and BaseStrategy” docs for custom strategies and PT/PS/VS details.

Examples

BaseStrategy is abstract; implement custom strategies by subclassing. The example below shows stable type output:

>>> import qteasy as qt
>>> qt.BaseStrategy.__name__
'BaseStrategy'
get_pars(*par_names)[source]

get the value of parameter by its name or id, alias as operator.par_name multiple parameters can be got at one time

info(verbose: bool = False, status: bool = False, stg_id: Optional[str] = None, extra_info: Optional[str] = None) None[source]

Print all related information and main attributes

Parameters:
  • verbose (bool, default False) – Whether to print extra details

  • status (bool, default False) – Whether to print strategy run state

  • stg_id (str, default None) – Strategy ID; if None, print the strategy name instead

  • extra_info (str, default None) – Extra text printed after main info and before parameters and data

Return type:

None

update_par_values(*par_values: Any, **kwargs: Any) None[source]

Quickly update strategy parameter values

Parameters:
  • par_values (tuple, optional) – Parameter values as a tuple in parameter order; if parameters are not set, pass kwargs instead

  • kwargs (dict) – Update specific parameters via a dict mapping names to values

Return type:

None

GeneralStg

class qteasy.GeneralStg(name: str = 'General', description: str = 'description of General strategy', **kwargs)[source]

Bases: BaseStrategy

General strategy class: define full trading logic and signal output in realize().

For a more detailed description of the GeneralStg class, see the qteasy documentation.

FactorSorter

class qteasy.FactorSorter(name: str = 'Factor', description: str = 'description of factor sorter strategy', max_sel_count: float = 0.5, condition: str = 'any', lbound: float = -inf, ubound: float = inf, sort_ascending: bool = False, weighting: str = 'even', **kwargs)[source]

Bases: BaseStrategy

FactorSorter stock picking strategy, which determines the weighting of each stock pick based on a user-defined stock picking factor filtering and sorting (please note that the FactorSorter strategy

The generated trading signals are between 0 and 1. It is recommended to set the signal_type to “PT”)

This type of strategy requires the user to extract a stock picking factor from the historical data and determine the trading signals for the stocks in the portfolio after sorting them according to the size of the stock picking factor The user needs to compute the stock picking factor in the realize() method, and once the factor is computed, the next sorting and stock picking logic does not need to be defined by the user. The strategy will filter out the factors that meet the criteria based on the predefined conditions, sort the remaining factors, select a specific number of stocks from them, and finally assign weights or signal values based on their factor values.

RuleIterator

class qteasy.RuleIterator(name: str = 'Rule-Iterator', description: str = 'description of rule iterator strategy', allow_multi_par: bool = True, **kwargs)[source]

Bases: BaseStrategy

Rule iterator strategy: apply the same rule to every share without per-share distinction.

RuleIterator can apply different parameters per share for the same rule—for example, different MA periods per stock. See qteasy docs for more on Strategy classes.

Per-share parameters: pass update_par_values({code: (p1, p2, ...), ...}) with keys matching share_names; a positional tuple shares one runtime parameter set across shares (see RuleIterator in “Three Strategy Base Classes”). Use default for shares not listed explicitly; others is not supported.

10. No policy parameters

Parameter defines tunable strategy parameter type, range, and value semantics.

class qteasy.Parameter(par_range, *, name: str = '', par_type=None, value=None)[source]

Tunable parameter object (discrete, continuous, enum, or array) affecting strategy results.

get_value()[source]

Get the current parameter value

set_value(value)[source]

Set the current parameter value

Parameters:

value (any) – Value to set; for enums, one enum value; for discrete/continuous types, int or float

Raises:

ValueErrorValueError if value is not on the allowed axis

update_par_range(new_range)[source]

Update parameter bounds

Parameters:

new_range (int, float, str or list or tuple of int, float, str) – For conti/discr, a length-2 list/tuple of lower/upper bounds; for enum, a list/tuple of allowed values

Raises:

ValueErrorValueError if the axis type is not supported