[EDU] Close Open Estimation Signals (COE Signals)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tarasenko_

//@version=5
indicator("[EDU] Close Open Estimation Signals (COE Signals)", shorttitle = "[EDU] COE Signals", overlay = 1)


// Lookback
src_inp = input.string("EMA-smoothed", "Source", group = "COE Settings", options= ["EMA-smoothed", "Classic"], tooltip = "EMA-smoothed: using EMA to smooth source data in order to 'cleaner' data.\n\nClassic: using classic sum of close-open formula.")
p = input(12, "Lookback", group = "COE Settings")
mp = input(26, "Smoother period", group = "COE Settings")
p1 = input(9, "EMA period", group = "COE Settings")


// Declarations & Calculations
zco(p, mp) =>
    z = 0.0
    ma = ta.ema(open, p1)
    for i = 1 to p
        z += src_inp == "EMA-smoothed" ? ma - ma[1] : close[i] - open[i]
    ta.ema(z, mp)

coe = zco(p, mp) 
v_coe = ta.ema(coe - coe[1], mp) // COE's volatility


// Signal logic
v_coe_entry = ta.ema(v_coe - v_coe[1], mp)
v_coe_rev = ta.ema(v_coe_entry - v_coe_entry[1], mp) // Behaviour os this one when crossing 0 is very similar to getting derivatives of higher ranks from a function in math.

b_entry = ta.crossover(v_coe_entry, 0)
s_entry = ta.crossunder(v_coe_entry, 0)
rev = ta.cross(v_coe_rev, 0)


// Plottings
plotshape(b_entry, style = shape.labelup, location = location.belowbar, size = size.tiny, text = "Buy", textcolor = color.white, color = #009B00)
plotshape(s_entry, style = shape.labeldown, size = size.tiny, text = "Sell", textcolor = color.white, color = #CC0000)