MM SIGMA STC+ADX
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © thealgorithmictrader

//@version=5
indicator("MM SIGMA STC+ADX")

var user_consensus = input.string(defval="", title="TYPE 'agree' TO ADD TO CHART. \nTrading involves a risk of loss, and may not be appropriate for every one. Please consider carefully if trading is appropriate for you. Past performance is not indicative of future results. Any and all indicator signals provided by 'MM SIGMA STC+ADX' are for educational purposes only. Is your responsibility knowing that by typing 'agree' you are accepting that the AI would trade on your behalf at your own risk. \nRELEASE INFORMATION 2021 © Money Moves Investments", confirm = true, group="consensus")
var icc = false
if user_consensus == "agree"
    icc := true
else
    icc := false

var label lbl_agree_err = na
if not icc 
    label.delete(lbl_agree_err)
    lbl_agree_err := label.new(bar_index + 5, close, color = color.red, style=label.style_label_left, textcolor = color.white, text="Type 'agree' first in order to show the algo!")


//MM SIGMA ALGO OSC 
//{
g_sigma = "MM SIGMA ALGO OSC Stricts"
stc_entry = input.bool(true, "Entries", group = g_sigma, inline = "gen0")
stc_exits = input.bool(true, "Exits <- STC", group = g_sigma, inline = "gen0")

dmi_entry = input.bool(false, "Entries", group = g_sigma, inline = "gen1")
dmi_exits = input.bool(false, "Exits <- DMI", group = g_sigma, inline = "gen1")

dmi_entry_lev = input.string("TOP", "DMI Levels", group = g_sigma, options=["TOP","BOTTOM"], inline = "gen2")
dmi_entry_lev1 = input.float(20.0, "1", group = g_sigma, inline = "gen2")
dmi_entry_lev2 = input.float(25.0, "2", group = g_sigma, inline = "gen2")
//}


//STC
//{

g_stc = "STC SETTINGS"
show_stc= input.bool(true, "Show STC", group = g_stc)
fastLength = input.int(title="STC: Fast Length", defval=23,inline="stc0", group = g_stc)
slowLength = input.int(title="Slow Length", defval=50,inline="stc0", group = g_stc)
cycleLength = input.int(title="Cycle Length", defval=10, group = g_stc)
d1Length = input.int(title="1st %D Length", defval=3, group = g_stc)
d2Length = input.int(title="2nd %D Length", defval=3, group = g_stc)
src = input.source(title="Source", defval=close, group = g_stc)
upper = input.int(title="Thresholds: Upper", defval=70, group = g_stc)
lower = input.int(title="Lower", defval=30, group = g_stc)
highlightBreakouts = input.bool(title="Highlight Breakouts ?", defval=true, group = g_stc)






macd = ta.ema(src, fastLength) - ta.ema(src, slowLength)
k = nz(fixnan(ta.stoch(macd, macd, macd, cycleLength)))
d = ta.ema(k, d1Length)
kd = nz(fixnan(ta.stoch(d, d, d, cycleLength)))
stc = ta.ema(kd, d2Length)
stc := math.max(math.min(stc, 100), 0)

