//@version=5
//  Thanks to dynausmaux for the code..
//  Thanks to falconCoin for https://www.tradingview.com/script/KVfgBvDd-Market-Cipher-B-Free-version-with-Buy-and-sell/ inspired me to start this.
//  Thanks to LazyBear for WaveTrend Oscillator https://www.tradingview.com/script/2KE8wTuF-Indicator-WaveTrend-Oscillator-WT/
//  Thanks to RicardoSantos for https://www.tradingview.com/script/3oeDh0Yq-RS-Price-Divergence-Detector-V2/
//  Thanks to LucemAnb for Plain Stochastic Divergence https://www.tradingview.com/script/FCUgF8ag-Plain-Stochastic-Divergence/
//  Thanks to andreholanda73 for MFI+RSI Area https://www.tradingview.com/script/UlGZzUAr/
//  I especially thanks to TradingView for its platform that facilitates development and learning.
//
//  CIRCLES & TRIANGLES:
//    - LITTLE CIRCLE: They appear at all WaveTrend wave crossings.
//    - GREEN CIRCLE: The wavetrend waves are at the oversold level and have crossed up (bullish).
//    - RED CIRCLE: The wavetrend waves are at the overbought level and have crossed down (bearish).
//    - GOLD/ORANGE CIRCLE: When RSI is below 20, WaveTrend waves are below or equal to -80 and have crossed up after good bullish divergence (DONT BUY WHEN GOLD CIRCLE APPEAR).
//    - None of these circles are certain signs to trade. It is only information that can help you.
//    - PURPLE TRIANGLE: Appear when a bullish or bearish divergence is formed and WaveTrend waves crosses at overbought and oversold points.
//
//  NOTES:
//    - I am not an expert trader or know how to program pine script as such, in fact it is my first indicator only to study and all the code is copied and modified from other codes that are published in TradingView.
//    - I am very grateful to the entire TV community that publishes codes so that other newbies like me can learn and present their results. This is an attempt to imitate Market Cipher B.
//    - Settings by default are for 4h timeframe, divergences are more stronger and accurate. Haven't tested in all timeframes, only 2h and 4h.
//    - If you get an interesting result in other timeframes I would be very grateful if you would comment your configuration to implement it or at least check it.
//
//  CONTRIBUTIONS:
//    - Tip/Idea: Add higher timeframe analysis for bearish/bullish patterns at the current timeframe.
//    + Bearish/Bullish FLAG:
//      - MFI+RSI Area are RED (Below 0).
//      - Wavetrend waves are above 0 and crosses down.
//      - VWAP Area are below 0 on higher timeframe.
//      - This pattern reversed becomes bullish.
//    - Tip/Idea: Check the last heikinashi candle from 2 higher timeframe
//    + Bearish/Bullish DIAMOND:
//      - HT Candle is red
//      - WT > 0 and crossed down

indicator(title='VuManChu B Divergences', shorttitle='VMC Cipher_B_Divergences')

// PARAMETERS {

// WaveTrend Settings
wtShow = input.bool(true, title = 'Show WaveTrend', group = 'WaveTrend Settings')
wtBuyShow = input.bool(true, title = 'Show Buy dots', group = 'WaveTrend Settings')
wtGoldShow = input.bool(true, title = 'Show Gold dots', group = 'WaveTrend Settings')
wtSellShow = input.bool(true, title = 'Show Sell dots', group = 'WaveTrend Settings')
wtDivShow = input.bool(true, title = 'Show Div. dots', group = 'WaveTrend Settings')
vwapShow = input.bool(true, title = 'Show Fast WT', group = 'WaveTrend Settings')
wtChannelLen = input.int(9, title = 'WT Channel Length', minval=1, group = 'WaveTrend Settings')
wtAverageLen = input.int(12, title = 'WT Average Length', minval=1, group = 'WaveTrend Settings')
wtMASource = input.source(hlc3, title = 'WT MA Source', group = 'WaveTrend Settings')
wtMALen = input.int(3, title = 'WT MA Length', minval=1, group = 'WaveTrend Settings')

// WaveTrend Overbought & Oversold Levels
obLevel = input.int(53, title = 'WT Overbought Level 1', group = 'WaveTrend Settings')
obLevel2 = input.int(60, title = 'WT Overbought Level 2', group = 'WaveTrend Settings')
obLevel3 = input.int(100, title = 'WT Overbought Level 3', group = 'WaveTrend Settings')
osLevel = input.int(-53, title = 'WT Oversold Level 1', group = 'WaveTrend Settings')
osLevel2 = input.int(-60, title = 'WT Oversold Level 2', group = 'WaveTrend Settings')
osLevel3 = input.int(-75, title = 'WT Oversold Level 3', group = 'WaveTrend Settings')

// WaveTrend Divergence Settings
wtShowDiv = input.bool(true, title = 'Show WT Regular Divergences', group = 'WaveTrend Settings')
wtShowHiddenDiv = input.bool(false, title = 'Show WT Hidden Divergences', group = 'WaveTrend Settings')
showHiddenDiv_nl = input.bool(true, title = 'Not apply OB/OS Limits on Hidden Divergences', group = 'WaveTrend Settings')
wtDivOBLevel = input.int(45, title = 'WT Bearish Divergence min', group = 'WaveTrend Settings')
wtDivOSLevel = input.int(-65, title = 'WT Bullish Divergence min', group = 'WaveTrend Settings')

// Divergence Extra Range Settings
wtDivOBLevel_addshow = input.bool(true, title = 'Show 2nd WT Regular Divergences', group = 'WaveTrend Settings')
wtDivOBLevel_add = input.int(15, title = 'WT 2nd Bearish Divergence', group = 'WaveTrend Settings')
wtDivOSLevel_add = input.int(-40, title = 'WT 2nd Bullish Divergence 15 min', group = 'WaveTrend Settings')

// RSI + MFI Settings
rsiMFIShow = input.bool(true, title = 'Show MFI', group = 'MFI Settings')
rsiMFIperiod = input.int(60, title = 'MFI Period', minval=1, group = 'MFI Settings')
rsiMFIMultiplier = input.float(150, title = 'MFI Area multiplier', minval=0.0, group = 'MFI Settings')
rsiMFIPosY = input.float(2.5, title = 'MFI Area Y Pos', group = 'MFI Settings')

// RSI Settings
rsiShow = input.bool(true, title = 'Show RSI', group = 'RSI Settings')
rsiSRC = input.source(close, title = 'RSI Source', group = 'RSI Settings')
rsiLen = input.int(14, title = 'RSI Length', minval=1, group = 'RSI Settings')
rsiOversold = input.int(30, title = 'RSI Oversold', minval=0, maxval=100, group = 'RSI Settings')
rsiOverbought = input.int(60, title = 'RSI Overbought', minval=0, maxval=100, group = 'RSI Settings')

