Kahlman HullMA / WT Cross Strategy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// WT CROSS @author [LazyBear]
// Kahlman HullMA / WT Cross Strategy ©author [pigsq]

//@version=5
strategy("Kahlman HullMA / WT Cross Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=100)

//Inputs
_1 = input(false, '|| LONG TP SETTINGS ||')
long = input(true, 'Long On')
LongprofitPercent = input.float(10, "TP1 Profit Percent", minval = 0, maxval = 100, step = 5, tooltip="Percentage of balance to be taken for take profit 1-2.")
LongprofitPercent2 = input.float(15, "TP2 Profit Percent", minval = 0, maxval = 100, step = 5, tooltip="Percentage of balance to be taken for take profit 1-2.")
takeprofitLong = input.float(8.9, "Long Take Profit 1", minval = 0.1, step = 0.1, tooltip="Long take profit 1 percent setting")/100
var tp2L = "tp2L"
takeprofit2Long = input.float(13.6, "Long Take Profit 2", minval = 0.1, step = 0.1, tooltip="Long take profit 2 percent setting. / Close TP2", inline=tp2L)/100
tp2OnOffL = input(true, 'On', inline=tp2L)
var tp3L = "tp3L"
takeprofit3Long = input.float(13.6, "Long Take Profit 3", minval = 0.1, step = 0.1, tooltip="Long take profit 3 percent setting. / Close TP3", inline=tp3L)/100
tp3OnOffL = input(false, 'On', inline=tp3L)
src_long = input.string(title='Long TP1 Source', defval="hc2",
     options=["hc2", "co2", "hco3", "hlo3", "ho2", "lo2", "open", "close", "hlc3", "hl2", "ohlc4", "hlcc4", "low", "high"],
     tooltip="TP1 up or down control. Close process if selected 'source' is less than TP1")
src_long2 = input.string(title='Long TP2 Source', defval="hc2", 
     options=["hc2", "co2", "hco3", "hlo3", "ho2", "lo2", "open", "close", "hlc3", "hl2", "ohlc4", "hlcc4", "low", "high"], 
     tooltip="TP2 up or down control. Close process if selected 'source' is less than TP2")

_2 = input(false, '|| SHORT TP SETTINGS ||')
short = input(true, 'Short On')
ShortprofitPercent = input.float(50, "TP1 Profit Percent", minval = 0, maxval = 100, step = 5, tooltip="Percentage of balance to be taken for take profit 1-2.")
ShortprofitPercent2 = input.float(70, "TP2 Profit Percent", minval = 0, maxval = 100, step = 5, tooltip="Percentage of balance to be taken for take profit 1-2.")
takeprofitShort = input.float(8.2, "Short Take Profit 1", minval = 0.1, step = 0.1, tooltip="Short take profit 1 percent setting")/100
var tp2S = "tp2S"
takeprofit2Short = input.float(13.4, "Short Take Profit 2", minval = 0.1, step = 0.1, tooltip="Short take profit 2 percent setting. / Close TP3", inline=tp2S)/100
tp2OnOffS = input(true, 'On', inline=tp2S)
var tp3S = "tp3S"
takeprofit3Short = input.float(13.9, "Short Take Profit 3", minval = 0.1, step = 0.1, tooltip="Short take profit 3 percent setting. / Close TP3", inline=tp3S)/100
tp3OnOffS = input(false, 'On', inline=tp3S)
src_short = input.string(title='Short TP1 Source', defval="lc2",
     options=["lc2", "lco3", "hco3", "hc2", "co2", "hlo3", "lo2", "ho2", "close", "hlc3", "hl2", "ohlc4", "hlcc4", "low", "high", "open"],
     tooltip="TP1 up or down control. Close process if selected 'source' is greater than TP1")
src_short2 = input.string(title='Short TP2 Source', defval="lco3", 
     options=["lc2", "lco3", "hco3", "hc2", "co2", "hlo3", "lo2", "ho2", "close", "hlc3", "hl2", "ohlc4", "hlcc4", "low", "high", "open"],
     tooltip="TP2 up or down control. Close process if selected 'source' is greater than TP2")

_3 = input(false, '|| STOP LOSS SETTINGS ||')
stoplossLong = input.float(7.1, "Long", minval = 0.1, step = 0.1, tooltip="Stop loss percent(Red line)")/100
stoplossShort = input.float(7.9, "Short", minval = 0.1, step = 0.1)/100

_4 = input(false, '|| TRAILING STOP SETTINGS ||')
trailingSLOnOff = input(true, 'Long Trailing SL On', tooltip="Trailing stop loss mode.")
trailingStopChangeL = input(true, 'Change Line', tooltip="On: If price is greater than TP1. Off: If price is greater than Long Position Check.")
trailingPercent = input.float(3.6, "Long", minval = 0.1, step = 0.1,
     tooltip="Trailing stop loss percent. Note: Activated if greater than TP1. Otherwise, the stop level is the value on a bottom line.(Yellow dot line)")/100
trailingSLOnOff2 = input(false, 'Short Trailing SL On', tooltip="Trailing stop loss mode.")
trailingStopChangeS = input(true, 'Change Line', tooltip="On: If price is greater than TP1. Off: If price is greater than Short Position Check.")
trailingPercent2 = input.float(3.4, "Short", minval = 0.1, step = 0.1,
     tooltip="Trailing stop loss percent. Note: Activated if greater than TP1. Otherwise, the stop level is the value on a bottom line.(Yellow dot line)")/100

_5 = input(false, '|| POSITION CHECK SETTINGS ||')
LongpositionCheckOnOff = input(true, 'Long Position Check On',
     tooltip="Long Position check mode. Depends on TP1 percentage. It works as a '%X' / 'Senstive Value'.")
src_LongpositionCheck = input.string(title='Source', defval="co2",
     options=["hco3", "lco3", "hc2", "lc2", "co2", "ho2", "lo2", "hlo3", "close", "open", "hl2", "hlc3", "ohlc4", "hlcc4", "low", "high"],
     tooltip="Long check senstive up or down control. Close process if selected 'source' is less than Position Check Line(Yellow solid line)")
LongpositionCheck = input.float(1.7, "Senstive", minval = 1, step = 0.01)
ShortpositionCheckOnOff = input(true, 'Short Position Check On',
     tooltip="Short Position check mode. It works as a '%X' / 'Senstive Value'.")
src_ShortpositionCheck = input.string(title='Source', defval="low",
     options=["lco3", "hco3", "lc2", "hc2", "co2", "lo2", "ho2", "hlo3", "close", "open", "hl2", "hlc3", "ohlc4", "hlcc4", "low", "high"],
     tooltip="Short check senstive up or down control. Close process if selected 'source' is greater than Position Check Line(Yellow solid line)")
ShortpositionCheck = input.float(1.4, "Senstive", minval = 1, step = 0.01)

//Long TP1 source
float source_long = switch src_long
	"hc2" => (high + close)/2
	"hco3" => (high + close + open)/3
	"co2" => (open + close)/2
	"hlo3" => (open + high + low)/3
	"ho2" => (open + high)/2
	"lo2" => (low + open)/2
	"lc2" => (low + close)/2
	"lco3" => (low + close + open)/3
	"open" => open
	"close" => close
	"hlc3" => hlc3
	"hl2" => hl2
	"ohlc4" => ohlc4
	"hlcc4" => hlcc4
	"low" => low
	"high" => high
//Long TP2 source
float source_long2 = switch src_long2
	"hc2" => (high + close)/2
	"hco3" => (high + close + open)/3
	"co2" => (open + close)/2
	"lc2" => (low + close)/2
	"lco3" => (low + close + open)/3
	"hlo3" => (open + high + low)/3
	"lo2" => (open + low)/2
	"ho2" => (high + open)/2
	"open" => open
	"close" => close
	"hlc3" => hlc3
	"hl2" => hl2
	"ohlc4" => ohlc4
	"hlcc4" => hlcc4
	"low" => low
	"high" => high
//Short TP1 source
float source_short = switch src_short
	"lc2" => (low + close)/2
	"lco3" => (low + close + open)/3
	"co2" => (open + close)/2
	"hlo3" => (open + high + low)/3
	"lo2" => (open + low)/2
	"ho2" => (high + open)/2
	"hc2" => (high + close)/2
	"hco3" => (high + close + open)/3
	"close" => close
	"hlc3" => hlc3
	"hl2" => hl2
	"ohlc4" => ohlc4
	"hlcc4" => hlcc4
	"low" => low
	"high" => high
	"open" => open
//Short TP2 source
float source_short2 = switch src_short2
	"lc2" => (low + close)/2
	"lco3" => (low + close + open)/3
	"co2" => (open + close)/2
	"hlo3" => (open + high + low)/3
	"lo2" => (open + low)/2
	"ho2" => (high + open)/2
	"hc2" => (high + close)/2
	"hco3" => (high + close + open)/3
	"close" => close
	"hlc3" => hlc3
	"hl2" => hl2
	"ohlc4" => ohlc4
	"hlcc4" => hlcc4
	"low" => low
	"high" => high
	"open" => open
//Long position check source
float source_CheckLong = switch src_LongpositionCheck
	"hco3" => (high + close + open)/3
	"lco3" => (low + close + open)/3
	"hc2" => (high + close)/2
	"lc2" => (low + close)/2
	"hlo3" => (open + high + low)/3
	"co2" => (open + close)/2
	"ho2" => (open + high)/2
	"lo2" => (low + open)/2
	"close" => close
	"hl2" => hl2
	"hlc3" => hlc3
	"ohlc4" => ohlc4
	"hlcc4" => hlcc4
	"low" => low
	"high" => high
	"open" => open
//Short position check source
float source_CheckShort = switch src_ShortpositionCheck
	"lco3" => (low + close + open)/3
	"hco3" => (high + close + open)/3
	"lc2" => (low + close)/2
	"hc2" => (high + close)/2
	"hlo3" => (open + high + low)/3
	"co2" => (open + close)/2
	"lo2" => (open + low)/2
	"ho2" => (high + open)/2
	"close" => close
	"hl2" => hl2
	"hlc3" => hlc3
	"ohlc4" => ohlc4
	"hlcc4" => hlcc4
	"high" => high
	"open" => open
	"low" => low

// WT Cross Setup
_6 = input(false, '|| WT CROSS SETTINGS ||')
var wtlong = "WT LONG"
n1L = input(6, 'Channel Length')
n2L = input(23, 'Average Length')
apL = input.string(title='Source', defval='hlc3', 
     options=["hc2", "hco3", "lc2", "lco3", "co2", "hlo3", "lo2", "close", "hlc3", "hl2", "ohlc4", "hlcc4", "low", "high", "open"])
float apL_src = switch apL
    "hc2" => (high + close)/2
	"hco3" => (high + close + open)/3
	"lc2" => (low + close)/2
	"lco3" => (low + close + open)/3
	"co2" => (open + close)/2
	"hlo3" => (open + high + low)/3
	"lo2" => (open + low)/2
	"close" => close
	"hlc3" => hlc3
	"hl2" => hl2
	"ohlc4" => ohlc4
	"hlcc4" => hlcc4
	"low" => low
	"high" => high
	"open" => open
	
esaL = ta.ema(apL_src, n1L)
rL = ta.ema(math.abs(apL_src - esaL), n1L)
ciL = (apL_src - esaL) / (0.015 * rL)
tciL = ta.ema(ciL, n2L)

wt1L = tciL
wt2L = ta.sma(wt1L, 4)

// WT SHORT

var wtshort = "WT SHORT"
n1S = input(8, 'Channel Length')
n2S = input(19, 'Average Length')

apS = input.string(title='Source', defval='hlc3', 
     options=["hc2", "hco3", "lc2", "lco3", "co2", "hlo3", "lo2", "close", "hlc3", "hl2", "ohlc4", "hlcc4", "low", "high", "open"])
float apS_src = switch apS
    "hc2" => (high + close)/2
	"hco3" => (high + close + open)/3
	"lc2" => (low + close)/2
	"lco3" => (low + close + open)/3
	"co2" => (open + close)/2
	"hlo3" => (open + high + low)/3
	"lo2" => (open + low)/2
	"close" => close
	"hlc3" => hlc3
	"hl2" => hl2
	"ohlc4" => ohlc4
	"hlcc4" => hlcc4
	"low" => low
	"high" => high
	"open" => open

esaS = ta.ema(apS_src, n1S)
rS = ta.ema(math.abs(apS_src - esaS), n1S)
ciS = (apS_src - esaS) / (0.015 * rS)
tciS = ta.ema(ciS, n2S)

wt1S = tciS
wt2S = ta.sma(wt1S, 4)

// Hull Kahlman Setup
_7 = input(false, '|| HULLMA SETTINGS ||')

hull_source = input.string(title='Source', defval='hl2', 
     options=["hc2", "hco3", "lc2", "lco3", "co2", "hlo3", "lo2", "close", "hlc3", "hl2", "ohlc4", "hlcc4", "low", "high", "open"])
lengthhull = input(24, 'Lookback')
gain = input(10000, 'Gain')

float hull_src = switch hull_source
    "hc2" => (high + close)/2
	"hco3" => (high + close + open)/3
	"lc2" => (low + close)/2
	"lco3" => (low + close + open)/3
	"co2" => (open + close)/2
	"hlo3" => (open + high + low)/3
	"lo2" => (open + low)/2
	"close" => close
	"hlc3" => hlc3
	"hl2" => hl2
	"ohlc4" => ohlc4
	"hlcc4" => hlcc4
	"low" => low
	"high" => high
	"open" => open

hma(_hull_src, _lengthhull) =>
    ta.wma((2 * ta.wma(_hull_src, _lengthhull / 2)) - ta.wma(_hull_src, _lengthhull), math.round(math.sqrt(_lengthhull)))

hma3(_hull_src, _lengthhull) =>
    p = lengthhull / 2
    ta.wma(ta.wma(close, p / 3) * 3 - ta.wma(close, p / 2) - ta.wma(close, p), p)

kahlman(x, g) =>
    kf = 0.0
    dk = x - nz(kf[1], x)
    smooth = nz(kf[1], x) + dk * math.sqrt(g / 10000 * 2)
    velo = 0.0
    velo := nz(velo[1], 0) + g / 10000 * dk
    kf := smooth + velo
    kf

a = kahlman(hma(hull_src, lengthhull), gain)
b = kahlman(hma3(hull_src, lengthhull), gain)
c = b > a ? color.lime : color.red
crossdn = a > b and a[1] < b[1]
crossup = b > a and b[1] < a[1]

p1hma = plot(a, color=color.new(c, 75), linewidth=1, title='Long Plot')
p2hma = plot(b, color=color.new(c, 75), linewidth=1, title='Short Plot')
fill(p1hma, p2hma, color=color.new(c, 75), title='Fill')

//Stoch RSI Settings
_8 = input(false, '|| STOCH RSI SETTINGS ||')
rsistochOnOff = input(true, 'Stoch RSI', tooltip="Stoch RSI On/Off")
smoothK = input.int(2, "K", minval=1)
smoothD = input.int(3, "D", minval=1)
lengthRSI = input.int(13, "RSI Length", minval=1)
lengthStoch = input.int(13, "Stochastic Length", minval=1)
src = input.string(title='RSI Source', defval='hl2', 
     options=["hc2", "hco3", "lc2", "lco3", "co2", "hlo3", "lo2", "close", "hlc3", "hl2", "ohlc4", "hlcc4", "low", "high", "open"])
float src_src = switch src
    "hc2" => (high + close)/2
	"hco3" => (high + close + open)/3
	"lc2" => (low + close)/2
	"lco3" => (low + close + open)/3
	"co2" => (open + close)/2
	"hlo3" => (open + high + low)/3
	"lo2" => (open + low)/2
	"close" => close
	"hlc3" => hlc3
	"hl2" => hl2
	"ohlc4" => ohlc4
	"hlcc4" => hlcc4
	"low" => low
	"high" => high
	"open" => open
rsi1 = ta.rsi(src_src, lengthRSI)
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.sma(k, smoothD)

LStoch = rsistochOnOff ? k > d:close
SStoch = rsistochOnOff ? k < d:close

//Vol Setup
_15 = input(false, '|| VOLUME SETTINGS ||')
volumeonoff = input(true, 'On/Off')
volume_f = input.float(0.6, title="Mult.", minval = 0, step = 0.1)
sma_length = input.int(3, title="Sma volume lenght", minval = 1)
vwma_length = input.int(3, title="Vwma volume lenght", minval = 1)
wma_length = input.int(4, title="Wma volume lenght", minval = 1)

vol = ((ta.sma(volume, sma_length) + ta.vwma(volume, vwma_length) + ta.wma(volume, wma_length)) / 3) * volume_f
volEntry = volumeonoff ? volume > vol:volume

_16 = input(false, '|| ADX SETTINGS ||')
adxon = input(false, 'On')
adxlen = input(14, title="ADX Smoothing")
dilen = input(14, title="DI Length")
adxBuyLevel = input(20, title="Buy Level")
dirmov(len) =>
	up = ta.change(high)
	down = -ta.change(low)
	plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
	minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
	truerange = ta.rma(ta.tr, len)
	plus = fixnan(100 * ta.rma(plusDM, len) / truerange)
	minus = fixnan(100 * ta.rma(minusDM, len) / truerange)
	[plus, minus]
adx(dilen, adxlen) =>
	[plus, minus] = dirmov(dilen)
	sum = plus + minus
	adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
sig = adx(dilen, adxlen)

ADXEntry = if adxon
    sig > adxBuyLevel
else
    sig < 100

// Wunderbit Settings
_9 = input(false, '|| WUNDERBIT SETTINGS ||')
var wunder = "Wunderbit Comments"
longentryComment = input("ENTER-LONG", title = "Long Entry Comment", group=wunder)
longexitComment = input("EXIT-LONG", title = "Long Exit Comment", group=wunder)
shortentryComment = input("ENTER-SHORT", title = "Short Entry Comment", group=wunder)
shortexitComment = input("EXIT-SHORT", title = "Short Exit Comment", group=wunder)

// Date Setup
_10 = input(false, '|| DATE & DATE RANGE SETTINGS ||')
startDate = input.time(timestamp("1 Jan 2020 00:00 +0000"), "Start Date")
endDate = input.time(timestamp("1 Jan 9999 03:00 +0000"), "End Date")
var rangeL = "LONG"
sessionOnOff = input(true, 'Long/Time Range & Weekdays On/Off', tooltip="Start Date and End Date not inclueded", group=rangeL)
timeSession = input.session("0000-0000", 'Time Range', group=rangeL)
d_monday        = input.bool(title="Monday", defval=true, inline="D1", group=rangeL)
d_tuesday       = input.bool(title="Tuesday", defval=true, inline="D1", group=rangeL)
d_wednesday     = input.bool(title="Wednesday", defval=true, inline="D1", group=rangeL)
d_thursday      = input.bool(title="Thursday", defval=true, inline="D2", group=rangeL)
d_friday        = input.bool(title="Friday", defval=true, inline="D2", group=rangeL)
d_saturday      = input.bool(title="Saturday", defval=false, inline="D2", group=rangeL)
d_sunday        = input.bool(title="Sunday", defval=true, tooltip="Trade days?", inline="D2", group=rangeL)
days = d_sunday ? "1" : ""
days := days + (d_monday ? "2" : "")
days := days + (d_tuesday ? "3" : "")
days := days + (d_wednesday ? "4" : "")
days := days + (d_thursday ? "5" : "")
days := days + (d_friday ? "6" : "")
days := days + (d_saturday ? "7" : "")
strategyTime = time >= startDate and time <= endDate ? true : false
inSession(session) =>
    if sessionOnOff
        na(time(timeframe.period, session + ":" + days)) == false and strategyTime
    else
        strategyTime
        
// For Short
var rangeS = "SHORT"
sessionOnOff2 = input(false, 'Short/Time Range & Weekdays On/Off', tooltip="Start Date and End Date not inclueded", group=rangeS)
timeSession2 = input.session("0300-0300", 'Time Range', group=rangeS)
d_monday2       = input.bool(title="Monday", defval=true, inline="D3", group=rangeS)
d_tuesday2       = input.bool(title="Tuesday", defval=true, inline="D3", group=rangeS)
d_wednesday2     = input.bool(title="Wednesday", defval=true, inline="D3", group=rangeS)
d_thursday2      = input.bool(title="Thursday", defval=true, inline="D4", group=rangeS)
d_friday2        = input.bool(title="Friday", defval=true, inline="D4", group=rangeS)
d_saturday2      = input.bool(title="Saturday", defval=true, inline="D4", group=rangeS)
d_sunday2        = input.bool(title="Sunday", defval=true, tooltip="Trade days?", inline="D4", group=rangeS)
days2 = d_sunday2 ? "1" : ""
days2 := days2 + (d_monday2 ? "2" : "")
days2 := days2 + (d_tuesday2 ? "3" : "")
days2 := days2 + (d_wednesday2 ? "4" : "")
days2 := days2 + (d_thursday2 ? "5" : "")
days2 := days2 + (d_friday2 ? "6" : "")
days2 := days2 + (d_saturday2 ? "7" : "")
inSession2(session2) =>
    if sessionOnOff2
        na(time(timeframe.period, session2 + ":" + days2)) == false and time >= startDate and time <= endDate
    else
        time >= startDate and time <= endDate ? true : false

// SL-TP Setups
longConditionDirection = strategy.position_size >= 0
shortConditionDirection = strategy.position_size <= 0

// Long Line Setup
exitSL = if longConditionDirection
    strategy.position_avg_price * (1 - stoplossLong)
else
    strategy.position_avg_price * (1 + stoplossShort)
exitTP = if longConditionDirection
    strategy.position_avg_price * (1 + takeprofitLong)
else
    strategy.position_avg_price * (1 - takeprofitShort)
exitTP2 = if longConditionDirection
    strategy.position_avg_price * (1 + takeprofit2Long)
else
    strategy.position_avg_price * (1 - takeprofit2Short)
exitTP3 = if longConditionDirection
    strategy.position_avg_price * (1 + takeprofit3Long)
else
    strategy.position_avg_price * (1 - takeprofit3Short)
    
positionCheckLong = if LongpositionCheckOnOff
    longConditionDirection ? strategy.position_avg_price * (1 + takeprofitLong/LongpositionCheck):na
positionCheckShort = if ShortpositionCheckOnOff
    shortConditionDirection ? strategy.position_avg_price * (1 - takeprofitShort/ShortpositionCheck):na

 // Long-Short Condition Setup
longCondition = crossup and ta.crossover(wt1L,wt2L) and LStoch or
     crossup and ta.crossover(wt1L[1],wt2L[1]) and LStoch or
     crossup and ta.crossover(wt1L[2],wt2L) and LStoch

shortCondition = crossdn and ta.crossunder(wt1S,wt2S) and SStoch

// Trailing Long Stop Setup
trailingStopChangeL1 = trailingStopChangeL ? high >= exitTP or high[1] >= exitTP:
     high >= positionCheckLong or high[1] >= positionCheckLong
priceStopL = 0.0
priceStopL := if longConditionDirection and trailingStopChangeL1 and trailingSLOnOff
    stopMeasure = close * (1 - trailingPercent)
    math.max(stopMeasure, priceStopL[1])
else if longConditionDirection
    exitSL

// Trailing Short Stop Setup
trailingStopChangeS1 = trailingStopChangeS ? low <= exitTP or low[1] <= exitTP:
     low <= positionCheckShort or low[1] <= positionCheckShort
priceStopS = 0.0
priceStopS := if shortConditionDirection and trailingStopChangeS1 and trailingSLOnOff2
    stopMeasure = close * (1 + trailingPercent2)
    math.min(stopMeasure, priceStopS[1])
else if shortConditionDirection
    exitSL

// Position Check Control
positionCheckLong2 =
     high >= positionCheckLong and ta.crossunder(wt1L,wt2L) and source_CheckLong < positionCheckLong or
     high >= positionCheckLong and ta.crossunder(wt1S,wt2S) and source_CheckLong < positionCheckLong or
     high >= positionCheckLong and a > b and source_CheckLong < positionCheckLong and wt1L < wt2L and not volEntry or
     high >= positionCheckLong and a > b and source_CheckLong < positionCheckLong and wt1S < wt2S and not volEntry or
     high >= positionCheckLong and crossdn and source_CheckLong < positionCheckLong

positionCheckShort2 =
     low <= positionCheckShort and ta.crossover(wt1S,wt2S) and source_CheckShort > positionCheckShort or
     low <= positionCheckShort and ta.crossover(wt1L,wt2L) and source_CheckShort > positionCheckShort or
     low <= positionCheckShort and a < b and source_CheckShort > positionCheckShort and wt1S > wt2S and not volEntry or
     low <= positionCheckShort and a < b and source_CheckShort > positionCheckShort and wt1L > wt2L and not volEntry or
     low <= positionCheckShort and crossup and source_CheckShort > positionCheckShort

// TP's Control
longTStop = high >= exitTP and source_long <= exitTP or
     high >= exitTP2 and source_long2 <= exitTP2 or
     high >= exitTP2 and a > b

shortTStop = low <= exitTP and source_short >= exitTP or
     low <= exitTP2 and source_short2 >= exitTP2 or
     low <= exitTP2 and a < b

// Long-Short Condition Close
longConditionExit = if high <= strategy.position_avg_price or high >= positionCheckLong
    shortCondition or positionCheckLong2
shortConditionExit = if low >= strategy.position_avg_price or low <= positionCheckShort
    longCondition or positionCheckShort2

if strategyTime
    if longConditionDirection and longCondition and long and volEntry and ADXEntry
        if inSession(sessionOnOff ? timeSession:na)
            strategy.entry("LONG", strategy.long, comment=longentryComment)
    strategy.exit("LONG", stop=priceStopL, qty_percent=tp2OnOffL ? LongprofitPercent:100, limit=exitTP, comment=longexitComment)
    if tp2OnOffL
        strategy.exit("LONG1", stop=priceStopL, qty_percent=tp3OnOffL ? LongprofitPercent2:100, limit=exitTP2, comment=longexitComment)
    if tp3OnOffL
        strategy.exit("LONG2", stop=priceStopL, qty_percent=100, limit=exitTP3, comment=longexitComment)
    if longConditionExit or longTStop
        strategy.close("LONG", comment=longexitComment)

    if shortConditionDirection and shortCondition and short and volEntry and ADXEntry
        if inSession2(sessionOnOff2 ? timeSession2:na)
            strategy.entry("SHORT", strategy.short, comment=shortentryComment)
    strategy.exit("SHORT", stop=priceStopS, qty_percent=tp2OnOffS ? ShortprofitPercent:100, limit=exitTP, comment=shortexitComment)
    if tp2OnOffS
        strategy.exit("SHORT1", stop=priceStopS, qty_percent=tp3OnOffS ? ShortprofitPercent2:100, limit=exitTP2, comment=shortexitComment)
    if tp3OnOffS
        strategy.exit("SHORT2", stop=priceStopS, qty_percent=100, limit=exitTP3, comment=shortexitComment)
    if shortConditionExit or shortTStop
        strategy.close("SHORT", comment=shortexitComment)

// SL-TP Lines
LSTP2Lines = longConditionDirection ? tp2OnOffL ? exitTP2:na:shortConditionDirection ? tp2OnOffS ? exitTP2:na:na
LSTP3Lines = longConditionDirection ? tp3OnOffL ? tp2OnOffL ? exitTP3:na:na:shortConditionDirection ? tp3OnOffS ? tp2OnOffS ? exitTP3:na:na:na

plot(exitTP, title='Long/Short TP1', color=color.lime, style=plot.style_linebr, linewidth=1)
plot(LSTP2Lines, title='Long/Short TP2', color=color.lime, style=plot.style_linebr, linewidth=1)
plot(LSTP3Lines, title='Long/Short TP3', color=color.lime, style=plot.style_linebr, linewidth=1)

// L/S Stop Loss Lines
plot(exitSL, title='Long/Short SL', color=color.red, style=plot.style_linebr, linewidth=1)

// Position Check Lines
PositionCheckColors = LongpositionCheckOnOff and positionCheckLong ? color.yellow :
     ShortpositionCheckOnOff and positionCheckShort ? color.yellow:na
PositionCheckLines = LongpositionCheckOnOff and longConditionDirection ? positionCheckLong :
     ShortpositionCheckOnOff and shortConditionDirection ? positionCheckShort:na

plot(PositionCheckLines, title='Long/Short Position Check', color=PositionCheckColors, style=plot.style_linebr, linewidth=1)

// Trailing Stop Lines
TrailingStopLines = trailingSLOnOff ? longConditionDirection and trailingStopChangeL1 ? priceStopL:na:na
TrailingStopLines2 = trailingSLOnOff2 ? shortConditionDirection and trailingStopChangeS1 ? priceStopS:na:na

plot(TrailingStopLines, title='Long Trailing Stop', color=color.yellow, style=plot.style_circles, linewidth=1)
plot(TrailingStopLines2, title='Short Trailing Stop', color=color.yellow, style=plot.style_circles, linewidth=1)

// Date Range Background Color
// bgcolor(sessionOnOff ? inSession(timeSession) ? color.rgb(91, 255, 0, 95):na:na, title="Long Time Range Bg Color")
// bgcolor(sessionOnOff2 ? inSession2(timeSession2) ? color.rgb(91, 255, 0, 95):na:na, title="Short Time Range Bg Color")