True Momentum Oscillator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LeafAlgo

//@version=5
indicator("True Momentum Oscillator", shorttitle = 'TMO', overlay=false)

//Inputs//
tmoleng= input.int(14,title='TMO Length')
calcLeng = input(5, title='Calcuated Length')
smoothLeng = input(3, title='Smooth Line Length')
lengType = input.string('EMA', title='TMO length moving average selection', options=['EMA', 'SMA', 'RMA','WVMA', 'VWAP','HULL'])
calcLengType = input.string('EMA', title='Calcalculated length moving average selection', options=['EMA', 'SMA', 'RMA','WVMA','VWAP','HULL'])
smoothLengType = input.string('EMA', title='Smooth length moving average selection', options=['EMA', 'SMA', 'RMA', 'WVMA', 'VWAP','HULL'])

//Moving Average Selection//
Multi_ma(type, src, len) =>
    float result = 0
    if type == "EMA"
        result := ta.ema(src, len)
    if type == "SMA"
        result := ta.sma(src, len)
    if type == "RMA"
        result := ta.rma(src, len)
    if type == 'WVMA'  
        result := ta.vwma(src,len)
    if type == 'VWAP'
        result := ta.vwap(src, len)
    if type == 'HULL'
        result := ta.hma(src, len)
    result

//TMO//
OS=open
CS= close

dataset = 0
for i1 = 1 to tmoleng -1
    if CS > OS[i1]
        dataset := dataset + 1
    if CS < OS[i1]
        dataset := dataset - 1



MA = Multi_ma(lengType, dataset, calcLeng)
MainSignal = Multi_ma(calcLengType, MA, smoothLeng)
SmoothSignal =Multi_ma(smoothLengType, MainSignal, smoothLeng)
Hist=MainSignal-SmoothSignal

mom_len = input.int(14, minval=1, title='Momentum Length')
tmo_mom_main = MainSignal - MainSignal[mom_len]
tmo_mom_smooth = SmoothSignal - SmoothSignal[mom_len]


//PLots//

ob = hline(math.round(tmoleng * .7), title='overbought line', color=color.gray, linestyle=hline.style_solid)
os = hline(-math.round(tmoleng * .7), title='oversold line', color=color.gray, linestyle=hline.style_solid)
upper = hline(tmoleng, title='upper line', color=color.red, linestyle=hline.style_solid)
lower = hline(-tmoleng, title='lower line', color=color.green, linestyle=hline.style_solid)
zero = hline(0, title='zero line', color=color.gray, linestyle=hline.style_solid)

main = plot(MainSignal, title='main line', color=MainSignal>SmoothSignal?color.green:color.red, linewidth=2)
signal = plot(SmoothSignal, title='signal line', color=color.gray, linewidth=2)
cross = plot(ta.cross(MainSignal,SmoothSignal)?MainSignal:na, title="crossover dot", color=MainSignal>SmoothSignal?color.green:color.red, style=plot.style_circles, linewidth=3)
histogram = plot(Hist, title='histogram', color=MainSignal>SmoothSignal?color.green:color.red, style=plot.style_histogram)
fill(main, signal, title='main and signal fill', color=MainSignal>SmoothSignal?color.green:color.red)
fill(ob, upper, title='overbought fill', color=color.new(color.gray,20) )
fill(os, lower, title='oversold fill', color=color.new(color.gray,20))

mommain = plot(tmo_mom_main, color=color.orange, title='TMO Main Momentum')
momsignal= plot(tmo_mom_smooth, color=color.yellow, title='TMO Smooth Momentum')

bc= MainSignal > SmoothSignal ? color.green : MainSignal < SmoothSignal ? color.red : color.gray
barcolor(bc)