MACD x SuperTrend with trailing stoploss
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ChamodSachin

//@version=5
indicator("MACD x SuperTrend with trailing stoploss", "MACDxSTxTSL", overlay=true)

// Switches for long and short entries. They act to filter out multiple signals in adjacent candles.
var longEntry = false
var shortEntry = false

// holds short and long stoploss and take profit
var longSLPrice = 0.0
var longTPPrice = 0.0
var shortSLPrice = 0.0
var shortTPPrice = 0.0

long = false
short = false

// Risk to reward ratio
var RR = input(title="Risk to Reward", defval=2.0)

// supertrend indicator initiation
factor = input(title="Factor", defval=3.0)
atrPeriod = input(title="ATR Period", defval=10)
[supertrend, direction] = ta.supertrend(factor, atrPeriod)

// Determine the trend. Uptrend for long entries, downtrend for short entries
upTrend = direction < 0
downTrend = direction > 0

// get macd data as user input
fastlen = input(title="Fast Length", defval=12)
slowlen = input(title="Slow Length", defval=26)
siglen = input(title="Signal Smoothing", defval=9)
[macdLine, signalLine, histLine] = ta.macd(close, fastlen, slowlen, siglen)

// cross points of macd and signalLine. crossover for bullish signals. crossunder for bearish signals
crossover = ta.crossover(macdLine, signalLine)
crossunder = ta.crossunder(macdLine, signalLine)

// above or below zero line
above = true
below = true

consider = input(title="Consider Zero Line", defval=true)
if consider
    above := macdLine > 0 and signalLine > 0
    below := macdLine < 0 and signalLine < 0


// long entry signal logic
long := upTrend and crossover and not longEntry and not shortEntry and below

if long
    longEntry := true
    longTPPrice := close + (close-supertrend) * RR
    
    shortSLPrice := na
    shortTPPrice := na
    
if longEntry
    longSLPrice :=  supertrend
    
longProfit = longTPPrice < high and longEntry

if longProfit
    longEntry := false
    
    longSLPrice := na
    longTPPrice := na

longLoss = longSLPrice > low and longEntry

if longLoss
    longEntry := false
    
    longSLPrice := na
    longTPPrice := na

plotshape(long, style=shape.triangleup, size=size.tiny, color=color.green, text='long', location=location.abovebar)
plotshape(longProfit, style=shape.diamond, size=size.tiny, color=color.green, text='long profit', location=location.abovebar)
plotshape(longLoss, style=shape.xcross, size=size.tiny, color=color.orange, text='long loss', location=location.abovebar)


// short entry signal logic
short := downTrend and crossunder and not shortEntry and not longEntry and above
if short
    shortEntry := true
    shortTPPrice := close - (supertrend-close) * RR
    
    longSLPrice := na
    longTPPrice := na
    
if shortEntry
    shortSLPrice := supertrend
    
shortProfit = shortTPPrice > low and shortEntry

if shortProfit
    shortEntry := false
    
    shortSLPrice := na
    shortTPPrice := na

shortLoss = shortSLPrice < high and shortEntry

if shortLoss
    shortEntry := false
    
    shortSLPrice := na
    shortTPPrice := na

plotshape(short, style=shape.triangledown, size=size.tiny, color=color.red, text='short', location=location.belowbar)
plotshape(shortProfit, style=shape.diamond, size=size.tiny, color=color.red, text='short profit', location=location.belowbar)
plotshape(shortLoss, style=shape.xcross, size=size.tiny, color=color.orange, text='short loss', location=location.belowbar)

profit = color.new(#4cbb17, 20)
loss = color.new(#800000, 20)

plot(shortSLPrice, style=plot.style_linebr, color=loss, title="short loss", offset=1, linewidth=3)
plot(shortTPPrice, style=plot.style_linebr,  color=profit, title="short profit", offset=1, linewidth=3)
plot(longSLPrice, style=plot.style_linebr,  color=loss, title="long loss", offset=1, linewidth=3)
plot(longTPPrice, style=plot.style_linebr,  color=profit, title="long profit", offset=1, linewidth=3)

ema = ta.ema(close,200)
plot(ema, color=color.purple, linewidth=2)