OPEN-SOURCE SCRIPT

TH Uptrend Consolidation

//version=5
indicator("Custom Buy Signal with Time Filter", overlay=true)

// Inputs for RSI
rsi_length = input.int(14, title="RSI Length")
rsi_source = input.source(close, title="RSI Source")
rsi_lower = input.float(50, title="RSI Lower Bound")
rsi_upper = input.float(65, title="RSI Upper Bound")

// Inputs for EMA
ema_short_length = input.int(20, title="EMA Short Length (EMA20)")
ema_long_length = input.int(50, title="EMA Long Length (EMA50)")

// Inputs for Time Filter
signal_cooldown = input.int(14, title="Cooldown Period (Days)")

// Calculate RSI and EMAs
rsi_value = ta.rsi(rsi_source, rsi_length)
ema20 = ta.ema(close, ema_short_length)
ema50 = ta.ema(close, ema_long_length)

// Calculate Price Distance from EMA20
price_distance_percent = math.abs(close - ema20) / ema20 * 100

// Conditions
rsi_condition = (rsi_value >= rsi_lower and rsi_value <= rsi_upper)
ema_condition = ema20 > ema50
price_condition = close > ema20
distance_condition = price_distance_percent <= 5

// Final Buy Signal Conditions
buy_signal_raw = rsi_condition and ema_condition and price_condition and distance_condition

// Time Filter Logic
var float last_signal_time = na // Persistent variable for last signal time
is_cooldown_over = na(last_signal_time) or (time - last_signal_time > signal_cooldown * 86400000) // 86400000 ms in a day
buy_signal = buy_signal_raw and is_cooldown_over

if buy_signal
last_signal_time := time // Update last signal time when a new signal is generated

// Plot EMAs
plot(ema20, color=color.blue, title="EMA20")
plot(ema50, color=color.orange, title="EMA50")

// Highlight Buy Signals
plotshape(series=buy_signal, style=shape.labelup, location=location.belowbar, color=color.green, size=size.small, title="Buy Signal")
Chart patternsMoving Averages

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer