
이 전략은 두 개의 이동 평균 (((MA) 을 사용하여 거래 신호를 생성한다. 짧은 기간의 MA가 아래에서 위쪽으로 더 긴 기간의 MA를 통과하면 구매 신호를 생성하고, 짧은 기간의 MA가 위쪽으로 더 긴 기간의 MA를 통과하면 판매 신호를 생성한다. 이 전략은 거래 시간대 (((UTC 8시부터 20시까지) 와 정지점 (((150점) 을 동시에 설정한다.
이 전략은 두 개의 다른 주기의 이동 평균의 교차를 기반으로 거래 신호를 생성하고, 트렌딩 시장에 적합하다. 거래 기간과 고정 스톱포트를 설정하여 위험을 어느 정도 제어할 수 있다. 그러나 이 전략은 흔들리는 시장에서 좋지 않은 성능을 발휘할 수 있으며, 고정 스톱포트는 전략의 수익 공간을 제한할 수 있다.
/*backtest
start: 2024-03-01 00:00:00
end: 2024-03-31 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy("Moving Average Crossover Strategy", overlay=true)
// User-defined moving average periods
ma1Periods = input(5, title="First Moving Average Periods")
ma2Periods = input(20, title="Second Moving Average Periods")
// Calculate moving averages
ma1 = sma(close, ma1Periods)
ma2 = sma(close, ma2Periods)
// Plot moving averages
plot(ma1, color=color.red, linewidth=2, title="First Moving Average")
plot(ma2, color=color.blue, linewidth=2, title="Second Moving Average")
// Detect crossovers and crossunders
bullishCross = crossover(ma1, ma2)
bearishCross = crossunder(ma1, ma2)
// Define trading hours (8 AM to 2 PM UTC)
startHour = 8
endHour = 20
utcHour = hour(time, "UTC")
isMarketOpen = true
// Define profit target
profitTarget = 150
// Check if the price has closed above/below the MA for the past 4 bars
aboveMa = close[4] > ma1[4] and close[3] > ma1[3] and close[2] > ma1[2] and close[1] > ma1[1]
belowMa = close[4] < ma1[4] and close[3] < ma1[3] and close[2] < ma1[2] and close[1] < ma1[1]
// Create buy and sell signals
if (bullishCross and isMarketOpen and aboveMa)
strategy.entry("Buy", strategy.long)
strategy.exit("Sell", "Buy", profit=profitTarget)
if (bearishCross and isMarketOpen and belowMa)
strategy.entry("Sell", strategy.short)
strategy.exit("Cover", "Sell", profit=profitTarget)
// Plot shapes on crossovers
plotshape(series=bullishCross and isMarketOpen and aboveMa, location=location.belowbar, color=color.green, style=shape.labelup, text="Buy")
plotshape(series=bearishCross and isMarketOpen and belowMa, location=location.abovebar, color=color.red, style=shape.labeldown, text="Sell")