Rob Hoffman's 50/80/90/Price Trailing Stop Loss
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © D499

// D D D    4     4
// D    D   4     4
// D     D  4 4 4 4
// D    D         4
// D D D          4

//@version=5
indicator("Hoffman Trailing Stop", overlay = true)

// @ INPUTS ------------------------------

entry = input.price(title = "Entry Price: ", defval = 0.0, confirm = true)
limit = input.price(title = "Take Profit Price: ", defval = 0.0, confirm = true)
stop  = input.price(title = "Stop Price: ", defval = 0.0, confirm = true)
border_thickness = input.int(title = "Table Thickness", defval = 3, maxval = 4)
table_color = input.color(title = "Table Background Color", defval = color.new(#FFF1C1, 10))
border_color = input.color(title = "Table Border Color", defval = color.new(#000000, 10))

// @ COLORS ------------------------------

gray = color.new(color.gray, 70)
green = color.new(color.green, 70)
red = color.new(color.red, 70)

// @ CALCULATION --------------------------

// H - ((H - L) * %)
// L + ((H - L) * %)

p50 = (entry + limit)/2
p80 = limit > entry ? (0.8 * (limit - entry) + entry) : math.abs(0.8 * (entry - limit) - entry)
p90 = limit > entry ? (0.9 * (limit - entry) + entry) : math.abs(0.9 * (entry - limit) - entry)

tsl1 = limit > p50 ? math.abs(0.5 * (limit - p50) - p50) : (0.5 * (p50 - limit) + p50)
tsl2 = limit > p80 ? math.abs(0.8 * (limit - p80) - p80) : (0.8 * (p80 - limit) + p80)
tsl3 = limit > p90 ? math.abs(0.9 * (limit - p90) - p90) : (0.9 * (p90 - limit) + p90)

// @ TABLE --------------------------------

t = table.new(position.middle_right, 10, 10, table_color, border_color = border_color, border_width = border_thickness)
table.cell(t, 0, 0, "TSL 1: ")
table.cell(t, 1, 0, str.tostring(math.round_to_mintick(tsl1)))
table.cell(t, 0, 1, "TSL 2: ")
table.cell(t, 1, 1, str.tostring(math.round_to_mintick(tsl2)))
table.cell(t, 0, 2, "TSL 3: ")
table.cell(t, 1, 2, str.tostring(math.round_to_mintick(tsl3)))
table.cell(t, 2, 0, "50% of TP: ")
table.cell(t, 3, 0, str.tostring(math.round_to_mintick(p50)))
table.cell(t, 2, 1, "80% of TP: ")
table.cell(t, 3, 1, str.tostring(math.round_to_mintick(p80)))
table.cell(t, 2, 2, "90% of TP: ")
table.cell(t, 3, 2, str.tostring(math.round_to_mintick(p90)))
t2 = table.new(position.top_right, 10, 10)
table.cell(t2, 0, 0, "Once price reaches 50% of take profit, place your stop loss at TSL 1\nOnce price reaches 80% of take profit, place your stop loss at TSL 2\nOnce price reaches 90% of take profit, place your stop loss at TSL 3")
