LNL Trend System
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//
// @version=5
//
// L&L Trend System
//
// L&L Trend System is an ATR based day trading system specifically designed for intra-day traders and scalpers.
//
// The System works on any chart time frame & can be applied to any market. The study involves two components: 1.) Trend Line which is basically a fast gauge
// represented by the 13 EMA. 2.) Stop Line is an ATR deviaton line with special calculation based on the previous bar ATRs and position of the price in relation
// to the current and previous values of 13 EMA.
//
// There are 5 different trend modes available. Each mode is visualizing different ATR settings which provides either aggressive or more conservative approach. The 
// more "tigher" modes such as tight or normal can be used on less volatile markets such as S&P Futures (ES). Looses & FOMC modes are designed for active high volatility
// markets such as NQ or YM futures or BTC or perhaps some currency pairs. Then there is the "Net" mode. The net mode is basically the combination of all modes in one
// stop line system which creates "the net" effect. The system also includes additional higher time frame (HTF) trend system. This can be set to any time frame by manual 
// HTF mode. HTF mode set to "auto" will automatically choose the best suitable higher time frame trend system based on my backtesting results.
//
// By default, the bars are colored (Trend Bars) based on the DMI and ADX indicators. Whenever the DMI is bearish and ADX is above 20 the candles paint themselfs red. 
// And vice versa for the green candles and bullish DMI. Whenever the ADX falls below the 20, candles are netural which means there is no real trend in place.
// 
// In terms of visualization, each trend system is fully customizable through the inputs settings. There is also an option to turn on/off the background clouds behind the 
// stop lines. These clouds can make the charts more clean & visible. 
//
// Created by © L&L Capital
// 

indicator("LNL Trend System", shorttitle = "LNL Trend System", overlay=true)

// Inputs

string TrendMode = input.string("Normal", "Trend Mode", options = ["Tight", "Normal", "Loose", "FOMC", "Net"], group = "Settings", tooltip = "There are several trend modes available. The mods are lined up based on the aggressiveness of the ATR. Tight & Normal modes are the going to flip way much often whereas the Loose or FOMC will provide much higher wiggle room. The good rule of thumb to use is to just stick with first two modes when trading less volatile sessions or ranges, and use the other two on fast moving expanding environments. The Net mode provides the combination of all modes in one giant net. Some might prefer this mode since it suits well to the scale in scale out methods. ")
string HTFMode = input.string("Auto", "HTF Mode", options = ["Auto", "Manual"], group = "Settings", tooltip = "Changes the higher time frame mode. The HTF mode set to auto will automatically change the HTF Trend System time frame for you. The auto mode is choosing the most suitable time frames based on the pre-defined time frame pairs that are the most suitable ones. If you prefer your own time frame choose the manual mode.")
TimeFrameM = input.timeframe('60', "HTF Aggregation", options = ['1','2','3','5','10','15','20','30','45','60','120','180','240','D','2D','3D','4D','W','2W','3W','M','2M','3M'], group = "Settings", tooltip = "Set the manual time frame for the HTF Trend System.")

