Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
27 kB
10
Indexable
Never
//@version=5
strategy("MTF SSL+TDFI v1.0 (YM futures alert)", overlay=true, margin_long=100, margin_short=100, process_orders_on_close = true, calc_on_every_tick = true)

//----------------------------------COLORS------------------------------------//
bullcolor=input.color(color.green,"Color Bull",inline="--Master Colors--",group="-----Colors-----")
bearcolor=input.color(color.red,  "Color Bear",inline="--Master Colors--",group="-----Colors-----")
slcolor=color.new(color.red, 90)
becolor=color.new(color.yellow, 90),
tpcolor=color.new(color.green, 90)
//----------------------------------COLORS END--------------------------------//

//---------------------------Session Settings---------------------------------//
//kill zones
NYColor = input.color(color.new(color.white, 70),"New York",inline="--Session Colors--", group="-----Colors-----")

///Sessions
res_sessions = 'D'
ny = input.session(title='New York Session', defval='0800-1100', group='-----SESSION SETTINGS-----')

//Bars
is_newbar(sess) =>
    t_sessions = time(res_sessions, sess)
    na(t_sessions[1]) and not na(t_sessions) or t_sessions[1] < t_sessions

is_session(sess) =>
    not na(time(timeframe.period, sess))

//New York
NY = input.bool(title='New York Session Background Hi-Lite', defval=false, group='-----SESSION SETTINGS-----')
nyNewbar = is_newbar(ny)
nySession = is_session(ny)
bgcolor(nySession and NY ? NYColor : na, title='NY', transp=90)
nyc = nySession and NY

//---------------------------Session Settings---------------------------------//

//-------------------------ATR Functions--------------------------------------//
lengthatr = 14
getMA(series, lengthatr) =>
    ta.rma(series, lengthatr)
getTV(value) =>
    math.round(value * 100000) / 100000
atr = getMA(ta.tr(true), lengthatr) 
//-------------------------ATR Functions END----------------------------------//

//-------------------------HTF TDFI--------------------------------------------//
group1 = "⚠️On/off       Timeframe            MA type/length                             line"

tf1Tog = input.bool(true, title = "", group = group1, inline = "1")
tf1 = input.timeframe("D", title = "", group = group1, inline = "1")

tf2Tog = input.bool(true, title = "", group = group1, inline = "2")
tf2 = input.timeframe("W", title = "", group = group1, inline = "2")

tf3Tog = input.bool(true, title = "", group = group1, inline = "3")
tf3 = input.timeframe("M", title = "", group = group1, inline = "3")

//SSL 
wicks = false
highlightState = true
ma(source, length, type) =>
    type == 'SMA' ? ta.sma(source, length) : type == 'EMA' ? ta.ema(source, length) : type == 'SMMA (RMA)' ? ta.rma(source, length) : type == 'WMA' ? ta.wma(source, length) : type == 'VWMA' ? ta.vwma(source, length) : na

show_ma1 = input.bool(true, 'MA High', inline='MA #1', group='SSL Channel')
ma1_type = input.string('SMA', inline='MA #1', options=['SMA', 'EMA', 'SMMA (RMA)', 'WMA', 'VWMA'], group='SSL Channel')
ma1_source = input.source(high, '', inline='MA #1', group='SSL Channel')
ma1_length = input.int(200, '', inline='MA #1', minval=1, group='SSL Channel')
ma1_color = bullcolor
ma1 = ma(ma1_source, ma1_length, ma1_type)

show_ma2 = input.bool(true, 'MA Low', inline='MA #2', group='SSL Channel')
ma2_type = input.string('SMA', inline='MA #2', options=['SMA', 'EMA', 'SMMA (RMA)', 'WMA', 'VWMA'], group='SSL Channel')
ma2_source = input.source(low, '', inline='MA #2', group='SSL Channel')
ma2_length = input.int(200, '', inline='MA #2', minval=1, group='SSL Channel')
ma2_color = bearcolor
ma2 = ma(ma2_source, ma2_length, ma2_type)
showLabels1 = false

Hlv1 = float(na)
Hlv1 := (close) > ma1 ? 1 : (close) < ma2 ? -1 : Hlv1[1]
sslUp1 = Hlv1 < 0 ? ma2 : ma1
sslDown1 = Hlv1 < 0 ? ma1 : ma2

