Ниже представлен код на Pine Script для TradingView, который объединяет несколько теорий и индикаторов для определения режима флета и тренда. Индикатор использует 7 различных подходов для оценки рыночного режима и выводит результат в виде цветовых индикаций на графике.
//@version=5
indicator("Флет vs Тренд Индикатор", overlay=true)
// 1. ADX для определения силы тренда
adx_length = input.int(14, title="ADX Length")
adx_value = ta.adx(adx_length)
is_trend = adx_value > 25 ? true : false
// 2. Угол наклона скользящей средней (200 SMA)
sma_length = input.int(200, title="SMA Length")
sma_angle = ta.slope(ta.sma(close, sma_length), sma_length)
is_flat_sma = abs(sma_angle) < input.float(0.1, title="SMA Angle Threshold") ? true : false
// 3. Волатильность (ATR)
atr_length = input.int(14, title="ATR Length")
atr_value = ta.atr(atr_length)
atr_threshold = input.float(10, title="ATR Threshold")
is_flat_atr = atr_value < atr_threshold ? true : false
// 4. Диапазон цен (High-Low)
hl_range = high - low
hl_ma = ta.sma(hl_range, 20)
hl_threshold = input.float(0.1, title="High-Low Range Threshold")
is_flat_hl = hl_ma < hl_threshold * ta.sma(close, 20) ? true : false
// 5. Корреляция цены и скользящей средней
corr_length = input.int(50, title="Correlation Length")
corr_coef = ta.correl(close, ta.sma(close, corr_length), corr_length)
corr_threshold = input.float(0.7, title="Correlation Threshold")
is_trend_corr = abs(corr_coef) > corr_threshold ? true : false
// 6. Индекс направленного движения (DMI)
pdi = ta.pdi(14)
mdi = ta.mdi(14)
dmi_diff = pdi - mdi
dmi_threshold = input.float(5, title="DMI Threshold")
is_trend_dmi = abs(dmi_diff) > dmi_threshold ? true : false
// 7. Анализ автокорреляции (простая реализация)
autocorr_length = input.int(10, title="Autocorrelation Length")
price_diff = close - close[autocorr_length]
price_diff_ma = ta.sma(price_diff, autocorr_length)
autocorr_threshold = input.float(0.5, title="Autocorrelation Threshold")
is_trend_autocorr = abs(price_diff_ma) > autocorr_threshold * ta.sma(close, autocorr_length) ? true : false
// Комбинированное решение
is_flat = is_flat_sma and is_flat_atr and is_flat_hl
is_trend_combined = is_trend and is_trend_corr and is_trend_dmi and is_trend_autocorr
// Визуализация
color_flat = is_flat ? color.new(color.blue, 90) : na
color_trend = is_trend_combined ? color.new(color.red, 90) : na
bgcolor(color_flat)
bgcolor(color_trend, title="Тренд", overlay=true)
// Отображение текста
label_text = is_flat ? "Флет" : is_trend_combined ? "Тренд" : "Неопределено"
label.new(bar_index, high, text=label_text, textcolor=color.white, style=label.style_label_up, yloc=yloc.abovebar)
// Вывести значения индикаторов в панель
plot(adx_value, color=color.orange, title="ADX")
hline(25, "ADX Threshold", color=color.gray)
plot(atr_value, color=color.purple, title="ATR")
hline(atr_threshold, "ATR Threshold", color=color.gray)
plot(hl_ma, color=color.green, title="High-Low Range")
plot(corr_coef, color=color.yellow, title="Correlation")
plot(dmi_diff, color=color.pink, title="DMI Diff")
plot(price_diff_ma, color=color.aqua, title="Autocorrelation")