//@version=6 strategy('Trend Signals', shorttitle = 'Trend Signals', overlay = true) // Trend Continuation Signals with TP & SL src = input(ohlc4, title = 'Source', group = 'Trend Continuation Signals with TP & SL') Multiplier = input.float(2, title = 'Sensitivity (0.5 - 5)', step = 0.1, minval = 0.5, maxval = 5, group = 'Trend Continuation Signals with TP & SL') atrPeriods = input.int(14, title = 'ATR Length', group = 'Trend Continuation Signals with TP & SL') prevent_multiple_positions = input.bool(true, title = 'Prevent Multiple Positions', group = 'Position Management') atr_tp_multiplier = input.float(2.0, title = 'ATR TP Multiplier', group = 'TP & SL', inline = 'ATR') atr_sl_multiplier = input.float(1.0, title = 'ATR SL Multiplier', group = 'TP & SL', inline = 'ATR') trailOffset = input.float(40, title = 'Trail Offset', group = 'TP & SL', inline = 'ATR') vwap = ta.vwap(ohlc4) atr = ta.atr(atrPeriods) up = src - Multiplier * atr up1 = nz(up[1], up) up := close[1] > up1 ? math.max(up, up1) : up dn = src + Multiplier * atr dn1 = nz(dn[1], dn) dn := close[1] < dn1 ? math.min(dn, dn1) : dn trend = 1 trend := nz(trend[1], trend) trend := trend == -1 and ohlc4 > dn1 ? 1 : trend == 1 and ohlc4 < up1 ? -1 : trend buySignal = trend == 1 and trend[1] == -1 sellSignal = trend == -1 and trend[1] == 1 pos = 0.0 pos := buySignal ? 1 : sellSignal ? -1 : pos[1] var bool pendingLong = false var bool pendingShort = false // set signal pending if buySignal and pos[1] != 1 pendingLong := true if sellSignal and pos[1] != -1 pendingShort := true // confirm + fire longCond = pendingLong and ohlc4 >= vwap shortCond = pendingShort and ohlc4 <= vwap // reset once trade taken if longCond pendingLong := false if shortCond pendingShort := false entryOfLongPosition = ta.valuewhen(longCond, close, 0) entryOfShortPosition = ta.valuewhen(shortCond, close, 0) stopLossForLong = entryOfLongPosition - atr_sl_multiplier * atr stopLossForShort = entryOfShortPosition + atr_sl_multiplier * atr takeProfitForLong = entryOfLongPosition + atr_tp_multiplier * atr takeProfitForShort = entryOfShortPosition - atr_tp_multiplier * atr long_sl = low < stopLossForLong and pos[1] == 1 short_sl = high > stopLossForShort and pos[1] == -1 if long_sl or short_sl pos := 0 pos lindex = ta.valuewhen(longCond, bar_index, 0) sindex = ta.valuewhen(shortCond, bar_index, 0) entryColor = pos == 1 ? color.blue : color.purple if pos != 0 lineEntry = line.new(bar_index+50, pos > 0 ? entryOfLongPosition : entryOfShortPosition, pos > 0 ? lindex : sindex, pos > 0 ? entryOfLongPosition : entryOfShortPosition, color = entryColor) line.delete(lineEntry[1]) stopLine = line.new(bar_index+50, pos > 0 ? stopLossForLong : stopLossForShort, pos > 0 ? lindex : sindex, pos > 0 ? stopLossForLong : stopLossForShort, color = color.red) tpLine = line.new(bar_index+50, pos > 0 ? takeProfitForLong : takeProfitForShort, pos > 0 ? lindex : sindex, pos > 0 ? takeProfitForLong : takeProfitForShort, color = color.green) line.delete(stopLine[1]) line.delete(tpLine[1]) labelEntry = label.new(bar_index+50, pos > 0 ? entryOfLongPosition : entryOfShortPosition, color = entryColor, textcolor = #000000, style = label.style_label_left, text = 'Entry Price: ' + str.tostring(pos > 0 ? entryOfLongPosition : entryOfShortPosition)) label.delete(labelEntry[1]) labelStop = label.new(bar_index+50, pos > 0 ? stopLossForLong : stopLossForShort, color = color.red, textcolor = #000000, style = label.style_label_left, text = 'Stop Loss Price: ' + str.tostring(math.round((pos > 0 ? stopLossForLong : stopLossForShort) * 100) / 100)) labelTp = label.new(bar_index+50, pos > 0 ? takeProfitForLong : takeProfitForShort, color = color.green, textcolor = #000000, style = label.style_label_left, text = 'Take Profit: ' + str.tostring(math.round((pos > 0 ? takeProfitForLong : takeProfitForShort) * 100) / 100)) label.delete(labelStop[1]) label.delete(labelTp[1]) trailPoints = trailOffset * syminfo.mintick // Strategy entry and exit conditions if longCond and (not prevent_multiple_positions or strategy.opentrades == 0) strategy.entry('Long', strategy.long,limit = ohlc4) if shortCond and (not prevent_multiple_positions or strategy.opentrades == 0) strategy.entry('Short', strategy.short,limit = ohlc4) if strategy.position_size > 0 strategy.exit('Take Profit/Stop Loss', 'Long', stop = stopLossForLong,trail_points = trailPoints, limit = takeProfitForLong) if strategy.position_size < 0 strategy.exit('Take Profit/Stop Loss', 'Short', stop = stopLossForShort,trail_points = trailPoints, limit = takeProfitForShort) plotshape(buySignal and pos[1] != 1 ? up : na, title = 'UpTrend Begins', location = location.belowbar, style = shape.circle, size = size.tiny, color = color.new(color.teal, transp = 50)) plotshape(buySignal and pos[1] != 1 ? up : na, title = 'Buy', text = 'Buy', location = location.belowbar, style = shape.labelup, size = size.tiny, color = color.new(color.teal, transp = 50), textcolor = color.white) plotshape(sellSignal and pos[1] != -1 ? dn : na, title = 'DownTrend Begins', location = location.abovebar, style = shape.circle, size = size.tiny, color = color.new(color.red, transp = 50)) plotshape(sellSignal and pos[1] != -1 ? dn : na, title = 'Sell', text = 'Sell', location = location.abovebar, style = shape.labeldown, size = size.tiny, color = color.new(color.red, transp = 50), textcolor = color.white)