// RSI Divergence Settings
rsiShowDiv = input.bool(false, title = 'Show RSI Regular Divergences', group = 'RSI Settings')
rsiShowHiddenDiv = input.bool(false, title = 'Show RSI Hidden Divergences', group = 'RSI Settings')
rsiDivOBLevel = input.int(60, title = 'RSI Bearish Divergence min', group = 'RSI Settings')
rsiDivOSLevel = input.int(30, title = 'RSI Bullish Divergence min', group = 'RSI Settings')

// Stochastic RSI Settings
stochShow = input.bool(true, title = 'Show Stochastic RSI', group = 'Stoch Settings')
stochUseLog = input.bool(true, title='Use Log?', group = 'Stoch Settings')
stochAvg = input.bool(false, title='Use Average of both K & D', group = 'Stoch Settings')
stochSRC = input.source(close, title = 'Stochastic RSI Source', group = 'Stoch Settings')
stochLen = input.int(14, title = 'Stochastic RSI Length', minval=1, group = 'Stoch Settings')
stochRsiLen = input.int(14, title = 'RSI Length', minval=1, group = 'Stoch Settings')
stochKSmooth = input.int(3, title = 'Stochastic RSI K Smooth', minval=1, group = 'Stoch Settings')
stochDSmooth = input.int(3, title = 'Stochastic RSI D Smooth', minval=1, group = 'Stoch Settings')

// Stochastic Divergence Settings
stochShowDiv = input.bool(false, title = 'Show Stoch Regular Divergences', group = 'Stoch Settings')
stochShowHiddenDiv = input.bool(false, title = 'Show Stoch Hidden Divergences', group = 'Stoch Settings')

// Schaff Trend Cycle Settings
tcLine = input.bool(false, title="Show Schaff TC line", group = 'Schaff Settings')
tcSRC = input.source(close, title = 'Schaff TC Source', group = 'Schaff Settings')
tclength = input.int(10, title="Schaff TC", minval=1, group = 'Schaff Settings')
tcfastLength = input.int(23, title="Schaff TC Fast Length", minval=1, group = 'Schaff Settings')
tcslowLength = input.int(50, title="Schaff TC Slow Length", minval=1, group = 'Schaff Settings')
tcfactor = input.float(0.5, title="Schaff TC Factor", minval=0.0, group = 'Schaff Settings')

// Sommi Flag Settings
sommiFlagShow = input.bool(false, title = 'Show Sommi flag', group = 'Sommi Settings')
sommiShowVwap = input.bool(false, title = 'Show Sommi F. Wave', group = 'Sommi Settings')
sommiVwapTF = input.string('720', title = 'Sommi F. Wave timeframe', group = 'Sommi Settings')
sommiVwapBearLevel = input.int(0, title = 'F. Wave Bear Level (less than)', group = 'Sommi Settings')
sommiVwapBullLevel = input.int(0, title = 'F. Wave Bull Level (more than)', group = 'Sommi Settings')
soomiFlagWTBearLevel = input.int(0, title = 'WT Bear Level (more than)', group = 'Sommi Settings')
soomiFlagWTBullLevel = input.int(0, title = 'WT Bull Level (less than)', group = 'Sommi Settings')
soomiRSIMFIBearLevel = input.int(0, title = 'Money flow Bear Level (less than)', group = 'Sommi Settings')
soomiRSIMFIBullLevel = input.int(0, title = 'Money flow Bull Level (more than)', group = 'Sommi Settings')

// Sommi Diamond Settings
sommiDiamondShow = input.bool(false, title = 'Show Sommi diamond', group = 'Sommi Settings')
sommiHTCRes = input.string('60', title = 'HTF Candle Res. 1', group = 'Sommi Settings')
sommiHTCRes2 = input.string('240', title = 'HTF Candle Res. 2', group = 'Sommi Settings')
soomiDiamondWTBearLevel = input.int(0, title = 'WT Bear Level (More than)', group = 'Sommi Settings')
soomiDiamondWTBullLevel = input.int(0, title = 'WT Bull Level (Less than)', group = 'Sommi Settings')

// MACD Colors Settings
macdWTColorsShow = input.bool(false, title = 'Show MACD Colors', group = 'MACD Settings')
macdWTColorsTF = input.string('240', title = 'MACD Colors MACD TF', group = 'MACD Settings')

darkMode = input.bool(false, title = 'Dark mode', group = 'Mode Settings')

// Colors
colorRed = color.new(color.red, 0)
colorPurple = color.new("#e600e6", 0)
colorGreen = color.new("#3fff00", 0)
colorOrange = color.new("#e2a400", 0)
colorYellow = color.new("#ffe500", 0)
colorWhite = color.new(color.white, 0)
colorPink = color.new("#ff00f0", 0)
colorBluelight = color.new("#31c0ff", 0)
colorWT2 = color.new("#0d47a1", 0)
colorWT2_ = color.new("#131722", 0)
colormacdWT1a = color.new("#4caf58", 0)
colormacdWT1b = color.new("#af4c4c", 0)
colormacdWT1c = color.new("#7ee57e", 0)
colormacdWT1d = color.new("#ff3535", 0)
colormacdWT2a = color.new("#305630", 0)
colormacdWT2b = color.new("#310101", 0)
colormacdWT2c = color.new("#132213", 0)
colormacdWT2d = color.new("#770000", 0)

// }


// FUNCTIONS {

// Divergences
f_top_fractal(src) =>
    src[4] < src[2] and src[3] < src[2] and src[2] > src[1] and src[2] > src[0]

f_bot_fractal(src) =>
    src[4] > src[2] and src[3] > src[2] and src[2] < src[1] and src[2] < src[0]

f_fractalize(src) =>
    f_top_fractal(src) ? 1 : f_bot_fractal(src) ? -1 : 0

f_findDivs(src, topLimit, botLimit, useLimits) =>
    fractalTop = f_fractalize(src) > 0 and (useLimits ? src[2] >= topLimit : true) ? src[2] : na
    fractalBot = f_fractalize(src) < 0 and (useLimits ? src[2] <= botLimit : true) ? src[2] : na
    highPrev = ta.valuewhen(fractalTop, src[2], 0)[2]
    highPrice = ta.valuewhen(fractalTop, high[2], 0)[2]
    lowPrev = ta.valuewhen(fractalBot, src[2], 0)[2]
    lowPrice = ta.valuewhen(fractalBot, low[2], 0)[2]
    bearSignal = fractalTop and high[2] > highPrice and src[2] < highPrev
    bullSignal = fractalBot and low[2] < lowPrice and src[2] > lowPrev
    bearDivHidden = fractalTop and high[2] < highPrice and src[2] > highPrev
    bullDivHidden = fractalBot and low[2] > lowPrice and src[2] < lowPrev
    [fractalTop, fractalBot, lowPrev, bearSignal, bullSignal, bearDivHidden, bullDivHidden]

