本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Market Regime Analyzer", "Market Regime", overlay = true)
// === 1. Inputs ===
langSel = input.string("한국어", "Display language", options = ["한국어", "English"], display = display.none, tooltip = "Language of the status table texts on the chart.")
bool isKo = langSel == "한국어"
ethBtcSource = input.source(close, "ETHBTC source", group = "ETHBTC settings", display = display.none)
maLength = input.int(20, "ETHBTC trend MA length", minval = 1, group = "ETHBTC settings", display = display.none)
// Sideways filter (CCAOS CHOP / ADX rule applied to BTCUSDT)
grpChop = "Sideways filter (CCAOS)"
useChopFilter = input.bool(true, "Hold regime changes in sideways markets", group = grpChop, display = display.none, tooltip = "While BTCUSDT is sideways by the CCAOS rule (CHOP above the sideways level or ADX below the minimum trend strength), the regime is not changed and the previous regime is kept.")
confirmBars = input.int(2, "Bars to confirm a regime change", minval = 1, maxval = 10, group = grpChop, display = display.none, tooltip = "A new regime must appear on this many bars in a row before the regime changes. 1 = change immediately.")
chopLength = input.int(14, "CHOP Length", minval = 1, group = grpChop, display = display.none)
adxLength = input.int(9, "ADX Smoothing Length", minval = 1, group = grpChop, display = display.none)
diLength = input.int(9, "DI Length", minval = 1, group = grpChop, display = display.none)
chopChoppyThresh = input.float(61.8, "CHOP sideways level (consolidation >)", group = grpChop, display = display.none)
adxTrendThresh = input.float(15.0, "ADX minimum trend strength (>)", group = grpChop, display = display.none)
// === 2. Multi-ticker data (BTCUSDT and ETHBTC) ===
btcUsdtClose = request.security("BINANCE:BTCUSDT", timeframe.period, close, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
ethBtcClose = request.security("BINANCE:ETHBTC", timeframe.period, ethBtcSource, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
// CCAOS sideways check (calculated on the requested symbol's bars)
f_choppy() =>
float ci = 100 * math.log10(math.sum(ta.atr(1), chopLength) / (ta.highest(high, chopLength) - ta.lowest(low, chopLength))) / math.log10(chopLength)
float up = ta.change(high)
float down = -ta.change(low)
float plusDM = na(up) ? na : (up > down and up > 0 ? up : 0.0)
float minusDM = na(down) ? na : (down > up and down > 0 ? down : 0.0)
float trur = ta.rma(ta.tr, diLength)
float plus = fixnan(100 * ta.rma(plusDM, diLength) / trur)
float minus = fixnan(100 * ta.rma(minusDM, diLength) / trur)
float s = plus + minus
float adx = 100 * ta.rma(math.abs(plus - minus) / (s == 0 ? 1 : s), adxLength)
[(ci > chopChoppyThresh) or (adx < adxTrendThresh), ci, adx]
[btcChoppy, btcChop, btcAdx] = request.security("BINANCE:BTCUSDT", timeframe.period, f_choppy(), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
bool isChoppy = useChopFilter and btcChoppy == true
// ETHBTC trend: price vs. its moving average
ethBtcMa = ta.sma(ethBtcClose, maLength)
ethBtcRising = ethBtcClose > ethBtcMa
// BTC direction: change over the last 3 bars
btcChange = ta.change(btcUsdtClose, 3)
isBtcUp = btcChange > 0
isBtcFlat = math.abs(btcChange) <= (btcUsdtClose * 0.002) // within 0.2% = flat
// === 3. Four market regimes ===
// 1 = alt-season signal, 2 = BTC dominance, 3 = alt defense/rebound, 4 = risk-off
bool isAltSeasonSignal = ethBtcRising and (isBtcUp or isBtcFlat)
bool isBtcDominance = (not ethBtcRising) and isBtcUp
bool isRiskOff = (not ethBtcRising) and (not isBtcUp)
bool isEthDefensive = ethBtcRising and (not isBtcUp)
int rawRegime = isAltSeasonSignal ? 1 : isBtcDominance ? 2 : isEthDefensive ? 3 : isRiskOff ? 4 : 0
// === 3-1. Regime confirmation (sideways hold + consecutive bars) ===
// - While BTCUSDT is sideways, the previous regime is kept
// - Otherwise a new regime must appear on confirmBars bars in a row
var int regime = 0
var int candRegime = 0
var int candCount = 0
if rawRegime != 0
if regime == 0
regime := rawRegime
else if rawRegime == regime
candRegime := 0
candCount := 0
else if isChoppy
candRegime := 0
candCount := 0
else
if rawRegime == candRegime
candCount += 1
else
candRegime := rawRegime
candCount := 1
if candCount >= confirmBars
regime := rawRegime
candRegime := 0
candCount := 0
// === 4. Background color and status text ===
string regimeText = isKo ?
(regime == 1 ? "🚀 [알트 불장 전조] ETH 상대적 강세 (BTC 상승/횡보 + ETH 수급 유입)" :
regime == 2 ? "👑 [비트코인 독주] 자금 쏠림 (BTC 상승 + ETH 횡보/약세)" :
regime == 3 ? "🛡️ [알트 방어/반등] BTC 하락 속 ETH 상대적 방어" :
regime == 4 ? "⚠️ [하락장/위험회피] 전반적 위축 (BTC/ETH 동반 하락)" : "") :
(regime == 1 ? "🚀 [Alt-season signal] ETH relative strength (BTC up/flat + inflow to ETH)" :
regime == 2 ? "👑 [BTC dominance] Money concentrating in BTC (BTC up + ETH flat/weak)" :
regime == 3 ? "🛡️ [Alt defense/rebound] ETH holding up while BTC falls" :
regime == 4 ? "⚠️ [Risk-off] Broad weakness (BTC and ETH both falling)" : "")
color regimeColor = regime == 1 ? color.new(color.green, 85) : regime == 2 ? color.new(color.orange, 85) :
regime == 3 ? color.new(color.blue, 85) : regime == 4 ? color.new(color.red, 85) : color.new(color.gray, 85)
// Background color
bgcolor(regimeColor, title = "Regime background")
// Sideways bars (regime change on hold): small dot at the bottom
plotshape(isChoppy, title = "Sideways (regime change on hold)", style = shape.circle, location = location.bottom, color = color.new(color.gray, 40), size = size.tiny, display = display.pane)
// === 5. Status table (top right) ===
var table statusTable = table.new(position = position.top_right, columns = 1, rows = 3, bgcolor = color.black, border_color = color.gray, border_width = 1)
if barstate.islast
table.cell(statusTable, 0, 0, isKo ? "시장 국면 분석기 (Market Regime)" : "Market Regime Analyzer", text_color = color.white, text_size = size.small, bgcolor = color.gray)
table.cell(statusTable, 0, 1, regimeText, text_color = color.white, text_size = size.small, bgcolor = color.black)
string vals = " (CHOP " + str.tostring(btcChop, "#.#") + " / ADX " + str.tostring(btcAdx, "#.#") + ")"
string chopTxt = not useChopFilter ? (isKo ? "횡보 필터 꺼짐" : "Sideways filter off") : isChoppy ? (isKo ? "BTC 횡보 중: 국면 변경 보류" : "BTC sideways: regime change on hold") + vals : (isKo ? "BTC 추세 구간" : "BTC trending") + vals
table.cell(statusTable, 0, 2, chopTxt, text_color = isChoppy ? color.orange : color.silver, text_size = size.small, bgcolor = color.black)
// === 6. Alert (only when the confirmed regime changes) ===
alertcondition(regime != regime[1] and regime[1] != 0, title = "Market regime change", message = "Market regime changed.")