turtle_quant_1.strategies.engine¶
turtle_quant_1.strategies.engine
¶
Strategy engine for aggregating multiple strategies and generating trading signals.
WeightedMeanStrategyEngine
¶
WeightedMeanStrategyEngine(
strategies: List[BaseStrategy],
weights: Dict[str, float] = {},
buy_unit_threshold: float = 0.3,
sell_threshold: float = -0.3,
n_workers: int = MAX_WORKERS,
)
Bases: BaseStrategyEngine
Strategy engine that aggregates multiple strategies to generate trading signals.
The engine combines multiple strategy scores using weighted averages and converts the final aggregated score to actionable trading signals (BUY, HOLD, SELL).
This implementation uses instance-level multiprocessing for thread-safety and proper resource management in multi-process environments.
Initialize the strategy engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategies
|
List[BaseStrategy]
|
List of strategies to aggregate. |
required |
weights
|
Dict[str, float]
|
Optional dict mapping strategy class names to weights. If empty, equal weights are used. |
{}
|
buy_unit_threshold
|
float
|
Minimum aggregated score to generate a BUY signal (default: 0.3). |
0.3
|
sell_threshold
|
float
|
Maximum aggregated score to generate a SELL signal (default: -0.3). |
-0.3
|
n_workers
|
int
|
Maximum number of worker processes for parallel execution (default: MAX_WORKERS). |
MAX_WORKERS
|
Source code in src/turtle_quant_1/strategies/engine.py
aggregate_scores
¶
Aggregate scores from all strategies using weighted average with parallel processing.
This method uses instance-level multiprocessing for better CPU utilization and process safety when multiple WeightedMeanStrategyEngine instances are used simultaneously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame with OHLCV data. |
required |
symbol
|
str
|
The symbol being analyzed. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Aggregated score between -1.0 and +1.0. |
Source code in src/turtle_quant_1/strategies/engine.py
calc_stop_loss_value
¶
Calculate the stop loss value for a given symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame with OHLCV data. |
required |
symbol
|
str
|
The symbol being analyzed. |
required |
Source code in src/turtle_quant_1/strategies/engine.py
calc_take_profit_value
¶
Calculate the take profit value for a given symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame with OHLCV data. |
required |
symbol
|
str
|
The symbol being analyzed. |
required |
Source code in src/turtle_quant_1/strategies/engine.py
from_config
classmethod
¶
from_config(
config: Dict[str, Any],
) -> WeightedMeanStrategyEngine
Create a strategy engine from a configuration dictionary.
Source code in src/turtle_quant_1/strategies/engine.py
generate_signal
¶
generate_signal(data: DataFrame, symbol: str) -> Signal
Generate a trading signal by aggregating all strategies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame with OHLCV data. |
required |
symbol
|
str
|
The symbol being analyzed. |
required |
Returns:
| Type | Description |
|---|---|
Signal
|
Signal object containing action, score, and contributing strategies. |
Source code in src/turtle_quant_1/strategies/engine.py
get_breakdown
¶
Get individual strategy scores breakdown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame with OHLCV data. |
required |
symbol
|
str
|
The symbol being analyzed. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
Dictionary mapping strategy names to their individual scores. |
Source code in src/turtle_quant_1/strategies/engine.py
get_signal_agreement
¶
Post-analysis metric. Calculate agreement level between strategies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame with OHLCV data. |
required |
symbol
|
str
|
The symbol being analyzed. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Agreement level between 0.0 (complete disagreement) and 1.0 (perfect agreement). |
Source code in src/turtle_quant_1/strategies/engine.py
get_signal_confidence
¶
Post-analysis metric. Calculate confidence level for the generated signal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame with OHLCV data. |
required |
symbol
|
str
|
The symbol being analyzed. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Confidence level between 0.0 (low confidence) and 1.0 (high confidence). |