Color1 = Hlv1 == 1 ? ma1_color : ma2_color
fillColor1 = highlightState ? color.new(Color1, 90) : na

highLine1 = plot(show_ma1 ? sslUp1 : na, title='UP', linewidth=2, color=Color1)
lowLine1 = plot(show_ma2 ? sslDown1 : na, title='DOWN', linewidth=2, color=Color1)

plotshape(show_ma1 and showLabels1 and Hlv1 == 1 and Hlv1[1] == -1, title='Buy Label', text='Buy', location=location.belowbar, style=shape.labelup, size=size.tiny, color=Color1, textcolor=color.new(color.white, 0))
plotshape(show_ma2 and showLabels1 and Hlv1 == -1 and Hlv1[1] == 1, title='Sell Label', text='Sell', location=location.abovebar, style=shape.labeldown, size=size.tiny, color=Color1, textcolor=color.new(color.white, 0))

fill(highLine1, lowLine1, color=fillColor1)

//-----------------------------MTF MA's------------------//
smooth = input.bool(false, 'Smooth HTF MAs', group='-----SESSION SETTINGS-----')

maType1 = input.string('SMA', title = "", group = group1, inline = "1", options=['EMA', 'HEMA', 'SMA', 'HMA', 'WMA', 'DEMA', 'VWMA', 'VWAP', 'T3', 'IT'])
maType2 = input.string('SMA',  title = "",group = group1, inline = "2", options=['EMA', 'HEMA', 'SMA', 'HMA', 'WMA', 'DEMA', 'VWMA', 'VWAP', 'T3', 'IT'])
maType3 = input.string('SMA',  title = "",group = group1, inline = "3", options=['EMA', 'HEMA', 'SMA', 'HMA', 'WMA', 'DEMA', 'VWMA', 'VWAP', 'T3', 'IT'])

maLength1 = input.int(200, title = "",group = group1, inline = "1")
maLength2 = input.int(200, title = "",group = group1, inline = "2")
maLength3 = input.int(200,title = "", group = group1, inline = "3")

lineW1 = input.int(2, title = "",group = group1, inline = "1")
lineW2 = input.int(3,  title = "",group = group1, inline = "2")
lineW3 = input.int(4,  title = "",group = group1, inline = "3")

_NRP(tf, src) =>
    request.security(syminfo.tickerid, tf, src[barstate.isrealtime ? 1 : 0], barmerge.gaps_on)[barstate.isrealtime ? 0 : 1]

_NRPrt(tf, src) =>
    request.security(syminfo.tickerid, tf, src[barstate.isrealtime ? 1 : 0], barmerge.gaps_off)[barstate.isrealtime ? 0 : 1]

_haOpen() =>
    haClose = (open + high + low + close) / 4
    haOpen = float(na)
    haOpen := na(haOpen[1]) ? (open + close) / 2 : (nz(haOpen[1]) + nz(haClose[1])) / 2

    haOpen
HaOpen = _haOpen()

getMA1(type, length) =>
    maPrice = ta.ema(close, length)
    if type == 'HEMA'
        maPrice := ta.ema(HaOpen, length)
        maPrice
    if type == 'SMA'
        maPrice := ta.sma(close, length)
        maPrice
    if type == 'HMA'
        maPrice := ta.hma(close, length)
        maPrice
    if type == 'WMA'
        maPrice := ta.wma(close, length)
        maPrice
    if type == 'VWMA'
        maPrice := ta.vwma(close, length)
        maPrice
    if type == 'VWAP'
        maPrice := ta.vwap
        maPrice
    if type == 'DEMA'
        e1 = ta.ema(close, length)
        e2 = ta.ema(e1, length)
        maPrice := 2 * e1 - e2
        maPrice
    if type == 'T3'
        axe1 = ta.ema(close, length)
        axe2 = ta.ema(axe1, length)
        axe3 = ta.ema(axe2, length)
        axe4 = ta.ema(axe3, length)
        axe5 = ta.ema(axe4, length)
        axe6 = ta.ema(axe5, length)
        ab = 0.7
        ac1 = -ab * ab * ab
        ac2 = 3 * ab * ab + 3 * ab * ab * ab
        ac3 = -6 * ab * ab - 3 * ab - 3 * ab * ab * ab
        ac4 = 1 + 3 * ab + ab * ab * ab + 3 * ab * ab
        maPrice := ac1 * axe6 + ac2 * axe5 + ac3 * axe4 + ac4 * axe3
        maPrice
    
        maPrice
    maPrice