stcColor1 = stc > stc[1] ? color.new(#7edbca,0) : color.new(#dd64db,0)
stcColor2 = stc > upper ? color.new(#7edbca,0) : stc <= lower ? color.new(#dd64db,0) : color.gray

stcColor = highlightBreakouts ? stcColor2 : stcColor1
stcPlot = plot(icc?stc:na, title="STC", color=stcColor, transp=0)

upperLevel = plot(upper, title="Upper", color=color.gray)
hline(50.0, title="Middle", linestyle=hline.style_dotted)
lowerLevel = plot(lower, title="Lower", color=color.gray)

fill(upperLevel, lowerLevel, title="Middle Zone", color=color.gray, transp=90)

upperFillColor = stc > upper and highlightBreakouts ? color.new(#7edbca,70) : na
lowerFillColor = stc < lower and highlightBreakouts ? color.new(#dd64db,70) : na

fill(upperLevel, stcPlot, color=upperFillColor, transp=80)
fill(lowerLevel, stcPlot, color=lowerFillColor, transp=80)

buySignal =      ta.crossover(stc, lower)
sellSignal =     ta.crossunder(stc, upper)
upperCrossover = ta.crossover(stc, upper)
upperCrossunder= ta.crossunder(stc, upper)
lowerCrossover = ta.crossover(stc, lower)
lowerCrossunder= ta.crossunder(stc, lower)


plotshape(icc and upperCrossunder ? upper : na, title="Sell Signal", location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(#dd64db,0), transp=0)
plotshape(icc and lowerCrossover ? lower : na, title="Buy Signal", location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(#7edbca,0), transp=0)



//}

//DMI+EMA
//{
g_dmi = "DMI + EMA SETTINGS"

show_dmi= input.bool(true, "Show DMI", group = g_dmi)

lensig = input.int(14, title="ADX Smoothing", minval=1, maxval=50, group=g_dmi)
len = input.int(14, minval=1, title="DI Length", group=g_dmi)
show_dmi_ema= input.bool(true, "Show DMI EMA", group = g_dmi)
__len = input.int(9, minval=1, title="DMI EMA Length", group=g_dmi)
offset = input.int(title="Offset", defval=0, minval=-500, maxval=500, group=g_dmi)

typeMA = input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group=g_dmi)


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)
trur = ta.rma(ta.tr, len)
plus = fixnan(100 * ta.rma(plusDM, len) / trur)
minus = fixnan(100 * ta.rma(minusDM, len) / trur)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)
plot(icc and show_dmi?adx:na, color=#F50057, title="ADX")
hline(dmi_entry_lev1,"DMI Level 1",color.new(#F50057,70),linestyle=hline.style_solid, linewidth=2)
hline(dmi_entry_lev2,"DMI Level 2",color.new(#F50057,70),linestyle=hline.style_solid, linewidth=2)
//plot(plus, color=#2962FF, title="+DI")
//plot(minus, color=#FF6D00, title="-DI")
    
    
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)


out = ma(adx, __len,typeMA)
plot(icc?out:na, title="DMI EMA", color=color.yellow, offset=offset)


lev_to_trigger = dmi_entry_lev=="TOP"? (dmi_entry_lev1>dmi_entry_lev2?dmi_entry_lev1:dmi_entry_lev2) : (dmi_entry_lev1>dmi_entry_lev2?dmi_entry_lev2:dmi_entry_lev1)

//}


//STRICTS DISPLAY
//{

var string entry = ""
var bool exit = false

if stc_entry and entry == ""
    entry := lowerCrossover?"LONG":upperCrossunder?"SHORT":""

    if dmi_entry and entry=="LONG"
        entry := adx>=lev_to_trigger?"LONG":""
        
    if dmi_entry and entry=="SHORT"
        entry := adx>=lev_to_trigger?"SHORT":""
        
if entry == "LONG" or entry == "SHORT"
    if stc_exits 
        exit := entry == "LONG"?upperCrossunder:entry == "SHORT"?lowerCrossover:false
    
    if dmi_exits 
        exit:=adx<=out
        
    if exit 
        entry:=""
        

plotshape(icc and lowerCrossover and entry=="LONG" and entry[1] == ""?lower : na , title = "LONG", style = shape.triangleup, location = location.bottom, text = "BUY", textcolor = color.white, color=color.new(#7edbca,0), size=size.small)
plotshape(icc and upperCrossunder and entry=="SHORT" and entry[1] == ""?upper : na , title = "SHORT", style = shape.triangledown, location = location.top, text = "SELL", textcolor = color.white, color=color.new(#dd64db,0), size=size.small)

plotshape(icc and exit and entry[1]=="LONG", title = "EXIT LONG", style = shape.triangledown, location = location.top, text = "EXIT LONG", textcolor = color.white, color=color.new(#7edbca,0), size=size.small)
plotshape(icc and exit and entry[1]=="SHORT", title = "EXIT SHORT", style = shape.triangleup, location = location.bottom, text = "EXIT SHORT", textcolor = color.white, color=color.new(#7edbca,0), size=size.small)

//}
