turtle_quant_1.strategies.mean_reversion¶
turtle_quant_1.strategies.mean_reversion
¶
BollingerBand
¶
Bases: BaseStrategy
A strategy that uses the Bollinger bands to generate buy and sell signals.
Leading / Lagging: Lagging in construction. Leading in interpretation.
Lag period (in candles): (lookback_candles - 1) / 2
Effect: Delay in recognizing that price has deviated far enough from equilibrium.
Initialize the Bollinger band strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lookback_candles
|
int
|
The number of periods to use for the Bollinger bands. |
180
|
n_std
|
int
|
The number of standard deviations to use for the Bollinger bands. |
3
|
Source code in src/turtle_quant_1/strategies/mean_reversion/bollinger_band.py
generate_historical_scores
¶
Generate a historical score array for a symbol based on market data.
NOTE: Assume that the data is sorted by datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
The data to use for the strategy. |
required |
symbol
|
str
|
The symbol to use for the strategy. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
Score array with each value between -1.0 and +1.0, indexed by the original data's index. |
Source code in src/turtle_quant_1/strategies/mean_reversion/bollinger_band.py
generate_prediction_score
¶
Generate a score for the strategy.
NOTE: Assume that the data is sorted by datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
The data to use for the strategy. |
required |
symbol
|
str
|
The symbol to use for the strategy. |
required |
Source code in src/turtle_quant_1/strategies/mean_reversion/bollinger_band.py
FastStochOscCrossover
¶
Bases: BaseStrategy
A strategy that uses Fast Stochastic Oscillator %K/%D crossovers to generate buy and sell signals.
Can be intuitively interpreted as whether a stock is overbought or oversold. Refer to: https://www.investopedia.com/terms/s/stochasticoscillator.asp
Initialize the Stochastic Oscillator strategy.
Leading / Lagging: Lagging in construction. Leading in interpretation.
Lag period (in candles): (k_period - 1) / 2 + (d_period - 1) / 2
Effect: Delay in recognizing that price has deviated far enough from equilibrium.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k_period
|
int
|
The number of candles to calculate %K. |
14
|
d_period
|
int
|
The number of candles to smooth %K into %D. |
3
|
Source code in src/turtle_quant_1/strategies/mean_reversion/fast_stoch_osc_crossover.py
generate_historical_scores
¶
Generate historical scores based on %K/%D crossovers.
NOTE: Assume that the data is sorted by datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame with OHLCV data. |
required |
symbol
|
str
|
The symbol being analyzed. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
Score array with values between -1.0 and +1.0, indexed by the original data's index. |
Source code in src/turtle_quant_1/strategies/mean_reversion/fast_stoch_osc_crossover.py
generate_prediction_score
¶
Generate the latest prediction score.
NOTE: Assume that the data is sorted by datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
The data to use for the strategy. |
required |
symbol
|
str
|
The symbol being analyzed. |
required |
Returns:
| Type | Description |
|---|---|
float
|
A float between -1.0 and +1.0 representing the most recent crossover signal. |
Source code in src/turtle_quant_1/strategies/mean_reversion/fast_stoch_osc_crossover.py
MfiDivergence
¶
Bases: BaseStrategy
Initialize the MFI Divergence strategy.
Leading / Lagging: Lagging in construction. Leading in interpretation.
Lag period (in candles): (mfi_period - 1) / 2
Effect: Delay in recognizing that price has deviated far enough from equilibrium.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mfi_period
|
int
|
Number of candles for MFI calculation. |
14
|
local_extrema_window
|
int
|
Window for local maxima/minima detection. |
5
|
Source code in src/turtle_quant_1/strategies/mean_reversion/mfi_divergence.py
generate_historical_scores
¶
Generate historical MFI divergence scores.
NOTE: Assume that the data is sorted by datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
The data to use for the strategy. |
required |
symbol
|
str
|
The symbol to use for the strategy. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
Score array with each value between -1.0 and +1.0, indexed by the original data's index. |
Source code in src/turtle_quant_1/strategies/mean_reversion/mfi_divergence.py
generate_prediction_score
¶
Generate the prediction score for the latest candle.
NOTE: Assume that the data is sorted by datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
The data to use for the strategy. |
required |
symbol
|
str
|
The symbol to use for the strategy. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Score array with each value between -1.0 and +1.0, indexed by datetime |
Source code in src/turtle_quant_1/strategies/mean_reversion/mfi_divergence.py
RocMultiConditionCrossover
¶
RocMultiConditionCrossover(
roc_period: int = 14,
signal_period: int = 9,
trend_period: int = 90,
min_threshold: float = 1.0,
use_zero_filter: bool = True,
use_trend_filter: bool = True,
)
Bases: BaseStrategy
A strategy using RoC crossover with optional zero-line, threshold, and trend filters.
This strategy is a combination of the RoC crossover strategy and the trend filter strategy.
The following filters are applied:
- Zero-line filter: Ignore signals if RoC > 0 for buys and RoC < 0 for sells.
- Threshold filter: Ignore signals if |RoC| < min_threshold.
- Trend filter: Only take long signals if price > SMA(trend_period),
short signals if price < SMA(trend_period).
The filters are put in place to mute signal noise, thus reduce false signals.
Leading / Lagging: TODO Lag period (in candles): TODO Effect: TODO
Refer to: https://www.investopedia.com/terms/r/rateofchange.asp
Initialize the RoC multi-condition crossover strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
roc_period
|
int
|
The number of candles to calculate RoC. |
14
|
signal_period
|
int
|
The number of candles to smooth RoC into a signal line. |
9
|
trend_period
|
int
|
Lookback period for trend filter (SMA). |
90
|
min_threshold
|
float
|
As a percentage. Ignore signals if |
1.0
|
use_zero_filter
|
bool
|
If True, require RoC > 0 for buys and RoC < 0 for sells. |
True
|
use_trend_filter
|
bool
|
If True, only take long signals if price > SMA(trend_period), short signals if price < SMA(trend_period). |
True
|
Source code in src/turtle_quant_1/strategies/mean_reversion/roc_multi_condition_crossover.py
generate_historical_scores
¶
Generate historical scores based on RoC crossovers with filters.
NOTE: Assume that the data is sorted by datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame with OHLCV data. |
required |
symbol
|
str
|
The symbol being analyzed. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
Score array with values between -1.0 and +1.0, indexed by the original data's index. |
Source code in src/turtle_quant_1/strategies/mean_reversion/roc_multi_condition_crossover.py
generate_prediction_score
¶
Generate the latest prediction score.
NOTE: Assume that the data is sorted by datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
The data to use for the strategy. |
required |
symbol
|
str
|
The symbol being analyzed. |
required |
Returns:
| Type | Description |
|---|---|
float
|
A float between -1.0 and +1.0 representing the most recent signal. |
Source code in src/turtle_quant_1/strategies/mean_reversion/roc_multi_condition_crossover.py
RsiSupResDivergence
¶
Bases: BaseStrategy
Initialize the RSI strategy.
Leading / Lagging: Lagging in construction. Leading in interpretation.
Lag period (in candles): (lookback_candles - 1) / 2
Effect: Delay in recognizing that price has deviated far enough from equilibrium.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lookback_candles
|
int
|
The number of periods to use for the RSI. |
60
|
local_extrema_window
|
int
|
The size of the window for the local maxima and minima. |
5
|
Source code in src/turtle_quant_1/strategies/mean_reversion/rsi_sup_res_divergence.py
generate_historical_scores
¶
Generate the historical scores.
NOTE: Assume that the data is sorted by datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
The data to generate the scores from. |
required |
symbol
|
str
|
The symbol to generate the scores for. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
Score array with each value between -1.0 and +1.0, indexed by the original data's index. |
Source code in src/turtle_quant_1/strategies/mean_reversion/rsi_sup_res_divergence.py
generate_prediction_score
¶
Generate the prediction score.
NOTE: Assume that the data is sorted by datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
The data to generate the score from. |
required |
symbol
|
str
|
The symbol to generate the score for. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The prediction score. |
Source code in src/turtle_quant_1/strategies/mean_reversion/rsi_sup_res_divergence.py
SlowStochOscCrossover
¶
Bases: BaseStrategy
A strategy that uses Slow Stochastic Oscillator %K/%D crossovers to generate buy and sell signals.
Can be intuitively interpreted as whether a stock is overbought or oversold. Refer to: https://www.investopedia.com/terms/s/stochasticoscillator.asp
Leading / Lagging: Lagging in construction. Leading in interpretation.
Lag period (in candles): (k_period - 1) / 2 + (d_period - 1) / 2 + (k_smooth - 1) / 2
Effect: Delay in recognizing that price has deviated far enough from equilibrium.
Initialize the Slow Stochastic Oscillator strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k_period
|
int
|
The number of candles to calculate %K. |
14
|
k_smooth
|
int
|
The smoothing period applied to %K to create Slow %K. |
3
|
d_period
|
int
|
The smoothing period applied to Slow %K to create Slow %D. |
3
|
Source code in src/turtle_quant_1/strategies/mean_reversion/slow_stoch_osc_crossover.py
generate_historical_scores
¶
Generate historical scores based on Slow %K/%D crossovers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame with OHLCV data. |
required |
symbol
|
str
|
The symbol being analyzed. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
Score array with values between -1.0 and +1.0, indexed by the original data's index. |
Source code in src/turtle_quant_1/strategies/mean_reversion/slow_stoch_osc_crossover.py
generate_prediction_score
¶
Generate the latest prediction score.
NOTE: Assume that the data is sorted by datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
The data to use for the strategy. |
required |
symbol
|
str
|
The symbol being analyzed. |
required |
Returns:
| Type | Description |
|---|---|
float
|
A float between -1.0 and +1.0 representing the most recent crossover signal. |