// RSI + MFI
f_rsimfi(_period, _multiplier, _tf) =>
    ta.sma(((close - open) / (high - low)) * _multiplier, _period) - rsiMFIPosY

// WaveTrend
f_wavetrend(src, chlen, avg, malen, tf) =>
    tfsrc = request.security(syminfo.tickerid, tf, src)
    esa = ta.ema(tfsrc, chlen)
    de = ta.ema(math.abs(tfsrc - esa), chlen)
    ci = (tfsrc - esa) / (0.015 * de)
    wt1 = request.security(syminfo.tickerid, tf, ta.ema(ci, avg))
    wt2 = request.security(syminfo.tickerid, tf, ta.sma(wt1, malen))
    wtVwap = wt1 - wt2
    wtOversold = wt2 <= osLevel
    wtOverbought = wt2 >= obLevel
    wtCross = ta.cross(wt1, wt2)
    wtCrossUp = (wt2 - wt1) <= 0
    wtCrossDown = (wt2 - wt1) >= 0
    wtCrosslast = ta.cross(wt1[2], wt2[2])
    wtCrossUplast = (wt2[2] - wt1[2]) <= 0
    wtCrossDownlast = (wt2[2] - wt1[2]) >= 0
    [wt1, wt2, wtOversold, wtOverbought, wtCross, wtCrossUp, wtCrossDown, wtCrosslast, wtCrossUplast, wtCrossDownlast, wtVwap]

// Schaff Trend Cycle
f_tc(src, length, fastLength, slowLength) =>
    ema1 = ta.ema(src, fastLength)
    ema2 = ta.ema(src, slowLength)
    macdVal = ema1 - ema2
    alpha = ta.lowest(macdVal, length)
    beta = ta.highest(macdVal, length) - alpha
    gamma = (macdVal - alpha) / beta * 100
    gamma := beta > 0 ? gamma : nz(gamma[1])
    delta = gamma
    delta := nz(delta[1]) + (gamma - nz(delta[1])) * tcfactor
    epsilon = ta.lowest(delta, length)
    zeta = ta.highest(delta, length) - epsilon
    eta = (delta - epsilon) / zeta * 100
    eta := zeta > 0 ? eta : nz(eta[1])
    stcReturn = eta
    stcReturn := nz(stcReturn[1]) + (eta - nz(stcReturn[1])) * tcfactor
    stcReturn

// Stochastic RSI
f_stochrsi(_src, _stochlen, _rsilen, _smoothk, _smoothd, _log, _avg) =>
    src = _log ? math.log(_src) : _src
    rsi_val = ta.rsi(src, _rsilen)
    kk = ta.sma(ta.stoch(rsi_val, rsi_val, rsi_val, _stochlen), _smoothk)
    d1 = ta.sma(kk, _smoothd)
    avg_1 = (kk + d1) / 2
    k = _avg ? avg_1 : kk
    [k, d1]

// MACD
f_macd(src, fastlen, slowlen, sigsmooth, tf) =>
    fast_ma = request.security(syminfo.tickerid, tf, ta.ema(src, fastlen))
    slow_ma = request.security(syminfo.tickerid, tf, ta.ema(src, slowlen))
    macd = fast_ma - slow_ma
    signal = request.security(syminfo.tickerid, tf, ta.sma(macd, sigsmooth))
    hist = macd - signal
    [macd, signal, hist]

// MACD Colors on WT
f_macdWTColors(tf) =>
    hrsimfi = f_rsimfi(rsiMFIperiod, rsiMFIMultiplier, tf)
    [macd, signal, hist] = f_macd(close, 28, 42, 9, tf)
    macdup = macd >= signal
    macddown = macd <= signal
    macdWT1Color = macdup ? (hrsimfi > 0 ? colormacdWT1c : colormacdWT1a) : (macddown ? (hrsimfi < 0 ? colormacdWT1d : colormacdWT1b) : na)
    macdWT2Color = macdup ? (hrsimfi < 0 ? colormacdWT2c : colormacdWT2a) : (macddown ? (hrsimfi < 0 ? colormacdWT2d : colormacdWT2b) : na)
    [macdWT1Color, macdWT2Color]

// Get higher timeframe candle
f_getTFCandle(_tf) =>
    _open  = request.security(syminfo.tickerid, _tf, ta.hlc3, lookahead=barmerge.lookahead_on)
    _close = request.security(syminfo.tickerid, _tf, close, lookahead=barmerge.lookahead_on)
    _high  = request.security(syminfo.tickerid, _tf, high, lookahead=barmerge.lookahead_on)
    _low   = request.security(syminfo.tickerid, _tf, low, lookahead=barmerge.lookahead_on)
    hl2   = (_high + _low) / 2.0
    newBar = ta.change(_open)
    candleBodyDir = _close > _open
    [candleBodyDir, newBar]

// Sommi flag
f_findSommiFlag(tf, wt1, wt2, rsimfi, wtCross, wtCrossUp, wtCrossDown) =>
    [hwt1, hwt2, hwtOversold, hwtOverbought, hwtCross, hwtCrossUp, hwtCrossDown, hwtCrosslast, hwtCrossUplast, hwtCrossDownlast, hwtVwap] = f_wavetrend(wtMASource, wtChannelLen, wtAverageLen, wtMALen, tf)
    
    bearPattern = (rsimfi < soomiRSIMFIBearLevel) and
                  (wt2 > soomiFlagWTBearLevel) and 
                  wtCross and 
                  wtCrossDown and 
                  (hwtVwap < sommiVwapBearLevel)
                   
    bullPattern = (rsimfi > soomiRSIMFIBullLevel) and 
                  (wt2 < soomiFlagWTBullLevel) and 
                  wtCross and 
                  wtCrossUp and 
                  (hwtVwap > sommiVwapBullLevel)
    
    [bearPattern, bullPattern, hwtVwap]

// Sommi diamond
f_findSommiDiamond(tf, tf2, wt1, wt2, wtCross, wtCrossUp, wtCrossDown) =>
    [candleBodyDir, newBar] = f_getTFCandle(tf)
    [candleBodyDir2, newBar2] = f_getTFCandle(tf2)
    bearPattern = (wt2 >= soomiDiamondWTBearLevel) and
                  wtCross and
                  wtCrossDown and
                  not candleBodyDir and
                  not candleBodyDir2
    bullPattern = (wt2 <= soomiDiamondWTBullLevel) and
                  wtCross and
                  wtCrossUp and
                  candleBodyDir and
                  candleBodyDir2
    [bearPattern, bullPattern]

