
이 전략은 이동 평균 (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