[PUZ] MACD MTB System MTF
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PuzzlesAnalytik

//@version=5
indicator("[PUZ] MACD MTB System MTF", overlay = false)

timeframe = input.timeframe("", "Timeframe Value", group = "Timeframe")

long_threshold_L1           =           input.int(  0, title   = "Crossover Thrshold Long L1", group = "Thresholds")
short_threshold_L1          =           input.int(  0, title   = "Crossunder Threshold Short L1", group = "Thresholds")
long_threshold_L2           =           input.int(  0, title   = "Risingcondition Thrshold Long L2", group = "Thresholds")
short_threshold_L2          =           input.int(  0, title   = "Falingcondition Threshold Short L2", group = "Thresholds")
long_threshold_L3           =           input.int(-50, title   = "Thrshold Long L3", group = "Thresholds")
short_threshold_L3          =           input.int(100, title   = "Threshold Short L3", group = "Thresholds")

bool showalerts_L1          =           input.bool(true, 'Show alerts L1',group='Alerts', inline='1a')
bool showalerts_L2          =           input.bool(true, 'Show alerts L2',group='Alerts', inline='1a')
bool showalerts_L3          =           input.bool(true, 'Show alerts L3',group='Alerts', inline='1a')

risingcolor                 =           input.color(color.green, "Rising Color", group = "Colors")
falingcolor                 =           input.color(color.yellow, "Falin Color", group = "Colors")
defaultcolor                =           input.color(color.gray, "Default Color", group = "Colors")
SlowLineUpColor             =           input.color(color.blue, "Rising Signal Line Color", group = "Colors")
SlowLineDownColor           =           input.color(color.red, "Faling Signal Line Color", group = "Colors")

[macdLine, signalLine, histLine] = request.security(symbol = syminfo.tickerid, timeframe = timeframe, expression = ta.macd(close, 12, 26, 9))

histLine := ta.sma(histLine, 10)

//{ Color Condition 

//MACD Line Color

risingcondition = macdLine > macdLine[1] and macdLine < signalLine
falingcondition = macdLine < macdLine[1] and macdLine > signalLine


colorCondition = risingcondition ? color.new(risingcolor, 0) :
                 (falingcondition ? color.new(falingcolor, 0) : defaultcolor)

//Signal Line Color 
var color SignalLineColor = na

SignalLineColor := ta.crossover(macdLine, signalLine) ? SlowLineUpColor : ta.crossunder(macdLine, signalLine) ? SlowLineDownColor : SignalLineColor


//Hist Line Color 

var color histcolor = na 

a = color.rgb(76, 177, 79)
a_1 = color.rgb(181, 233, 183)

b = color.red
b_1 = color.rgb(250, 143, 143)

histcolor := histLine > histLine[1] and histLine < 0 ? b_1 : histLine < 0 ? b : histLine < histLine[1] and histLine > 0 ? a_1 : histLine > 0 ? a : histcolor

//}

// Creating the Alarms Start {
//************************************************************************************************************
bool    a_L1_long      = false
bool    a_L1_short     = false
bool    a_L2_long      = false
bool    a_L2_short     = false
bool    a_L3_long      = false
bool    a_L3_short     = false



// Long Condition 


if macdLine < long_threshold_L1
    a_L1_long := ta.crossover(macdLine, signalLine)

if macdLine < long_threshold_L2
    a_L2_long := risingcondition ? true : false

if macdLine < long_threshold_L3
    a_L3_long := histLine > histLine[1] and histLine[1] > histLine[2] and histLine < 0 ? true : false


// Short Condition 
if macdLine > short_threshold_L1
    a_L1_short := ta.crossunder(macdLine, signalLine)

if macdLine > short_threshold_L2
    a_L2_short := falingcondition ? true : false

if macdLine > short_threshold_L3
    a_L3_short := histLine < histLine[1] and histLine[1] < histLine[2] and histLine > 0 ? true : false


