Hx MTF Moving Average
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HDrbx
//@version=5

indicator(title="Hx MTF Moving Average", shorttitle = "Hx MTF MA", overlay=true)
ma1Type = input.string(title='MA Type', defval='Smma', options=['Sma', 'Ema', 'VWma', 'Wma', 'Rma', 'Hull', 'Smma'], inline='1')
ma1Length = input.int(title='Length', defval=50, inline='1')
ma1Source = input.source(title='Source', defval=close, inline='1')

ma1Show = input.bool(title='', defval=true, inline='2')
tf1Input = input.timeframe(title="TF1", defval="15", inline = '2')
ma1Color = input.color(title='  Color', defval=color.red, inline='2')
ma1Width = input.int(title='  Width', defval = 2, inline = '2')

ma2Show = input.bool(title='', defval=true, inline='3')
tf2Input = input.timeframe(title="TF2", defval="60", inline = '3')
ma2Color = input.color(title='  Color', defval=color.yellow, inline='3')
ma2Width = input.int(title='  Width', defval = 2, inline = '3')

ma3Show = input.bool(title='', defval=true, inline='4')
tf3Input = input.timeframe(title="TF3", defval="240", inline = '4')
ma3Color = input.color(title='  Color', defval=color.lime, inline='4')
ma3Width = input.int(title='  Width', defval = 2, inline = '4')

ma4Show = input.bool(title='', defval=true, inline='5')
tf4Input = input.timeframe(title="TF4", defval="D", inline = '5')
ma4Color = input.color(title='  Color', defval=color.blue, inline='5')
ma4Width = input.int(title='  Width', defval = 2, inline = '5')

currentTfIfDifferent = input.bool(title = 'current Timeframe if different', defval=false, inline = '6')
ma0Color = input.color(title=' Color', defval=color.gray, inline='6')
ma0Width = input.int(title='  Width', defval = 2, inline = '6')

tfInMinutes(simple string tf = "") => //{
    float chartTf = timeframe.multiplier * (timeframe.isseconds ? 1. / 60 : timeframe.isminutes ? 1. : timeframe.isdaily   ? 60. * 24 : timeframe.isweekly  ? 60. * 24 * 7 : timeframe.ismonthly ? 60. * 24 * 30.4375  : na)
    float result = tf == "" ? chartTf : request.security(syminfo.tickerid, tf, chartTf)
//}

smma(_src, _length) => //{
	float _smma = 0.0
	float _sma = ta.sma(_src, _length)
	_smma := na(_smma[1]) ? _sma : (_smma[1] * (_length - 1) + _src) / _length
	_smma
//}

maSelect(_src, _length, _type) =>  //{
    float _ma = _type == 'Sma' ? ta.sma(_src, _length) : _type == 'Ema' ? ta.ema(_src, _length) : _type == 'VWma' ? ta.vwma(_src, _length) : _type == 'Hull' ? 
     ta.hma(_src, _length) : _type == 'Wma' ? ta.wma(_src, _length) : _type == 'Rma' ? ta.rma(_src, _length) : _type == 'Smma' ? smma(_src, _length) : na
    _ma
//}

float chartTfInMinutes  = tfInMinutes()
float inputTf1InMinutes = tfInMinutes(tf1Input)
float inputTf2InMinutes = tfInMinutes(tf2Input)
float inputTf3InMinutes = tfInMinutes(tf3Input)
float inputTf4InMinutes = tfInMinutes(tf4Input)

ma0 = maSelect(ma1Source, ma1Length, ma1Type)
ma1 = request.security(syminfo.tickerid, tf1Input, maSelect(ma1Source, ma1Length, ma1Type),barmerge.gaps_on)
ma2 = request.security(syminfo.tickerid, tf2Input, maSelect(ma1Source, ma1Length, ma1Type),barmerge.gaps_on)
ma3 = request.security(syminfo.tickerid, tf3Input, maSelect(ma1Source, ma1Length, ma1Type),barmerge.gaps_on)
ma4 = request.security(syminfo.tickerid, tf4Input, maSelect(ma1Source, ma1Length, ma1Type),barmerge.gaps_on)

ma0Display = currentTfIfDifferent and chartTfInMinutes != inputTf1InMinutes and chartTfInMinutes != inputTf2InMinutes and chartTfInMinutes != inputTf3InMinutes and chartTfInMinutes != inputTf4InMinutes
ma1Display = ma1Show and chartTfInMinutes <= inputTf1InMinutes
ma2Display = ma2Show and chartTfInMinutes <= inputTf2InMinutes
ma3Display = ma3Show and chartTfInMinutes <= inputTf3InMinutes
ma4Display = ma4Show and chartTfInMinutes <= inputTf4InMinutes

plot(ma0Display ? ma0 : na, color=ma0Color, linewidth=ma0Width)
plot(ma1Display ? ma1 : na, title='MA 1', color=ma1Color, linewidth=ma1Width)
plot(ma2Display ? ma2 : na, title='MA 2', color=ma2Color, linewidth=ma2Width)
plot(ma3Display ? ma3 : na, title='MA 3', color=ma3Color, linewidth=ma3Width)
plot(ma4Display ? ma4 : na, title='MA 4', color=ma4Color, linewidth=ma4Width)





































