ST_trailing
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © IntelTrading

//@version=4
study("trailing", overlay = true)


pr = input(4024.0, title = 'Price enter')
date = input(title="Date and time enter", type=input.time, defval=timestamp("19 Nov 2021 08:00 +0700"))


perc = input(2.0, step = 0.1, title = 'Percent activate trail')
kp = input(0.6, step = 0.1, title = 'koeff trail')


var ent = 0.0
if time >= date
    ent := pr
    

var g = 0.0
if time > date and ohlc4 > ent*(1 + (perc)/100)
    g := ent * ((ohlc4/ent) - kp*(perc/100))


var stop = 0.0
var u = 0
if g > g[1] and g > stop[1] and time > date 
    stop := g
if g < stop[1] and time > date 
    stop := stop[1]
if crossover(stop, low) and time > date
    stop := stop[1]
    u := 1

entpr = plot(time >= date and u == 0 ? pr : na, "price", color = color.gray, style=plot.style_linebr, linewidth = 1)
cl = plot(stop == 0 or u == 1 ? na : ohlc4, color = color.black)
trail = plot(stop == 0 or u == 1 ? na : stop, "Trail", color = color.yellow, style=plot.style_linebr, linewidth = 2)
fill(cl, trail, color.new(color.green, 90), fillgaps=false)



prof = (stop - pr)*100/pr
if barstate.islast and u == 0
    label.new(time, prof, text = "profit, % = " + tostring(round(prof, 2)), color = color.white, xloc=xloc.bar_time, yloc=yloc.abovebar, size = size.large, style = label.style_label_lower_left)