// }


// CALCULATE INDICATORS {

// RSI
rsi = ta.rsi(rsiSRC, rsiLen)
rsiobcolor = input.color(color.new("#e13e3e", 0), 'RSI OverBought', group = 'Color Settings')
rsioscolor = input.color(color.new("#3ee145", 0), 'RSI OverSold', group = 'Color Settings')
rsinacolor = input.color(color.new("#c33ee1", 0), 'RSI InBetween', group = 'Color Settings')
rsiColor = rsi <= rsiOversold ? rsioscolor : rsi >= rsiOverbought ? rsiobcolor : rsinacolor

// RSI + MFI Area
rsiMFI = f_rsimfi(rsiMFIperiod, rsiMFIMultiplier, timeframe.period)
rsiMFIColorAbove = input.color(color.new("#3ee145", 0), 'MFI Color > 0', group = 'Color Settings')
rsiMFIColorBelow = input.color(color.new("#ff3d2e", 0), 'MFI Color < 0', group = 'Color Settings')
rsiMFIColor = rsiMFI > 0 ? rsiMFIColorAbove : rsiMFIColorBelow

// Calculates WaveTrend
[wt1, wt2, wtOversold, wtOverbought, wtCross, wtCrossUp, wtCrossDown, wtCross_last, wtCrossUplast, wtCrossDownlast, wtVwap] = 
    f_wavetrend(wtMASource, wtChannelLen, wtAverageLen, wtMALen, timeframe.period)

// Stochastic RSI
[stochK, stochD] = f_stochrsi(stochSRC, stochLen, stochRsiLen, stochKSmooth, stochDSmooth, stochUseLog, stochAvg)

// Schaff Trend Cycle
tcVal = f_tc(tcSRC, tclength, tcfastLength, tcslowLength)

// Sommi flag
[sommiBearish, sommiBullish, hvwap] = f_findSommiFlag(sommiVwapTF, wt1, wt2, rsiMFI, wtCross, wtCrossUp, wtCrossDown)

// Sommi diamond
[sommiBearishDiamond, sommiBullishDiamond] = f_findSommiDiamond(sommiHTCRes, sommiHTCRes2, wt1, wt2, wtCross, wtCrossUp, wtCrossDown)

// MACD colors
[macdWT1Color, macdWT2Color] = f_macdWTColors(macdWTColorsTF)

// WT Divergences
[wtFractalTop, wtFractalBot, wtLow_prev, wtBearDiv, wtBullDiv, wtBearDivHidden, wtBullDivHidden] = 
    f_findDivs(wt2, wtDivOBLevel, wtDivOSLevel, true)

[wtFractalTop_add, wtFractalBot_add, wtLow_prev_add, wtBearDiv_add, wtBullDiv_add, wtBearDivHidden_add, wtBullDivHidden_add] =  
    f_findDivs(wt2, wtDivOBLevel_add, wtDivOSLevel_add, true)

[wtFractalTop_nl, wtFractalBot_nl, wtLow_prev_nl, wtBearDiv_nl, wtBullDiv_nl, wtBearDivHidden_nl, wtBullDivHidden_nl] = 
    f_findDivs(wt2, 0, 0, false)

wtBearDivHidden_ = showHiddenDiv_nl ? wtBearDivHidden_nl : wtBearDivHidden
wtBullDivHidden_ = showHiddenDiv_nl ? wtBullDivHidden_nl : wtBullDivHidden

WTBearDivColorDown = input.color(color.new("#e60000", 0), 'WT Bear Div', group = 'Color Settings')
wtBullDivColorUp = input.color(color.new("#00e676", 0), 'WT Bull Div', group = 'Color Settings')

wtBearDivColor = (wtShowDiv and wtBearDiv) or (wtShowHiddenDiv and wtBearDivHidden_) ? WTBearDivColorDown : na
wtBullDivColor = (wtShowDiv and wtBullDiv) or (wtShowHiddenDiv and wtBullDivHidden_) ? wtBullDivColorUp : na

wtBearDivColor_add = (wtShowDiv and (wtDivOBLevel_addshow and wtBearDiv_add)) or 
                     (wtShowHiddenDiv and (wtDivOBLevel_addshow and wtBearDivHidden_add)) ? WTBearDivColorDown : na

wtBullDivColor_add = (wtShowDiv and (wtDivOBLevel_addshow and wtBullDiv_add)) or 
                     (wtShowHiddenDiv and (wtDivOBLevel_addshow and wtBullDivHidden_add)) ? wtBullDivColorUp : na

// RSI Divergences
[rsiFractalTop, rsiFractalBot, rsiLow_prev, rsiBearDiv, rsiBullDiv, rsiBearDivHidden, rsiBullDivHidden] = 
    f_findDivs(rsi, rsiDivOBLevel, rsiDivOSLevel, true)

[rsiFractalTop_nl, rsiFractalBot_nl, rsiLow_prev_nl, rsiBearDiv_nl, rsiBullDiv_nl, rsiBearDivHidden_nl, rsiBullDivHidden_nl] = 
    f_findDivs(rsi, 0, 0, false)

rsiBearDivHidden_ = showHiddenDiv_nl ? rsiBearDivHidden_nl : rsiBearDivHidden
rsiBullDivHidden_ = showHiddenDiv_nl ? rsiBullDivHidden_nl : rsiBullDivHidden

rsiBearColor = color.new("#e60000", 0)
rsiBullColor = color.new("#38ff42", 0)

rsiBearDivColor = (rsiShowDiv and rsiBearDiv) or 
                  (rsiShowHiddenDiv and rsiBearDivHidden_) ? rsiBearColor : na

rsiBullDivColor = (rsiShowDiv and rsiBullDiv) or 
                  (rsiShowHiddenDiv and rsiBullDivHidden_) ? rsiBullColor : na

// Stoch Divergences
[stochFractalTop, stochFractalBot, stochLow_prev, stochBearDiv, stochBullDiv, stochBearDivHidden, stochBullDivHidden] = 
    f_findDivs(stochK, 0, 0, false)

stochbearcolor = color.new("#e60000", 0)
stochbullcolor = color.new("#38ff42", 0)

stochBearDivColor = (stochShowDiv and stochBearDiv) or 
                    (stochShowHiddenDiv and stochBearDivHidden) ? stochbearcolor : na

stochBullDivColor = (stochShowDiv and stochBullDiv) or 
                    (stochShowHiddenDiv and stochBullDivHidden) ? stochbullcolor : na

