TheStrat Trailing Stop MTF

BannonMan85

New member
Platform
  1. TradingView
Can this be converted to Thinkorswim?

Code:
// 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")

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.')


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]


[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

plot(trail1,'Trailing Stop',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

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


long_signal = trail_color2 == color.green and trail_color == color.green and trail_color[1] != color.green
short_signal = trail_color2 == color.red and trail_color == color.red and trail_color[1] != color.red


// if long_signal
//     label.new(bar_index,close*0.999,'LONG BIAS',color=color.green,textcolor=color.rgb(0, 0, 0))
// if short_signal
//     label.new(bar_index,close*1.01,'SHORT BIAS',color=color.red,textcolor=color.rgb(0, 0, 0))


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