COG SSMACD COL combo with ADX Filter [orion35]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// by KIVANC using EHLERS' SUPER SMOOTHER FILTER, by alexgrover using Center Of Linearity & by KIVANC using Center of Gravity Oscillator
// Author : orion35

//@version=5
indicator('COG SSMACD COL combo with ADX Filter', 'COG, COL & SSMACD [orion35]', explicit_plot_zorder=true, max_labels_count=500)
brightBlue = #004EFF // double diamond condition down color
tip1 = 'If the COG, SSMACD signals are provided at the same time and the COL line supports it, a triple condition (▲ ▼) occurs.'
tip2 = 'The number of bars required to calculate the color intensity of the COL cloud.
 \n\nNote: Default value is 500 but Fibonacci values such as 34, 55, 89, 144, 233, 377, 610 can be used.'
tip3 = 'COG : Center of Gravity
 \nCOL : Center of Linearity
 \nSSMACD : Super Smoothed MACD
 \nH line : SSMACD Horizontal Line'

tip4 = '"Thold" is short for "threshold".
 \n\nThe minimum threshold value for ADX is left at 20. When ADX filtering is enabled, signals are not displayed in ADX cases smaller than this threshold value.'

tip5 = '1st MA=3 and 2nd MA=5 are the orjinal length values by KIVANÇ ÖZBİLGİÇ.'

tip6 = 'Filtering Methods:
 \n\nNormal: If the price is below the MavilimW line, "bull" signals are filtered out, and above "bear" signals are filtered out.
 \n\nReverse : Applies the opposite of the normal method.'

tip7 = 'Filtering by :
 \n\nclose : If the bar close is below the VMA line (line source = close), "bull" signals are filtered out, and above "bear" signals are filtered out.
 \n\nCOG : If the COG line is below the VMA line (line source = COG), "bull" signals are filtered out, and above "bear" signals are filtered out.
 \n\nSSMACD : If the SSMACD line is below the VMA line (line source = SSMACD), "bull" signals are filtered out, and above "bear" signals are filtered out.
 \n\nCOG & SSMACD : If the COG and SSMACD lines are below the VMA lines (line sources = COG and SSMACD), "bull" signals are filtered out, and above "bear" signals are filtered out.'


showCOG = input.bool(true, 'COG', inline='1', group='SHOW / HIDE SETTINGS')
showSSMACD = input.bool(false, 'SSMACD', inline='1', group='SHOW / HIDE SETTINGS')
showCOL = input.bool(false, 'COL Area', inline='1', group='SHOW / HIDE SETTINGS')
showCOLline = input.bool(false, 'COL line', inline='1', group='SHOW / HIDE SETTINGS')
showhline = input.bool(false, 'H line', inline='1', group='SHOW / HIDE SETTINGS', tooltip=tip3)

showVMA = input.bool(true, 'Plot VMA Line', inline='1', group='Variable Moving Average, VMA')
calibrateCOG = input.float(0., 'Tune for COG', step=0.0001, inline='2', group='Variable Moving Average, VMA')
lenVMACOG = input.int(6, 'Len', minval=1, step=1, inline='2', group='Variable Moving Average, VMA')
vmaWidthCOG = input.int(2, 'Width', step=1, inline='2', group='Variable Moving Average, VMA')
calibrateSSMACD = input.float(0., 'Tune for SSMACD', step=0.0001, inline='33', group='Variable Moving Average, VMA')
lenVMASSMACD = input.int(6, 'Len', minval=1, step=1, inline='33', group='Variable Moving Average, VMA')
vmaWidthSSMACD = input.int(2, 'Width', step=1, inline='33', group='Variable Moving Average, VMA')