// Small Circles WT Cross
signalcolorup = input.color(color.new("#00e676", 0), 'WT Buy Dot', group = 'Color Settings')
signalcolordown = input.color(color.new("#ff5252", 0), 'WT Sell Dot', group = 'Color Settings')

signalColor = wt2 - wt1 > 0 ? signalcolordown : signalcolorup

// Buy signal.
buySignal = wtCross and wtCrossUp and wtOversold

buySignalDiv = (wtShowDiv and wtBullDiv) or
              (wtShowDiv and wtBullDiv_add) or
              (stochShowDiv and stochBullDiv) or
              (rsiShowDiv and rsiBullDiv)

buySignalDiv_color = wtBullDiv ? colorGreen :
                      wtBullDiv_add ? color.new(colorGreen, 60) :
                      rsiShowDiv ? colorGreen : na

// Sell signal
sellSignal = wtCross and wtCrossDown and wtOverbought

sellSignalDiv = (wtShowDiv and wtBearDiv) or
               (wtShowDiv and wtBearDiv_add) or
               (stochShowDiv and stochBearDiv) or
               (rsiShowDiv and rsiBearDiv)

sellSignalDiv_color = wtBearDiv ? colorRed :
                       wtBearDiv_add ? color.new(colorRed, 60) :
                       rsiBearDiv ? colorRed : na

// Gold Buy
lastRsi = ta.valuewhen(wtFractalBot, rsi[2], 0)[2]
wtGoldBuy = ((wtShowDiv and wtBullDiv) or (rsiShowDiv and rsiBullDiv)) and
            wtLow_prev <= osLevel3 and
            wt2 > osLevel3 and
            wtLow_prev - wt2 <= -5 and
            lastRsi < 30

// }


// DRAW {

// Arka Plan Rengi
bgcolor(darkMode ? color.new(color.black, 0) : na)

// Sıfır Çizgisi
zLine = plot(0, color=color.new(colorWhite, 50))

// MFI BAR
rsiMfiBarTopLine = plot(rsiMFIShow ? -95 : na, title='MFI Bar TOP Line', color=color.new(color.white, 100))
rsiMfiBarBottomLine = plot(rsiMFIShow ? -99 : na, title='MFI Bar BOTTOM Line', color=color.new(color.white, 100))
fill(rsiMfiBarTopLine, rsiMfiBarBottomLine, title='MFI Bar Colors', color=rsiMFIColor, transp=75)

// WT Alanları
colorWT1blue = input.color(color.new("#4994ec", 0), "WT1 Fill", group='Color Settings')
colorWT2purple = input.color(color.new("#1f1559", 0), 'WT2 Fill', group='Color Settings')
plot(wtShow ? wt1 : na, style=plot.style_area, title='WT Wave 1', color=color.new(colorWT1blue, 30))
plot(wtShow ? wt2 : na, style=plot.style_area, title='WT Wave 2', color=color.new(colorWT2purple, 25))

// VWAP
VWAPColor = input.color(color.new("#ffffff", 50), "VWAP", group='Color Settings')
plot(vwapShow ? wtVwap : na, title='VWAP', color=VWAPColor, style=plot.style_area, linewidth=2, transp=45)

// MFI Alanı
plot(rsiMFIShow ? rsiMFI : na, style=plot.style_area, title='rsiMFI', color=color.new(rsiMFIColor, 50))

// WT Diverjansları
plot(wtFractalTop ? wt2[2] : na, title='WT Bearish Divergence', color=wtBearDivColor, linewidth=2, offset=-2)
plot(wtFractalBot ? wt2[2] : na, title='WT Bullish Divergence', color=wtBullDivColor, linewidth=2, offset=-2)

// WT İkinci Diverjanslar
plot(wtFractalTop_add ? wt2[2] : na, title='WT 2nd Bearish Divergence', color=wtBearDivColor_add, linewidth=2, offset=-2)
plot(wtFractalBot_add ? wt2[2] : na, title='WT 2nd Bullish Divergence', color=wtBullDivColor_add, linewidth=2, offset=-2)

// RSI
plot(rsiShow ? rsi : na, title='RSI', color=rsiColor, linewidth=2, transp=25)

// RSI Diverjansları
plot(rsiFractalTop ? rsi[2] : na, title='RSI Bearish Divergence', color=rsiBearDivColor, linewidth=1, offset=-2)
plot(rsiFractalBot ? rsi[2] : na, title='RSI Bullish Divergence', color=rsiBullDivColor, linewidth=1, offset=-2)

// Stochastic RSI
stochkcolor = input.color(color.new("#21baf3", 70), "Stoch K", group='Color Settings')
stochdcolor = input.color(color.new("#673ab7", 90), "Stoch D", group='Color Settings')

stochKplot = plot(stochShow ? stochK : na, title='Stoch K', color=stochkcolor, linewidth=2)
stochDplot = plot(stochShow ? stochD : na, title='Stoch D', color=stochdcolor, linewidth=1)
stochFillColor = stochK >= stochD ? color.new("#21baf3", 95) : color.new("#673ab7", 90)
fill(stochKplot, stochDplot, title='KD Fill', color=stochFillColor)

// Stochastic Diverjansları
plot(stochFractalTop ? stochK[2] : na, title='Stoch Bearish Divergence', color=stochBearDivColor, linewidth=1, offset=-2)
plot(stochFractalBot ? stochK[2] : na, title='Stoch Bullish Divergence', color=stochBullDivColor, linewidth=1, offset=-2)

// Schaff Trend Cycle
plot(tcLine ? tcVal : na, color=color.new("#673ab7", 25), linewidth=2, title="Schaff Trend Cycle 1")
plot(tcLine ? tcVal : na, color=color.new(colorWhite, 50), linewidth=1, title="Schaff Trend Cycle 2")

// Overbought & Oversold Çizgileri
oblvl2color = color.new(color.white, 0) // "OB LVL 2" için renk ayarı
oblvl3color = color.new(color.white, 0) // "OB LVL 3" için renk ayarı
oslvl2color = color.new(color.white, 0) // "OS LVL 2" için renk ayarı

plot(obLevel2, title='Over Bought Level 2', color=oblvl2color, linewidth=1, style=plot.style_stepline, transp=85)
plot(obLevel3, title='Over Bought Level 3', color=oblvl3color, linewidth=1, style=plot.style_circles, transp=95)

plot(osLevel2, title='Over Sold Level 2', color=oslvl2color, linewidth=1, style=plot.style_stepline, transp=85)

// Sommi Bayrakları
plotchar(sommiFlagShow and sommiBearish ? 108 : na, title='Sommi Bearish Flag', char='⚑', color=colorPink, location=location.absolute, size=size.tiny, transp=0)
plotchar(sommiFlagShow and sommiBullish ? -108 : na, title='Sommi Bullish Flag', char='⚑', color=colorBluelight, location=location.absolute, size=size.tiny, transp=0)
plot(sommiShowVwap ? ta.ema(hvwap, 3) : na, title='Sommi Higher VWAP', color=colorYellow, linewidth=2, style=plot.style_line, transp=55)

