Skip to content

contango.trading.analysis.graphing.downsample

contango.trading.analysis.graphing.downsample

downsample_curve

downsample_curve(curve: tuple[tuple[int, float], ...], max_points: int = 1500) -> tuple[tuple[int, float], ...]

Reduces a curve to at most max_points points via fixed stride sampling, always keeping the final point.

Parameters:

Name Type Description Default
curve tuple[tuple[int, float], ...]

The raw (timestamp, value) points, in order.

required
max_points int

The maximum number of points to keep.

1500

Returns:

Type Description
tuple[tuple[int, float], ...]

The downsampled curve. Returned unchanged if already within max_points.

Source code in src\contango\trading\analysis\graphing\downsample.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def downsample_curve(
    curve: tuple[tuple[int, float], ...],
    max_points: int = 1500,
) -> tuple[tuple[int, float], ...]:
    """
    Reduces a  curve to at most `max_points` points via
    fixed stride sampling, always keeping the final point.

    Args:
        curve: The raw (timestamp, value) points, in order.
        max_points: The maximum number of points to keep.

    Returns:
        The downsampled curve. Returned unchanged if already within max_points.
    """
    if len(curve) <= max_points:
        return curve

    step = len(curve) / max_points
    indices = [int(i * step) for i in range(max_points)]
    if indices[-1] != len(curve) - 1:
        indices.append(len(curve) - 1)

    return tuple(curve[i] for i in indices)