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

//@version=5
indicator('Consecutive Momentum')


MAX_Length = input.int(defval = 120, title='MAX Length', minval = 1, step=1, group = 'Length')
EMA_Length = input.int(defval = 120, title='WMA Length', minval = 1, step=1, group = 'Length')

show_dir = input.bool(defval = true, title='Show Direction?')
show_candle = input.string(defval = 'Percent', title='Show Consecutive Candle/Percent/Heikin Ashi Percent?', options =['Candle', 'Percent'])

heikinashi = input.bool(defval = false, title = 'Use Source as HeikinAshi', group = 'Source')

var int cnt_up = 0
var int cnt_down = 0

var float consecutive_plus = 0.0
var float consecutive_minus = 0.0

src_open = open
src_close = close

heikinashi_close = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close)
heikinashi_open = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, open)

if heikinashi
    src_close := heikinashi_close
    src_open := heikinashi_open


barup = src_close > src_open
bardown = src_close < src_open

if barup
    cnt_up := cnt_up[1] +1
    consecutive_plus := consecutive_plus[1] + (src_close[1]-src_open[1])
    cnt_down := 0
    consecutive_minus := 0
if bardown
    cnt_down := cnt_down[1] +1
    consecutive_minus := consecutive_minus[1] + (src_open[1] - src_close[1])
    cnt_up := 0
    consecutive_plus := 0
    
consecutive_plus_percent = (src_close-src_close[cnt_up])*100/src_close
consecutive_minus_percent = ((src_close[cnt_down])-src_close)*100/src_close[cnt_down]

max_consecutive_up = ta.highest(cnt_up, MAX_Length)
max_consecutive_down = ta.highest(cnt_down, MAX_Length)

ma_max = show_dir ? ta.wma(cnt_up, EMA_Length) : ta.ema((cnt_up+cnt_down)/2, EMA_Length)
ma_min = show_dir ? ta.wma(cnt_down, EMA_Length)*-1 : na


if show_candle == 'Percent'
    max_consecutive_up := ta.highest(consecutive_plus_percent, MAX_Length)
    max_consecutive_down := ta.highest(consecutive_minus_percent, MAX_Length)
    ma_max := show_dir ? ta.wma(max_consecutive_up, EMA_Length) : ta.wma((max_consecutive_up+max_consecutive_down)/2, EMA_Length)
    ma_min := show_dir ? ta.wma(max_consecutive_down, EMA_Length)*-1 : na


hline(5, title ='5', color=color.new(color.white,40), linestyle=hline.style_dashed)

hline(show_dir ? -5 : na, title ='-5', color=color.new(color.white,40), linestyle=hline.style_dashed)

plot(show_candle=='Candle' ? cnt_up : consecutive_plus_percent, title='Consecutive Bars Up', color=color.new(color.green, 0), style=plot.style_histogram, linewidth=1)

plot(max_consecutive_up, title = 'Max Consecutive Up Bars', color = color.new(#7ff2a4, 33), style=plot.style_linebr)

plot(show_candle=='Candle' ? (show_dir ? -cnt_down : cnt_down) : (show_dir ? -consecutive_minus_percent : consecutive_minus_percent), title='Consecutive Bars Down', color=color.new(color.red, 0), style=plot.style_histogram, linewidth=1)


plot(show_dir ? -max_consecutive_down : max_consecutive_down, title = 'Max Consecutive Down Bars', color = color.new(#e986af, 20), style=plot.style_linebr)



plot(ma_max, title = 'MA cnt_up', color=color.new(#21b1f3, 0), style=plot.style_line, trackprice = true)
plot(ma_min, title = 'MA cnt_down', color=color.new(#21b1f3, 20), style=plot.style_line, trackprice = true)


