이 전략은 다른 주기의 이동 평균을 계산하여 현재 트렌드 방향을 식별하고 RSI 지표와 결합하여 구매 시그널을 발송합니다. 짧은 이동 평균의 상단에서 장기 이동 평균을 통과하면 추세가 상승한다고 생각하고 구매합니다. 짧은 이동 평균의 아래에서 장기 이동 평균을 통과하면 추세가 역전되어 판매합니다. 동시에 RSI 지표와 결합하여 소량의 가격 변동으로 인한 잘못된 신호를 피합니다.
10일, 20일, 50일, 100일, 200일 간단한 이동 평균을 계산하세요.
14일 RSI값을 계산한다.
10일 SMA에서 50일 SMA를 통과하고 RSI가 30보다 크며 20일 SMA가 100일 SMA보다 높거나 같거나 50일 SMA가 100일 SMA보다 높거나 같을 때 구매한다.
구매 포인트로 스톱 손실 가격을 설정합니다.
판매는 다음의 경우입니다.
이 전략은 이동 평균을 통해 시장의 추세 방향을 판단하고 위험을 제어하기 위해 스톱을 설정합니다. RSI 지표는 가짜 브레이크를 필터링하는 데 사용됩니다. 단기 SMA에서 장기 SMA를 통과 할 때 구매하면 추세가 상승하고 주식을 보유 할 때 스톱 라인을 설정하여 위험을 제어합니다. 추세 반전 신호가 발생하거나 스톱 가격이 유발되면 주식을 판매합니다.
이동 평균 주기를 조정하거나, 스톱포트 지점을 조정하는 등의 방법으로 최적화 할 수 있다. 또한 다른 지표와 결합하여 의사 결정의 정확성을 향상시킬 수 있다.
이 전략의 전체적인 생각은 명확하고, 이동 평균을 사용하여 추세를 판단하고, 위험을 제어하기 위해 스톱을 설정하는 것이 전형적인 추세 추적 전략이다. 파라미터를 조정하고 다른 판단 지표를 추가함으로써 전략 피드백과 실장 성능을 더욱 향상시킬 수 있다. 그러나 어떤 전략도 완벽하지 않으며, 시장 환경에 따라 지속적인 조정과 최적화가 필요하며, 위험 관리와 함께 시장의 불확실성에 대응한다.
/*backtest
start: 2022-09-30 00:00:00
end: 2023-10-06 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy("MA_Script", overlay=true)
// STEP 1:
// Configure trail stop level with input options (optional)
longTrailPerc=input(title="Trail Long Loss (%)", type=input.float, minval=0.0, step=0.05, defval=0.1)
// Configure backtest start date with inputs
startDate=input(title="Start Date", type=input.integer, defval=1, minval=1, maxval=31)
startMonth=input(title="Start Month", type=input.integer, defval=1, minval=1, maxval=12)
startYear=input(title="Start Year", type=input.integer, defval=2020, minval=1800, maxval=2100)
// See if this bar's time happened on/after start date
afterStartDate=(time >=timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0))
// Calculate Relative Strength Index
rsiValue=rsi(close, 14)
// Calculate moving averages
MA10_Val =sma(close, 10)
//plot(MA10_Val, color=color.yellow, linewidth=1)
MA20_Val =sma(close, 20)
plot(MA20_Val, color=color.green, linewidth=1)
MA50_Val =sma(close, 50)
plot(MA50_Val, color=color.red, linewidth=1)
MA100_Val =sma(close, 100)
plot(MA100_Val, color=color.blue, linewidth=1)
MA200_Val =sma(close, 200)
plot(MA200_Val, color=color.purple, linewidth=1)
// Calculate candlestick
C_BodyHi = max(close, open)
C_BodyLo = min(close, open)
C_Body = C_BodyHi - C_BodyLo
C_UpShadow = high - C_BodyHi
C_DnShadow = C_BodyLo - low
// STEP 2:
// Calculate entry trading conditions
buyCondition_1=crossover(MA10_Val, MA50_Val) and (rsiValue > 30) and ((MA20_Val >= MA100_Val) or (MA50_Val >= MA100_Val))
avg_price = (close + open)/2
// First Entry
if (afterStartDate)
strategy.entry(id="Entry_Trade_1", long=true, limit=avg_price, when=buyCondition_1)
plotchar(afterStartDate and crossover(MA10_Val, MA50_Val), textcolor = color.blue, text = 'MA\n')
// Determine trail stop loss prices
longStopPrice=0.0
longStopPrice :=if (strategy.position_size > 0)
stopValue=C_BodyHi * (1 - longTrailPerc)
max(stopValue, longStopPrice[1])
else
0
plot(longStopPrice, color=color.orange, linewidth=1)
bought_1=strategy.position_size[0] > strategy.position_size[1]
entry_Point_1=valuewhen(bought_1, avg_price, 0)
// STEP 3:
// Calculate exit trading conditions
sellCondition_2=crossunder(MA10_Val, MA50_Val) and (close < MA20_Val)
sellCondition_3_temp=valuewhen((C_BodyHi >= entry_Point_1*1.2), 1, 0)
sellCondition_1=(entry_Point_1*0.95 > close) and (sellCondition_3_temp != 1)
sellCondition_3=(sellCondition_3_temp == 1) and (strategy.position_size > 0) and close <= longStopPrice
plotchar((sellCondition_3 == 1) and (strategy.position_size > 0) and close <= longStopPrice, textcolor = color.red, text = 'TS\n', show_last = 11)
plotchar(crossunder(MA10_Val, MA50_Val), textcolor = color.red, text = 'MA\n')
id_val = ""
stop_val = close
condition = false
if sellCondition_1
id_val := "Exit By Stop Loss At 7%"
stop_val := entry_Point_1*0.93
condition := true
else if sellCondition_2
id_val := "Exit By Take Profit based on MA"
stop_val := close
condition := true
else if sellCondition_3
id_val := "Exit By Trailing Stop"
stop_val := longStopPrice
condition := true
// Submit exit orders for trail stop loss price
if (strategy.position_size > 0)
//strategy.exit(id="Exit By Stop Loss At 7%", from_entry="Entry_Trade_1", stop=entry_Point_1*0.93, when=sellCondition_1)
//strategy.exit(id="Exit By Take Profit based on MA", from_entry="Entry_Trade_1", stop=close, when=sellCondition_2)
strategy.exit(id=id_val, from_entry="Entry_Trade_1", stop=stop_val, when=condition)