Skip to content

contango.strategy.rule_based.rules.rule

contango.strategy.rule_based.rules.rule

Rule

Rule(when: Condition[C], then: Action[C, R])

Bases: Generic[C, R]

Executes an action upon a condition being True.

Attributes:

Name Type Description
C

The context for the rule.

R

The return type for the rule.

Initializes a Rule with a condition & action.

Source code in src\contango\strategy\rule_based\rules\rule.py
22
23
24
25
26
27
def __init__(self, when: Condition[C], then: Action[C, R]) -> None:
    """
    Initializes a `Rule` with a condition & action.
    """
    self._when = when
    self._then = then

evaluate

evaluate(context: C) -> R | None

Evaluates the status of the condition, executing & returning the output from the action if True.

Source code in src\contango\strategy\rule_based\rules\rule.py
29
30
31
32
33
34
def evaluate(self, context: C) -> R | None:
    """
    Evaluates the status of the condition, executing & returning the output from the action if True.
    """
    if self._when.evaluate(context):
        return self._then.execute(context)