
이 전략은 여러 평균선과 여러 시간 주기 기반의 고급 정량 거래 시스템입니다. 그것은 거래자가 다양한 유형의 이동 평균을 선택 할 수 있도록 유연하게 허용합니다 (SMA, EMA, WMA, HMA 및 SMMA 포함) 그리고 시장 상황에 따라 일선, 주선 또는 달선과 같은 여러 시간 주기에서 자유롭게 교환 할 수 있습니다. 전략의 핵심 논리는 매출 가격과 선택한 평균선의 위치 관계를 비교하여 매출 신호를 결정하는 것이며, 다른 시간 주기 반복 검사를 결합하여 거래의 정확성을 향상시킵니다.
전략은 모듈화 설계로, 주로 네 개의 핵심 구성 요소를 포함합니다: 평행선 유형 선택 모듈, 시간 주기 선택 모듈, 신호 생성 모듈 및 포지션 관리 모듈. 종료 가격 위에 선택된 평행선을 통과하면, 시스템은 다음 거래 주기 시작할 때 여러 신호를 냅니다. 종료 가격 아래 평행선을 통과하면, 시스템은 다음 거래 주기 시작할 때 평행 신호를 냅니다. 전략은 request.security 함수를 통해 주기 간 데이터 계산을 구현하여 다양한 프레임 타임의 신호 정확성을 보장합니다.
이 전략은 완벽하게 설계된, 논리적으로 명확한 거래 시스템이며, 유연한 파라미터 설정과 여러 확인 메커니즘을 통해 거래자에게 신뢰할 수있는 거래 도구를 제공합니다. 전략의 모듈 디자인은 강력한 확장성을 갖출 수 있도록 해줍니다. 지속적인 최적화를 통해 성능을 더욱 향상시킬 수 있습니다.
/*backtest
start: 2019-12-23 08:00:00
end: 2024-11-27 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Flexible Moving Average Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// Input to select the review frequency (Daily, Weekly, Monthly)
check_frequency = input.string("Weekly", title="Review Frequency", options=["Daily", "Weekly", "Monthly"])
// Input to select the Moving Average method (SMA, EMA, WMA, HMA, SMMA)
ma_method = input.string("EMA", title="Moving Average Method", options=["SMA", "EMA", "WMA", "HMA", "SMMA"])
// Input to select the length of the Moving Average
ma_length = input.int(30, title="Moving Average Length", minval=1)
// Input to select the timeframe for Moving Average calculation
ma_timeframe = input.string("W", title="Moving Average Timeframe", options=["D", "W", "M"])
// Calculate all Moving Averages on the selected timeframe
sma_value = request.security(syminfo.tickerid, ma_timeframe, ta.sma(close, ma_length), lookahead=barmerge.lookahead_off)
ema_value = request.security(syminfo.tickerid, ma_timeframe, ta.ema(close, ma_length), lookahead=barmerge.lookahead_off)
wma_value = request.security(syminfo.tickerid, ma_timeframe, ta.wma(close, ma_length), lookahead=barmerge.lookahead_off)
hma_value = request.security(syminfo.tickerid, ma_timeframe, ta.hma(close, ma_length), lookahead=barmerge.lookahead_off)
smma_value = request.security(syminfo.tickerid, ma_timeframe, ta.rma(close, ma_length), lookahead=barmerge.lookahead_off) // Smoothed Moving Average (SMMA)
// Select the appropriate Moving Average based on user input
ma = ma_method == "SMA" ? sma_value :
ma_method == "EMA" ? ema_value :
ma_method == "WMA" ? wma_value :
ma_method == "HMA" ? hma_value :
smma_value // Default to SMMA
// Variable initialization
var float previous_close = na
var float previous_ma = na
var float close_to_compare = na
var float ma_to_compare = na
// Detect the end of the period (Daily, Weekly, or Monthly) based on the selected frequency
var bool is_period_end = false
if check_frequency == "Daily"
is_period_end := ta.change(time('D')) != 0
else if check_frequency == "Weekly"
is_period_end := ta.change(time('W')) != 0
else if check_frequency == "Monthly"
is_period_end := ta.change(time('M')) != 0
// Store the close and Moving Average values at the end of the period
if is_period_end
previous_close := close[0] // Closing price of the last day of the period
previous_ma := ma[0] // Moving Average value at the end of the period
// Strategy logic
is_period_start = is_period_end
// Check if this is the first bar of the backtest
is_first_bar = barstate.isfirst
if (is_period_start or is_first_bar)
// If the previous period values are not available, use current values
close_to_compare := not na(previous_close) ? previous_close : close[0]
ma_to_compare := not na(previous_ma) ? previous_ma : ma[0]
if close_to_compare < ma_to_compare
// Close price below the MA -> Sell
if strategy.position_size > 0
strategy.close("Long")
else
// Close price above the MA -> Buy/Hold
if strategy.position_size == 0
strategy.entry("Long", strategy.long)
// Close all positions at the end of the backtest period
if barstate.islastconfirmedhistory
strategy.close_all(comment="Backtest End")
// Plot the previous period's close price for comparison
plot(previous_close, color=color.red, title="Previous Period Close", style=plot.style_stepline)
plot(close_to_compare, color=color.blue, title="Close to Compare", style=plot.style_line)
// Plot the selected Moving Average
plot(ma, color=color.white, title="Moving Average", style=plot.style_line, linewidth=3)