Cavuca Technical Analysis
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
///\\===============================================================================//\\\
//████████╗██████╗  █████╗ ██████╗ ██╗███╗   ██╗ ██████╗ ██╗   ██╗██╗███████╗██╗    ██╗\\
//╚══██╔══╝██╔══██╗██╔══██╗██╔══██╗██║████╗  ██║██╔════╝ ██║   ██║██║██╔════╝██║    ██║\\
//   ██║   ██████╔╝███████║██║  ██║██║██╔██╗ ██║██║  ███╗██║   ██║██║█████╗  ██║ █╗ ██║\\
//   ██║   ██╔══██╗██╔══██║██║  ██║██║██║╚██╗██║██║   ██║╚██╗ ██╔╝██║██╔══╝  ██║███╗██║\\
//   ██║   ██║  ██║██║  ██║██████╔╝██║██║ ╚████║╚██████╔╝ ╚████╔╝ ██║███████╗╚███╔███╔╝\\
//   ╚═╝   ╚═╝  ╚═╝╚═╝  ╚═╝╚═════╝ ╚═╝╚═╝  ╚═══╝ ╚═════╝   ╚═══╝  ╚═╝╚══════╝ ╚══╝╚══╝ \\
//\\==============================================================================//\\ \\
// ----------------------------------------------------------------------------------- \\
// CAVUCA INDICATOR TECHNICAL ANALYSIS: Developed by Cavuca-Trader                     \\                                  
// ----------------------------------------------------------------------------------- \\
// ................................................................................... \\
// Name                  : Cavuca Technical Analysis                                   \\                                                  
// Surname               : Cavuca                                                      \\                       
// Description           : Technical Analysis Setup                                    \\
// Info                  : Signals inputs and outputs through technical coloring       \\
// Creation Date         : 11/20/2021                                                  \\
// Version TradingView   : 4                                                           \\
// ................................................................................... \\
// v1.0 - release = TrandingView = Ok                                                  \\
//=====================================================================================\\
// © Cavuca-Trader
//@version=4
study(title="Cavuca Technical Analysis",shorttitle="Cavuca", overlay=true)

//Parameters for the zigzag
length = input(4, title = "High/Low length")
h = highest(high, length * 2 + 1)
l = lowest(low, length * 2 + 1)
f_isMin(len) => 
    l == low[len]
f_isMax(len) => 
    h == high[len]

//Parameters
MMR = input(9, minval=1, title="Fast Moving Average")
MMM = input(21, minval=1, title="Moving Average Average")
MML = input(200, minval=1, title="Slow Moving Average")
src = input(close, title="Source")
offset = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)

//Calculation
MM1 = sma(src, MMR)
MM2 = sma(src, MMM)
MM3 = sma(src, MML)

// Calculation for ZigZag
var dirUp = false
var lastLow = high * 100
var lastHigh = 0.0
var timeLow = bar_index
var timeHigh = bar_index
var line li = na
var topo = 0.0
var fundo = high * 100

f_drawLine() =>
    _li_color = dirUp ? color.teal : color.orange
    line.new(timeHigh - length, lastHigh, timeLow - length, lastLow, xloc.bar_index, color=_li_color, width=1)
    
if dirUp
    if (f_isMin(length) and low[length] < lastLow)
        lastLow := low[length]
        timeLow := bar_index
        line.delete(li)
        li := f_drawLine()

    if (f_isMax(length) and high[length] > lastLow)
        lastHigh := high[length]
        timeHigh := bar_index
        topo := lastHigh
        dirUp := false
        li := f_drawLine()

if not dirUp
    if (f_isMax(length) and high[length] > lastHigh)
        lastHigh := high[length]
        timeHigh := bar_index
        line.delete(li)
        li := f_drawLine()
        
    if f_isMin(length) and low[length] < lastHigh
        lastLow := low[length]
        timeLow := bar_index
        fundo := lastLow
        dirUp := true
        li := f_drawLine()
        if (f_isMax(length) and high[length] > lastLow)
            lastHigh := high[length]
            timeHigh := bar_index
            dirUp := false
            li := f_drawLine()

//Candle coloring rule
Dgbar = close>topo
Drbar = close<fundo
Dwbar = close<topo and close > fundo

//Color of candles
barcolor(Dgbar ? color.green : na)
barcolor(Drbar ? color.red : na)
barcolor(Dwbar ? color.white : na)

// Plot
plot(MM1, color=color.red, title="MMR", offset=offset, linewidth=1)
plot(MM2, color=color.blue, title="MMM", offset=offset, linewidth=1)
plot(MM3, color=color.white, title="MML", offset=offset, linewidth=1)
plot(fundo, color=color.green, title="Fundo", offset=-length, style=plot.style_cross, linewidth=1) 
plot(topo, color=color.red, title="Topo", offset=-length, style=plot.style_cross, linewidth=1)

