追踪止损百分比策略是一种基于交易品种价格百分比来设置和调整止损单的策略。它可以在价格达到一定盈利水平后,将止损单调整至入场价位,实现保本止损。
该策略通过input参数设置长仓追踪止损的百分比,如3%。在开仓后,会实时计算追踪止损价格。计算方法是:
当价格超过入场价*(1+追踪止损百分比),则将止损价格调整至入场价,实现保本。
当价格低于上述水平,则止损价格为入场价*(1-追踪止损百分比)。
这样可以实现当价格达到一定盈利后保本止损,避免盈利全部收益被损失,同时防止过于激进的止损被价格正常波动顶出。
策略还绘制了追踪止损价格的图表进行确认,并设置了只做多头交易。在金叉时做多,死叉时平仓。做多后设置追踪止损单,实现该策略的止损逻辑。
该策略最大的优势是可以通过追踪止损实现盈利后保本,无论后市如何,至少可以保住本金,避免亏损。这对许多投资者有重要意义。
另外,该策略止损比较温和,追踪止损幅度不是太大,可以防止价格正常波动就被止损出场。这与一般的固定止损相比更加灵活和智能。
该策略主要风险在于止损幅度设置不当,如果设置太小,则难以实现保本止损;如果设置太大,则容易被价格正常波动顶出场。所以这里需要仔细测试和评估合适的止损幅度。
另一个风险是在异常市场时,价格突然大幅度跳空,这时止损价格可能来不及更新,导致止损无效。不过该概率较小。
该策略可以从以下几个方面进行优化:
增加平仓条件,如死叉、价格跌破SMA等规则,使策略更加全面。
加入止损百分比的动态调整机制,在不同市场环境下自动优化止损幅度。
增加离场策略,当价格运行一定距离后退出场外,固定利润。
可以研究不同品种的止损百分比参数差异,建立参数自适应优化机制。
追踪止损百分比策略整体来说非常实用,可以有效实现盈利后保本止损,避免亏损。该策略优化空间很大,值得进一步研究提高效果。总体而言,该策略适合追求稳定投资利润的投资者。
/*backtest
start: 2023-12-08 00:00:00
end: 2024-01-07 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © osmaras
// based on https://kodify.net/tradingview/orders/percentage-trail/
//@version=5
strategy("Break even stop loss (% of instrument price)", overlay=true)
// Configure trail stop level with input options (optional)
longTrailPerc = input.float(defval=3.0,step=0.1,title="Trail Long Loss (%)")* 0.01
longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
// Determine trail stop loss prices
longStopPrice = 0.0
lastEntryPrice = strategy.opentrades.entry_price(strategy.opentrades - 1)
longStopPrice := if (strategy.position_size > 0 and close > (lastEntryPrice * (1 + longTrailPerc)))
stopValue = lastEntryPrice
math.max(stopValue, longStopPrice[1])
else
longStopPrice := if (strategy.position_size > 0)
stopValue = lastEntryPrice * (1 - longTrailPerc)
math.max(stopValue, longStopPrice[1])
else
0
// Plot stop loss values for confirmation
plot(series=(strategy.position_size > 0) ? longStopPrice : na,
color=color.fuchsia, style=plot.style_cross,
linewidth=2, title="Long Trail Stop")
// set strategy only long
strategy.risk.allow_entry_in(strategy.direction.long)
// Submit entry orders
if (longCondition)
strategy.entry("Long", strategy.long)
shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
if (shortCondition)
strategy.close("Long")
// Submit exit orders for trail stop loss price
if (strategy.position_size > 0)
strategy.exit(id="Stop Loss", stop=longStopPrice)