condVMAcol = input.bool(true, 'Conditional Color', inline='3', group='Variable Moving Average, VMA')
col1 = input.color(color.new(color.lime, 0), '', inline='3', group='Variable Moving Average, VMA')
col12 = input.color(color.new(#FF0000, 0), '', inline='3', group='Variable Moving Average, VMA')
col13 = input.color(color.new(color.gray, 0), '', inline='3', group='Variable Moving Average, VMA')

applyVMA_filter = input.bool(true, 'Apply VMA Filter', inline='4', group='Variable Moving Average, VMA')
source = input.string('COG', 'Filter Method', ['close', 'COG', 'SSMACD', 'COG & SSMACD'], inline='5', group='Variable Moving Average, VMA', tooltip=tip7)


applyADX = input.bool(false, 'Filter by ADX', inline='1', group='ADX SETTINGS')
adxlen = input.int(14, title="Smooth", inline='8', group='ADX SETTINGS')
dilen = input.int(14, title="DI Len", inline='8', group='ADX SETTINGS')
threshold = input.float(14.5, minval=0., maxval=80., step=0.5, title="Thold.", inline='8', group='ADX SETTINGS', tooltip=tip4)

applyMAVW = input.bool(false, 'Filter by Mavilim W', inline='81', group='Mavilim W SETTINGS')
filterType = input.string('Normal', title='Filter Method', options=['Normal', 'Reverse'], inline='812', group='Mavilim W SETTINGS', tooltip=tip6)
fmal=input.int(3,"1st MA len", minval=1, inline='82', group='Mavilim W SETTINGS')
smal=input.int(5,"2nd MA len", minval=1, inline='82', group='Mavilim W SETTINGS', tooltip=tip5)

lwCOG = input.int(1, 'COG Line Width', [1,2,3], inline='4', group='LINE WIDTH SETTINGS')
colCOG1 = input.color(color.new(brightBlue, 0), title='', inline='4', group='LINE WIDTH SETTINGS')
colCOG2 = input.color(color.new(color.white, 0), title='', inline='4', group='LINE WIDTH SETTINGS')

lwSSMACD = input.int(1, 'SSMACD Line Width', [1,2,3], inline='42', group='LINE WIDTH SETTINGS')
colSSMACD1 = input.color(color.new(color.blue, 0), title='', inline='42', group='LINE WIDTH SETTINGS')
colSSMACD2 = input.color(color.new(color.red, 0), title='', inline='42', group='LINE WIDTH SETTINGS')
colLw = input.int(9, 'COL Line Width', [5,6,7,8,9,10], inline='43', group='LINE WIDTH SETTINGS')
offsetCOL = input.float(5.49, minval=5., maxval=6., step=0.005, title="COL level.", inline='43', group='LINE WIDTH SETTINGS')
colCOL1 = input.color(#2196f3, title='', inline='43', group='LINE WIDTH SETTINGS')
colCOL2 = input.color(#f57f17, title='', inline='43', group='LINE WIDTH SETTINGS')

lengthCOG = input.int(10, 'COG Len', minval=1, inline='5', group='Length SETTINGS')
lengthCOL = input.int(14, 'COL Len', minval=1, inline='5', group='Length SETTINGS')
setCOL_color_len = input.int(377, 'COL Color Length', minval=2, inline='6', group='Length SETTINGS', tooltip=tip2) 


len = input.int(8, minval=1, title='SSMACD Len 1', inline='7', group='Length SETTINGS')
len2 = input.int(13, minval=1, title='Len 2', inline='7', group='Length SETTINGS')
len3 = input.int(3, minval=1, title='Len 3', inline='7', group='Length SETTINGS')

single = input.bool(true, 'Single Signals, (x ●)', inline='3', group='SIGNAL SETTINGS')
singleCol1 = input.color(color.new(color.white, 50), title='Bull', inline='31', group='SIGNAL SETTINGS')
singleCol2 = input.color(color.new(color.white, 50), title='Bear', inline='31', group='SIGNAL SETTINGS')

double = input.bool(true, 'Double Signals, (x ●)', inline='32', group='SIGNAL SETTINGS')
doubleCol1 = input.color(color.new(color.yellow, 0), title='Bull', inline='33', group='SIGNAL SETTINGS')
doubleCol2 = input.color(color.new(color.yellow, 0), title='Bear', inline='33', group='SIGNAL SETTINGS')

triple = input.bool(true, 'Triple Signals, (▲ ▼)', inline='34', group='SIGNAL SETTINGS', tooltip=tip1)
tripleCol1 = input.color(color.new(brightBlue, 0), title='Bull', inline='35', group='SIGNAL SETTINGS')
tripleCol2 = input.color(color.new(brightBlue, 0), title='Bear', inline='35', group='SIGNAL SETTINGS')



//-------------------------------- VMA Filter ----------------------------

variant_variablema(src, len) =>
    k = 1.0 / len
    pdm = math.max((src - src[1]), 0)
    mdm = math.max((src[1] - src), 0)
    pdmS = 0.
    mdmS = 0.
    pdiS = 0.
    mdiS = 0.
    iS = 0.
    vma = 0.
    pdmS := ((1 - k)*nz(pdmS[1]) + k*pdm)
    mdmS := ((1 - k)*nz(mdmS[1]) + k*mdm)
    s = pdmS + mdmS
    pdi = pdmS / s
    mdi = mdmS / s
    pdiS := ((1 - k)*nz(pdiS[1]) + k*pdi)
    mdiS := ((1 - k)*nz(mdiS[1]) + k*mdi)
    d = math.abs(pdiS - mdiS)
    s1 = pdiS + mdiS
    iS := ((1 - k)*nz(iS[1]) + k*d / s1)
    hhv = ta.highest(iS, len) 
    llv = ta.lowest(iS, len) 
    d1 = hhv - llv
    vI = (iS - llv) / d1
    vma := (1 - k*vI)*nz(vma[1]) + k*vI*src
    vma


//------------ from COL ----------------------------------------------------------

//----
aCOL = 0.
for i = 1 to lengthCOL by 1
    aCOL += i * (close[lengthCOL - i] - close[i - 1])
    aCOL
//----
plot(showCOL ? aCOL : na, 'COL', aCOL > 0 ? color.new(#2196f3, 0) : color.new(#f57f17, 0), style=plot.style_area)



//-------- Calculate COL Color Intensity ------------------------------------------------------------------------------

thickness = aCOL > 0 ? aCOL : math.abs(aCOL)

normalized_data = (thickness - ta.lowest(thickness, setCOL_color_len)) / (ta.highest(thickness, setCOL_color_len) - ta.lowest(thickness, setCOL_color_len))
scaled_data = normalized_data * (100 - 0) + 0

plot(showCOLline and not (showSSMACD or showCOL) ? offsetCOL : showCOLline and showSSMACD and not showCOL ? 0. : na, 'COL Line', color=aCOL > 0 ? color.new(colCOL1, 90 - scaled_data) : color.new(colCOL2, 90 - scaled_data), style=plot.style_linebr, linewidth=colLw)


//------------ from SSMACD ----------------------------------------------------------

p = close
f = 1.414 * 3.14159 / len
a = math.exp(-f)
c2 = 2 * a * math.cos(f)
c3 = -a * a
c1 = 1 - c2 - c3
ssmooth = 0.0
ssmooth := c1 * (p + p[1]) * 0.5 + c2 * nz(ssmooth[1]) + c3 * nz(ssmooth[2])

f2 = 1.414 * 3.14159 / len2
a2 = math.exp(-f2)
c22 = 2 * a2 * math.cos(f2)
c32 = -a2 * a2
c12 = 1 - c22 - c32
ssmooth2 = 0.0
ssmooth2 := c12 * (p + p[1]) * 0.5 + c22 * nz(ssmooth2[1]) + c32 * nz(ssmooth2[2])

// macd = (ssmooth - ssmooth2)*10000000
macd = ssmooth - ssmooth2

f3 = 1.414 * 3.14159 / len3
a3 = math.exp(-f3)
c23 = 2 * a3 * math.cos(f3)
c33 = -a3 * a3
c13 = 1 - c23 - c33
ssmooth3 = 0.0
ssmooth3 := c13 * (macd + macd[1]) * 0.5 + c23 * nz(ssmooth3[1]) + c33 * nz(ssmooth3[2])

// hline(50, linestyle=hline.style_dotted, title="Upper Threshold" )
hline(showSSMACD and showhline ? 0 : na, title='Center Line')
// hline(-50, linestyle=hline.style_dotted, title="Lower Threshold")

plot(showSSMACD ? macd : na, color=colSSMACD1, linewidth=lwSSMACD, title='MACD Line')
plot(showSSMACD ? ssmooth3 : na, color=colSSMACD2, linewidth=lwSSMACD, title='Super Smoothed Line')


bullSSMACD_sig = ta.crossover(macd, ssmooth3)
bearSSMACD_sig = ta.crossunder(macd, ssmooth3)


//------------ from COG ----------------------------------------------------------

num = ta.wma(close, lengthCOG) * lengthCOG * (lengthCOG + 1) / 2
den = math.sum(close, lengthCOG)
CG = num / den
plot(showCOG and not showSSMACD ? CG : na, 'COG Trigger', color=colCOG1, linewidth=lwCOG)
plot(showCOG and not showSSMACD ? CG[1] : na, 'COG Line', color=colCOG2, linewidth=lwCOG)

cgBear = ta.crossover(CG[1], CG)
cgBull = ta.crossunder(CG[1], CG)

//----------------------------- Variable Moving Average, VMA ----------------------------------------------------------------------------------------------------------------

vma_for_COG = variant_variablema(CG, lenVMACOG)*(1 + calibrateCOG)
vma_for_SSMACD = variant_variablema(macd, lenVMASSMACD)*(1 + calibrateSSMACD*1000)
vma_for_close = variant_variablema(close, lenVMACOG)*(1 + calibrateCOG)

vma_for_COG_col = vma_for_COG[1] < vma_for_COG ? col1 : vma_for_COG[1] > vma_for_COG and condVMAcol ? col12 : vma_for_COG[1] == vma_for_COG and condVMAcol ? col13 : col1
vma_for_SSMACD_col = vma_for_SSMACD[1] < vma_for_SSMACD ? col1 : vma_for_SSMACD[1] > vma_for_SSMACD  and condVMAcol ? col12 : vma_for_SSMACD[1] == vma_for_SSMACD and condVMAcol ? col13 : col1

vma = showCOG and not showSSMACD and not showCOL ? vma_for_COG : showSSMACD ? vma_for_SSMACD : na
vma_col = showCOG and not showSSMACD and not showCOL ? vma_for_COG_col : showSSMACD ? vma_for_SSMACD_col : na

plot(showVMA ? vma : na, 'VMA', color=vma_col, linewidth=showCOG and not showSSMACD ? vmaWidthCOG : vmaWidthSSMACD)


vmaBullFilter = (vma_for_close < close and source == 'close' ? true : false) or (vma_for_COG < CG and source == 'COG' ? true : false) or (vma_for_SSMACD < macd and source == 'SSMACD' ? true : false)
 or (vma_for_COG < CG and vma_for_SSMACD < macd and source == 'COG & SSMACD' ? true : false)
vmaBearFilter = (vma_for_close > close and source == 'close' ? true : false) or (vma_for_COG > CG and source == 'COG' ? true : false) or (vma_for_SSMACD > macd and source == 'SSMACD' ? true : false)
 or (vma_for_COG > CG and vma_for_SSMACD > macd and source == 'COG & SSMACD' ? true : false)
 
//-------------- ADX Calculations -------------------------------------------------------------------------------------------------------------------------------------------

dirmov(len) =>
	up = ta.change(high)
	down = -ta.change(low)
	plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
	minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
	truerange = ta.rma(ta.tr, len)
	plus = fixnan(100 * ta.rma(plusDM, len) / truerange)
	minus = fixnan(100 * ta.rma(minusDM, len) / truerange)
	[plus, minus]
adx(dilen, adxlen) =>
	[plus, minus] = dirmov(dilen)
	sum = plus + minus
	adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
sig = adx(dilen, adxlen)


//-------------------------------- Mavilim W by Kıvanç Özbilgiç ----------------------------

tmal=fmal+smal
Fmal=smal+tmal
Ftmal=tmal+Fmal
Smal=Fmal+Ftmal

M1 = ta.wma(close, fmal)
M2 = ta.wma(M1, smal)
M3 = ta.wma(M2, tmal)
M4 = ta.wma(M3, Fmal)
M5 = ta.wma(M4, Ftmal)
MAVW = ta.wma(M5, Smal)

bearFilter = filterType == 'Normal' ? (close <= MAVW ? true : false) : (close <= MAVW ? false : true)
bullFilter = filterType == 'Normal' ? (close <= MAVW ? false : true) : (close <= MAVW ? true : false)


//-------------- Combined Conditions & Plots --------------------------


doubleBull = cgBull and bullSSMACD_sig and (applyADX ? (sig >= threshold) : true) and (applyMAVW ? bullFilter : true) and (applyVMA_filter ? vmaBullFilter : true)
doubleBear = cgBear and bearSSMACD_sig and (applyADX ? (sig >= threshold) : true) and (applyMAVW ? bearFilter : true) and (applyVMA_filter ? vmaBearFilter : true)

tripleBull = doubleBull and (aCOL <= 0) and (applyADX ? (sig >= threshold) : true) and (applyMAVW ? bullFilter : true) and (applyVMA_filter ? vmaBullFilter : true)
tripleBear = doubleBear and (aCOL > 0) and (applyADX ? (sig >= threshold) : true) and (applyMAVW ? bearFilter : true) and (applyVMA_filter ? vmaBearFilter : true)

singleCgBull = cgBull and not (tripleBull or doubleBull) and (applyADX ? (sig >= threshold) : true) and (applyMAVW ? bullFilter : true) and (applyVMA_filter ? vmaBullFilter : true)
singleCgBear = cgBear and not (tripleBear or doubleBear) and (applyADX ? (sig >= threshold) : true) and (applyMAVW ? bearFilter : true) and (applyVMA_filter ? vmaBearFilter : true)

singleSSMACDBull = bullSSMACD_sig and not (tripleBull or doubleBull) and (applyADX ? (sig >= threshold) : true) and (applyMAVW ? bullFilter : true) and (applyVMA_filter ? vmaBullFilter : true)
singleSSMACDBear = bearSSMACD_sig and not (tripleBear or doubleBear) and (applyADX ? (sig >= threshold) : true) and (applyMAVW ? bearFilter : true) and (applyVMA_filter ? vmaBearFilter : true)

colorBull = tripleBull ? tripleCol1 : doubleBull ? doubleCol1 : singleCgBull or singleSSMACDBull ? singleCol1 : na
colorBear = tripleBear ? tripleCol2 : doubleBear ? doubleCol2 : singleCgBear or singleSSMACDBear ? singleCol2 : na

plotshape(singleSSMACDBull and single, 'Single Bull SSMACD Signal', shape.xcross, location.bottom, colorBull, size=size.auto)
plotshape(singleSSMACDBear and single, 'Single Bear SSMACD Signal', shape.xcross, location.top, colorBear, size=size.auto)

plotshape(singleCgBull and single, 'Single Bull COG Signal', shape.circle, location.bottom, colorBull, size=size.auto)
plotshape(singleCgBear and single, 'Single Bear COG Signal', shape.circle, location.top, colorBear, size=size.auto)

plotshape(doubleBull and not tripleBull and double, 'Double Bull Signal', shape.circle, location.bottom, colorBull, size=size.auto)
plotshape(doubleBear and not tripleBear and double, 'Double Bear Signal', shape.circle, location.top, colorBear, size=size.auto)

plotshape(tripleBull and triple, 'Double Bull Signal', shape.triangleup, location.bottom, colorBull, size=size.auto)
plotshape(tripleBear and triple, 'Double Bear Signal', shape.triangledown, location.top, colorBear, size=size.auto)


// lenVMA

//------------------- ALERT CONDITIONS ----------------------------------------------------------------------------------------

alertcondition(tripleBull, '01 Triple Bull, ▲', 'BULL Triple ▲ COG, COL & SSMACD, {{ticker}}, Price = {{close}}')
alertcondition(tripleBear, '02 Triple Bear, ▼', 'BEAR Triple ▼ COG, COL & SSMACD, {{ticker}}, Price = {{close}}')
alertcondition(tripleBear or tripleBull, '03 Triple (any), ▲ ▼', 'Any Triple ▲ ▼ COG, COL & SSMACD, {{ticker}}, Price = {{close}}')

alertcondition(doubleBull and not tripleBull, '04 Double Bull, ● (yellow)', 'BULL Double (yellow ●), COG & SSMACD, {{ticker}}, Price = {{close}}')
alertcondition(doubleBear and not tripleBear, '05 Double Bear, ● (yellow)', 'BEAR Double (yellow ●), COG & SSMACD, {{ticker}}, Price = {{close}}')
alertcondition((doubleBear and not tripleBear) or (doubleBull and not tripleBull), '06 Double (any), ● (yellow)', 'Any Double (yellow ●), COG & SSMACD, {{ticker}}, Price = {{close}}')
alertcondition(singleSSMACDBull, '07 Single SSMACD Bull, (white x)', 'BULL Single SSMACD (white x), {{ticker}}, Price = {{close}}')
alertcondition(singleSSMACDBear, '08 Single SSMACD Bear, (white x)', 'BEAR Single SSMACD (white x), {{ticker}}, Price = {{close}}')
alertcondition(singleCgBull, '09 Single COG Bull, (white ●)', 'BULL Single COG (white ●), {{ticker}}, Price = {{close}}')
alertcondition(singleCgBear, '10 Single COG Bear, (white ●)', 'BEAR Single COG (white ●), {{ticker}}, Price = {{close}}')

alertcondition(singleCgBull or singleSSMACDBull or doubleBull or tripleBull, '11 Any Bull Condition', 'BULL Any Condition, {{ticker}}, Price = {{close}}')
alertcondition(singleCgBear or singleSSMACDBear or doubleBear or tripleBear, '12 Any Bear Condition', 'BEAR Any Condition, {{ticker}}, Price = {{close}}')



