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

//@version=5
indicator("Turtle", overlay=true)

entry_period = input.int(20, "Entry period", minval=2)
exit_period = input.int(10, "Exit period", minval=1)

show_trades = input.bool(false, "Show entries and exits")
show_levels = input.bool(true, "Show levels")
show_labels = input.bool(true, "Show level values")

// price levels
le = ta.highest(high, entry_period)[1]
lx = ta.lowest(low, exit_period)[1]
se = ta.lowest(low, entry_period)[1]
sx = ta.highest(high, exit_period)[1]

// trend
trend_state = 0
trend_state := nz(trend_state[1])
if trend_state[1] < 1 and high > le
    trend_state := 1
if trend_state[1] > 0 and low <= lx
    trend_state := 0
if trend_state[1] > -1 and low < se
    trend_state := -1
if trend_state[1] < 0 and high >= sx
    trend_state := 0

trend_active = trend_state != 0
trend_long = trend_state > 0
trend_short = trend_state < 0

// values in data window
plot(trend_state, "Trend", display=display.all-display.pane, color=trend_long?color.green:trend_short?color.red:color.yellow)
plot(le, "Long Entry", display=display.all-display.pane, color=color.green)
plot(lx, "Long Exit", display=display.all-display.pane, color=color.green)
plot(se, "Short Entry", display=display.all-display.pane, color=color.red)
plot(sx, "Short Exit", display=display.all-display.pane, color=color.red)

// price levels
color_long_entry = color.green
color_long_exit = color.aqua
color_short_entry = color.red
color_short_exit = color.yellow

plot(show_levels and trend_long[1] ? lx : na, "STLX", color=color_long_exit, linewidth=1, style=plot.style_linebr, display=display.pane)
plot(show_levels and trend_short[1] ? sx : na, "STSX", color=color_short_exit, linewidth=1, style=plot.style_linebr, display=display.pane)
plot(show_levels and not trend_active[1] ? le : na, "STLE", color=color_long_entry, linewidth=1, style=plot.style_linebr, display=display.pane)
plot(show_levels and not trend_active[1] ? se : na, "STSE", color=color_short_entry, linewidth=1, style=plot.style_linebr, display=display.pane)

// entry / exit points
long_entry = (trend_state > 0) and (trend_state[1] <= 0)
long_exit = (trend_state <= 0) and (trend_state[1] > 0)
short_entry = (trend_state < 0) and (trend_state[1] >= 0)
short_exit = (trend_state >= 0) and (trend_state[1] < 0)

plotshape(show_trades and long_entry ? le : na, style=shape.arrowup, color=color_long_entry, location=location.abovebar, display=display.pane, size=size.normal)
plotshape(show_trades and long_exit ? lx : na, style=shape.diamond, color=color_long_exit, location=location.belowbar, display=display.pane, size=size.small)
plotshape(show_trades and short_entry ? se : na, style=shape.arrowdown, color=color_short_entry, location=location.belowbar, display=display.pane, size=size.normal)
plotshape(show_trades and short_exit ? lx : na, style=shape.diamond, color=color_short_exit, location=location.abovebar, display=display.pane, size=size.small)

// labels
l_le = label(na)
l_se = label(na)
l_lx = label(na)
l_sx = label(na)
if not trend_active[1]
    label.delete(l_le[1])
    label.delete(l_se[1])
    if show_labels
        l_le := label.new(bar_index, le, str.tostring(le), yloc=yloc.price, color=color_long_entry, style=label.style_label_lower_right)
        l_se := label.new(bar_index, se, str.tostring(se), yloc=yloc.price, color=color_short_entry, style=label.style_label_upper_right)
else
    label.delete(l_lx[1])
    label.delete(l_sx[1])
    if show_labels
        if trend_long[1]
            l_lx := label.new(bar_index, lx, str.tostring(lx), yloc=yloc.price, color=color_long_exit, style=label.style_label_upper_left)
        if trend_short[1]
            l_sx := label.new(bar_index, sx, str.tostring(sx), yloc=yloc.price, color=color_short_exit, style=label.style_label_lower_left)