// Sommi Elmasları
plotchar(sommiDiamondShow and sommiBearishDiamond ? 108 : na, title='Sommi Bearish Diamond', char='◆', color=colorPink, location=location.absolute, size=size.tiny, transp=0)
plotchar(sommiDiamondShow and sommiBullishDiamond ? -108 : na, title='Sommi Bullish Diamond', char='◆', color=colorBluelight, location=location.absolute, size=size.tiny, transp=0)

// Küçük Çemberler WT Çaprazı
plot(wtCross ? wt2 : na, title='Buy and Sell Circle', color=signalColor, style=plot.style_circles, linewidth=3, transp=15)

// Alım Sinyalleri
plotchar(wtBuyShow and buySignal ? -107 : na, title='Buy Circle', char='·', color=colorGreen, location=location.absolute, size=size.small, transp=50)

// Satım Sinyalleri
plotchar(wtSellShow and sellSignal ? 105 : na, title='Sell Circle', char='·', color=colorRed, location=location.absolute, size=size.small, transp=50)

// Diverjans Alım Sinyalleri
plotchar(wtDivShow and buySignalDiv ? -106 : na, title='Divergence Buy Circle', char='•', color=buySignalDiv_color, location=location.absolute, size=size.small, offset=-2, transp=15)

// Diverjans Satım Sinyalleri
plotchar(wtDivShow and sellSignalDiv ? 106 : na, title='Divergence Sell Circle', char='•', color=sellSignalDiv_color, location=location.absolute, size=size.small, offset=-2, transp=15)

// Altın Alım Çemberi
plotchar(wtGoldBuy and wtGoldShow ? -106 : na, title='Gold Buy Circle', char='•', color=colorOrange, location=location.absolute, size=size.normal, offset=-2, transp=15)

// }


// ALERTS {

// BUY Alerts
alertcondition(buySignal, title='Buy (Big green circle)', message='Green circle WaveTrend Oversold')
alertcondition(buySignalDiv, title='Buy (Big green circle + Div)', message='Buy & WT Bullish Divergence & WT Overbought')
alertcondition(wtGoldBuy, title='GOLD Buy (Big GOLDEN circle)', message='Green & GOLD circle WaveTrend Overbought')
alertcondition(sommiBullish or sommiBullishDiamond, title='Sommi bullish flag/diamond', message='Blue flag/diamond')
alertcondition(wtCross and wtCrossUp, title='Buy (Small green dot)', message='Buy small circle')

// SELL Alerts
alertcondition(sommiBearish or sommiBearishDiamond, title='Sommi bearish flag/diamond', message='Purple flag/diamond')
alertcondition(sellSignal, title='Sell (Big red circle)', message='Red Circle WaveTrend Overbought')
alertcondition(sellSignalDiv, title='Sell (Big red circle + Div)', message='Sell & WT Bearish Divergence & WT Overbought')
alertcondition(wtCross and wtCrossDown, title='Sell (Small red dot)', message='Sell small circle')

// }


//@version=5
indicator(title='VuManChu B Divergences', shorttitle='VMC Cipher_B_Divergences', overlay=false)

// ----- PARAMETRES -----

// WaveTrend Settings
wtShow = input.bool(true, title='Show WaveTrend', group='WaveTrend Settings')
wtBuyShow = input.bool(true, title='Show Buy dots', group='WaveTrend Settings')
wtGoldShow = input.bool(true, title='Show Gold dots', group='WaveTrend Settings')
wtSellShow = input.bool(true, title='Show Sell dots', group='WaveTrend Settings')
wtDivShow = input.bool(true, title='Show Div. dots', group='WaveTrend Settings')
vwapShow = input.bool(true, title='Show Fast WT', group='WaveTrend Settings')
wtChannelLen = input.int(9, title='WT Channel Length', minval=1, group='WaveTrend Settings')
wtAverageLen = input.int(12, title='WT Average Length', minval=1, group='WaveTrend Settings')
wtMASource = input.source(hlc3, title='WT MA Source', group='WaveTrend Settings')
wtMALen = input.int(3, title='WT MA Length', minval=1, group='WaveTrend Settings')

// WaveTrend Overbought & Oversold Levels
obLevel = input.int(53, title='WT Overbought Level 1', group='WaveTrend Settings')
obLevel2 = input.int(60, title='WT Overbought Level 2', group='WaveTrend Settings')
obLevel3 = input.int(100, title='WT Overbought Level 3', group='WaveTrend Settings')
osLevel = input.int(-53, title='WT Oversold Level 1', group='WaveTrend Settings')
osLevel2 = input.int(-60, title='WT Oversold Level 2', group='WaveTrend Settings')
osLevel3 = input.int(-75, title='WT Oversold Level 3', group='WaveTrend Settings')

// WaveTrend Divergence Settings
wtShowDiv = input.bool(true, title='Show WT Regular Divergences', group='WaveTrend Settings')
wtShowHiddenDiv = input.bool(false, title='Show WT Hidden Divergences', group='WaveTrend Settings')
showHiddenDiv_nl = input.bool(true, title='Not apply OB/OS Limits on Hidden Divergences', group='WaveTrend Settings')
wtDivOBLevel = input.int(45, title='WT Bearish Divergence min', group='WaveTrend Settings')
wtDivOSLevel = input.int(-65, title='WT Bullish Divergence min', group='WaveTrend Settings')

// Divergence Extra Range Settings
wtDivOBLevel_addshow = input.bool(true, title='Show 2nd WT Regular Divergences', group='WaveTrend Settings')
wtDivOBLevel_add = input.int(15, title='WT 2nd Bearish Divergence', group='WaveTrend Settings')
wtDivOSLevel_add = input.int(-40, title='WT 2nd Bullish Divergence 15 min', group='WaveTrend Settings')

// RSI + MFI Settings
rsiMFIShow = input.bool(true, title='Show MFI', group='MFI Settings')
rsiMFIperiod = input.int(60, title='MFI Period', minval=1, group='MFI Settings')
rsiMFIMultiplier = input.float(150, title='MFI Area multiplier', minval=0.0, group='MFI Settings')
rsiMFIPosY = input.float(2.5, title='MFI Area Y Pos', group='MFI Settings')

// RSI Settings
rsiShow = input.bool(true, title='Show RSI', group='RSI Settings')
rsiSRC = input.source(close, title='RSI Source', group='RSI Settings')
rsiLen = input.int(14, title='RSI Length', minval=1, group='RSI Settings')
rsiOversold = input.int(30, title='RSI Oversold', minval=0, maxval=100, group='RSI Settings')
rsiOverbought = input.int(60, title='RSI Overbought', minval=0, maxval=100, group='RSI Settings')

