//@version=5
indicator("Trend Signals with TP & SL [UAlgo] MTF (No Repaint)", shorttitle="Trend Signals MTF",
overlay=true, max_lines_count=100, max_labels_count=100)
// === INPUTS ===
timeframe = input.timeframe("", title="Higher Timeframe (blank = current)")
src = input(hl2, title="Source")
Multiplier = input.float(2.0, title="Sensitivity (0.5 - 5)", step=0.1, minval=0.5, maxval=5)
atrPeriods = input.int(14, title="ATR Length")
atrMethod = input.string("Method 1", title="ATR Calculation Method", options=["Method 1",
"Method 2"])
stopLossPercent = input.float(2.0, title="Stop Loss Percent (0 = Disabled)", minval=0.0)
// === MTF HANDLING ===
src_htf = timeframe == "" ? src : request.security(syminfo.tickerid, timeframe, src)
close_htf = timeframe == "" ? close : request.security(syminfo.tickerid, timeframe, close)
open_htf = timeframe == "" ? open : request.security(syminfo.tickerid, timeframe, open)
high_htf = timeframe == "" ? high : request.security(syminfo.tickerid, timeframe, high)
low_htf = timeframe == "" ? low : request.security(syminfo.tickerid, timeframe, low)
// === CORE CALCULATIONS ===
atr_basic = ta.atr(atrPeriods)
atr_sma = ta.sma(ta.tr, atrPeriods)
atr = atrMethod == "Method 1" ? atr_basic : atr_sma
up = src_htf - (Multiplier * atr)
dn = src_htf + (Multiplier * atr)
var float entryPrice = na
var float stopLoss = na
var float tp1 = na
var float tp2 = na
var float tp3 = na
var int position = 0
var line entryLine = na
var line stopLine = na
var line tp1Line = na
var line tp2Line = na
var line tp3Line = na
var label buySellLabel = na
// === TRADE LOGIC ===
// Buy and Sell conditions
buySignal = close_htf > up
sellSignal = close_htf < dn
// Entry and TP/SL calculations
if (buySignal and na(entryPrice)) // Only trigger once for each entry
entryPrice := close_htf
stopLoss := entryPrice * (1 - stopLossPercent / 100)
tp1 := entryPrice * (1 + stopLossPercent / 100)
tp2 := entryPrice * (1 + 2 * stopLossPercent / 100)
tp3 := entryPrice * (1 + 3 * stopLossPercent / 100)
position := 1
// Draw lines and labels
line.delete(entryLine)
line.delete(stopLine)
line.delete(tp1Line)
line.delete(tp2Line)
line.delete(tp3Line)
label.delete(buySellLabel)
entryLine := line.new(bar_index, entryPrice, bar_index + 10, entryPrice, color=color.blue, width=2)
stopLine := line.new(bar_index, stopLoss, bar_index + 10, stopLoss, color=color.red)
tp1Line := line.new(bar_index, tp1, bar_index + 10, tp1, color=color.green)
tp2Line := line.new(bar_index, tp2, bar_index + 10, tp2, color=color.green)
tp3Line := line.new(bar_index, tp3, bar_index + 10, tp3, color=color.green)
buySellLabel := label.new(bar_index, entryPrice, "BUY\n" + str.tostring(entryPrice, "#.##"),
color=color.blue, style=label.style_label_up, size=size.small)
if (sellSignal and na(entryPrice)) // Only trigger once for each entry
entryPrice := close_htf
stopLoss := entryPrice * (1 + stopLossPercent / 100)
tp1 := entryPrice * (1 - stopLossPercent / 100)
tp2 := entryPrice * (1 - 2 * stopLossPercent / 100)
tp3 := entryPrice * (1 - 3 * stopLossPercent / 100)
position := -1
// Draw lines and labels
line.delete(entryLine)
line.delete(stopLine)
line.delete(tp1Line)
line.delete(tp2Line)
line.delete(tp3Line)
label.delete(buySellLabel)
entryLine := line.new(bar_index, entryPrice, bar_index + 10, entryPrice, color=color.purple,
width=2)
stopLine := line.new(bar_index, stopLoss, bar_index + 10, stopLoss, color=color.red)
tp1Line := line.new(bar_index, tp1, bar_index + 10, tp1, color=color.green)
tp2Line := line.new(bar_index, tp2, bar_index + 10, tp2, color=color.green)
tp3Line := line.new(bar_index, tp3, bar_index + 10, tp3, color=color.green)
buySellLabel := label.new(bar_index, entryPrice, "SELL\n" + str.tostring(entryPrice, "#.##"),
color=color.purple, style=label.style_label_down, size=size.small)
// === TP/SL PRICE LABELS ===
if not na(entryPrice)
var label slPriceLabel = na
var label tp1PriceLabel = na
var label tp2PriceLabel = na
var label tp3PriceLabel = na
label.delete(slPriceLabel)
label.delete(tp1PriceLabel)
label.delete(tp2PriceLabel)
label.delete(tp3PriceLabel)
slPriceLabel := label.new(bar_index + 5, stopLoss, "SL\n" + str.tostring(stopLoss, "#.##"),
color=color.red, style=label.style_label_left, size=size.tiny)
tp1PriceLabel := label.new(bar_index + 5, tp1, "TP1\n" + str.tostring(tp1, "#.##"),
color=color.green, style=label.style_label_left, size=size.tiny)
tp2PriceLabel := label.new(bar_index + 5, tp2, "TP2\n" + str.tostring(tp2, "#.##"),
color=color.green, style=label.style_label_left, size=size.tiny)
tp3PriceLabel := label.new(bar_index + 5, tp3, "TP3\n" + str.tostring(tp3, "#.##"),
color=color.green, style=label.style_label_left, size=size.tiny)