
이 전략은 Z 점수 (Z-Score) 에 기반한 통계학적 개념으로, 가격의 지역 평균에 대한 통계적 편차를 식별하기 위해 사용된다. 이 전략은 종전 가격의 Z 점수를 계산하고, 그 다음 단기 및 장기 이동 평균을 적용하여 Z 점수를 평형화한다.
이 전략의 핵심은 Z 점수의 계산과 적용이다. Z 점수는 표준 차이의 단위로 데이터 포인트의 표본 평균과 오차를 측정하는 통계에 사용된다. 이 전략에서 Z 점수를 계산하는 공식은 다음과 같다. Z = (폐쇄 가격 - SMA (폐쇄 가격, N)) / STDEV (폐쇄 가격, N) 여기서 N은 사용자 정의의 기본 주기이다.
정책의 실행 과정은 다음과 같습니다:
추가 조건은 다음과 같습니다.
해결책:
평평한 Z 점수 교차에 기반한 동적 최적화 가격 통계 거래 전략은 통계학 원칙에 기반한 간결한 거래 시스템으로, 가격의 지역 평균에 대한 편차와 회귀를 포착하는 데 중점을 두고 있다. 평평한 처리를 통해, 신호 간격 제어 및 동적 필터링을 통해, 이 전략은 노이즈 거래를 효과적으로 줄이고, 신호 품질을 향상시킨다. 이 전략은 특히 불안한 시장과 평균 회귀 행동이 명백한 금융 상품에 적합하다.
그러나 전략에는 통계적 가정, 변수 민감성 및 단일 요소 의사 결정에 의존하는 것과 같은 몇 가지 제한이 있습니다. 트렌드 식별, 변동성 조정, 다중 시간 프레임 분석, 스톱 로즈 메커니즘, 거래량 확인 및 다중 요소 조합과 같은 최적화 조치를 추가함으로써 전략의 거친성과 성능을 크게 향상시킬 수 있습니다.
종합적으로, 이것은 이론적 기반이 단단하고, 간결하고, 이해하기 쉽고, 확장 가능한 전략적 프레임워크를 구현하며, 거래 시스템의 기본 구성 요소 또는 트레이더가 거래에서 통계학의 응용을 이해하는 데 도움이되는 교육 도구로 적합합니다.
/*backtest
start: 2024-06-03 00:00:00
end: 2025-06-02 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"ETH_USDT"}]
*/
//@version=6
strategy("Price Statistical Strategy-Z Score V 1.01", overlay=true)
// === Enable / Disable Z-Score Strategy Block ===
enableZScore = input.bool(true, title="Enable Smoothed Z-Score Strategy", tooltip="When enabled, this block calculates a smoothed Z-Score of the closing price and generates entry/exit signals based on crossover behavior between short-term and long-term smoothed Z-Scores.\n\nRecommended for quick and classic detection of price deviation from mean.\nSensitive to outliers. Best suited for relatively normal-distributed market conditions.")
// === Z-Score Parameters ===
zBaseLength = input.int(3, minval=1, title="Z-Score Base Period")
shortSmooth = input.int(3, title="Short-Term Smoothing")
longSmooth = input.int(5, title="Long-Term Smoothing")
// === Z-Score Calculation Function ===
f_zscore(src, length) =>
mean = ta.sma(src, length)
std_dev = ta.stdev(src, length)
z = (src - mean) / std_dev
z
// === Z-Score Logic ===
zRaw = f_zscore(close, zBaseLength)
zShort = ta.sma(zRaw, shortSmooth)
zLong = ta.sma(zRaw, longSmooth)
// === Minimum gap between identical signals ===
gapBars = input.int(5, minval=1, title="Bars gap between identical signals", tooltip="Minimum number of bars required between two identical signals (entry or exit). Helps reduce signal noise.")
// === Candle-based momentum filters ===
bullish_3bars = close > close[1] and close[1] > close[2] and close[2] > close[3] and close[3] > close[4]
bearish_3bars = close < close[1] and close[1] < close[2] and close[2] < close[3] and close[3] < close[4]
// === Entry and Exit Logic with minimum signal gap and candle momentum filter ===
var int lastEntryBar = na
var int lastExitBar = na
if enableZScore
longCondition = (zShort > zLong)
exitCondition = (zShort < zLong)
if longCondition and (na(lastEntryBar) or bar_index - lastEntryBar > gapBars) and not bullish_3bars
strategy.entry("Z Score", strategy.long)
lastEntryBar := bar_index
if exitCondition and (na(lastExitBar) or bar_index - lastExitBar > gapBars) and not bearish_3bars
strategy.close("Z Score", comment="Z Score")
lastExitBar := bar_index
// === Real-time PnL Table for Last Open Position ===
var table positionTable = table.new(position.bottom_right, 2, 2, border_width=1)
// Header Labels
table.cell(positionTable, 0, 0, "Entry Price", text_color=color.white, bgcolor=color.gray)
table.cell(positionTable, 1, 0, "Unrealized PnL (%)", text_color=color.white, bgcolor=color.gray)
// Values (only when position is open)
isLong = strategy.position_size > 0
entryPrice = strategy.position_avg_price
unrealizedPnL = isLong ? (close - entryPrice) / entryPrice * 100 : na
// Define dynamic text color for PnL
pnlColor = unrealizedPnL > 0 ? color.green : unrealizedPnL < 0 ? color.red : color.gray
// Update Table Content
if isLong
table.cell(positionTable, 0, 1, str.tostring(entryPrice, "#.####"), text_color=color.gray, bgcolor=color.new(color.gray, 90))
table.cell(positionTable, 1, 1, str.tostring(unrealizedPnL, "#.##") + " %", text_color=pnlColor, bgcolor=color.new(pnlColor, 90))
else
table.cell(positionTable, 0, 1, "—", text_color=color.gray, bgcolor=color.new(color.gray, 90))
table.cell(positionTable, 1, 1, "—", text_color=color.gray, bgcolor=color.new(color.gray, 90))