turtle_quant_1.strategies.helpers.support_resistance¶
turtle_quant_1.strategies.helpers.support_resistance
¶
Support and resistance strategies.
StnryFibonacciRetrace
¶
StnryFibonacciRetrace(
peak_distance: int = 10,
peak_prominence_pct: float = 0.02,
fib_levels: List[float] = [],
)
Bases: BaseSupResStrategy
Stationary Fibonacci retracement support and resistance strategy.
This strategy identifies support and resistance levels by calculating Fibonacci retracement levels between significant swing highs and lows across the entire dataset. It finds the most significant swing points and computes the classic Fibonacci ratios: 23.6%, 38.2%, 50%, 61.8%, and 78.6%.
Initialize the StnryFibonacciRetrace strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
peak_distance
|
int
|
Minimum distance between peaks (in candlesticks). |
10
|
peak_prominence_pct
|
float
|
Minimum prominence for peaks (as fraction of price range). |
0.02
|
fib_levels
|
List[float]
|
List of Fibonacci ratios to use. Defaults to standard levels. |
[]
|
Source code in src/turtle_quant_1/strategies/helpers/support_resistance/stnry_fibonacci_retrace.py
generate_historical_levels
¶
Generate Fibonacci retracement 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 Fibonacci retracement levels. |
DataFrame
|
The columns are: ['datetime', 'level_values']. |
Source code in src/turtle_quant_1/strategies/helpers/support_resistance/stnry_fibonacci_retrace.py
StnryGaussianKde
¶
Bases: BaseSupResStrategy
Stationary Gaussian KDE support and resistance strategy.
Refer to: - https://github.com/neurotrader888/TechnicalAnalysisAutomation/blob/main/mp_support_resist.py - https://www.youtube.com/watch?v=mNWPSFOVoYA
This strategy identifies support and resistance levels by using Gaussian KDE to identify significant peaks in the market profile.
Initialize the StnryGaussianKde strategy.
Source code in src/turtle_quant_1/strategies/helpers/support_resistance/stnry_gaussian_kde.py
generate_historical_levels
¶
Generate Gaussian KDE 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 Gaussian KDE levels. |
DataFrame
|
The columns are: ['datetime', 'level_values']. |
Source code in src/turtle_quant_1/strategies/helpers/support_resistance/stnry_gaussian_kde.py
StnryLocalExtrema
¶
StnryLocalExtrema(
strong_peak_distance: int = 10,
strong_peak_prominence_pct: float = 0.01,
peak_distance: int = 5,
peak_rank_width: float = 2.0,
res_min_pivot_rank: int = 3,
)
Bases: BaseSupResStrategy
Stationary local extrema static support and resistance strategy.
Refer to https://medium.com/@itay1542/how-to-calculate-support-and-resistance-levels-using-python-a-step-by-step-guide-e94a33c6cbda.
This strategy identifies support and resistance levels by finding local extrema (peaks and troughs) in the price data and ranking them based on their frequency and proximity to other extrema.
Initialize the StnryLocalExtrema strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strong_peak_distance
|
int
|
Distance between strong peaks (in days). |
10
|
strong_peak_prominence_pct
|
float
|
Prominence for strong peaks (as fraction of price range). |
0.01
|
peak_distance
|
int
|
Distance between general peaks (in days). |
5
|
peak_rank_width
|
float
|
Width for grouping nearby peaks. |
2.0
|
res_min_pivot_rank
|
int
|
Minimum rank for resistance levels. |
3
|
Source code in src/turtle_quant_1/strategies/helpers/support_resistance/stnry_local_extrema.py
generate_historical_levels
¶
Generate all support and resistance 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 support and resistance levels. |
DataFrame
|
The columns are: ['datetime', 'level_values']. |
Source code in src/turtle_quant_1/strategies/helpers/support_resistance/stnry_local_extrema.py
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
generate_historical_levels
¶
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
SupResIndicator
¶
SupResIndicator(strategies: list[BaseSupResStrategy] = [])
Entrypoint for determining support and resistance zones for other strategies.
Initialize the SupResIndicator with a list of strategies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategies
|
list[BaseSupResStrategy]
|
List of support/resistance strategies to use for consensus. If None, will use default strategies. |
[]
|
Source code in src/turtle_quant_1/strategies/helpers/support_resistance/base.py
get_global_instance
classmethod
¶
get_global_instance() -> SupResIndicator
Get the global SupResIndicator instance.
To reduce duplicated calculations, the SupResIndicator can be instantiated as a global singleton. NOTE: As of now, doing this has little benefit as the SupResIndicator cache is also a global singleton.
Source code in src/turtle_quant_1/strategies/helpers/support_resistance/base.py
is_sup_res_zone_vecd
¶
Check if prices are in support or resistance zones for all timestamps (vectorized).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
The data to check, must contain 'High', 'Low', 'Close', 'datetime' columns. |
required |
symbol
|
str
|
The symbol being analyzed. |
required |
min_consensus
|
float
|
Minimum fraction of strategies that must agree (0.0 to 1.0). |
0.2
|
Returns:
| Type | Description |
|---|---|
Series
|
pd.Series of bool values indicating if each timestamp is in a support/resistance zone. |
Series
|
Index matches the input data index. |
Source code in src/turtle_quant_1/strategies/helpers/support_resistance/base.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
preload_global_instance_cache
classmethod
¶
Preload the global SupResIndicator instance with data for a symbol.
This reduces duplicated calculations across all SupResIndicator instances,
because the cache is a global singleton.
The calculations are done at the point of loading.
See the implementation of _load_sup_res_levels_data for more details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
The symbol to preload the data for. |
required |
data
|
DataFrame
|
The data to preload the data for. |
required |