turtle_quant_1.strategies.helpers.support_resistance.base¶
turtle_quant_1.strategies.helpers.support_resistance.base
¶
Base class for support and resistance strategies.
BaseSupResStrategy
¶
Bases: ABC
Base class for support and resistance strategies.
Initialize the strategy.
Source code in src/turtle_quant_1/strategies/helpers/support_resistance/base.py
generate_historical_levels
abstractmethod
¶
Generate all support and resistance levels for every historical point for a symbol based on market data.
The calculation of each level at each timestamp must be autoregressive. This means that the calculation of a level at a timestamp must only use data up to that timestamp. This is to prevent look-ahead bias whilst allowing for vectorization for efficiency.
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']. |
DataFrame
|
The 'level_values' column contains fixed-size NumPy arrays (128 elements) with support and resistance levels. |
Source code in src/turtle_quant_1/strategies/helpers/support_resistance/base.py
generate_prediction_levels
¶
Generate all support and resistance levels for the latest point for a symbol based on market 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']. |
DataFrame
|
The 'level_values' column contains fixed-size NumPy arrays (128 elements) with support and resistance levels. |
Source code in src/turtle_quant_1/strategies/helpers/support_resistance/base.py
pivoted_levels
¶
Pivot the levels_df and collect common levels into a single row for easier visualization.
This is a helper method for strategies that generate point-wise levels for every timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
levels_df
|
DataFrame
|
DataFrame with timestamps as index and lists of support and resistance levels as columns. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with support and resistance levels. |
DataFrame
|
The columns are: ['datetime_beg', 'datetime_end', 'level_values']. |
DataFrame
|
The 'level_values' column contains the list of support and resistance levels that apply to that time range. |
Source code in src/turtle_quant_1/strategies/helpers/support_resistance/base.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 |