Untitled

 avatar
unknown
plain_text
4 months ago
4.3 kB
32
Indexable
//@version=5
indicator(title="MACD RSN MTF", shorttitle="MACD_RSN_MTF", timeframe="", overlay=false)

// Timeframe Input
timeframe_input = input.timeframe(title="Timeframe", defval="D", tooltip="Select timeframe for the indicator")

// Getting inputs
fast_length = input.int(title="Fast Length", defval=3)
slow_length = input.int(title="Slow Length", defval=26)
signal_length = input.int(title="Signal Smoothing", minval=1, maxval=50, defval=9)
enable_comparison = input.bool(title="Enable XU100 Comparison", defval=false)
macd_ma_type = input.string(title="MACD MA Type", defval="EMA", options=["EMA", "SMA", "VAR", "ZEROLAG"])
signal_ma_type = input.string(title="Signal MA Type", defval="EMA", options=["EMA", "SMA", "VAR", "ZEROLAG"])

// Add trailing stop input
trailing_stop_percent = input.float(title="Trailing Stop %", minval=0.1, maxval=100.0, defval=12.0, step=0.1)

// Zerolag MA calculation
zerolag_ma(src, length) =>
    ema1 = ta.ema(src, length)
    ema2 = ta.ema(ema1, length)
    zerolag = 2 * ema1 - ema2
    zerolag

// Helper function for VAR calculation
var_ma(src, length) =>
    valpha = 2 / (length + 1)
    vud1 = src > src[1] ? src - src[1] : 0
    vdd1 = src < src[1] ? src[1] - src : 0
    vUD = math.sum(vud1, 9)
    vDD = math.sum(vdd1, 9)
    vCMO = nz((vUD - vDD) / (vUD + vDD))
    var_ma_value = 0.0
    var_ma_value := nz(valpha * math.abs(vCMO) * src) + (1 - valpha * math.abs(vCMO)) * nz(var_ma_value[1])
    var_ma_value

get_ma(src, length, ma_type) =>
    if ma_type == "EMA"
        ta.ema(src, length)
    else if ma_type == "SMA"
        ta.sma(src, length)
    else if ma_type == "VAR"
        var_ma(src, length)
    else
        zerolag_ma(src, length)

// Standard MACD calculation based on selected MA type
mtf_close = request.security(syminfo.tickerid, timeframe_input, close)

fast_ma = get_ma(mtf_close, fast_length, macd_ma_type)
slow_ma = get_ma(mtf_close, slow_length, macd_ma_type)
macd = fast_ma - slow_ma
signal = get_ma(macd, signal_length, signal_ma_type)
hist = macd - signal

// XU100 Comparative MACD Calculation
comparativeTickerId = "BIST:XU100"
baseSymbol = request.security(syminfo.tickerid, timeframe_input, close)
comparativeSymbol = request.security(comparativeTickerId, timeframe_input, close)
crs = baseSymbol / comparativeSymbol

// Scale factor for CRS (Comparative MACD)
scaleFactor = 1000
fastMA_crs = get_ma(crs * scaleFactor, fast_length, macd_ma_type)
slowMA_crs = get_ma(crs * scaleFactor, slow_length, macd_ma_type)
macd_crs = fastMA_crs - slowMA_crs
signal_crs = get_ma(macd_crs, signal_length, signal_ma_type)
hist_crs = macd_crs - signal_crs

// Select the MACD line to plot based on enable_comparison setting
plot_macd = enable_comparison ? macd_crs : macd
plot_signal = enable_comparison ? signal_crs : signal
plot_hist = enable_comparison ? hist_crs : hist

// Trailing Stop Logic
var float highestPrice = na
var bool inPosition = false

// Entry condition (when MACD crosses above signal)
enterLong = ta.crossover(plot_macd, plot_signal)

if (enterLong)
    inPosition := true
    highestPrice := mtf_close

// Update highest price when in position
if (inPosition)
    highestPrice := math.max(highestPrice, mtf_close)

// Calculate trailing stop price
stopPrice = highestPrice * (1 - trailing_stop_percent / 100)

// Exit condition (trailing stop hit or MACD crosses below signal)
exitLong = (inPosition and (mtf_close <= stopPrice)) or ta.crossunder(plot_macd, plot_signal)

if (exitLong)
    inPosition := false
    highestPrice := na

// Plotting
macd_color = plot_macd > 0 ? color.green : color.red
plot(plot_macd, title="MACD Line", color=macd_color, linewidth=2)
plot(plot_signal, title="Signal Line", color=color.orange, linewidth=2)

// Plot trailing stop level when in position
plot(inPosition ? stopPrice : na, title="Trailing Stop", color=color.red, style=plot.style_linebr)

// Zero line
hline(0, "Zero Line", color=color.new(#787B86, 50))

// Plot buy/sell signals
plotshape(enterLong, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(exitLong, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)
Editor is loading...
Leave a Comment