напиши код на pinescript вход от второго касания трендовой по направлению тренда // This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © QuantVue

//@version=5
indicator('Dynamic Touch Trendlines [QuantVue]', shorttitle = 'DTT [QuantVue]', overlay = true, max_lines_count = 500)

//----------settings----------//
pivLen      = input.int(9, 'Pivot Length')
minTouch    = input.int(3, 'Minimum Touches', minval = 2)
buffer      = input.float(0.015, 'Price Buffer %', minval = 0.0)/100
maxLines    = input.int(10, 'Maximum Trendlines', minval = 2)
tlCol       = input.color(color.lime, 'Trendline Style', inline = '1')
tlStyle     = input.string('Solid', '', ['Solid', 'Dotted', 'Dashed'], inline = '1')
tlWidth     = input.int(2, '', inline = '1', minval = 1, maxval = 4)
bsrc        = input.string('Wicks', 'Trendline Break Source', ['Wicks', 'Close'])
extLen      = input.int(10, 'Extend Broken Trendlines For X Bars', minval = 0)
bCol        = input.color(color.yellow, 'Broken Trendline Style', inline = '2')
bStyle      = input.string('Dotted', '', ['Solid', 'Dotted', 'Dashed'], inline = '2')
bWidth     = input.int(2, '', inline = '2', minval = 1, maxval = 4)
showBroken  = input.bool(true, 'Show Broken Trendlines', tooltip = 'If checked broken trendlines will not be removed from the chart')
remB        = input.int(2, 'Remove Broken Trendlines After X Bars', minval = 0, tooltip = 'Only valid if Show Broken Trendlines is turned off')

//----------types----------//
type trendline 
    int x1
    float y1
    int x2
    float y2
    line tl
    int touches = 0
    bool active = true
    bool valid = false
    int bBar = na

type piv 
    float price
    int bar

//----------methods----------//
method styler(string this)=>
    switch this
        'Solid' => line.style_solid
        'Dashed' => line.style_dashed
        'Dotted' => line.style_dotted

//----------variables----------//
ph = ta.pivothigh(pivLen,pivLen)
pl = ta.pivotlow(pivLen,pivLen)

var tlArrD = array.new<trendline>()
var tlArrU = array.new<trendline>()
var phArr = array.new<piv>()
var plArr = array.new<piv>()

//----------conditions----------//
if not na(ph)
    phArr.push(piv.new(high[pivLen],bar_index[pivLen]))
    if phArr.size() > 1
        prev = phArr.get(phArr.size()-2)
        if high[pivLen] < prev.price
            tlArrD.push(trendline.new(prev.bar, prev.price, bar_index[pivLen], high[pivLen], line.new(prev.bar, prev.price, bar_index[pivLen], high[pivLen], extend = extend.right, color = color.new(color.white,100), width = tlWidth)))
            if tlArrD.size() > maxLines
                last = tlArrD.shift()
                last.tl.delete()

if not na(pl)
    plArr.push(piv.new(low[pivLen],bar_index[pivLen]))
    if plArr.size() > 1
        prev = plArr.get(plArr.size()-2)
        if low[pivLen] > prev.price
            tlArrU.push(trendline.new(prev.bar, prev.price, bar_index[pivLen], low[pivLen], line.new(prev.bar, prev.price, bar_index[pivLen], low[pivLen], extend = extend.right, color = color.new(color.white,100), width = tlWidth)))
            if tlArrU.size() > maxLines
                last = tlArrU.shift()
                last.tl.delete()

//----------tl management----------//
for tl in tlArrD
    src = bsrc == 'Wicks' ? high : close
    if tl.active
        if bar_index - tl.x1 <= 2500
            if not tl.valid
                tl.touches := 0
                for i = bar_index - tl.x1 to 0
                    if high[i] > tl.tl.get_price(bar_index[i]) * (1 + buffer)
                        tl.active := false
                        tl.tl.delete()
                        break
                    if high[i] <= tl.tl.get_price(bar_index[i]) * (1 + buffer) and high[i] >= tl.tl.get_price(bar_index[i]) * (1 - buffer)
                        tl.touches += 1
                if tl.touches >= minTouch
                    tl.valid := true
                    break
        else
            tl.active := false
            tl.tl.delete()
        if tl.valid
            tl.tl.set_color(tlCol)
            tl.tl.set_style(tlStyle.styler())
            alert('New Trendline Detected', alert.freq_once_per_bar_close)
            if src > tl.tl.get_price(bar_index)
                tl.valid := false
                tl.active := false
                tl.bBar := bar_index
                tl.tl.set_style(bStyle.styler())
                tl.tl.set_color(bCol)
                tl.tl.set_xy2(bar_index+1+extLen, tl.tl.get_price(bar_index+1+extLen))
                tl.tl.set_extend(extend.none)
                tl.tl.set_width(bWidth)
                alert('Trendline Broken', alert.freq_once_per_bar)
    else if not tl.active and not na(tl.bBar) and not showBroken
        if bar_index > tl.bBar + remB
            tl.tl.delete()

for tl in tlArrU
    src = bsrc == 'Wicks' ? low : close
    if tl.active
        if bar_index - tl.x1 <= 2500
            if not tl.valid
                tl.touches := 0
                for i = bar_index - tl.x1 to 0
                    if low[i] < tl.tl.get_price(bar_index[i]) * (1 - buffer)
                        tl.active := false
                        tl.tl.delete()
                        break
                    if low[i] >= tl.tl.get_price(bar_index[i]) * (1 - buffer) and low[i] <= tl.tl.get_price(bar_index[i]) * (1 + buffer)
                        tl.touches += 1
                if tl.touches >= minTouch
                    tl.valid := true
                    break
        else
            tl.active := false
            tl.tl.delete()
        if tl.valid
            tl.tl.set_color(tlCol)
            tl.tl.set_style(tlStyle.styler())
            alert('New Trendline Detected', alert.freq_once_per_bar_close)
            if src < tl.tl.get_price(bar_index)
                tl.valid := false
                tl.active := false
                tl.bBar := bar_index
                tl.tl.set_style(bStyle.styler())
                tl.tl.set_color(bCol)
                tl.tl.set_xy2(bar_index+1+extLen, tl.tl.get_price(bar_index+1+extLen))
                tl.tl.set_extend(extend.none)
                tl.tl.set_width(bWidth)
                alert('Trendline Broken', alert.freq_once_per_bar)
    else if not tl.active and not na(tl.bBar) and not showBroken
        if bar_index > tl.bBar + remB
            tl.tl.delete()
 тейк профит 7 % стоп лосс 2 %