MACD strategy + Trailstop indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Bretzay

//@version=5
indicator("MACD strategy + SuperTrend",overlay=true,timeframe_gaps = false, timeframe = "")
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Inputs-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Validation inputs :
int     emalength       =           input(200,title="EMAlength",tooltip="Choose the length of the EMA") //EMA LENGTH
color   bullmc          =           input(title="Bullish trend",defval=color.new(color.green,70)) //Confirmation background color
color   bearmc          =           input(title="Bullish trend",defval=color.new(color.red,70)) //Confirmation background color
bool    filter          =           input.bool(title="EMA Filter ?",defval = true,tooltip = "We filter noise by don't taking signals if one of the 14 candle before the signal closes on the other side of the EMA.") //EMA Filter
bool    background      =           input.bool(title="Background confirmation ?",defval=true,tooltip = "It makes appear (or not) the background on each place where there's a cross over in your filtering settings.") //  Confirmation Background
bool    TSFilter        =           input.bool(title="Only Supertrend ?",defval=false)
//Risk inputs
//RR                      =           input.float(title="Risk Reward", tooltip = "Choose your Risk to Reward ratio", group = "Risk",minval=0,defval=1.5,step=0.1)

//SL Inputs (ATR)
float   Mult            =           input.float(title="ATR Multiplier", tooltip="Increase of decrease the SL range from price", group="ATR",defval=3,step=0.1)
int     atrLength       =           input.int(title="ATR Length", defval=10, minval=1,group="ATR")

// MACD Inputs
int     fast_length     =           input(title="Fast Length", defval=12,group="MACD")
int     slow_length     =           input(title="Slow Length", defval=26,group="MACD")
src                     =           input(title="Source", defval=close,group="MACD")
int     signal_length   =           input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9,group="MACD")
string  sma_signal      =           input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"],group="MACD")
string  sma_source      =           input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"],group="MACD")
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Calculating-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Super Trend Indicator
[supertrend, direction] = ta.supertrend(Mult, atrLength)
bodyMiddle = plot((open + close) / 2, display=display.none)
// MACD
fast_ma                 =           sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma                 =           sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd                    =           fast_ma - slow_ma
signal                  =           sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)

// Validating trend
MACD                    =           ta.macd(close,12,26,9)
EMA                     =           ta.ema(close,emalength)
//Filters for moves close to EMA
closeunder = if close[14]<EMA or close[13]<EMA or close[12]<EMA or close[11]<EMA or close[10]<EMA or close[9]<EMA or close[8]<EMA or close[7]<EMA or close[6]<EMA or close[5]<EMA or close[4]<EMA or close[3]<EMA or close[2]<EMA or close[1]<EMA
    false
else
    true
closeabove = if close[14]>EMA or close[13]>EMA or close[12]>EMA or close[11]>EMA or close[10]>EMA or close[9]>EMA or close[8]>EMA or close[7]>EMA or close[6]>EMA or close[5]>EMA or close[4]>EMA or close[3]>EMA or close[2]>EMA or close[1]>EMA
    false
else
    true
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Strategy-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Calculate the TrailStop
ATR                     =           ta.atr(atrLength)
Stop                    =           Mult * ATR
//Is the market in bull/bear and MACD confirmation
var Bull = ta.crossover(macd, signal) and close > EMA and macd < 0 and closeunder
var Bear = ta.crossunder(macd, signal) and close < EMA and macd > 0 and closeabove
if filter == true
    Bull                    :=           ta.crossover(macd, signal) and close > EMA and macd < 0 and closeunder
    Bear                    :=           ta.crossunder(macd, signal) and close < EMA and macd > 0 and closeabove
else
    Bull                    :=           ta.crossover(macd, signal) and close > EMA and macd < 0
    Bear                    :=           ta.crossunder(macd, signal) and close < EMA and macd > 0

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Trail Stop-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Trailstop movement
// SELL TRAIL STOP
HL                     = (high+low)/2
var SellATR             =           0.00
SellATR                 :=          if close < SellATR[1] and close[1] < SellATR[1]
    math.min(SellATR[1], HL[1]+Stop)
else 
    HL+Stop
// BUY TRAIL STOP
var BuyATR              =           0.00
BuyATR                  :=          if close > BuyATR[1] and close[1] > BuyATR[1]
    math.max(BuyATR[1], HL[1]-Stop)
else 
    HL-Stop
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------Filtering TrailStop for Visual Improvement------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
var Scl = color.new(color.red,0)
var Bcl = color.new(color.green,0)

if TSFilter == false and close < EMA and direction < 0
    Scl:=color.new(color.red,0) //Selling color = Red trail stop
    Bcl:=color.new(color.green,100) //Buying color = Green trail stop
else if TSFilter == false and close > EMA and direction > 0
    Scl:=color.new(color.red,100) //Selling color = Red trail stop
    Bcl:=color.new(color.green,0) //Buying color = Green trail stop
else
    Scl:=color.new(color.red,100) //Selling color = Red trail stop
    Bcl:=color.new(color.green,100) //Buying color = Green trail stop


//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Showing-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//What appear on screen
bgcolor(background ? Bull ? color(bullmc) : Bear ? color(bearmc) : na : na, title = "Confirmation background color")
plot(EMA,"EMA")
Dtrend = plot(SellATR,color=Scl,title="Only change 0 and 1 (Trailing Stop)",style=plot.style_linebr)
Utrend = plot(BuyATR,color=Bcl,title="Only change 0 (Trailing Stop)",style=plot.style_linebr)
// Super Trend Indicator
upTrend = plot(close > EMA ? direction < 0 ? supertrend : na : na, "Up Trend", color = color.green, style=plot.style_linebr)
downTrend = plot(close < EMA ? direction < 0? na : supertrend : na, "Down Trend", color = color.red, style=plot.style_linebr)
fill(bodyMiddle, upTrend, color.new(color.green, 90), fillgaps=false)
fill(bodyMiddle, downTrend, color.new(color.red, 90), fillgaps=false)