// ================================== //
// ----> V̦͍a̢͔͎r̙͉͔i̠͖͜a̪͉͚b̻̺̘l͇̙̦e͖̪̻ C̢͓̞a͇͇͙l͖̻̫c͙̦͙u͇̠͔l̺̞̠a͚͍͔t͓͓͙i̘͉͙o̻͜͜n̝̝͚s͚͓ <----- //
// ================================== //

htf_ma1 = getMA1(maType1, maLength1)
htf_ma2 = getMA1(maType2, maLength2 == 0 ? 1 : maLength2)
htf_ma3 = getMA1(maType3, maLength3 == 0 ? 1 : maLength3)

ma1step = _NRPrt(tf1, htf_ma1)
ma2step = _NRPrt(tf2, htf_ma2)
ma3step = _NRPrt(tf3, htf_ma3)

ma1smooth = _NRP(tf1, htf_ma1)
ma2smooth = _NRP(tf2, htf_ma2)
ma3smooth = _NRP(tf3, htf_ma3)


// ================================== //
// ----> C̠̪̫o̼̫͔n̫̝̪d̺͚͎i̦͔͎t̺͖͖i͓͜͜o͉̦̘n̢͎͉a̞̼̠l̼͓͎ P̝͕͜a͕̝͜r̠̼͍a͚͍͓m͙̟̪e͚̫͎t͔̘̟e̺̙͎r̠͇͍s̘̠ <---- //
// ================================== //

ma1Direc = htf_ma1 >= htf_ma1[1]
ma2Direc = htf_ma2 >= htf_ma2[1]
ma3Direc = htf_ma3 >= htf_ma3[1]

ma1MtfDirec = _NRPrt(tf1, ma1Direc)
ma2MtfDirec = _NRPrt(tf2, ma2Direc)
ma3MtfDirec = _NRPrt(tf3, ma3Direc)

ma1Con = tf1 == '' ? ma1Direc : ma1MtfDirec
ma2Con = tf2 == '' ? ma2Direc : ma2MtfDirec
ma3Con = tf3 == '' ? ma3Direc : ma3MtfDirec

maMtf1 = smooth ? ma1smooth : ma1step
maMtf2 = smooth ? ma2smooth : ma2step
maMtf3 = smooth ? ma3smooth : ma3step

plotMa1 = tf1 == '' ? htf_ma1 : maMtf1
plotMa2 = tf2 == '' ? htf_ma2 : maMtf2
plotMa3 = tf3 == '' ? htf_ma3 : maMtf3

ma1Bar = tf1 == '' ? close >= plotMa1 : close >= ma1step
ma2Bar = tf2 == '' ? close >= plotMa2 : close >= ma2step
ma3Bar = tf3 == '' ? close >= plotMa3 : close >= ma3step

plotMA2 = maType2 == 'IT' ? plotMa2 : maLength2 == 0 ? na : plotMa2
plotMA3 = maType3 == 'IT' ? plotMa3 : maLength3 == 0 ? na : plotMa3

// ================================== //
// ------> G̡̘͍r̪̻̼a͇͔͜p̫͎͍h͔͇̞i͉͇̞c͍͚a͕͔͉l̝͇ D̡̦͓i̡̡̞s͚͇̫p͓̟͙l͙̪̻a͉͍y̻͖̻ <------- //
// ================================== //

htf1_color=request.security(syminfo.tickerid,tf1,close) > plotMa1 ? bullcolor : bearcolor
htf2_color=request.security(syminfo.tickerid,tf2,close) > plotMA2 ? bullcolor  : bearcolor
htf3_color=request.security(syminfo.tickerid,tf3,close) > plotMA3 ? bullcolor  : bearcolor

plot(plotMa1, 'Moving Average #1', htf1_color, linewidth=lineW1)
plot(plotMA2, 'Moving Average #2', htf2_color, linewidth=lineW2)
plot(plotMA3, 'Moving Average #3', htf3_color, linewidth=lineW3)

