Code:
# Define inputs
input obvLength = 21;
input rsiLength = 14;
input rsiOverbought = 70;
input rsiOversold = 30;
input signalThreshold = 2;
# Calculate OBV and RSI
def obv = OnBalanceVolume()[-obvLength];
def rsi = reference RSI(length = rsiLength);
# Define conditions for buy and sell signals
def buySignal = obv > obv[1] and rsi < rsiOversold;
def sellSignal = obv < obv[1] and rsi > rsiOverbought;
# Count the number of occurrences of buy and sell signals over the threshold
def buySignalCount = sum(buySignal, signalThreshold);
def sellSignalCount = sum(sellSignal, signalThreshold);
# Plot the signals on the chart
plot buy = if buySignalCount == signalThreshold then low - 5 * TickSize() else Double.NaN;
plot sell = if sellSignalCount == signalThreshold then high + 5 * TickSize() else Double.NaN;
# Fill the buy and sell signals with colors
buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buy.SetDefaultColor(Color.GREEN);
buy.SetLineWeight(3);
sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sell.SetDefaultColor(Color.RED);
sell.SetLineWeight(3);
# Create a label to show buy or sell signals in the watchlist
AddLabel(yes, if buySignalCount == signalThreshold then "Buy" else if sellSignalCount == signalThreshold then "Sell" else "",
if buySignalCount == signalThreshold then Color.GREEN else if sellSignalCount == signalThreshold then Color.RED else Color.GRAY);