Skip to content

turtle_quant_1.strategies.helpers.support_resistance.stnry_pivot_point

turtle_quant_1.strategies.helpers.support_resistance.stnry_pivot_point

Stationary pivot point support and resistance strategy.

StnryPivotPoint

StnryPivotPoint()

Bases: BaseSupResStrategy

Stationary pivot point support and resistance strategy.

This strategy calculates quarterly pivot points and their associated support (S1, S2) and resistance (R1, R2) levels based on the previous quarter's high, low, and close prices.

Formulas: - PP (Pivot Point) = (High + Low + Close) / 3 - R1 = 2 * PP - Low - R2 = PP + (High - Low) - S1 = 2 * PP - High - S2 = PP - (High - Low)

Initialize the StnryPivotPoint strategy.

Source code in src/turtle_quant_1/strategies/helpers/support_resistance/stnry_pivot_point.py
def __init__(self):
    """Initialize the StnryPivotPoint strategy."""
    super().__init__()

    self.sup_res_zone_threshold = 0.01  # Within 2% of the level

generate_historical_levels

generate_historical_levels(
    data: DataFrame, symbol: str
) -> DataFrame

Generate quarterly pivot point levels for historical data.

Parameters:

Name Type Description Default
data DataFrame

DataFrame with OHLCV data containing columns: ['datetime', 'Open', 'High', 'Low', 'Close', 'Volume']

required
symbol str

The symbol being analyzed.

required

Returns:

Type Description
DataFrame

DataFrame with pivot point levels.

DataFrame

The columns are: ['datetime', 'level_values'].

DataFrame

The 'level_values' column contains [PP, S1, S2, R1, R2] for each timestamp.

Source code in src/turtle_quant_1/strategies/helpers/support_resistance/stnry_pivot_point.py
def generate_historical_levels(
    self, data: pd.DataFrame, symbol: str
) -> pd.DataFrame:
    """Generate quarterly pivot point levels for historical data.

    Args:
        data: DataFrame with OHLCV data containing columns:
              ['datetime', 'Open', 'High', 'Low', 'Close', 'Volume']
        symbol: The symbol being analyzed.

    Returns:
        DataFrame with pivot point levels.
        The columns are: ['datetime', 'level_values'].
        The 'level_values' column contains [PP, S1, S2, R1, R2] for each timestamp.
    """
    # Ensure we have the required columns
    required_cols = ["High", "Low", "Close"]
    if not all(col in data.columns for col in required_cols):
        raise ValueError(f"Data must contain {required_cols} columns")

    # TODO: This is a magic number.
    data_resampled = DataUnitConverter.convert_to_1h_data(symbol, data)
    level_values = self._calc_sup_res_levels(data_resampled)

    # Create output DataFrame with 1-to-1 mapping to original data
    result = pd.DataFrame(
        {
            "datetime": pd.to_datetime(data_resampled["datetime"]).reset_index(
                drop=True
            ),
            "level_values": level_values,
        }
    )

    result = data[["datetime"]].merge(result, on="datetime", how="left")
    result["level_values"] = result["level_values"].bfill().ffill()

    return result