[MAD] Multi-MA MTF
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © djmad
// @ for Fibstep -> Sofien-Kaabar
//@version=5

indicator(title='[MAD] Multi-MA MTF', overlay=true)


import djmad/MAD_MATH/3 as mathematics



//************************************************************************************************************
// Variables and Inputs {
//************************************************************************************************************
labels_switch = input.bool(false, title="Show labels", group = "Lables")

string B1_TF1       = input.timeframe('', title='TF1',group="MA-1", inline="1a")
int B1_len          = input.int(20, minval=1, title='Length',group="MA-1", inline="1a")
string B1_type      = input.string("SMA", "MA Type", options=['OFF','WMA','HMA','VWMA','LMA','RMA','SMA','EMA','Ehlers EMA','Ehlers Gaussian','Ehlers Smoother','Ehlers Supersmoother','Ehlers Butterworth','ChebyshevI','ChebyshevII'],group="MA-1")

string  B2_TF2      = input.timeframe('', title='TF 2',group="MA-2", inline="2a")
int B2_len          = input.int(50, minval=1, title='Length',group="MA-2", inline="2a")
string  B2_type     = input.string("SMA", "MA Type", options=['OFF','WMA','HMA','VWMA','LMA','RMA','SMA','EMA','Ehlers EMA','Ehlers Gaussian','Ehlers Smoother','Ehlers Supersmoother','Ehlers Butterworth','ChebyshevI','ChebyshevII'],group="MA-2")

string  B3_TF3      = input.timeframe('', title='TF 3',group="MA-3", inline="3a")
int B3_len          = input.int(100, minval=1, title='Length',group="MA-3", inline="3a")
string  B3_type     = input.string("SMA", "MA Type", options=['OFF','WMA','HMA','VWMA','LMA','RMA','SMA','EMA','Ehlers EMA','Ehlers Gaussian','Ehlers Smoother','Ehlers Supersmoother','Ehlers Butterworth','ChebyshevI','ChebyshevII'],group="MA-3")

string  B4_TF4      = input.timeframe('', title='TF 3',group="MA-4", inline="4a")
int B4_len          = input.int(200, minval=1, title='Length',group="MA-4", inline="4a")
string  B4_type     = input.string("SMA", "MA Type", options=['OFF','WMA','HMA','VWMA','LMA','RMA','SMA','EMA','Ehlers EMA','Ehlers Gaussian','Ehlers Smoother','Ehlers Supersmoother','Ehlers Butterworth','ChebyshevI','ChebyshevII'],group="MA-4")

string  B5_TF5      = input.timeframe('', title='TF 3',group="MA-5", inline="5a")
int B5_len          = input.int(300, minval=1, title='Length',group="MA-5", inline="5a")
string  B5_type     = input.string("SMA", "MA Type", options=['OFF','WMA','HMA','VWMA','LMA','RMA','SMA','EMA','Ehlers EMA','Ehlers Gaussian','Ehlers Smoother','Ehlers Supersmoother','Ehlers Butterworth','ChebyshevI','ChebyshevII'],group="MA-5")

string  B6_TF6      = input.timeframe('', title='TF 3',group="MA-6", inline="6a")
int B6_len          = input.int(600, minval=1, title='Length',group="MA-6", inline="6a")
string  B6_type     = input.string("SMA", "MA Type", options=['OFF','WMA','HMA','VWMA','LMA','RMA','SMA','EMA','Ehlers EMA','Ehlers Gaussian','Ehlers Smoother','Ehlers Supersmoother','Ehlers Butterworth','ChebyshevI','ChebyshevII'],group="MA-6")


MA_res_01 = request.security(syminfo.tickerid, B1_TF1,  mathematics.f_getall(_type = B1_type, _src = close, _length = B1_len),  barmerge.gaps_off, barmerge.lookahead_off)
MA_res_02 = request.security(syminfo.tickerid, B2_TF2,  mathematics.f_getall(_type = B2_type, _src = close, _length = B2_len),  barmerge.gaps_off, barmerge.lookahead_off)
MA_res_03 = request.security(syminfo.tickerid, B3_TF3,  mathematics.f_getall(_type = B3_type, _src = close, _length = B3_len),  barmerge.gaps_off, barmerge.lookahead_off)
MA_res_04 = request.security(syminfo.tickerid, B4_TF4,  mathematics.f_getall(_type = B4_type, _src = close, _length = B4_len),  barmerge.gaps_off, barmerge.lookahead_off)
MA_res_05 = request.security(syminfo.tickerid, B5_TF5,  mathematics.f_getall(_type = B5_type, _src = close, _length = B5_len),  barmerge.gaps_off, barmerge.lookahead_off)
MA_res_06 = request.security(syminfo.tickerid, B6_TF6,  mathematics.f_getall(_type = B6_type, _src = close, _length = B6_len),  barmerge.gaps_off, barmerge.lookahead_off)