ShowTrendBars = input(defval=true, title="Show Trend Bars", group = "Trend Bars", tooltip = "Trend Bars are based on the DMI and ADX indicators. Whenever the DMI is bearish and ADX is above 20 the candles paint themselfs red. And vice versa for the green candles and bullish DMI. Whenever the ADX falls below the 20, candles are netural which means there is no real trend in place.")
TrendBarBullish = input(#27c22e, title="Bullish", group = "Trend Bars")
TrendBarBearish = input(#ff0000, title="Bearish", group = "Trend Bars")
TrendBarNeutral = input(#434651, title="Neutral", group = "Trend Bars")
ShowTrend = input(defval=true, title="Show Trend Line", group = "Trend Line", tooltip = "Trend Line is the first part of the L&L Trend System. The trend line is nothing simplier than the 13 exponential moving average. The color of the Trend Line depends on the position of multiple exponential averages and whether they are stacked on top of each other or not.")
TrendBullish = input(#27c22e, title="Bullish", group = "Trend Line")
TrendBearish = input(#ff0000, title="Bearish", group = "Trend Line")
TrendNeutral = input(#434651, title="Neutral", group = "Trend Line")
ShowStop = input(defval=true, title="Show Stop Line", group = "Stop Line", tooltip = "Stop Line is the main and most important part of the system. It is based on a special ATR calculation that takes into consideration the past ATRs and prices of the 13 EMA. Stop Line provides zones that no moving average can. To make it simple it is something like a moving average that uses the ATR not the average price of the previous bars.")
StopBullish = input(#27c22e, title="Bullish", group = "Stop Line")
StopBearish = input(#ff0000, title="Bearish", group = "Stop Line")
ShowTrend2 = input(defval=false, title="Show HTF Trend Line", group = "Higher Time Frame Trend Line", tooltip = "Higher Time Frame Trend Line.")
TrendBullish2 = input(#27c22e, title="Bullish", group = "Higher Time Frame Trend Line")
TrendBearish2 = input(#ff0000, title="Bearish", group = "Higher Time Frame Trend Line")
TrendNeutral2 = input(#434651, title="Neutral", group = "Higher Time Frame Trend Line")
ShowStop2 = input(defval=false, title="Show HTF Stop Line", group = "Higher Time Frame Stop Line", tooltip = "Higher Time Frame Stop Line")
StopBullish2 = input(#27c22e, title="Bullish", group = "Higher Time Frame Stop Line")
StopBearish2 = input(#ff0000, title="Bearish", group = "Higher Time Frame Stop Line")
ShowCloud = input(defval=true, title="Show Cloud", group = "Trend Cloud", tooltip = "Cloud will paint the area behind the Trend Line and Stop Line with custom color.")
CloudBullish = input(color.rgb(39,194,46,85), title="Bullish", group = "Trend Cloud")
CloudBearish = input(color.rgb(255,0,0,85), title="Bearish", group = "Trend Cloud")
ShowHTFCloud = input(defval=false, title="Show HTF Cloud", group = "Higher Time Frame Trend Cloud", tooltip = "Higher Time Frame Cloud.")
CloudBullish2 = input(color.rgb(39,194,46,85), title="Bullish", group = "Higher Time Frame Trend Cloud")
CloudBearish2 = input(color.rgb(255,0,0,85), title="Bearish", group = "Higher Time Frame Trend Cloud")

// Trend Bars (DMI Colored Candles)

BullishDMI = (high - high[1]) > (low[1] - low) and (high - high[1]) > 0 ? (high - high[1]) : 0
BearishDMI = (low[1] - low) > (high - high[1]) and (low[1] - low) > 0 ? (low[1] - low) : 0
DMIUp = 100 * ta.rma(BullishDMI,14) / ta.rma(ta.tr(true),14)
DMIDown = 100 * ta.rma(BearishDMI,14) / ta.rma(ta.tr(true),14)
ADXx = (DMIUp + DMIDown) > 0 ? 100 * math.abs(DMIUp - DMIDown) / (DMIUp + DMIDown) : na
ADX = ta.rma(ADXx,14)
ColorBars = ShowTrendBars and (DMIUp > DMIDown and ADX > 20) ? TrendBarBullish : ShowTrendBars and (DMIUp < DMIDown and ADX > 20) ? TrendBarBearish : ShowTrendBars ? TrendBarNeutral : na
barcolor(color = ColorBars, editable = false)

// Trend System (First Time Frame)

ema8 =  ta.vwma(close, 8)
ema13 = ta.vwma(close, 13)
ema21 = ta.vwma(close, 21)
ema34 = ta.vwma(close, 34)
emaup = ema8 > ema13  and ema13 > ema21 and ema21 > ema34
emadn = ema8 < ema13  and ema13 < ema21 and ema21 < ema34

Trend = ta.ema(close, 13)
TrendColor = ShowTrend and emadn and close <= Trend ? TrendBearish : ShowTrend and emaup and close >= Trend ? TrendBullish  : ShowTrend ? TrendNeutral : na
plot(Trend,title = "Trend", color = TrendColor, linewidth = 2, editable = false)

ATRLength = if TrendMode == "Tight"
    60
else if TrendMode == "Normal"
    80
else if TrendMode == "Loose"
    100
else if TrendMode == "FOMC"
    120
else if TrendMode == "Net"
    140

ATR = (ATRLength/100) * ta.ema(ta.tr(true),8)
Up = close > (Trend + ATR)
Down = close < (Trend - ATR)
var T = 0.0
T := Up ? 1 : Down ? -1 : T[1]

StopLineColor = ShowStop and T == 1 ? StopBullish : ShowStop ? StopBearish : na
plotchar(T == 1  ? (Trend-ATR) : T == -1  ? (Trend+ATR) : T[1], title = "StopLine",char = "-", location = location.absolute, size = size.tiny, color = StopLineColor, editable = false)

ATRA = (ATRLength - 20) /100 * ta.ema(ta.tr(true),8)
Up11 = close > (Trend + ATRA)
Down11 = close < (Trend - ATRA)
var T11 = 0.0
T11 := Up11 ? 1 : Down11 ? -1 : T11[1]

StopLineColor1 = ShowStop and T11 == 1 ? StopBullish : ShowStop ? StopBearish : na
plotchar(T11 == 1  ? (Trend-ATRA) : T11 == -1  ? (Trend+ATRA) : T11[1], title = "StopLine2",char = "-", location = location.absolute, size = size.tiny, color = StopLineColor1, editable = false)

ATRNET = TrendMode == "Net" ? (ATRLength - 40) /100 * ta.ema(ta.tr(true),8) : na
UpNET = close > (Trend + ATRNET)
DownNET = close < (Trend - ATRNET)
var TNET = 0.0
TNET := UpNET ? 1 : DownNET ? -1 : TNET[1]

StopLineColorNET = ShowStop and TNET == 1 ? StopBullish : ShowStop ? StopBearish : na
plotchar(TNET == 1  ? (Trend-ATRNET) : TNET == -1  ? (Trend+ATRNET) : TNET[1], title = "StopLineNET",char = "-", location = location.absolute, size = size.tiny, color = StopLineColorNET, editable = false)

ATRNET1 = TrendMode == "Net" ? (ATRLength - 60) /100 * ta.ema(ta.tr(true),8) : na
UpNET1 = close > (Trend + ATRNET1)
DownNET1 = close < (Trend - ATRNET1)
var TNET1 = 0.0
TNET1 := UpNET1 ? 1 : DownNET1 ? -1 : TNET1[1]

StopLineColorNET1 = ShowStop and TNET1 == 1 ? StopBullish : ShowStop ? StopBearish : na
plotchar(TNET1 == 1  ? (Trend-ATRNET1) : TNET1 == -1  ? (Trend+ATRNET1) : TNET1[1], title = "StopLineNET1",char = "-", location = location.absolute, size = size.tiny, color = StopLineColorNET1, editable = false)

ATRNET2 = TrendMode == "Net" ? (ATRLength - 80) /100 * ta.ema(ta.tr(true),8) : na
UpNET2 = close > (Trend + ATRNET2)
DownNET2 = close < (Trend - ATRNET2)
var TNET2 = 0.0
TNET2 := UpNET2 ? 1 : DownNET2 ? -1 : TNET2[1]

StopLineColorNET2 = ShowStop and TNET2 == 1 ? StopBullish : ShowStop ? StopBearish : na
plotchar(TNET2 == 1  ? (Trend-ATRNET2) : TNET2 == -1  ? (Trend+ATRNET2) : TNET2[1], title = "StopLineNET2",char = "-", location = location.absolute, size = size.tiny, color = StopLineColorNET2, editable = false)

// Higher Time Frame Aggregations

TimeFrameA =
      timeframe.period == '1' ? '5' : 
      timeframe.period == '2' ? '5' :
      timeframe.period == '3' ? '5' : 
      timeframe.period == '4' ? '5' : 
      timeframe.period == '5' ? '30' : 
      timeframe.period == '10' ? '30' : 
      timeframe.period == '15' ? '30' : 
      timeframe.period == '30' ? '240' : 
      timeframe.period == '60' ? '240' : 
      timeframe.period == '120' ? '240' : 
      timeframe.period == '180' ? 'D' : 
      timeframe.period == '240' ? 'D' : 
      timeframe.period == 'D' ? 'W' :
      timeframe.period == 'W' ? 'M' : 
      timeframe.period == 'M' ? '3M' : timeframe.period 

TimeFrame = if HTFMode == "Auto"
    TimeFrameA

else if HTFMode == "Manual"
    TimeFrameM

// Trend System (Second Time Frame)

ema82 =  request.security(syminfo.tickerid, TimeFrame, ta.vwma(close, 8))
ema132 = request.security(syminfo.tickerid, TimeFrame, ta.vwma(close, 13))
ema212 = request.security(syminfo.tickerid, TimeFrame, ta.vwma(close, 21))
ema342 = request.security(syminfo.tickerid, TimeFrame, ta.vwma(close, 34))
emaup2 = ema82 > ema132  and ema132 > ema212 and ema212 > ema342
emadn2 = ema82 < ema132  and ema132 < ema212 and ema212 < ema342

Trend2 = request.security(syminfo.tickerid, TimeFrame, ta.ema(close, 13))
TrendColor2 = ShowTrend2 and emadn2 and request.security(syminfo.tickerid, TimeFrame, close) <= Trend2 ? TrendBearish2 : ShowTrend2 and emaup2 and request.security(syminfo.tickerid, TimeFrame, close) >= Trend2 ? TrendBullish2 : ShowTrend2 ? TrendNeutral2 : na
plot(Trend2, title = "Trend2", color = TrendColor2, linewidth = 2, editable = false)

ATRLength2 = if TrendMode == "Tight"
    60
else if TrendMode == "Normal"
    80
else if TrendMode == "Loose"
    100
else if TrendMode == "FOMC"
    120
else if TrendMode == "Net"
    140

ATR2 = (ATRLength2/100) * request.security(syminfo.tickerid, TimeFrame, ta.ema(ta.tr(true),8))
Up2 = request.security(syminfo.tickerid, TimeFrame, close) > (Trend2 + ATR2)
Down2 = request.security(syminfo.tickerid, TimeFrame, close) < (Trend2 - ATR2)
var T2 = 0.0
T2 := Up2 ? 1 : Down2 ? -1 : T2[1]

StopLineColor2 = ShowStop2 and T2 == 1 ? StopBullish2 : ShowStop2 ? StopBearish2 : na
plotchar(T2 == 1  ? (Trend2-ATR2) : T2 == -1  ? (Trend2+ATR2) : T2[1], title = "StopLine2",char = "-", location = location.absolute, size = size.tiny, color = StopLineColor2, editable = false)

ATR2A = (ATRLength2 - 20) /100 * request.security(syminfo.tickerid, TimeFrame, ta.ema(ta.tr(true),8)) 
Up2A = request.security(syminfo.tickerid, TimeFrame, close) > (Trend2 + ATR2A)
Down2A = request.security(syminfo.tickerid, TimeFrame, close) < (Trend2 - ATR2A)
var T2A = 0.0
T2A := Up2A ? 1 : Down2A[1] ? -1 : T2A[1]

StopLineColor2A = ShowStop2 and T2A == 1 ? StopBullish2 : ShowStop2 ? StopBearish2 : na
plotchar(T2A == 1  ? (Trend2-ATR2A) : T2A == -1  ? (Trend2+ATR2A) : T2A[1], title = "StopLine2",char = "-", location = location.absolute, size = size.tiny, color = StopLineColor2A, editable = false)

ATR2ANET = TrendMode == "Net" ? (ATRLength2 - 40) /100 * request.security(syminfo.tickerid, TimeFrame, ta.ema(ta.tr(true),8)) : na
Up2ANET = request.security(syminfo.tickerid, TimeFrame, close) > (Trend2 + ATR2ANET)
Down2ANET = request.security(syminfo.tickerid, TimeFrame, close) < (Trend2 - ATR2ANET)
var T2ANET = 0.0
T2ANET := Up2ANET ? 1 : Down2ANET[1] ? -1 : T2ANET[1]

StopLineColor2ANET = ShowStop2 and T2ANET == 1 ? StopBullish2 : ShowStop2 ? StopBearish2 : na
plotchar(T2ANET == 1  ? (Trend2-ATR2ANET) : T2ANET == -1  ? (Trend2+ATR2ANET) : T2ANET[1], title = "StopLine2",char = "-", location = location.absolute, size = size.tiny, color = StopLineColor2ANET, editable = false)

ATR2ANET1 = TrendMode == "Net" ? (ATRLength2 - 60) /100 * request.security(syminfo.tickerid, TimeFrame, ta.ema(ta.tr(true),8)) : na
Up2ANET1 = request.security(syminfo.tickerid, TimeFrame, close) > (Trend2 + ATR2ANET1)
Down2ANET1 = request.security(syminfo.tickerid, TimeFrame, close) < (Trend2 - ATR2ANET1)
var T2ANET1 = 0.0
T2ANET1 := Up2ANET1 ? 1 : Down2ANET1[1] ? -1 : T2ANET1[1]

StopLineColor2ANET1 = ShowStop2 and T2ANET1 == 1 ? StopBullish2 : ShowStop2 ? StopBearish2 : na
plotchar(T2ANET1 == 1  ? (Trend2-ATR2ANET1) : T2ANET1 == -1  ? (Trend2+ATR2ANET1) : T2ANET1[1], title = "StopLine2",char = "-", location = location.absolute, size = size.tiny, color = StopLineColor2ANET1, editable = false)

ATR2ANET2 = TrendMode == "Net" ? (ATRLength2 - 80) /100 * request.security(syminfo.tickerid, TimeFrame, ta.ema(ta.tr(true),8)) : na
Up2ANET2 = request.security(syminfo.tickerid, TimeFrame, close) > (Trend2 + ATR2ANET2)
Down2ANET2 = request.security(syminfo.tickerid, TimeFrame, close) < (Trend2 - ATR2ANET2)
var T2ANET2 = 0.0
T2ANET2 := Up2ANET2 ? 1 : Down2ANET2[1] ? -1 : T2ANET2[1]

StopLineColor2ANET2 = ShowStop2 and T2ANET2 == 1 ? StopBullish2 : ShowStop2 ? StopBearish2 : na
plotchar(T2ANET2 == 1  ? (Trend2-ATR2ANET2) : T2ANET2 == -1  ? (Trend2+ATR2ANET2) : T2ANET2[1], title = "StopLine2",char = "-", location = location.absolute, size = size.tiny, color = StopLineColor2ANET2, editable = false)

// Trend Clouds

p1 = plot(Trend, title = "Trend Line", color = TrendColor, linewidth = 2, display = display.none, editable = false)
p2 = plot(T == 1  ? (Trend-ATR) : T == -1  ? (Trend+ATR) : T[1],title ="StopLine ", color = StopLineColor, linewidth = 2, display = display.none, editable = false)
Cloud = ShowCloud and T == 1 ? CloudBullish : ShowCloud ? CloudBearish : na
fill(p1,p2,title="TrendCloud",color = Cloud, editable = false)

p3 = plot(Trend2, title = "Trend Line 2", color = TrendColor2, linewidth = 2, display = display.none, editable = false)
p4 = plot(T2 == 1  ? (Trend2-ATR2) : T2 == -1  ? (Trend2+ATR2) : T2[1],title ="StopLine 2", color = StopLineColor2, linewidth = 2, display = display.none, editable = false)
Cloud2 = ShowHTFCloud and T2 == 1 ? CloudBullish2 : ShowHTFCloud ? CloudBearish2 : na
fill(p3,p4,title="TrendCloud2",color = Cloud2, editable = false)