// RSI Divergence Settings
rsiShowDiv = input.bool(false, title='Show RSI Regular Divergences', group='RSI Settings')
rsiShowHiddenDiv = input.bool(false, title='Show RSI Hidden Divergences', group='RSI Settings')
rsiDivOBLevel = input.int(60, title='RSI Bearish Divergence min', group='RSI Settings')
rsiDivOSLevel = input.int(30, title='RSI Bullish Divergence min', group='RSI Settings')

// Stochastic RSI Settings
stochShow = input.bool(true, title='Show Stochastic RSI', group='Stoch Settings')
stochUseLog = input.bool(true, title='Use Log?', group='Stoch Settings')
stochAvg = input.bool(false, title='Use Average of both K & D', group='Stoch Settings')
stochSRC = input.source(close, title='Stochastic RSI Source', group='Stoch Settings')
stochLen = input.int(14, title='Stochastic RSI Length', minval=1, group='Stoch Settings')
stochRsiLen = input.int(14, title='RSI Length', minval=1, group='Stoch Settings')
stochKSmooth = input.int(3, title='Stochastic RSI K Smooth', minval=1, group='Stoch Settings')
stochDSmooth = input.int(3, title='Stochastic RSI D Smooth', minval=1, group='Stoch Settings')

// Stochastic Divergence Settings
stochShowDiv = input.bool(false, title='Show Stoch Regular Divergences', group='Stoch Settings')
stochShowHiddenDiv = input.bool(false, title='Show Stoch Hidden Divergences', group='Stoch Settings')

// Schaff Trend Cycle Settings
tcLine = input.bool(false, title='Show Schaff TC line', group='Schaff Settings')
tcSRC = input.source(close, title='Schaff TC Source', group='Schaff Settings')
tclength = input.int(10, title='Schaff TC', minval=1, group='Schaff Settings')
tcfastLength = input.int(23, title='Schaff TC Fast Length', minval=1, group='Schaff Settings')
tcslowLength = input.int(50, title='Schaff TC Slow Length', minval=1, group='Schaff Settings')
tcfactor = input.float(0.5, title='Schaff TC Factor', minval=0.0, group='Schaff Settings')

// Sommi Flag Settings
sommiFlagShow = input.bool(false, title='Show Sommi flag', group='Sommi Settings')
sommiShowVwap = input.bool(false, title='Show Sommi F. Wave', group='Sommi Settings')
sommiVwapTF = input.string('720', title='Sommi F. Wave timeframe', group='Sommi Settings')
sommiVwapBearLevel = input.int(0, title='F. Wave Bear Level (less than)', group='Sommi Settings')
sommiVwapBullLevel = input.int(0, title='F. Wave Bull Level (more than)', group='Sommi Settings')
soomiFlagWTBearLevel = input.int(0, title='WT Bear Level (more than)', group='Sommi Settings')
soomiFlagWTBullLevel = input.int(0, title='WT Bull Level (less than)', group='Sommi Settings')
soomiRSIMFIBearLevel = input.int(0, title='Money flow Bear Level (less than)', group='Sommi Settings')
soomiRSIMFIBullLevel = input.int(0, title='Money flow Bull Level (more than)', group='Sommi Settings')

// Sommi Diamond Settings
sommiDiamondShow = input.bool(false, title='Show Sommi diamond', group='Sommi Settings')
sommiHTCRes = input.string('60', title='HTF Candle Res. 1', group='Sommi Settings')
sommiHTCRes2 = input.string('240', title='HTF Candle Res. 2', group='Sommi Settings')
soomiDiamondWTBearLevel = input.int(0, title='WT Bear Level (More than)', group='Sommi Settings')
soomiDiamondWTBullLevel = input.int(0, title='WT Bull Level (Less than)', group='Sommi Settings')

// MACD Colors Settings
macdWTColorsShow = input.bool(false, title='Show MACD Colors', group='MACD Settings')
macdWTColorsTF = input.string('240', title='MACD Colors MACD TF', group='MACD Settings')

// Mode Settings
darkMode = input.bool(false, title='Dark mode', group='Mode Settings')

// Colors
colorRed = color.new(color.red, 0)
colorPurple = color.new("#e600e6", 0)
colorGreen = color.new("#3fff00", 0)
colorOrange = color.new("#e2a400", 0)
colorYellow = color.new("#ffe500", 0)
colorWhite = color.new(color.white, 0)
colorPink = color.new("#ff00f0", 0)
colorBluelight = color.new("#31c0ff", 0)
colorWT2 = color.new("#0d47a1", 0)
colorWT2_ = color.new("#131722", 0)
colormacdWT1a = color.new("#4caf58", 0)
colormacdWT1b = color.new("#af4c4c", 0)
colormacdWT1c = color.new("#7ee57e", 0)
colormacdWT1d = color.new("#ff3535", 0)
colormacdWT2a = color.new("#305630", 0)
colormacdWT2b = color.new("#310101", 0)
colormacdWT2c = color.new("#132213", 0)
colormacdWT2d = color.new("#770000", 0)

// ----- FUNCTIONS -----

// Divergences
f_top_fractal(src) =>
    src[4] < src[2] and src[3] < src[2] and src[2] > src[1] and src[2] > src[0]

f_bot_fractal(src) =>
    src[4] > src[2] and src[3] > src[2] and src[2] < src[1] and src[2] < src[0]

f_fractalize(src) =>
    f_top_fractal(src) ? 1 : f_bot_fractal(src) ? -1 : 0

f_findDivs(src, topLimit, botLimit, useLimits) =>
    fractalTop = f_fractalize(src) > 0 and (useLimits ? src[2] >= topLimit : true) ? src[2] : na
    fractalBot = f_fractalize(src) < 0 and (useLimits ? src[2] <= botLimit : true) ? src[2] : na
    highPrev = ta.valuewhen(fractalTop, src[2], 0)[2]
    highPrice = ta.valuewhen(fractalTop, high[2], 0)[2]
    lowPrev = ta.valuewhen(fractalBot, src[2], 0)[2]
    lowPrice = ta.valuewhen(fractalBot, low[2], 0)[2]
    bearSignal = fractalTop and high[2] > highPrice and src[2] < highPrev
    bullSignal = fractalBot and low[2] < lowPrice and src[2] > lowPrev
    bearDivHidden = fractalTop and high[2] < highPrice and src[2] > highPrev
    bullDivHidden = fractalBot and low[2] > lowPrice and src[2] < lowPrev
    [fractalTop, fractalBot, lowPrev, bearSignal, bullSignal, bearDivHidden, bullDivHidden]