//Trend Force Direction Index
tdfi_entry=input.bool(true,"TDFI filter", group='⚠️-----RISK MANAGEMENT SETTINGS-----⚠️')
lookback = 13
mmaLength = 13
mmaMode = 'ema'
smmaLength = 13
smmaMode = 'ema'
nLength = 3
filterHigh = input(0.05, title='TDFI Filter High')
filterLow = input(-0.05, title='TDFI Filter Low')
price = close
price_tf1= request.security(syminfo.tickerid,timeframe = tf1, expression = close)
price_tf2= request.security(syminfo.tickerid,timeframe = tf2, expression = close)
price_tf3= request.security(syminfo.tickerid,timeframe = tf3,expression =  close)

tema(src, len) =>
    ema1 = ta.ema(src, len)
    ema2 = ta.ema(ema1, len)
    ema3 = ta.ema(ema2, len)
    3 * ema1 - 3 * ema2 + ema3

maT(mode, src, len) =>
    mode == 'ema' ? ta.ema(src, len) : mode == 'wma' ? ta.wma(src, len) : mode == 'swma' ? ta.swma(src) : mode == 'vwma' ? ta.vwma(src, len) : mode == 'hull' ? ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), math.round(math.sqrt(len))) : mode == 'tema' ? tema(src, len) : ta.sma(src, len)

tdfi() =>
    mma = maT(mmaMode, price * 1000, mmaLength)
    smma = maT(smmaMode, mma, smmaLength)
    impetmma = mma - mma[1]
    impetsmma = smma - smma[1]
    divma = math.abs(mma - smma)
    averimpet = (impetmma + impetsmma) / 2
    tdf = math.pow(divma, 1) * math.pow(averimpet, nLength)
    tdf / ta.highest(math.abs(tdf), lookback * nLength)

signal = tdfi()
c = signal > filterHigh ? bullcolor : signal < filterLow ? bearcolor : color.gray
barcolor(color=c)

tdfi1() =>
    mma = maT(mmaMode, price * 1000, mmaLength)
    smma = maT(smmaMode, mma, smmaLength)
    impetmma = mma - mma[1]
    impetsmma = smma - smma[1]
    divma = math.abs(mma - smma)
    averimpet = (impetmma + impetsmma) / 2
    tdf = math.pow(divma, 1) * math.pow(averimpet, nLength)
    tdf / ta.highest(math.abs(tdf), lookback * nLength)