alertcondition(a_L3_long, 'MACDD  -50  Crossover',     'MACDD -50  Crossover')
alertcondition(a_L2_long, 'MACDD  -20  Crossover',     'MACDD -20  Crossover')
alertcondition(a_L1_long, 'MACDD    0  Crossover',     'MACDD   0  Crossover')
alertcondition(a_L1_short, 'MACDD   0  Crossunder',    'MACDD   0  Crossunder')
alertcondition(a_L3_short, 'MACDD  50  Crossunder',    'MACDD  50  Crossunder')
alertcondition(a_L3_short, 'MACDD 100  Crossunder',    'MACDD 100  Crossunder')

//}


plot(macdLine, color=colorCondition, linewidth = 2)
plot(signalLine, color=SignalLineColor, linewidth = 2)
plot(histLine, color=histcolor, linewidth = 4 ,style=plot.style_histogram)


// Plotting of Alarms {

plotshape(showalerts_L1? a_L1_long:    na  ,
 style=shape.triangleup, location=location.bottom, color=color.new(#ebff00, 60), size=size.tiny)
plotshape(showalerts_L1? a_L1_short:   na  ,
 style=shape.triangledown, location=location.top, color=color.new(#ebff00, 60), size=size.tiny)

plotshape(showalerts_L2? a_L2_long:    na  ,
 style=shape.triangleup, location=location.bottom, color=color.new(#00ff20, 30), size=size.small)
plotshape(showalerts_L2? a_L2_short:   na  ,
 style=shape.triangledown, location=location.top, color=color.new(#ff0000, 30), size=size.small)

plotshape(showalerts_L3? a_L3_long:    na  ,
 style=shape.diamond, location=location.bottom, color=color.new(#00ff20, 0), size=size.small)
plotshape(showalerts_L3? a_L3_short:   na  ,
 style=shape.diamond, location=location.top, color=color.new(#ff0000, 0), size=size.small)
 
//}


// SIGNAL Daisychain {

string  inputtype           =   input.string('NoInput' , title='Signal Type', group='Multibit signal config', options=['MultiBit', 'MultiBit_pass', 'NoInput'], tooltip='Multibit Daisychain with and without infusing\nMutlibit is the Signal-Type used in my Backtestsystem',inline='3a')
float   inputModule         =   input(title='Select L1 Indicator Signal', group='Multibit signal config', defval=close, inline='3a')

Signal_Channel_Line1=   input.int(-1,   'L1 long channel',   minval=-1,   maxval=15,group='Multibit',inline='1a')
Signal_Channel_Line2=   input.int(-1,   'L1 short channel',  minval=-1,   maxval=15,group='Multibit',inline='1a')
Signal_Channel_Line3=   input.int(-1,   'L2 long channel',   minval=-1,   maxval=15,group='Multibit',inline='1b')
Signal_Channel_Line4=   input.int(-1,   'L2 short channel',  minval=-1,   maxval=15,group='Multibit',inline='1b')
Signal_Channel_Line5=   input.int(-1,   'L3 long channel',   minval=-1,   maxval=15,group='Multibit',inline='1c')
Signal_Channel_Line6=   input.int(-1,   'L3 short channel',  minval=-1,   maxval=15,group='Multibit',inline='1c')




//*********** MULTIBIT Implementation
import djmad/Signal_transcoder_library/7 as transcode

bool [] Multibit = array.new<bool>(16,false)
if inputtype == 'MultiBit' or inputtype == 'MultiBit_pass'
    Multibit := transcode._16bit_decode(inputModule)

if inputtype != 'MultiBit_pass'
    transcode.f_infuse_signal(Signal_Channel_Line1, a_L1_long, Signal_Channel_Line2, a_L1_short, Signal_Channel_Line3, a_L2_long, Signal_Channel_Line4, a_L2_short, Signal_Channel_Line5, a_L3_long, Signal_Channel_Line6, a_L3_short, Multibit)

float plot_output = transcode._16bit_encode(Multibit)

plot(plot_output,title='MultiBit Signal',display=display.none)




