OPEN-SOURCE SCRIPT

Custom Indicator with Normalized MACD, 5-in-1 RSI, and SMA

117
//version=6
indicator("Custom Indicator with Normalized MACD, 5-in-1 RSI, and SMA", overlay=true)

// --- Normalized MACD by Glaz ---
// Settings: Fast MA = 13, rest default (Slow MA = 34, Signal = 5)
fastLength = input.int(13, title="Fast MA Length", minval=1)
slowLength = input.int(34, title="Slow MA Length", minval=1)
signalLength = input.int(5, title="Signal Length", minval=1)

[macdLine, signalLine, histogram] = ta.macd(close, fastLength, slowLength, signalLength)
highestMacd = ta.highest(macdLine, 100)
lowestMacd = ta.lowest(macdLine, 100)
macNorm = 100 * (macdLine - lowestMacd) / (highestMacd - lowestMacd)
triggerLine = ta.ema(macNorm, signalLength)

// Condition 1: Normalized MACD
macdLongCondition = macNorm > triggerLine
macdShortCondition = macNorm < triggerLine

// --- 5-in-1 by Marco Valente (RSI and SMA RSI only) ---
// Settings: RSI Length = 21, SMA RSI Length = 55, rest default
rsiLength = input.int(21, title="RSI Length", minval=1)
smaRsiLength = input.int(55, title="SMA RSI Length", minval=1)

rsi = ta.rsi(close, rsiLength)
smaRsi = ta.sma(rsi, smaRsiLength)

// Condition 2: RSI vs SMA RSI
rsiLongCondition = rsi > smaRsi
rsiShortCondition = rsi < smaRsi

// --- Simple Moving Average ---
// Settings: Length = 13
smaLength = input.int(13, title="SMA Length", minval=1)
sma = ta.sma(close, smaLength)

// Candle color detection (green = close > open, red = close < open)
isGreenCandle = close > open
isRedCandle = close < open

// Long Signal Logic:
// 1. MACD Norm > Trigger
// 2. RSI > SMA RSI
// 3. First green candle closes above SMA
longCondition = macdLongCondition and rsiLongCondition and isGreenCandle and close > sma
var bool hasLongSignal = false // Tracks if a buy signal has been printed
buySignal = longCondition and not hasLongSignal and not hasLongSignal[1] // Only first signal

if buySignal
hasLongSignal := true // Mark that a buy signal has occurred
if not macdLongCondition or not rsiLongCondition // Reset when conditions reverse
hasLongSignal := false

// Short Signal Logic:
// 1. MACD Norm < Trigger
// 2. RSI < SMA RSI
// 3. First red candle closes below SMA
shortCondition = macdShortCondition and rsiShortCondition and isRedCandle and close < sma
var bool hasShortSignal = false // Tracks if a sell signal has been printed
sellSignal = shortCondition and not hasShortSignal and not hasShortSignal[1] // Only first signal

if sellSignal
hasShortSignal := true // Mark that a sell signal has occurred
if not macdShortCondition or not rsiShortCondition // Reset when conditions reverse
hasShortSignal := false

// Plotting
plot(sma, title="SMA", color=color.blue, linewidth=2)
plotshape(buySignal, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small, text="BUY")
plotshape(sellSignal, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small, text="SELL")

免責聲明

這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。