signal_tf1 = request.security(syminfo.tickerid,tf1,tdfi1(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
tf1_signal= signal_tf1 >filterHigh ? 1 : signal_tf1 < filterLow ? 2 : 3
tf1_signalcolor=signal_tf1 >filterHigh ? bullcolor : signal_tf1 < filterLow ? bearcolor : color.gray

tdfi2() =>
    mma = maT(mmaMode, price_tf2 * 1000, mmaLength)
    smma = maT(smmaMode, mma, smmaLength)
    impetmma = mma - mma[1]
    impetsmma = smma - smma[1]
    divma = math.abs(mma - smma)
    averimpet = (impetmma + impetsmma) / 2
    tdf = math.pow(divma, 1) * math.pow(averimpet, nLength)
    tdf / ta.highest(math.abs(tdf), lookback * nLength)

signal_tf2 = request.security(syminfo.tickerid,tf2,tdfi2(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
tf2_signal= signal_tf2 >filterHigh ? 1 : signal_tf2 < filterLow ? 2 : 3
tf2_signalcolor=signal_tf2 >filterHigh ? bullcolor : signal_tf2 < filterLow ? bearcolor : color.gray

tdfi3() =>
    mma = maT(mmaMode, price_tf3 * 1000, mmaLength)
    smma = maT(smmaMode, mma, smmaLength)
    impetmma = mma - mma[1]
    impetsmma = smma - smma[1]
    divma = math.abs(mma - smma)
    averimpet = (impetmma + impetsmma) / 2
    tdf = math.pow(divma, 1) * math.pow(averimpet, nLength)
    tdf / ta.highest(math.abs(tdf), lookback * nLength)

signal_tf3 =request.security(syminfo.tickerid,tf3,tdfi3(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
tf3_signal= signal_tf3 >filterHigh ? 1 : signal_tf3 < filterLow ? 2 : 3
tf3_signalcolor=signal_tf3 >filterHigh ? bullcolor: signal_tf3 < filterLow ? bearcolor : color.gray


trade_onlong=(close>sslUp1) and (close>sslDown1) and (tdfi_entry ? c==bullcolor : close) and (tf1Tog ? tf1_signal==1 : close) and (tf2Tog ? tf2_signal==1 : close) and (tf3Tog ? tf3_signal==1 : close) and (tf1Tog ? close>plotMa1 : close) and (tf2Tog ? close>plotMA2 : close) and (tf3Tog ? close > plotMA3 : close) 
trade_onshort=(close<sslUp1) and (close<sslDown1) and (tdfi_entry ? c==bearcolor : close) and (tf1Tog ? tf1_signal==2 : close) and (tf2Tog ? tf2_signal==2 : close) and (tf3Tog ? tf3_signal==2 : close) and (tf1Tog ? close<plotMa1 : close) and (tf2Tog ? close<plotMA2 : close) and (tf3Tog ? close < plotMA3 : close)
trade_on_color=(trade_onlong ? bullcolor : trade_onshort ? bearcolor : color.gray)
trade_on= trade_onshort or trade_onlong
plotchar(trade_onshort and not trade_onshort[1],char='🔥',location = location.abovebar)
plotchar(trade_onlong and not trade_onlong[1],char='🔥',location = location.belowbar)
//-------------------------HTF TDFI End---------------------------------------//

showDash = input.bool(true,title = "Show Dashboard", group = "Dashboard")
dashLoc  = str.lower(str.replace_all(input.string("Top Right",title =  "Location", options = ["Top Right", "Bottom Right", "Bottom Left"] , group = "Dashboard")," ","_"))
textSize = str.lower(input.string("Small", title = "Size", options = ["Tiny", "Small", "Normal"], group = "Dashboard"))
vert = input.string("Vertical", title = "Orientation", group = "Dashboard", options = ["Vertical", "Horizontal"]) == "Vertical"

//Table setup
var tb = table.new(dashLoc, 10, 10
  , bgcolor      = #1e222d
  , border_color = #373a46
  , border_width = 1
  , frame_color  = #373a46
  , frame_width  = 3) 

//Getting the widths for each display style
vert_width = textSize == "normal" ? 1 : 1
flat_width = textSize == "normal" ? 2 : 1

//Sending everything to the table
if showDash
    if tf1Tog
        tb.cell((vert?0:0),(vert?0:1), tf1,text_color = color.white, text_size = textSize)
        tb.cell((vert?1:0),(vert?0:0), text= "TD", text_color = color.rgb(255, 208, 0), text_size = textSize, bgcolor = tf1_signalcolor, height = 1, width = (vert?vert_width:flat_width))
        tb.cell((vert?2:0),(vert?0:0), text= "MA1", text_color =  color.rgb(255, 208, 0), text_size = textSize, bgcolor = htf1_color, height = 1, width = (vert?vert_width:flat_width))

    if tf2Tog
        tb.cell((vert?0:1),(vert?1:1), tf2,text_color = color.white, text_size = textSize)
        tb.cell((vert?1:1),(vert?1:0), text= "TD", text_color =  color.rgb(255, 208, 0), text_size = textSize,bgcolor = tf2_signalcolor, height = 1, width = (vert?vert_width:flat_width))
        tb.cell((vert?2:1),(vert?1:0), text= "MA2", text_color =  color.rgb(255, 208, 0),text_size = textSize, bgcolor = htf2_color, height = 1, width = (vert?vert_width:flat_width))

    if tf3Tog
        tb.cell((vert?0:2),(vert?2:1), tf3,text_color = color.white, text_size = textSize)
        tb.cell((vert?1:2),(vert?2:0), text= "TD", text_color = color.rgb(255, 208, 0), text_size = textSize,bgcolor = tf3_signalcolor, height = 1, width = (vert?vert_width:flat_width))
        tb.cell((vert?2:2),(vert?2:0), text= "MA3",text_color =  color.rgb(255, 208, 0),text_size = textSize, bgcolor = htf3_color, height = 1, width = (vert?vert_width:flat_width))

//------------------------------Entry filters--------------------------------//

buyfilteron = trade_onlong
sellfilteron = trade_onshort

var bool killzones = input.bool(false, '⚠️Session Hi-lite Entry Filter On', group='-----SESSION SETTINGS-----')
buykzfilteron = (killzones ? nyc : na) 
sellkzfilteron = (killzones ? nyc : na) 

var bool longentry = input.bool(true, 'Long Entry', inline='Entries',group='⚠️-----RISK MANAGEMENT SETTINGS-----⚠️')
var bool shortentry = input.bool(true, 'Short Entry', inline='Entries',group='⚠️-----RISK MANAGEMENT SETTINGS-----⚠️')
//-------------------------------End Filters----------------------------------//

//-----------------------------Entry Signals----------------------------------//
idealbuy = buyfilteron 
idealsell= sellfilteron 

longCond = idealbuy and not idealbuy[1]
shortCond= idealsell and not idealsell[1]

showsell= shortentry and shortCond  and (killzones ? buykzfilteron : na)
showbuy = longentry and longCond and (killzones ? sellkzfilteron : na)
//-----------------------------Entry Signals END------------------------------//barcolor()

//-----------------------------Risk management--------------------------------//barcolor()
//-----settings------//
var bool be = input.bool(false, 'Enable Break Even 1:1', group='⚠️-----RISK MANAGEMENT SETTINGS-----⚠️')
bepip=input.float(defval=0, title='Breakeven Step in Pips', step=0.1, minval=0.0000, group='⚠️-----RISK MANAGEMENT SETTINGS-----⚠️')
usefixtp=input.float(defval=10, title='Fixed TP Pips', step=0.1, minval=0.0000, group='⚠️-----RISK MANAGEMENT SETTINGS-----⚠️')
usefixsl=input.float(defval=5, title='Fixed SL Pips', step=0.1, minval=0.0000, group='⚠️-----RISK MANAGEMENT SETTINGS-----⚠️')
rr = input.float(2, 'Risk Reward Ratio (Swing Hi/Lo)', minval=0.1, group='⚠️-----RISK MANAGEMENT SETTINGS-----⚠️')
sllookback = input.int(defval=5, title='Swing lo/hi lookback', group='⚠️-----RISK MANAGEMENT SETTINGS-----⚠️')
sllow=ta.lowest(sllookback) 
slhigh=ta.highest(sllookback)
takeprofit = input.float(2, 'ATR TP', minval=0.1, group='⚠️-----RISK MANAGEMENT SETTINGS-----⚠️')
stoploss = input.float(1, 'ATR SL', minval=0.1, group='⚠️-----RISK MANAGEMENT SETTINGS-----⚠️')
maxsl=input.float(defval=5, title='Max SL Pips(Swing Hi/Lo & ATR)', step=0.1, minval=0.0000, group='⚠️-----RISK MANAGEMENT SETTINGS-----⚠️')

//sl/tp options 
fixedtpl= ta.valuewhen(showbuy, close + usefixtp, 0)
fixedsll= ta.valuewhen(showbuy, close - usefixsl, 0)
fixedtps=ta.valuewhen(showsell, close - usefixtp, 0)
fixedsls=ta.valuewhen(showsell, close + usefixsl, 0)

//-----TP/BE calcs MSB----//
longslmax = ta.valuewhen(showbuy, close, 0) - ta.valuewhen(showbuy, sllow, 0) > maxsl ? ta.valuewhen(showbuy, close - maxsl, 0) : sllow
shortslmax = ta.valuewhen(showsell, slhigh, 0) - ta.valuewhen(showsell, close, 0) > maxsl ? ta.valuewhen(showsell, close + maxsl, 0) : slhigh
longentryval=ta.valuewhen(showbuy,close,0) 
shortentryval=ta.valuewhen(showsell,close,0) 
longvalfixedsldistance=longentryval-usefixsl
shortvalfixedsldistance=usefixsl-shortentryval
longentrytosldistance=longentryval-longslmax
shortentrytosldistance=shortslmax-shortentryval
long2sldist=longentryval-usefixsl
short2sldist=usefixsl-shortentryval
tplong=longentryval+(longentrytosldistance*rr)
tpshort=shortentryval-(shortentrytosldistance*rr)
fixedbelong=ta.valuewhen(showbuy, close + usefixsl, 0) 
fixedbeshort=ta.valuewhen(showsell, close - usefixsl, 0) 
swingbelong=longentryval+(longentrytosldistance)
swingbeshort=shortentryval-(shortentrytosldistance)

//-----MSB ATR TP/SL calc's----//
longtpatr = ta.valuewhen(showbuy, close + takeprofit * atr, 0)
longslatr = ta.valuewhen(showbuy, close - stoploss * atr, 0)
shorttpatr = ta.valuewhen(showsell, close - takeprofit * atr, 0)
shortslatr = ta.valuewhen(showsell, close + stoploss * atr, 0)
belongatr = ta.valuewhen(showbuy, close + stoploss * atr, 0)
beshortatr = ta.valuewhen(showsell, close - stoploss * atr, 0)
longslmaxatr = ta.valuewhen(showbuy, close, 0) - ta.valuewhen(showbuy, close - stoploss * atr, 0) > maxsl ? ta.valuewhen(showbuy, close - maxsl, 0) : longslatr
shortslmaxatr = ta.valuewhen(showsell, close + stoploss * atr, 0) - ta.valuewhen(showsell, close, 0) > maxsl ? ta.valuewhen(showsell, close + maxsl, 0) : shortslatr

//-----sl/tp variables----//
var fixedtp= 0.0
var fixedsl= 0.0
var fixedbe= 0.0
var atrsl  = 0.0
var atrtp  = 0.0
var atrbe  = 0.0
var swingsl= 0.0
var swingtp= 0.0
var swingbe= 0.0

if showbuy and not showbuy[1]
    fixedtp:=fixedtpl
    fixedsl:=fixedsll
    fixedbe:=fixedbelong
    swingsl:=longslmax
    swingtp:=longentryval+((longentryval-swingsl)*rr)
    swingbe:=swingbelong 
    atrsl:=longslmaxatr
    atrtp:=longtpatr
    atrbe:=belongatr
    
if showsell and not showsell[1]
    fixedtp:=fixedtps
    fixedsl:=fixedsls
    fixedbe:=fixedbeshort
    swingsl:=shortslmax
    swingtp:=shortentryval-((swingsl-shortentryval)*rr)
    swingbe:=swingbeshort
    atrsl:=shortslmaxatr
    atrtp:=shorttpatr
    atrbe:=beshortatr
    
tptype= input.string('Fixed',title="⚠️TP/SL Type",inline='Entries',options=['Fixed','Swing Hi/Lo','ATR'], group='⚠️-----RISK MANAGEMENT SETTINGS-----⚠️')

ENTRYtp=tptype=='Fixed' ? fixedtp : tptype=='Swing Hi/Lo' ? swingtp : tptype=='ATR' ? atrtp : na
ENTRYsl=tptype=='Fixed' ? fixedsl : tptype=='Swing Hi/Lo' ? swingsl : tptype=='ATR' ? atrsl : na
ENTRYbe=tptype=='Fixed' ? fixedbe : tptype=='Swing Hi/Lo' ? swingbe : tptype=='ATR' ? atrbe : na

//------------------------SL/TP's for MSB-------------------------------------//
var sl = 0.0
var be1 = 0.0
var tp = 0.0

long = be ? strategy.position_size > 0 : na
short = be ? strategy.position_size < 0 : na

if showbuy and not showbuy[1] and strategy.position_size ==0
    sl := ENTRYsl
    be1 := ENTRYbe
    tp := ENTRYtp
    alert(instrument=YM 09-24; command=PLACE; action=BUY; qty=1; order_type=MARKET; TIF=DAY; strategy=YM;',alert.freq_once_per_bar_close) 

if long and high > be1
    sl := strategy.position_avg_price + bepip
    
if showsell and not showsell[1] and strategy.position_size ==0
    sl := ENTRYsl
    be1 := ENTRYbe
    tp := ENTRYtp
    alert(instrument=YM 09-24; command=PLACE; action=SELL; qty=1; order_type=MARKET; TIF=DAY; strategy=YM;',alert.freq_once_per_bar_close) 

if short and low < be1
    sl := strategy.position_avg_price - bepip
//------------------------END SL/TP's  ---------------------------------//

//------------------- ORDER Plots-------------------------------------------//
strategy.entry('buy fix', strategy.long, when=showbuy and tptype=='Fixed')
strategy.entry('sell fix', strategy.short, when=showsell and tptype=='Fixed')
strategy.entry('buy atr', strategy.long, when=showbuy and tptype=='ATR')
strategy.entry('sell atr', strategy.short, when=showsell and tptype=='ATR')
strategy.entry('buy swing', strategy.long, when=showbuy and tptype=='Swing Hi/Lo')
strategy.entry('sell swing', strategy.short, when=showsell and tptype=='Swing Hi/Lo')
strategy.exit(id='buy tp fix', from_entry='buy fix', limit=tp,stop=sl,alert_message='key=JG7GuFxDReo2L2vBTRvfLf01mPOYtrfJ2H9q_KA7r0I; account=DEMO3251020,Sim101; instrument=YM 09-24; command=CLOSEPOSITION;')
strategy.exit(id='sell tp fix', from_entry='sell fix', limit=tp, stop=sl,alert_message='key=JG7GuFxDReo2L2vBTRvfLf01mPOYtrfJ2H9q_KA7r0I; account=DEMO3251020,Sim101; instrument=YM 09-24; command=CLOSEPOSITION;')
strategy.exit(id='buy tp atr', from_entry='buy atr', limit=tp,stop=sl, alert_message='key=JG7GuFxDReo2L2vBTRvfLf01mPOYtrfJ2H9q_KA7r0I; account=DEMO3251020,Sim101; instrument=YM 09-24; command=CLOSEPOSITION;')
strategy.exit(id='sell tp atr', from_entry='sell atr', limit=tp, stop=sl,alert_message='key=JG7GuFxDReo2L2vBTRvfLf01mPOYtrfJ2H9q_KA7r0I; account=DEMO3251020,Sim101; instrument=YM 09-24; command=CLOSEPOSITION;')
strategy.exit(id='buy tp swing', from_entry='buy swing', limit=tp,stop=sl,alert_message='key=JG7GuFxDReo2L2vBTRvfLf01mPOYtrfJ2H9q_KA7r0I; account=DEMO3251020,Sim101; instrument=YM 09-24; command=CLOSEPOSITION;')
strategy.exit(id='sell tp swing', from_entry='sell swing', limit=tp, stop=sl,alert_message='key=JG7GuFxDReo2L2vBTRvfLf01mPOYtrfJ2H9q_KA7r0I; account=DEMO3251020,Sim101; instrument=YM 09-24; command=CLOSEPOSITION;') 

p1 = plot(strategy.position_size != 0 and strategy.position_entry_name =='buy fix' or strategy.position_entry_name =='sell fix' or strategy.position_entry_name =='buy atr' or strategy.position_entry_name =='sell atr' or strategy.position_entry_name =='buy swing' or strategy.position_entry_name =='sell swing' ? strategy.position_avg_price : na, color=color.new(color.gray, 0), style=plot.style_linebr)
p2 = plot(strategy.position_size != 0 and strategy.position_entry_name =='buy fix' or strategy.position_entry_name =='sell fix' or strategy.position_entry_name =='buy atr' or strategy.position_entry_name =='sell atr' or strategy.position_entry_name =='buy swing' or strategy.position_entry_name =='sell swing'? sl : na, color=color.new(color.red, 0), style=plot.style_linebr)
p3 = plot(strategy.position_size != 0 and strategy.position_entry_name =='buy fix' or strategy.position_entry_name =='sell fix' or strategy.position_entry_name =='buy atr' or strategy.position_entry_name =='sell atr' or strategy.position_entry_name =='buy swing' or strategy.position_entry_name =='sell swing'? tp : na, color=color.new(color.green, 0), style=plot.style_linebr)
p5 = plot(strategy.position_size != 0 and strategy.position_entry_name =='buy fix' or strategy.position_entry_name =='sell fix' or strategy.position_entry_name =='buy atr' or strategy.position_entry_name =='sell atr' or strategy.position_entry_name =='buy swing' or strategy.position_entry_name =='sell swing'? be1 : na, style=plot.style_linebr)

fill(p1, p2, color=slcolor)
fill(p1, p5, color=becolor)
fill(p5, p3, color=tpcolor)

Leave a Comment