该策略基于移动平均线(MA)的斜率和价格与MA的相对位置来进行交易决策。当MA斜率大于最小斜率阈值且价格高于MA时,策略进行买入。同时,策略采用追踪止损(Trailing Stop Loss)来管理风险,并在特定条件下重新进场(Re-Entry)。该策略旨在捕捉上升趋势中的机会,同时通过动态止损和重新进场机制来优化收益和风险。
该策略通过移动平均线的斜率和价格与移动平均线的相对位置来判断趋势,并采用追踪止损和有条件重新进场的机制来管理交易。策略的优势在于趋势跟踪能力、动态止损保护和重新进场机会的捕捉。然而,策略也存在参数敏感性、趋势识别误差、止损频率和重新进场风险等潜在问题。可以根据优化方向,如优化趋势识别、止损方法、重新进场条件和仓位管理等,来矫正策略的弊端。在实际应用中,应根据具体市场特点和交易风格谨慎评估和调整。
/*backtest
start: 2024-05-01 00:00:00
end: 2024-05-31 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("MA Incline Strategy with Trailing Stop-Loss and Conditional Re-Entry", overlay=true, calc_on_every_tick=true)
// Input parameters
windowSize = input.int(10, title="Window Size")
maLength = input.int(150, title="Moving Average Length")
minSlope = input.float(0.001, title="Minimum Slope")
trailingStopPercentage = input.float(2.8, title="Trailing Stop Percentage (%)") / 100
reEntryPercentage = input.float(4.2, title="Re-Entry Percentage Above MA (%)") / 100
// Calculate the moving average
ma = ta.sma(close, maLength)
// Calculate the slope of the moving average over the window size
previousMa = ta.sma(close[windowSize], maLength)
slopeMa = (ma - previousMa) / windowSize
// Check conditions
isAboveMinSlope = slopeMa > minSlope
isAboveMa = close > ma
// Variables to track stop loss and re-entry condition
var bool stopLossOccurred = false
var float trailStopPrice = na
// Buy condition
buyCondition = isAboveMinSlope and isAboveMa and ((not stopLossOccurred) or (stopLossOccurred and low < ma * (1 + reEntryPercentage)))
// Execute strategy
if (buyCondition and strategy.opentrades == 0)
if (stopLossOccurred and close < ma * (1 + reEntryPercentage))
strategy.entry("Long", strategy.long)
stopLossOccurred := false
else if (not stopLossOccurred)
strategy.entry("Long", strategy.long)
// Trailing stop-loss
if (strategy.opentrades == 1)
// Calculate the trailing stop price
trailStopPrice := close * (1 - trailingStopPercentage)
// Use the built-in strategy.exit function with the trailing stop
strategy.exit("Trail Stop", "Long", stop=close * (1 - trailingStopPercentage))
// Exit condition
sellCondition = ta.crossunder(close, ma)
if (sellCondition and strategy.opentrades == 1)
strategy.close("Long")
// Check if stop loss occurred
if (strategy.closedtrades > 0)
lastExitPrice = strategy.closedtrades.exit_price(strategy.closedtrades - 1)
if (not na(trailStopPrice) and lastExitPrice <= trailStopPrice)
stopLossOccurred := true
// Reset stop loss flag if the price crosses below the MA
if (ta.crossunder(close, ma))
stopLossOccurred := false