CustomStoplossWithPSAR Strategy Analysis
Strategy Number: #5 (5th of 465 strategies)
Strategy Type: PSAR Dynamic Stoploss Example Strategy
Timeframe: 1 hour (1h)
1. Strategy Overview
CustomStoplossWithPSAR is an example strategy primarily demonstrating how to use Freqtrade's custom_stoploss() function to implement dynamic stoploss based on PSAR (Parabolic SAR). The strategy itself is not intended for production environments, but serves as a learning template for developers.
Core Features
| Feature | Description |
|---|---|
| Entry Conditions | 1 simple condition: PSAR declining |
| Exit Conditions | No technical exit signals, relies on stoploss exit |
| Protection | PSAR dynamic stoploss |
| Timeframe | 1 hour |
| Dependencies | TA-Lib |
| Special Features | Uses custom_stoploss() for dynamic stoploss |
2. Configuration Analysis
2.1 Base Risk Parameters
# Hard stoploss
stoploss = -0.2 # -20%
# Custom stoploss
use_custom_stoploss = True
Design Logic:
- Hard Stoploss Backup: -20% as final defense line
- Dynamic Stoploss: Implements PSAR trailing stoploss via
custom_stoploss() - Example Nature: Strategy focus is on demonstrating technical implementation, not trading logic
2.2 Order Type Configuration
Uses Freqtrade default configuration.
3. Entry Conditions Explained
3.1 Entry Logic
# Entry conditions
dataframe.loc[
(dataframe["sar"] < dataframe["sar"].shift()), # PSAR declining
"buy",
] = 1
Logic Analysis:
- PSAR Declining: Triggers buy when PSAR indicator turns from rising to declining
- Trend Reversal Signal: PSAR is a trend-following indicator, declining indicates potential uptrend
- Simplified Logic: As an example strategy, entry conditions are very simple
4. Exit Logic Explained
4.1 Custom Stoploss Function
def custom_stoploss(
self,
pair: str,
trade: "Trade",
current_time: datetime,
current_rate: float,
current_profit: float,
**kwargs,
) -> float:
result = 1
if self.custom_info and pair in self.custom_info and trade:
dataframe, _ = self.dp.get_analyzed_dataframe(pair=pair, timeframe=self.timeframe)
last_candle = dataframe.iloc[-1].squeeze()
relative_sl = last_candle["sar"]
if relative_sl is not None:
new_stoploss = (current_rate - relative_sl) / current_rate
result = new_stoploss - 1
return result
Working Mechanism:
- Get latest candle's PSAR value
- Calculate stoploss ratio of PSAR relative to current price
- Return dynamic stoploss value
4.2 No Technical Exit Signals
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe.loc[:, "sell"] = 0 # No exit signals set
return dataframe
Note: Strategy relies entirely on PSAR dynamic stoploss for exit, does not use technical exit signals.
5. Technical Indicator System
5.1 Core Indicators
| Indicator Category | Specific Indicator | Parameters | Usage |
|---|---|---|---|
| Trend | PSAR (SAR) | Default | Dynamic stoploss + Entry signals |
5.2 PSAR Indicator Characteristics
- Parabolic SAR: Automatically adjusts stoploss level with price movement
- Trend Following: In uptrend, PSAR is below price and rising
- Stoploss Function: PSAR naturally suitable as dynamic stoploss reference
6. Risk Management Features
6.1 PSAR Dynamic Stoploss
Working Principle:
- Calculate PSAR in
populate_indicators()and store incustom_info - Read latest PSAR value in
custom_stoploss() - Calculate relative stoploss ratio and return
Advantages:
- Stoploss level automatically adjusts with price movement
- Can lock in more profits in trending conditions
- Smarter than fixed percentage stoploss
6.2 Hard Stoploss Backup
stoploss = -0.2 # -20%
Purpose: Final defense line when PSAR stoploss fails.
7. Strategy Strengths and Limitations
✅ Strengths
- Dynamic Stoploss Example: Perfectly demonstrates
custom_stoploss()usage - PSAR Application: Shows PSAR implementation as stoploss reference
- Concise Code: Only about 70 lines, easy to understand
- High Learning Value: Suitable for learning custom stoploss techniques
- Timeframe Friendly: 1-hour level suitable for most traders
⚠️ Limitations
- Example Nature: Entry logic too simple, not suitable for production
- No Trend Filter: No EMA/SMA trend judgment
- No BTC Correlation: Does not detect Bitcoin market trend
- No Technical Exit: Relies entirely on stoploss for exit
- Data Storage Limitation:
custom_infoonly available in backtest/optimization
8. Recommended Usage Scenarios
| Market Environment | Recommended Configuration | Notes |
|---|---|---|
| Learning Purpose | Highly Recommended | First choice example for learning custom_stoploss() |
| Production Environment | Not Recommended | Need to完善 entry logic and protection mechanisms |
| Trending Conditions | Use after modification | PSAR stoploss performs well in trends |
| Ranging Conditions | Not Recommended | PSAR frequently stoplosses in ranging markets |
9. Suitable Market Environments Explained
CustomStoplossWithPSAR is an educational example strategy, main value is for learning.
9.1 Strategy Core Logic
- PSAR Stoploss: Uses PSAR as dynamic stoploss reference
- Trend Reversal Entry: Buys when PSAR declines
- No Technical Exit: Relies entirely on stoploss for exit
9.2 Performance in Different Market Environments
| Market Type | Performance Rating | Reason Analysis |
|---|---|---|
| 📈 Slow Bull/Ranging Upward | ★★★☆☆ | PSAR stoploss can follow trend, but entry logic is simple |
| 🔄 Wide Ranging | ★★☆☆☆ | PSAR frequently stoplosses in ranging markets |
| 📉 One-Way Crash | ★☆☆☆☆ | No trend filter, may lose consecutively |
| ⚡️ Extreme Sideways | ★☆☆☆☆ | PSAR frequently crosses, signals confused |
9.3 Key Configuration Recommendations
| Configuration | Recommended Value | Notes |
|---|---|---|
| Number of Pairs | 10-20 | Example strategy, should not be too many |
| Max Open Trades | 1-3 orders | Control risk |
| Timeframe | 1h | Mandatory |
10. Important Reminder: Example Strategy Positioning
10.1 Learning Value
This strategy is an excellent example for learning Freqtrade custom stoploss functionality:
custom_stoploss()function usagecustom_infodata storage techniques- PSAR indicator application methods
10.2 Production Environment Recommendations
Do not use directly for live trading, need to:
- Improve entry logic (add more confirmation conditions)
- Add trend filter
- Add technical exit signals
- Add BTC correlation analysis
- Optimize parameter configuration
10.3 Backtest vs Live Trading Differences
custom_info behaves differently in live trading vs backtest:
- In backtest, can directly use
current_timefor indexing - In live trading, need to use
get_analyzed_dataframe()to get data
11. Summary
CustomStoplossWithPSAR is an educational example strategy, its core value lies in:
- Technical Demonstration: Perfectly demonstrates
custom_stoploss()function usage - PSAR Application: Shows PSAR implementation as dynamic stoploss reference
- Concise Code: Only about 70 lines, easy to understand and learn
- Learning Template: Suitable for developing own strategies on this basis
For quantitative traders, this is an excellent learning template. Recommendations:
- Use as an introductory case for learning custom stoploss functionality
- Understand
custom_infodata storage and retrieval methods - Improve entry logic and protection mechanisms on this basis
- Do not use directly for production environments