Нужно исправить мой код и улучшить индикатор для проноза цены биткоина. Также сейчас тренд опрееляется плохо, есть серые неопределенные зоны

//@version=5
indicator("CVD Buy/Sell Signals", overlay=true)

anchorInput = input.timeframe("1D", "Anchor period")
lowerTimeframeTooltip = "The indicator scans lower timeframe data to approximate up and down volume used in the delta calculation. By default, the timeframe is chosen automatically. These inputs override this with a custom timeframe.
\n\nHigher timeframes provide more historical data, but the data will be less precise."
useCustomTimeframeInput = input.bool(false, "Use custom timeframe", tooltip = lowerTimeframeTooltip)
lowerTimeframeInput = input.timeframe("1", "Timeframe")

upAndDownVolume() =>
posVol = 0.0
negVol = 0.0

var isBuyVolume = true

switch
close > open => isBuyVolume := true
close < open => isBuyVolume := false
close > close[1] => isBuyVolume := true
close < close[1] => isBuyVolume := false

if isBuyVolume
posVol += volume
else
negVol -= volume

posVol + negVol

var lowerTimeframe = switch
useCustomTimeframeInput => lowerTimeframeInput
timeframe.isseconds => "1S"
timeframe.isintraday => "1"
timeframe.isdaily => "5"
=> "60"

diffVolArray = request.security_lower_tf(syminfo.tickerid, lowerTimeframe, upAndDownVolume())

getHighLow(arr) =>
float cumVolume = na
float maxVolume = na
float minVolume = na

for item in arr
cumVolume := nz(cumVolume) + item
maxVolume := math.max(nz(maxVolume), cumVolume)
minVolume := math.min(nz(minVolume), cumVolume)

[maxVolume, minVolume, cumVolume]

[maxVolume, minVolume, lastVolume] = getHighLow(diffVolArray)

var cumulLastVolume = 0.0
anchorChange = timeframe.change(anchorInput) or (not na(lastVolume) and na(lastVolume[1]))
cumulOpenVolume = anchorChange ? 0.0 : cumulLastVolume[1]
cumulMaxVolume = cumulOpenVolume + maxVolume
cumulMinVolume = cumulOpenVolume + minVolume
cumulLastVolume := cumulOpenVolume + lastVolume

col = cumulLastVolume >= cumulOpenVolume ? color.teal : color.red
hline(0)
plotcandle(cumulOpenVolume, cumulMaxVolume, cumulMinVolume, cumulLastVolume, "CVD", color = col, bordercolor = col, wickcolor = col)

var cumVol = 0.
cumVol += nz(volume)
if barstate.islast and cumVol == 0
runtime.error("The data vendor doesn't provide volume data for this symbol.")




// Параметры
lookback_period = input.int(20, "Lookback Period", minval=5)
price_trend_period = input.int(10, "Price Trend Period", minval=5)
volume_threshold = input.float(1.5, "Volume Threshold", minval=1, step=0.1)
rsi_period = input.int(14, "RSI Period", minval=1)

// Цвета для подсветки и линий
sky_color = input.color(color.new(color.green, 80), "Sky Color")
earth_color = input.color(color.new(color.red, 80), "Earth Color")

// Расчеты
price_trend = ta.ema(close, price_trend_period)
volume_sma = ta.sma(volume, lookback_period)
relative_volume = volume / volume_sma
rsi = ta.rsi(close, rsi_period)

// Динамические пороговые значения RSI
rsi_upper = ta.highest(rsi, lookback_period)
rsi_lower = ta.lowest(rsi, lookback_period)
rsi_mid = (rsi_upper + rsi_lower) / 2

// Балансовый Объем (OBV)
obv = ta.obv

// Функция для определения дивергенции
divergence(indicator, price, length) =>
indicator_change = indicator - indicator[length]
price_change = close - close[length]
(indicator_change > 0 and price_change < 0) or (indicator_change < 0 and price_change > 0)

// Улучшенная логика сигналов
buy_signal = cumulLastVolume > cumulLastVolume[1] and
close > price_trend and
relative_volume >= volume_threshold and
divergence(cumulLastVolume, close, lookback_period) and
rsi < rsi_mid and
obv > obv[1]

sell_signal = cumulLastVolume < cumulLastVolume[1] and
close < price_trend and
relative_volume >= volume_threshold and
divergence(cumulLastVolume, close, lookback_period) and
rsi > rsi_mid and
obv < obv[1]



// Дополнительные параметры
trend_length = input.int(50, "Trend Length", minval=10)
exit_rsi_threshold = input.int(70, "Exit RSI Threshold", minval=50, maxval=100)
exit_volume_threshold = input.float(0.8, "Exit Volume Threshold", minval=0.5, maxval=2)

// Расчеты трендовых индикаторов
ema_short = ta.ema(close, price_trend_period)
ema_long = ta.ema(close, trend_length)
[macd_line, signal_line, _] = ta.macd(close, 12, 26, 9)

// Определение тренда
trend = 0
if ema_short > ema_long and macd_line > signal_line
trend := 1
else if ema_short < ema_long and macd_line < signal_line
trend := -1

// Функция для определения потенциального выхода из позиции
exit_condition(is_long) =>
volume_condition = relative_volume < exit_volume_threshold
rsi_condition = is_long ? rsi > exit_rsi_threshold : rsi < (100 - exit_rsi_threshold)
trend_change = is_long ? ema_short < ema_long : ema_short > ema_long

volume_condition and (rsi_condition or trend_change)

// Определение сигналов входа и выхода
var in_position = false
var entry_price = 0.0
var position_type = 0 // 1 для длинной, -1 для короткой

if buy_signal and not in_position
in_position := true
entry_price := close
position_type := 1

if sell_signal and not in_position
in_position := true
entry_price := close
position_type := -1

exit_signal = in_position and ((position_type == 1 and exit_condition(true)) or (position_type == -1 and exit_condition(false)))

if exit_signal
in_position := false
entry_price := 0.0
position_type := 0

// Отображение тренда линией
trend_color = trend == 1 ? color.green : trend == -1 ? color.red : color.gray
plot(ema_long, color=trend_color, linewidth=2, title="Trend Line")

// Отображение сигналов входа и выхода
plotshape(buy_signal and not in_position, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(sell_signal and not in_position, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)
plotshape(exit_signal, title="Exit Signal", location=location.absolute, color=color.yellow, style=shape.circle, size=size.small)

// Отображение вертикальной подсветки
bgcolor(buy_signal ? sky_color : na)
bgcolor(sell_signal ? earth_color : na)

// Отображение вертикальных линий
if buy_signal
line.new(x1=bar_index, y1=low, x2=bar_index, y2=high, color=color.blue, width=4)

if sell_signal
line.new(x1=bar_index, y1=low, x2=bar_index, y2=high, color=color.orange, width=4)

// Отображение светящихся надписей
plotchar(buy_signal, char="▲", location=location.belowbar, color=color.new(color.green, 0), size=size.small, text="НЕБО", textcolor=color.new(color.green, 0))
plotchar(sell_signal, char="▼", location=location.abovebar, color=color.new(color.red, 0), size=size.small, text="ЗЕМЛЯ", textcolor=color.new(color.red, 0))

// Отображение CVD
plot(cumulLastVolume, title="CVD", color=color.blue, style=plot.style_line, linewidth=2)

// Отображение OBV (опционально)
plot(obv, title="OBV", color=color.purple, style=plot.style_line, linewidth=1, display=display.none)