如果当前颜色超过 30 分钟,是否有一种方法可以只显示 MACD?

Is there a way to only display MACD if the current color has been the same color for longer than 30 minues?

我试图添加一个时间戳和标志,一旦方向改变就会切换,但我不知道如何让它做我想做的事。

trackdirectionC = 0
trackdirectionP = 0
timerstart = time(timeframe.period)

if direction < 0 
    trackdirectionC := 1
else
    trackdirectionC := -1
    
if trackdirectionC != trackdirectionP
    trackdirectionP == trackdirectionC
    timerstart == time(timeframe.period)

一个想法是将图表的时间范围转换为分钟。然后计算条件为真时的条数,并将其乘以以分钟为单位的分辨率。

//@version=5
indicator("My script")

// Getting inputs
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
src = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])
// Plot colors
col_macd = input(#2962FF, "MACD Line  ", group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "Signal Line  ", group="Color Settings", inline="Signal")
col_grow_above = input(#26A69A, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")
// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
plot(macd, title="MACD", color=col_macd)
plot(signal, title="Signal", color=col_signal)

////////////////////////////            Strategy            ////////////////////////////
in_up_time = input.int(10, "Up time")
in_dwn_time = input.int(15, "Down time")

f_resInMinutes() => 
    _resInMinutes = timeframe.multiplier * (
      timeframe.isseconds ? 1. / 60             :
      timeframe.isminutes ? 1.                  :
      timeframe.isdaily   ? 60. * 24            :
      timeframe.isweekly  ? 60. * 24 * 7        :
      timeframe.ismonthly ? 60. * 24 * 30.4375  : na)

var up_cnt = 0
var dwn_cnt = 0

up_cnt := hist >= 0 ? up_cnt + 1 : 0        // Increase the counter if the current hist is green, reset otherwise
dwn_cnt := hist < 0 ? dwn_cnt + 1 : 0       // Increase the counter if the current hist is red, reset otherwise

up_cnt_time = f_resInMinutes() * up_cnt
dwn_cnt_time = f_resInMinutes() * dwn_cnt

buy = up_cnt_time > in_up_time
sell = dwn_cnt_time > in_dwn_time

_green = color.new(color.green, 85)
_red = color.new(color.red, 85)

bgcolor(buy ? _green : na)
bgcolor(sell ? _red : na)

当然,这只有在您的图表时间范围小于您的目标时才有效。