Skip to content

contango.trading.execution.backtest.fill_behavior.rules.partial_position_rule

contango.trading.execution.backtest.fill_behavior.rules.partial_position_rule

PartialPositionRule

Bases: FillRule[RuleContext]

Rule that only allows full trades (no partial trades). This means that you must sell all of the shares you bought in a previous order every time.

validate

validate(context: RuleContext) -> bool

Returns True if there is currently a position & the desired quantity is negative, and adding the current posiiton and desired quantity correctly results in no position.

Source code in src\contango\trading\execution\backtest\fill_behavior\rules\partial_position_rule.py
16
17
18
19
20
21
22
23
24
25
26
27
def validate(self, context: RuleContext) -> bool:
    """
    Returns True if there is currently a position & the desired quantity is negative, and adding the 
    current posiiton and desired quantity correctly results in no position.
    """
    if context.desired_quantity >= 0:
        return True

    return (
        context.current_position > 0
        and context.current_position + context.desired_quantity == 0
    )