plot(MA_res_01, color=color.new(color.red, 0))
plot(MA_res_02, color=color.new(color.orange, 0))
plot(MA_res_03, color=color.new(#d4aa00, 0))
plot(MA_res_04, color=color.new(#00d420, 0))
plot(MA_res_05, color=color.new(#006ad4, 0))
plot(MA_res_06, color=color.new(#0019d4, 0))

f_round(_val, _decimals) =>
    // Rounds _val to _decimals places.
    _p = math.pow(10, _decimals)
    math.round(math.abs(_val) * _p) / _p * math.sign(_val)

textcoloring = input(color.rgb(255, 255, 255, 0), title='textcolor')

//LABELS
var label T1 = na
var label T2 = na
var label T3 = na
var label T4 = na
var label T5 = na
var label T6 = na

label.delete(T1)
label.delete(T2)
label.delete(T3)
label.delete(T4)
label.delete(T5)
label.delete(T6)

T1 := labels_switch? label.new(bar_index, MA_res_01, style=label.style_label_left, text=B1_type + '-' + str.tostring(B1_len) + '-' + B1_TF1 + ' - ' + str.tostring(f_round(MA_res_01, 2)), textcolor=textcoloring, color=#00000000):na
T2 := labels_switch? label.new(bar_index, MA_res_02, style=label.style_label_left, text=B2_type + '-' + str.tostring(B2_len) + '-' + B2_TF2 + ' - ' + str.tostring(f_round(MA_res_02, 2)), textcolor=textcoloring, color=#00000000):na
T3 := labels_switch? label.new(bar_index, MA_res_03, style=label.style_label_left, text=B3_type + '-' + str.tostring(B3_len) + '-' + B3_TF3 + ' - ' + str.tostring(f_round(MA_res_03, 2)), textcolor=textcoloring, color=#00000000):na
T4 := labels_switch? label.new(bar_index, MA_res_04, style=label.style_label_left, text=B4_type + '-' + str.tostring(B4_len) + '-' + B4_TF4 + ' - ' + str.tostring(f_round(MA_res_04, 2)), textcolor=textcoloring, color=#00000000):na
T5 := labels_switch? label.new(bar_index, MA_res_05, style=label.style_label_left, text=B5_type + '-' + str.tostring(B5_len) + '-' + B5_TF5 + ' - ' + str.tostring(f_round(MA_res_05, 2)), textcolor=textcoloring, color=#00000000):na
T6 := labels_switch? label.new(bar_index, MA_res_06, style=label.style_label_left, text=B6_type + '-' + str.tostring(B6_len) + '-' + B6_TF6 + ' - ' + str.tostring(f_round(MA_res_06, 2)), textcolor=textcoloring, color=#00000000):na

//////////////////// 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,   "MA 1 switch",   minval=-1,   maxval=15,group='Multibit',inline='1a')
Signal_Channel_Line2=   input.int(-1,   "MA 2 switch",  minval=-1,   maxval=15,group='Multibit',inline='1a')
Signal_Channel_Line3=   input.int(-1,   "MA 3 switch",   minval=-1,   maxval=15,group='Multibit',inline='1b')
Signal_Channel_Line4=   input.int(-1,   "MA 4 switch",   minval=-1,   maxval=15,group='Multibit',inline='1b')
Signal_Channel_Line5=   input.int(-1,   "MA 5 switch",  minval=-1,   maxval=15,group='Multibit',inline='1c')
Signal_Channel_Line6=   input.int(-1,   "MA 6 switch",   minval=-1,   maxval=15,group='Multibit',inline='1c')


bool a_MAs_1 = close > MA_res_01
bool a_MAs_2 = close > MA_res_02
bool a_MAs_3 = close > MA_res_03
bool a_MAs_4 = close > MA_res_04
bool a_MAs_5 = close > MA_res_05
bool a_MAs_6 = close > MA_res_06

//*********** 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_MAs_1, Signal_Channel_Line2, a_MAs_2, Signal_Channel_Line3, a_MAs_3, Signal_Channel_Line4, a_MAs_4, Signal_Channel_Line5, a_MAs_5, Signal_Channel_Line6, a_MAs_6, Multibit)

float plot_output = transcode._16bit_encode(Multibit)

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