Fusion: Monster Breakout Index
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Koalems

// ***************************************************************************************************
// CREDITS
// ***************************************************************************************************
// Modified from the Monster Breakout Index by racer8 under the licence of 
//    This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

// ***************************************************************************************************
// TYPE
// ***************************************************************************************************

// @version=5
indicator(
  shorttitle        = 'Fusion: MBI', 
  title             = 'Fusion: Monster Breakout Index', 
  overlay=false)

// ***************************************************************************************************
// INPUT
// ***************************************************************************************************
Group6500 = '6500 Monster breakout index (MBI) - SHOW'
mbiShowBars                           = input.bool(true,                            group=Group6500,                                                                  title='Bars')
mbiShowBarsContinuation               = input.bool(true,                            group=Group6500,                                                                  title='  Continuation bars')
mbiShowChannel                        = input.bool(false,                           group=Group6500,                                                                  title='Channel')
mbiShowBarBackMarker                  = input.bool(false,                           group=Group6500,                                                                  title='Bar back marker')

Group6520 = '6520 MBI - RULES'
mbiRuleNewTrumpsOld                   = input.bool(false,                           group=Group6520,                                                                  title='New direction overrides old',  tooltip="If both an up and down signal occur on the same bar than then one that's a new direction wins and the old direction demoted.\n\nA domoted signal will be colored purple.")
mbiRuleExcludeDual                    = input.bool(false,                           group=Group6520,                                                                  title='Exclude dual directions',      tooltip="If both an up and down signal occur on the same bar then both signals are demoted. If 'New direction overrides old' is also set then this rule override it.\n\nA domoted signal will be colored purple.")

Group6530 = '6530 MBI - SETTINGS'
tooltipLength="The brighter colors represent when the current candle's high is higher (or low is lower) than the highest/lowest found for the length number of candles.\n\nThe softer colors represent a candle that does not meet the above criterion but is just \'following on\' from a prior found higher/lower candle. This distinguishing may be useful for rules of entries and exits."
mbiHighestLength                      = input.int(18,                               group=Group6530, minval=2,                                                        title='Highest length', tooltip=tooltipLength)
mbiLowestLength                       = input.int(18,                               group=Group6530, minval=2,                                                        title='Lowest length',  tooltip=tooltipLength)
mbiSourceHighest                      = input.source(hl2,                           group=Group6530,                                                                  title='Source Highest')
mbiSourceLowest                       = input.source(hl2,                           group=Group6530,                                                                  title='Source Lowest')

// ***************************************************************************************************
// Indicator
// ***************************************************************************************************
mbiH = ta.highest(mbiSourceHighest, mbiHighestLength)
mbiL = ta.lowest (mbiSourceLowest,  mbiLowestLength)

mbiHigh = mbiH[1]
mbiLow  = mbiL[1]

mbiUp = high > mbiHigh ?  1 : na
mbiDn = low  < mbiLow  ? -1 : na

var mbiSoftLevel = 1

mbiSoftUp = na(mbiUp) and not na(mbiUp[1]) and not mbiDn ?  mbiSoftLevel : na
mbiSoftDn = na(mbiDn) and not na(mbiDn[1]) and not mbiUp ? -mbiSoftLevel : na

if na(mbiSoftUp) and not na(mbiSoftUp[1]) and not mbiDn
    mbiSoftUp := mbiSoftLevel

if na(mbiSoftDn) and not na(mbiSoftDn[1]) and not mbiUp
    mbiSoftDn := -mbiSoftLevel

float mbiConflictUp = na
float mbiConflictDn = na

if mbiUp and mbiDn
    if mbiRuleExcludeDual
        mbiUp := na
        mbiDn := na
        mbiConflictUp :=  mbiSoftLevel
        mbiConflictDn := -mbiSoftLevel

    else if mbiRuleNewTrumpsOld
        if mbiUp[1]
            mbiUp := na
            mbiConflictUp :=  mbiSoftLevel

        if mbiDn[1]
            mbiDn := na
            mbiConflictDn := -mbiSoftLevel

// ***************************************************************************************************
// SHOW
// ***************************************************************************************************
plot(mbiShowBars ? mbiUp : na, color=color.new(color.lime, 30), style=plot.style_columns, title='MBI up')
plot(mbiShowBars ? mbiDn : na, color=color.new(color.red,  30), style=plot.style_columns, title='MBI down')

plot(mbiShowBars and mbiShowBarsContinuation ? mbiSoftUp : na , color=color.new(color.lime, 80), style=plot.style_columns, title='MBI soft up')
plot(mbiShowBars and mbiShowBarsContinuation ? mbiSoftDn : na , color=color.new(color.red,  70), style=plot.style_columns, title='MBI soft down')

plot(mbiShowBars ? mbiConflictUp : na , color=color.new(color.purple, 25), style=plot.style_columns, title='MBI conflict up')
plot(mbiShowBars ? mbiConflictDn : na , color=color.new(color.purple, 25), style=plot.style_columns, title='MBI conflict down')

mbiHighPlot = plot(mbiShowChannel ? mbiHigh : na, color=color.new(#006064, 0), linewidth=1, style=plot.style_line, title='MBI high')
mbiLowPlot  = plot(mbiShowChannel ? mbiLow  : na, color=color.new(#006064, 0), linewidth=1, style=plot.style_line, title='MBI low')

fill(mbiHighPlot, mbiLowPlot, color=color.new(#43c2be, 92), title="MBI fill")
plotchar(mbiShowBarBackMarker and barstate.isrealtime, offset=-mbiHighestLength - 1, char='↡', location=location.top, color=color.yellow, title='MBI bar back')
plotchar(mbiShowBarBackMarker and barstate.isrealtime, offset=-mbiLowestLength  - 1, char='↡', location=location.top, color=color.yellow, title='MBI bar back')
