Skip to content

trading.indicators.calculations.true_range

trading.indicators.calculations.true_range

TrueRange

TrueRange()

Bases: Indicator[float]

The volatility during a single trading period.

Initializes TrueRange.

Source code in src\contango\trading\indicators\calculations\true_range.py
29
30
31
32
33
def __init__(self) -> None:
    """
    Initializes `TrueRange`.
    """
    self._previous_close: USD | None = None

update

update(high: USD, low: USD, close: USD) -> float

Updates the state of the true range for every bar.

Parameters:

Name Type Description Default
high USD

The highest price reached in USD for a bar.

required
low USD

The lowest price reached in USD for a bar.

required
close USD

The closing price in USD for a bar.

required

Returns:

Name Type Description
float float

The true range for the bar.

Source code in src\contango\trading\indicators\calculations\true_range.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def update(self, high: USD, low: USD, close: USD) -> float:
    """
    Updates the state of the true range for every bar.

    Args:
        high: The highest price reached in USD for a bar.
        low: The lowest price reached in USD for a bar.
        close: The closing price in USD for a bar.

    Returns:
        float: The true range for the bar.
    """
    if self._previous_close is None:
        tr = high - low
    else:
        tr = max(
            high - low,
            abs(high - self._previous_close),
            abs(low - self._previous_close),
        )

    self._previous_close = close
    return tr