// RSI + MFI
f_rsimfi(_period, _multiplier, _tf) =>
    request.security(syminfo.tickerid, _tf, ta.sma(((close - open) / (high - low)) * _multiplier, _period)) - rsiMFIPosY

// WaveTrend
f_wavetrend(src, chlen, avg, malen, tf) =>
    tfsrc = request.security(syminfo.tickerid, tf, src)
    esa = ta.ema(tfsrc, chlen)
    de = ta.ema(math.abs(tfsrc - esa), chlen)
    ci = (tfsrc - esa) / (0.015 * de)
    wt1 = request.security(syminfo.tickerid, tf, ta.ema(ci, avg))
    wt2 = request.security(syminfo.tickerid, tf, ta.sma(wt1, malen))
    wtVwap = wt1 - wt2
    wtOversold = wt2 <= osLevel
    wtOverbought = wt2 >= obLevel
    wtCross = ta.cross(wt1, wt2)
    wtCrossUp = (wt2 - wt1) <= 0
    wtCrossDown = (wt2 - wt1) >= 0
    wtCrosslast = ta.cross(wt1[2], wt2[2])
    wtCrossUplast = (wt2[2] - wt1[2]) <= 0
    wtCrossDownlast = (wt2[2] - wt1[2]) >= 0
    [wt1, wt2, wtOversold, wtOverbought, wtCross, wtCrossUp, wtCrossDown, wtCrosslast, wtCrossUplast, wtCrossDownlast, wtVwap]

// Schaff Trend Cycle
f_tc(src, length, fastLength, slowLength) =>
    ema1 = ta.ema(src, fastLength)
    ema2 = ta.ema(src, slowLength)
    macdVal = ema1 - ema2
    alpha = ta.lowest(macdVal, length)
    beta = ta.highest(macdVal, length) - alpha
    gamma = (macdVal - alpha) / beta * 100
    gamma := beta > 0 ? gamma : nz(gamma[1])
    delta = gamma
    delta := nz(delta[1]) + (gamma - nz(delta[1])) * tcfactor
    epsilon = ta.lowest(delta, length)
    zeta = ta.highest(delta, length) - epsilon
    eta = (delta - epsilon) / zeta * 100
    eta := zeta > 0 ? eta : nz(eta[1])
    stcReturn = eta
    stcReturn := nz(stcReturn[1]) + (eta - nz(stcReturn[1])) * tcfactor
    stcReturn

// Stochastic RSI
f_stochrsi(_src, _stochlen, _rsilen, _smoothk, _smoothd, _log, _avg) =>
    src = _log ? math.log(_src) : _src
    rsi_val = ta.rsi(src, _rsilen)
    kk = ta.sma(ta.stoch(rsi_val, rsi_val, rsi_val, _stochlen), _smoothk)
    d1 = ta.sma(kk, _smoothd)
    avg_1 = (kk + d1) / 2
    k = _avg ? avg_1 : kk
    [k, d1]

// MACD
f_macd(src, fastlen, slowlen, sigsmooth, tf) =>
    fast_ma = request.security(syminfo.tickerid, tf, ta.ema(src, fastlen))
    slow_ma = request.security(syminfo.tickerid, tf, ta.ema(src, slowlen))
    macd = fast_ma - slow_ma
    signal = request.security(syminfo.tickerid, tf, ta.sma(macd, sigsmooth))
    hist = macd - signal
    [macd, signal, hist]

// MACD Colors on WT
f_macdWTColors(tf) =>
    hrsimfi = f_rsimfi(rsiMFIperiod, rsiMFIMultiplier, tf)
    [macd, signal, hist] = f_macd(close, 28, 42, 9, tf)
    macdup = macd >= signal
    macddown = macd <= signal
    macdWT1Color = macdup ? (hrsimfi > 0 ? colormacdWT1c : colormacdWT1a) : (macddown ? (hrsimfi < 0 ? colormacdWT1d : colormacdWT1b) : na)
    macdWT2Color = macdup ? (hrsimfi < 0 ? colormacdWT2c : colormacdWT2a) : (macddown ? (hrsimfi < 0 ? colormacdWT2d : colormacdWT2b) : na)
    [macdWT1Color, macdWT2Color]

// Get higher timeframe candle
f_getTFCandle(_tf) =>
    _open  = request.security(syminfo.tickerid, _tf, ta.hlc3, lookahead=barmerge.lookahead_on)
    _close = request.security(syminfo.tickerid, _tf, close, lookahead=barmerge.lookahead_on)
    _high  = request.security(syminfo.tickerid, _tf, high, lookahead=barmerge.lookahead_on)
    _low   = request.security(syminfo.tickerid, _tf, low, lookahead=barmerge.lookahead_on)
    hl2    = (_high + _low) / 2.0
    newBar = ta.change(_open)
    candleBodyDir = _close > _open
    [candleBodyDir, newBar]

// Sommi flag
f_findSommiFlag(tf, wt1, wt2, rsimfi, wtCross, wtCrossUp, wtCrossDown) =>
    [hwt1, hwt2, hwtOversold, hwtOverbought, hwtCross, hwtCrossUp, hwtCrossDown, hwtCrosslast, hwtCrossUplast, hwtCrossDownlast, hwtVwap] = f_wavetrend(wtMASource, wtChannelLen, wtAverageLen, wtMALen, tf)
    
    bearPattern = (rsimfi < soomiRSIMFIBearLevel) and
                  (wt2 > soomiFlagWTBearLevel) and 
                  wtCross and 
                  wtCrossDown and 
                  (hwtVwap < sommiVwapBearLevel)
                   
    bullPattern = (rsimfi > soomiRSIMFIBullLevel) and 
                  (wt2 < soomiFlagWTBullLevel) and 
                  wtCross and 
                  wtCrossUp and 
                  (hwtVwap > sommiVwapBullLevel)
    
    [bearPattern, bullPattern, hwtVwap]

// Sommi diamond
f_findSommiDiamond(tf, tf2, wt1, wt2, wtCross, wtCrossUp, wtCrossDown) =>
    [candleBodyDir, newBar] = f_getTFCandle(tf)
    [candleBodyDir2, newBar2] = f_getTFCandle(tf2)
    bearPattern = (wt2 >= soomiDiamondWTBearLevel) and
                  wtCross and
                  wtCrossDown and
                  not candleBodyDir and
                  not candleBodyDir2
    bullPattern = (wt2 <= soomiDiamondWTBullLevel) and
                  wtCross and
                  wtCrossUp and
                  candleBodyDir and
                  candleBodyDir2
    [bearPattern, bullPattern]    bu koddaki  hataları düzelt.