In trading strategies, take profit and stop loss are the core of risk control. Trailing Stop, as a dynamic take profit method, is used to protect profits while keeping positions flexible. In this article, we will delve into the principles, applications, and practical effects of trailing stops through custom indicator code, helping everyone use trailing stops more efficiently to control the market.
1. Why Use Trailing Stop
Ordinary fixed take profit prices are relatively rigid and may not respond to the dynamic fluctuations of the market. This leads to:
Insufficient profit locking: Rapid pullbacks after price increases result in significant profit loss.
Missing more opportunities: Strong price breakouts and frequent fixed take profit points lead to premature exits.
Adapting to volatile markets: When market conditions change significantly, trailing stops can follow trends instead of being constrained by a fixed value.
Therefore, trailing stops help us dynamically adjust take profit points, allowing us to capture trend continuation profits while timely protecting current gains, making it a more flexible take profit method.
2. Specific Operation of Trailing Stop
The core mechanism of trailing stops is to dynamically adjust the take profit point as prices change. Currently, common operational methods include:
Fixed Point Difference Mode (Quantitative Method):
Set a fixed offset value based on the current price (e.g., 10% or $20). When the market price moves in the trend direction, the take profit point rises or falls accordingly.
Dynamic Adjustment Based on Technical Indicators (Qualitative Method):
Automatically adjust the take profit trigger position based on technical indicator results, such as moving averages, Average True Range (ATR), etc. For example, set a protective line using twice the ATR value to make the take profit point more adaptable to different market volatility conditions.
Trend Line or Support and Resistance Level Method:
Define the take profit position after the entry point based on trend lines or key technical levels in the candlestick structure, and dynamically adjust it with changes in trends and support/resistance.
In this issue's Custom Indicator · Trailing Stop, we explore the Quantitative Method:
Specific operation:
Activate trailing stop at 10% profit, execute take profit when retracing to 80% of the maximum profit.
3. Trailing Stop Signal Effect
Figure OKX-BTCUSDT perpetual contract 4-hour cycle
Figure OKX-ETHUSDT perpetual contract 4-hour cycle
4. Trailing Stop Backtesting
Figure OKX-BTCUSDT perpetual contract 4-hour cycle
Backtesting highlights:
Return Rate: 1076.55%
Win Rate: 92.06%
Figure OKX-ETHUSDT perpetual contract 4-hour cycle
Backtesting highlights:
Return Rate: 990.46%
Win Rate: 94.92%
5. Indicator Source Code
// @version=2
// Calculate MACD indicator
[dif, dea, macd] = macd(close, 12, 26, 9, EMA, EMA);
// Define golden cross, i.e., DIF line crosses above DEA line
golden_cross = crossup(dif, dea);
var check_profit = 0
var checke_profit_max = 0
var checke_profit_profit = 0
// Stop loss ratio
stop_loss = 0.07
// Take profit
active_trail = 0.01 // Activate trailing stop at 10% profit. The benchmark price for calculating profit is based on the latest closing price at the time the signal occurs, which may have some slippage compared to the actual order price (signal occurrence means market price order).
drawdown = 0.5 // Execute take profit when retracing to 80% of the maximum profit
var long_signal = false
var long_count = 0
var long_price = 0
// Calculate take profit and stop loss - long
var active_long_profit_monitor = false
var long_max_profit = 0
var active_long_drawdown_order = false
var stop_long_monitor = false
if(long_count > 0){
profit = (close - long_price) / long_price
if (profit > long_max_profit) {
long_max_profit := profit
}
if (profit > active_trail and long_max_profit > active_trail) {
active_long_profit_monitor := true
}
if (active_long_profit_monitor) {
back = profit/long_max_profit
if (back < drawdown and profit > 0) {
active_long_drawdown_order := true
}
check_profit := back
checke_profit_max := long_max_profit
checke_profit_profit := profit
}
// Calculate stop loss
if(profit <= -1 * stop_loss){
stop_long_monitor := true
}
}else{
active_long_drawdown_order := false
active_long_profit_monitor := false
long_max_profit := -999999999
long_price := 0
stop_long_monitor := false
}
exitLongPercent(active_long_drawdown_order, id = long_take_profit, price=`market`, percent=100)
plotText(active_long_drawdown_order, title=`active_long_drawdown_order`, text = Long · Trailing Stop, refSeries = high, bgColor=`red`, color=`white`, fontSize=14, placement=`top`, display=true);
if (active_long_drawdown_order) {
long_count := 0
long_price := 0
}
if(golden_cross and long_count == 0){
long_signal := true
long_count := 1
long_price := close
}else{
long_signal := false
}
plotText(long_signal, title=`long_signal`, text = Long, refSeries = high, bgColor=`green`, color=`white`, fontSize=14, placement=`bottom`, display=true);
enterLong(long_signal, price=`market`, amount=1)
6. Conclusion
Trailing stop, as an intelligent dynamic risk control tool, is particularly effective in trending markets. Compared to traditional fixed take profit, it not only provides flexibility in protecting profits but also maximizes the capture of trend continuation gains. However, in practical use, it is necessary to reasonably set offset values based on market volatility, strategy style, and other factors, and optimize the effects through backtesting and adjustments.
In summary, trailing stop is a powerful risk management tool that should not be overlooked in trading system design. I hope this article's introduction and source code examples can provide inspiration and practical reference for your trading strategy research!
免责声明:本文章仅代表作者个人观点,不代表本平台的立场和观点。本文章仅供信息分享,不构成对任何人的任何投资建议。用户与作者之间的任何争议,与本平台无关。如网页中刊载的文章或图片涉及侵权,请提供相关的权利证明和身份证明发送邮件到support@aicoin.com,本平台相关工作人员将会进行核查。