shadow bar
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ceshead
//This indicator shows a new candle bar formation looking back a period (n) of time. 
//In this way the trader can see how the traditional bars are part of a larger formation that can show a trend or a range. 
//The new shadow bars are drawn with a degree of transparency that makes it possible to distinguish traditional candlesticks; 
//and where shadow candles can be seen as support or resistance to traditional candlesticks. 
//When the traditional candlesticks are the same size as the shadow candlesticks, it can be expected that we are forming a compression or range that will result in a strong move. 
//In addition to the shadow bars, there is a ribbon created from the new candlestick data that is formed as a line from the linear regression function and another that is the previous line smoothed by its exponential moving average. 
//This ribbon allows you to see the trend more clearly and at the crossings of the lines that form the possible reversals or continuations of the trend. 
//The indicator has the timeframe field active, which allows the indicator to be displayed in other temporalities.
//@version=5

indicator("shadow bar","shadow bar",format =format.price,overlay=true, timeframe = "", precision = 5)

//inputs
n=input(60,"bars back", group="shadow bars")
tryforce=input(false,title = "Automatic selection of bars back", group="shadow bars")
mins=timeframe.multiplier

if(tryforce)
    if(timeframe.period == "D")
        n:=30
    else if(timeframe.period == "W")
        n:=52
    else if(timeframe.period == "M")
        n:=12
    else if(timeframe.multiplier<15)
        n:=n
    else if(timeframe.multiplier>=15 and timeframe.multiplier<1440)
        n:=10080/mins

//counter
var counter=0
if(counter==n)
    counter:=0
counter:=counter+1
//close counter

//Ombligo
ombligo=open>close?(open-close)/2+close:(close-open)/2+open

//inputs
formationTypeInput = input.string("f1", title="bar formation type", options=["f1", "f2", "f3", "f4"], group="shadow bars")
displayBars=input(true,"show shadow bars", group = "shadow bars")?display.all - display.status_line:display.none
displayWick=input(true,"show wicks", group = "shadow bars")
longColor=input.color(#089981,"green bar", group = "shadow bars")
shortColor=input.color(#f23645,"red bar", group = "shadow bars")
t=input(75,"Bars transparency", group="shadow bars")
oc2=input(false,"Show ribbon", group="shadow ribbon")
ep=input(60,"ribbon period", group="shadow ribbon")
greenColor=input.color(color.green,"green band", group = "shadow ribbon")
redColor=input.color(color.red,"red band", group = "shadow ribbon")
c=input(35,"Band transparency", group="shadow ribbon")
extreme_band=input(true, title="extreme lines",group= "Only for f1 type formation - goldenRatio lines") 
goldenRatioband=input(true, group= "Only for f1 type formation - goldenRatio lines", title="Show golden ratio lines")

//bar formation 
formation(type) =>
    switch type
        "f1" => 
            formLow=ta.lowest(low,n)
            formHigh=ta.highest(high,n)
            [formLow, formHigh]
        "f2" => 
            formLow=(low[n]+low)/2 
            formHigh=(high[n]+high)/2
            [formLow, formHigh]
        "f3" => 
            formLow=low[n] 
            formHigh=high[n]
            [formLow, formHigh]
        "f4" => 
            formLow=ta.linreg(low,n,1)
            formHigh=ta.linreg(high,n,1)
            [formLow, formHigh]

//Candels
newOpen=open[n]
[newLow, newHigh]=formation(formationTypeInput)
newLow:=newOpen<newLow?newOpen:newLow
newHigh:=newOpen>newHigh?newOpen:newHigh
newCLose=close

//Tapes and bands
tape=math.avg(newOpen,newCLose,newHigh,newLow)
tape:=ta.linreg(tape,ep,4)
ema=ta.ema(tape,8)
changeColor=tape<ema?color.new(redColor,c):color.new(greenColor,c)
delta=newHigh-newLow
mediaExtrema=(newHigh+newLow)/2
goldenRup=delta*0.618+newLow //0.618 ó 1-0.368 ó (0.618+(1-0.368))/2
goldenRdn=delta*(1-0.618)+newLow //1-0.618 ó 0.368 ó ((1-0.618)+0.368)/2
goldenExtup=delta*0.786+newLow//newHigh+delta*((1-0.618)+0.368)/2
goldenExtdn=delta*(1-0.786)+newLow//-delta*((1-0.618)+0.368)/2


//Plots
positiveCross=(ta.crossover(ombligo,goldenRdn) and close<mediaExtrema) or (ta.crossover(ombligo,goldenRup) and ombligo>open[1] and high>high[1]) or (low>goldenRup) or (ombligo>mediaExtrema)
negativeCross=(ta.crossunder(ombligo,goldenRup) and close>mediaExtrema) or (ta.crossunder(ombligo,goldenRdn) and ombligo<open[1] and low<low[1]) or (high<goldenRdn) or (ombligo<mediaExtrema)
candelcolor=newCLose>newOpen and ombligo>mediaExtrema?longColor:shortColor
candelcolor:=color.new(candelcolor,t)

show_extremeLine=extreme_band and (formationTypeInput=="f1")
displayExtreme=show_extremeLine?display.all - display.status_line:display.none
displayGolden=goldenRatioband and (formationTypeInput=="f1")?display.all - display.status_line:display.none
plotcandle(newOpen,newHigh,newLow,newCLose,"shadow bars", candelcolor,color.new(candelcolor,displayWick?t:100), bordercolor = candelcolor, display = displayBars)
plotema=plot(oc2?tape:na, color=changeColor, linewidth = 2, display = display.all - display.status_line)
plotema2=plot(oc2?ema:na, color=changeColor, display = display.all - display.status_line)
fill(plotema,plotema2,color = changeColor)
plot(newHigh,color=color.gray, display = displayExtreme, linewidth = 2)
plot(newLow, color=color.gray, display = displayExtreme, linewidth = 2)
plot(goldenExtup, display = displayGolden, color=color.purple, display = display.all - display.status_line)
plot(goldenExtdn, display = displayGolden, color=color.purple, display = display.all - display.status_line)
plot(mediaExtrema, color=color.aqua, display = displayGolden, display = display.all - display.status_line)
plot(goldenRup, color=color.orange, display = displayGolden)
plot(goldenRdn, color=color.orange, display = displayGolden)
//end