Skip to main content

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

FeatureDescription
Entry Conditions1 simple condition: PSAR declining
Exit ConditionsNo technical exit signals, relies on stoploss exit
ProtectionPSAR dynamic stoploss
Timeframe1 hour
DependenciesTA-Lib
Special FeaturesUses 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:

  1. Get latest candle's PSAR value
  2. Calculate stoploss ratio of PSAR relative to current price
  3. 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 CategorySpecific IndicatorParametersUsage
TrendPSAR (SAR)DefaultDynamic 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:

  1. Calculate PSAR in populate_indicators() and store in custom_info
  2. Read latest PSAR value in custom_stoploss()
  3. 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

  1. Dynamic Stoploss Example: Perfectly demonstrates custom_stoploss() usage
  2. PSAR Application: Shows PSAR implementation as stoploss reference
  3. Concise Code: Only about 70 lines, easy to understand
  4. High Learning Value: Suitable for learning custom stoploss techniques
  5. Timeframe Friendly: 1-hour level suitable for most traders

⚠️ Limitations

  1. Example Nature: Entry logic too simple, not suitable for production
  2. No Trend Filter: No EMA/SMA trend judgment
  3. No BTC Correlation: Does not detect Bitcoin market trend
  4. No Technical Exit: Relies entirely on stoploss for exit
  5. Data Storage Limitation: custom_info only available in backtest/optimization

Market EnvironmentRecommended ConfigurationNotes
Learning PurposeHighly RecommendedFirst choice example for learning custom_stoploss()
Production EnvironmentNot RecommendedNeed to完善 entry logic and protection mechanisms
Trending ConditionsUse after modificationPSAR stoploss performs well in trends
Ranging ConditionsNot RecommendedPSAR 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 TypePerformance RatingReason 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

ConfigurationRecommended ValueNotes
Number of Pairs10-20Example strategy, should not be too many
Max Open Trades1-3 ordersControl risk
Timeframe1hMandatory

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 usage
  • custom_info data storage techniques
  • PSAR indicator application methods

10.2 Production Environment Recommendations

Do not use directly for live trading, need to:

  1. Improve entry logic (add more confirmation conditions)
  2. Add trend filter
  3. Add technical exit signals
  4. Add BTC correlation analysis
  5. 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_time for 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:

  1. Technical Demonstration: Perfectly demonstrates custom_stoploss() function usage
  2. PSAR Application: Shows PSAR implementation as dynamic stoploss reference
  3. Concise Code: Only about 70 lines, easy to understand and learn
  4. 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_info data storage and retrieval methods
  • Improve entry logic and protection mechanisms on this basis
  • Do not use directly for production environments