Strat Trail Stop by AlexsOptions
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AlexSopa

//@version=5
indicator("Strat Trail Stop by AlexsOptions","STS by A.O",overlay=true)

timeframe = input.timeframe('D',"Timeframe Input",tooltip = "This will allow you to trail based on whatever timeframe you'd like")
timeframe2 = input.timeframe('W',"Higher Timeframe Input",tooltip = "This will filter trail signals agains the HTF Trend")
timeframe3 = input.timeframe('M', "Higher higher timeframe input",tooltip="This will filter trail signals only with HHTF and HTF trend")

gap_timeframe = input.timeframe('D',"This timeframe will be played as a gapper. It must be one of the 3 selected timeframes above.")
display_partials = input.bool(false,"Alert partials [corrective activity]")
gapper_trades = input.bool(true,"Alert gapper trades")
use_HTF_open = input.bool(false, 'Use the HTF open instead of High/Low',tooltip='This will better get you playing in the direction of continuity.')
tto_only = input.bool(true,"Only display TTOs on the chart")
vix_price = request.security("VIX",timeframe,close,barmerge.gaps_off,lookahead=barmerge.lookahead_on)
price_filter = input.int(17,"VIX Price Filter",0,99,tooltip="If vix is below this price the indicator will paint gray")

request_htf_candle(timeframe,bar) =>
    [o,h,l,c] = request.security(syminfo.tickerid,timeframe,[open[bar],high[bar],low[bar],close[bar]],barmerge.gaps_off,lookahead=barmerge.lookahead_on)

    [o,h,l,c]

[ltf_open,ltfh1,ltfl2,ltfc3]  = request_htf_candle(timeframe,0)

[gtfo,gtfh,gtfl,gtfc] = request_htf_candle(gap_timeframe,0)
[gtfo2,gtfh2,gtfl2,gtfc2] = request_htf_candle(gap_timeframe,1)


[pdo1,pdh1,pdl1,pdc1] = request_htf_candle(timeframe,1)
[pdo2,pdh2,pdl2,pdc2] = request_htf_candle(timeframe,2)


var trail1 = 0.0

trail1:= pdh1 > pdh2 and pdl1 > pdl2 ? pdl1 : pdh2 > pdh1 and pdl1 < pdl2 ? pdh1 : trail1[1]

trail_color = close > trail1 ? color.green : close < trail1 ? color.red : color.gray

trail_color:= vix_price > price_filter ? trail_color : color.rgb(120, 123, 134, 71)

plot(trail1,'Trailing Stop [LTF]',trail_color,linewidth=2)


[htf_open,htf1,htf2,htf3]  = request_htf_candle(timeframe2,0)

[pwo1,pwh1,pwl1,pwc1] = request_htf_candle(timeframe2,1)
[pwo2,pwh2,pwl2,pwc2] = request_htf_candle(timeframe2,2)


var trail2 = 0.0

trail2:= use_HTF_open ? htf_open : pwh1 > pwh2 and pwl1 > pwl2 ? pwl1 : pwh2 > pwh1 and pwl1 < pwl2 ? pwh1 : trail2[1]

trail_color2 = close > trail2 ? color.green : close < trail2 ? color.red : color.gray

trail_color2:= vix_price > price_filter ? trail_color2 : color.rgb(120, 123, 134, 71)

plot(trail2,'Trailing Stop [HTF]',trail_color2,linewidth=2)



[htf_open2,htf4,htf5,htf6]  = request_htf_candle(timeframe3,0)

[pmo2,pmh2,pml2,pmc2] = request_htf_candle(timeframe3,2)
[pmo1,pmh1,pml1,pmc1] = request_htf_candle(timeframe3,1)


var trail3 = 0.0

trail3:= use_HTF_open ? htf_open2 : pmh1 > pmh2 and pml1 > pml2 ? pml1 : pmh2 > pmh1 and pml1 < pml2 ? pmh1 : trail3[1]

trail_color3 = close > trail3 ? color.green : close < trail3 ? color.red : color.gray

trail_color3:= vix_price > price_filter ? trail_color3 : color.rgb(120, 123, 134, 71)

plot(trail3,'Trailing Stop [HHTF]',trail_color3,linewidth=2)





color1_red_to_green = trail_color == color.green and trail_color[1] == color.red
color2_red_to_green = trail_color2 == color.green and trail_color2[1] == color.red
color3_red_to_green = trail_color3 == color.green and trail_color3[1] == color.red

color1_green_to_red = trail_color == color.red and trail_color[1] == color.green
color2_green_to_red = trail_color2 == color.red and trail_color2[1] == color.green
color3_green_to_red = trail_color3 == color.red and trail_color3[1] == color.green

gap_dn_gapTF = gtfo < gtfh2 and gtfo < gtfl2 
gap_up_gapTF =gtfo > gtfh2 and gtfo > gtfl2
two_up_red = gtfo > gtfc

var previous_week = 0

if trail2[1] != trail2
    previous_week == trail2[1]

if trail_color == color.green and trail_color[1] == color.red and trail_color2 == color.green and trail_color3 == color.green and ltfc3 > ltf_open and tto_only==false
    if gap_up_gapTF == false 
        label.new(bar_index,high*1.01,"[" + "BUY" + "] ",color = color.green)
if trail_color == color.red and trail_color[1] == color.green and trail_color2 == color.red and trail_color3 == color.red and tto_only==false
    if gap_dn_gapTF == false or gap_up_gapTF
        label.new(bar_index,high*1.01,"[" + "SELL" + "] ",color = color.red)
//TFC SHIFT

if color3_red_to_green and trail_color == color.green and trail_color2 == color.green and tto_only==false
    label.new(bar_index,high*1.01,"[" + "NEG-BUY" + "]",color = color.orange)
if color2_red_to_green and trail_color3 == color.green and trail_color == color.green and tto_only==false
    if gap_up_gapTF == false
        label.new(bar_index,high*1.01,"[" + "BUY" + "]",color = color.blue)

if color1_green_to_red and trail_color2 == color.green and trail_color3 == color.green and display_partials and tto_only==false
    label.new(bar_index,high*1.01,"[" + "P" + "]",color = color.purple)

if color1_green_to_red and gap_up_gapTF and two_up_red and tto_only==false
    if gapper_trades
        label.new(bar_index,high*1.01,"[" + "G-SELL" + "]",color = color.rgb(215, 4, 4))

tto_signal = trail_color3 == color.green and trail_color2 == color.red and close[5] > trail2
if tto_signal and tto_signal[1] != true
    label.new(bar_index,high*1.005,"[TTO]",color=color.orange)

//if color1_green_to_red and trail_color3 == color.green and trail_color2 == color.green
    //label.new(bar_index,high*1.01,"[" + "C/A" + "]",color = color.yellow)

//   plotshape(long_signal,"Long",shape.circle,location.belowbar,color.green)
// plotshape(short_signal,"Short",shape.circle,location.abovebar,color.red)