Lesson 9: Time Range Testing
β± Duration: 1.5 hours
π― Learning Objectives: Validate strategy performance in different market environments
π Difficulty: ββ Backtesting practical
π Course Overview
A strategy performing well in one time period doesn't guarantee it will perform well in other periods. This lesson will teach you how to validate strategy stability and adaptability through time range testing, avoiding overfitting traps.
9.1 Why Test Different Time Periods?
Three Core Reasons
1. Avoid Overfitting
What is Overfitting?
A strategy overfits specific characteristics of historical data, leading to poor performance on new data.
Overfitting Manifestations:
Training Period (2025-01-01 ~ 2025-06-30):
Returns: +35%, Win Rate: 92%, Sharpe: 4.5 β
Testing Period (2025-07-01 ~ 2025-09-30):
Returns: -8%, Win Rate: 45%, Sharpe: 0.3 β
Conclusion: Serious overfitting!
How to Identify Overfitting:
- β Excellent performance in training period, poor in testing period
- β Strategy parameters overly complex (> 10 conditions)
- β Strategy only works in specific time periods
- β Performance fluctuates dramatically with minor parameter adjustments
2. Validate Strategy Consistency
Definition of Consistency:
Strategy maintains relatively consistent performance across different time periods.
Consistency Metrics:
Q1 (Jan-Mar): Returns +8%, Drawdown -3%
Q2 (Apr-Jun): Returns +12%, Drawdown -4%
Q3 (Jul-Sep): Returns +9%, Drawdown -3.5%
Q4 (Oct-Dec): Returns +11%, Drawdown -3.2%
Conclusion: Strategy stable, consistent quarterly performance β
vs
Q1: Returns +45%, Drawdown -2%
Q2: Returns -15%, Drawdown -18%
Q3: Returns +30%, Drawdown -5%
Q4: Returns -20%, Drawdown -22%
Conclusion: Strategy unstable, high volatility β
3. Identify Applicable Markets
Different strategies are suitable for different market conditions:
Trend Strategy vs Range Strategy:
Trend Strategy (MovingAverageCross):
Bull Market (2025-01~03): +35% β
Range-Bound (2025-04~06): -8% β
Bear Market (2025-07~09): +28% β
Mean Reversion Strategy (MeanReversion):
Bull Market: +5% β οΈ
Range-Bound: +22% β
Bear Market: +3% β οΈ
Conclusions:
- Trend strategies suitable for bull and bear markets (trending markets)
- Mean reversion strategies suitable for range-bound markets (range fluctuations)
9.2 Bull Market vs Bear Market vs Range-Bound Market
Market Type Characteristics
1. Bull Market
Features:
- π Continuous upward trend (gain > 20%)
- πΊ Small pullback magnitude (< 10%)
- π Increased trading volume
- πͺ Optimistic market sentiment
- β± Duration: weeks to months
Identification Method:
# Simple judgment: MA50 rising and price above MA50
price > MA50 and MA50 > MA50.shift(30)
Suitable Strategies:
- β Trend Following
- β Momentum
- β Breakout
- β Not suitable: Mean Reversion
Backtesting Points:
# Test bull market (assume 2025-01~03 is bull market)
freqtrade backtesting \
-c config.json \
--strategy MomentumTrendStrategy \
--timerange 20250101-20250331
2. Bear Market
Features:
- π Continuous downward trend (decline > 20%)
- π» Weak rebounds (< 10%)
- π Decreased trading volume
- π° Pessimistic market sentiment
- β± Duration: weeks to months
Identification Method:
# Simple judgment: MA50 falling and price below MA50
price < MA50 and MA50 < MA50.shift(30)
Response Strategies:
- β οΈ Reduce position size (50% or lower)
- β οΈ Raise stop loss standards
- β οΈ Decrease trading frequency
- π Consider suspending trading
Suitable Strategies:
- β Conservative strategies
- β Short selling strategies (if supported)
- β Not suitable: Aggressive momentum-chasing strategies
Backtesting Points:
# Test bear market (assume 2025-07~09 is bear market)
freqtrade backtesting \
-c config.json \
--strategy Strategy001 \
--timerange 20250701-20250930
# Focus on drawdown and stop loss performance
3. Range-Bound Market
Features:
- βοΈ Sideways consolidation (fluctuation < 10%)
- π Range-bound oscillation
- π Moderate trading volume
- π Neutral market sentiment
- β± Duration: days to weeks
Identification Method:
# Simple judgment: price oscillating within range
price_range = (high_30d - low_30d) / low_30d
if price_range < 0.15: # Fluctuation < 15%
print("Range-bound market")
Suitable Strategies:
- β Mean Reversion
- β Grid Trading
- β RSI overbought/oversold strategies
- β Not suitable: Trend Following
Backtesting Points:
# Test range-bound market
freqtrade backtesting \
-c config.json \
--strategy MeanReversionStrategy \
--timerange 20250401-20250630
Market Identification in Practice
Using TradingView for Identification:
- Open TradingView
- View BTC/USDT daily chart
- Add MA50 and MA200 indicators
- Observe the last 6-12 months of trends
Judgment Criteria:
Price > MA50 > MA200, and MA50 rising β Bull Market
Price < MA50 < MA200, and MA50 falling β Bear Market
Price fluctuating around MA50, MA50 flat β Range-Bound Market
9.3 Out-of-Sample Testing
What is Out-of-Sample Testing?
Definition:
Divide data into two parts:
- Training Set (In-Sample): Used for strategy development and optimization
- Testing Set (Out-of-Sample): Used to validate strategy effectiveness
Importance:
- β Validate strategy generalization ability
- β Avoid overfitting
- β Simulate real trading scenarios
Time Splitting Methods
Method 1: 70/30 Split
Standard Division:
Total Data: 2024-07-01 ~ 2025-09-30 (15 months)
Training Set: 2024-07-01 ~ 2025-03-31 (9 months, 60%)
Testing Set: 2025-04-01 ~ 2025-09-30 (6 months, 40%)
Backtesting Commands:
# Training period backtest
freqtrade backtesting \
-c config.json \
--strategy Strategy001 \
--timerange 20240701-20250331
# Testing period backtest
freqtrade backtesting \
-c config.json \
--strategy Strategy001 \
--timerange 20250401-20250930
Method 2: Multi-Period Rolling Test (Walk-Forward)
Principle:
Data: 12 months
Period 1: Train (Jan-Jun) β Test (Jul-Aug)
Period 2: Train (Mar-Aug) β Test (Sep-Oct)
Period 3: Train (May-Oct) β Test (Nov-Dec)
Advantages:
- More comprehensive validation
- Closer to live trading scenarios
- Reduce single-period bias
Implementation Script:
#!/bin/bash
STRATEGY="Strategy001"
CONFIG="config.json"
echo "=== Walk-Forward Testing ==="
# Period 1
echo "Period 1: Train 2024-07~Dec, Test 2025-01~Feb"
freqtrade backtesting -c $CONFIG --strategy $STRATEGY --timerange 20240701-20241231
freqtrade backtesting -c $CONFIG --strategy $STRATEGY --timerange 20250101-20250228
# Period 2
echo "Period 2: Train 2024-Sep~2025-Feb, Test 2025-Mar~Apr"
freqtrade backtesting -c $CONFIG --strategy $STRATEGY --timerange 20240901-20250228
freqtrade backtesting -c $CONFIG --strategy $STRATEGY --timerange 20250301-20250430
# Period 3
echo "Period 3: Train 2024-Nov~2025-Apr, Test 2025-May~Jun"
freqtrade backtesting -c $CONFIG --strategy $STRATEGY --timerange 20241101-20250430
freqtrade backtesting -c $CONFIG --strategy $STRATEGY --timerange 20250501-20250630
Method 3: Quarterly Split Testing
Principle:
Q1 (Jan-Mar): Test
Q2 (Apr-Jun): Test
Q3 (Jul-Sep): Test
Q4 (Oct-Dec): Test
Compare consistency of quarterly performance
Backtesting Commands:
# Q1
freqtrade backtesting -c config.json --strategy Strategy001 --timerange 20250101-20250331
# Q2
freqtrade backtesting -c config.json --strategy Strategy001 --timerange 20250401-20250630
# Q3
freqtrade backtesting -c config.json --strategy Strategy001 --timerange 20250701-20250930
# Q4
freqtrade backtesting -c config.json --strategy Strategy001 --timerange 20251001-20251231
Out-of-Sample Testing Evaluation Criteria
Qualified Standards:
Training Period Returns: +20%
Testing Period Returns: +15% (β₯ 75% of training period)
Training Period Drawdown: -5%
Testing Period Drawdown: -7% (β€ 150% of training period)
Training Period Sharpe: 3.0
Testing Period Sharpe: 2.3 (β₯ 75% of training period)
Conclusion: β
Strategy stable, can be used
Warning Signals:
Training Period Returns: +30%
Testing Period Returns: +2% (only 6.7% of training period) β
Training Period Drawdown: -3%
Testing Period Drawdown: -15% (5x training period) β
Training Period Sharpe: 4.5
Testing Period Sharpe: 0.5 (only 11% of training period) β
Conclusion: β Serious overfitting, cannot be used
9.4 Avoiding Overfitting
Overfitting Identification Signals
Signal 1: Perfect Training Period, Poor Testing Period
Case:
Training Period (6 months):
Trade Count: 150
Win Rate: 95%
Total Returns: +45%
Sharpe: 5.2
Testing Period (3 months):
Trade Count: 72
Win Rate: 48%
Total Returns: -12%
Sharpe: -0.3
Diagnosis: Typical overfitting!
Signal 2: Extremely Complex Strategy Parameters
Overfitting Strategy Example:
def populate_entry_trend(self, dataframe, metadata):
dataframe.loc[
(
# 10 condition combinations
(dataframe['ema5'] > dataframe['ema10']) &
(dataframe['ema10'] > dataframe['ema20']) &
(dataframe['rsi'] > 52.3) & # Too precise
(dataframe['rsi'] < 57.8) & # Too precise
(dataframe['macd'] > 0.0023) & # Too precise
(dataframe['volume'] > dataframe['volume'].shift(1) * 1.234) & # Too precise
(dataframe['close'] > dataframe['bb_lowerband'] * 1.012) &
(dataframe['adx'] > 23.7) &
(dataframe['cci'] < 87.3) &
(dataframe['mfi'] > 42.1)
),
'enter_long'] = 1
Problems:
- β Parameters too precise (e.g., 52.3, 57.8)
- β Too many conditions (10 conditions)
- β May only suit specific historical data
Signal 3: Dramatic Performance Changes with Minor Parameter Adjustments
Test:
RSI Threshold = 30 β Returns +25%
RSI Threshold = 31 β Returns +2%
RSI Threshold = 29 β Returns -5%
Conclusion: Strategy extremely sensitive to parameters, overfitting!
Overfitting Prevention Methods
1. Simplify Strategy
Principles:
- β Condition count β€ 5
- β Use integer parameters (e.g., 30, not 30.3)
- β Clear and understandable logic
Improvement Example:
# Simplified strategy
def populate_entry_trend(self, dataframe, metadata):
dataframe.loc[
(
# Keep only 3 core conditions
(dataframe['ema20'] > dataframe['ema50']) & # Trend confirmation
(dataframe['rsi'] > 30) & # Not oversold
(dataframe['volume'] > 0) # Has volume
),
'enter_long'] = 1
2. Increase Testing Period
Recommendations:
Short-term strategies (5m-15m): At least 3 months of data
Medium-term strategies (1h-4h): At least 6 months of data
Long-term strategies (1d): At least 12 months of data
3. Multi-Market Testing
Validation Checklist:
β
Bull market testing
β
Bear market testing
β
Range-bound market testing
β
High volatility period testing
β
Low volatility period testing
4. Parameter Stability Testing
Method:
Fine-tune parameters, observe result changes
# Test RSI threshold stability
# Modify RSI threshold in strategy: 25, 30, 35
freqtrade backtesting -c config.json --strategy StrategyRSI25 --timerange 20250701-20250930
freqtrade backtesting -c config.json --strategy StrategyRSI30 --timerange 20250701-20250930
freqtrade backtesting -c config.json --strategy StrategyRSI35 --timerange 20250701-20250930
# If three versions have similar results, strategy is stable
5. Use Regularization Techniques
Methods:
- Limit maximum open positions
- Set reasonable ROI gradients
- Use conservative stop losses
Example:
# Conservative settings to prevent overfitting
stoploss = -0.10 # 10% stop loss (not too tight)
max_open_trades = 3 # Limit position count
minimal_roi = {
"0": 0.10, # Reasonable target (not too high)
"120": 0.05,
"240": 0.02
}
π‘ Practical Tasks
Task 1: Different Time Period Comparison Testing
Choose a strategy (recommend Strategy001), test 3 different time periods:
# Period 1: 2024-10-01 ~ 2024-12-31 (Q4 2024)
freqtrade backtesting -c config.json --strategy Strategy001 --timerange 20241001-20241231 --timeframe 15m
# Period 2: 2025-01-01 ~ 2025-03-31 (Q1 2025)
freqtrade backtesting -c config.json --strategy Strategy001 --timerange 20250101-20250331 --timeframe 15m
# Period 3: 2025-04-01 ~ 2025-06-30 (Q2 2025)
freqtrade backtesting -c config.json --strategy Strategy001 --timerange 20250401-20250630 --timeframe 15m
# Period 4: 2025-07-01 ~ 2025-09-30 (Q3 2025)
freqtrade backtesting -c config.json --strategy Strategy001 --timerange 20250701-20250930 --timeframe 15m
Task 2: Create Time Period Comparison Table
Time Period | Trade Count | Win Rate% | Total Return% | Max Drawdown% | Sharpe | Market Type |
---|---|---|---|---|---|---|
2024 Q4 | ? | ? | ? | ? | ? | ? |
2025 Q1 | ? | ? | ? | ? | ? | ? |
2025 Q2 | ? | ? | ? | ? | ? | ? |
2025 Q3 | ? | ? | ? | ? | ? | ? |
Average | ? | ? | ? | ? | ? | - |
Std Dev | ? | ? | ? | ? | ? | - |
Task 3: Out-of-Sample Testing
# Training period: 2024-07-01 ~ 2025-03-31 (9 months)
freqtrade backtesting -c config.json --strategy Strategy001 --timerange 20240701-20250331 --timeframe 15m
# Testing period: 2025-04-01 ~ 2025-09-30 (6 months)
freqtrade backtesting -c config.json --strategy Strategy001 --timerange 20250401-20250930 --timeframe 15m
Comparative Analysis:
Training Period Performance:
Total Returns: ___________%
Win Rate: ___________%
Max Drawdown: ___________%
Sharpe: ___________
Testing Period Performance:
Total Returns: ___________% (____% of training period)
Win Rate: ___________% (____% difference)
Max Drawdown: ___________% (____% of training period)
Sharpe: ___________ (____% of training period)
Conclusion:
β Strategy stable, testing period performance close to training period (β₯ 75%)
β Strategy unstable, testing period performance significantly decreased (< 50%)
β Possible overfitting
Task 4: Judge Strategy Stability
Based on your test results, answer the following questions:
-
Is the standard deviation of quarterly performance large?
- Return standard deviation < 5% β Stable β
- Return standard deviation > 10% β Unstable β
-
How is out-of-sample testing performance?
- Testing period β₯ 75% of training period β Stable β
- Testing period < 50% of training period β Overfitting β
-
Can it profit in different market conditions?
- 3/4 quarters profitable β Strong adaptability β
- Only 1/4 quarters profitable β Poor adaptability β
Final Judgment:
β Strategy stable and reliable, can enter simulation trading phase
β Strategy needs optimization, return to strategy adjustment phase
β Strategy seriously overfitted, abandon this strategy
π Knowledge Check
Basic Questions
- What is overfitting?
- What is the purpose of out-of-sample testing?
- How to judge if a strategy is stable?
Answers
- Strategy overfits specific characteristics of historical data, leading to poor performance on new data
- Validate strategy generalization ability, ensure strategy doesn't only work in specific time periods
- Consistent performance across different time periods, testing period performance close to training period (β₯ 75%)
Advanced Questions
- Why need to test strategies in bull, bear, and range-bound markets?
- Good performance in training period, poor in testing period, what does it indicate?
- How to prevent overfitting?
Thought Questions
- If a strategy performs excellently in all historical periods, does it mean live trading will also be good?
- What proportion of total data is most appropriate for out-of-sample testing?
- What advantages does Walk-Forward testing have over simple 70/30 split?
π Reference Materials
Supporting Documentation
- π TESTING_GUIDE.md - Time range recommendations
- π STRATEGY_SELECTION_GUIDE.md - Strategy evaluation methods
Recommended Reading
π Key Points Summary
- Different time period testing is key to validating strategy stability
- Out-of-sample testing > In-sample testing: Testing period performance is more important
- Overfitting is the biggest trap in quantitative trading
- Strategies should be tested in bull, bear, and range-bound markets
- Consistency > Returns: Consistent performance is more important than occasional high returns
- Simple strategy > Complex strategy: Fewer conditions reduce overfitting risk
β‘οΈ Next Lesson Preview
Lesson 10: Trading Pair Selection and Testing
In the next lesson, we will:
- Learn how to select suitable trading pairs
- Evaluate liquidity and volatility of trading pairs
- Test strategy performance on different trading pairs
- Build multi-pair portfolios
Preparation:
- β Download data for multiple pairs like BTC/USDT, ETH/USDT, BNB/USDT
- β Select a strategy with stable performance
- β Understand the difference between mainstream and altcoins
π― Learning Check Standards:
- β Can independently conduct out-of-sample testing
- β Can judge if a strategy is overfitted
- β Understand the impact of different market conditions on strategies
- β Can evaluate strategy stability
After completing these tasks, you have mastered the core skills of strategy validation! Ready to move on to trading pair selection learning! π
Top comments (0)