Converging Pullbacks and Peaks
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © EsIstTurnt

//@version=5
indicator("Converging Pullbacks and Peaks",overlay=true)
//Multi Timeframe Converging Lines Indicator. Using the highest/lowest Values at 2 seperate lengths,
//all changeable in settings. Convergence created by taking the highest/lowest value and subtracting/adding the # of bars
//since the highest/lowest bar was set multiplied by the price multiplied by the float.
//Curves are created from averaging out the emas of the center lines of the extremeties.
//Helps show trendlines automatically alot of the time but can be tweaked by changing the floats or Fast/Slow lengths


tf1=input.timeframe('60')
tf2=input.timeframe('640')
float1=input.float(0.0000618,"Converge Float TimeFrame 1",step=0.00001)
float2=input.float(0.0000618,"Converge Float TimeFrame 2",step=0.00001)
bool1=input.bool(true,'Enable TimeFrame 1')
bool2=input.bool(true,'Enable TimeFrame 2')
bool3=input.bool(true,'Fast')
bool4=input.bool(true,'Slow')
bool5=input.bool(false,'Curves')
bool6=input.bool(false,'Toggle Custom Source')
src=input.source(close,'Custom Sourcing')
Fast=input.int(64,'Fast Length')
Slow=input.int(256,'Slow Length')
//TimeFrame 1 highest and lowest values
high1a =request.security(syminfo.ticker,tf1,ta.highest(bool6 ?src : high,Fast ))
low1a  =request.security(syminfo.ticker,tf1,ta.lowest (bool6 ?src : low ,Fast ))
high2a =request.security(syminfo.ticker,tf1,ta.highest(bool6 ?src : high,Slow ))
low2a  =request.security(syminfo.ticker,tf1,ta.lowest (bool6 ?src : low ,Slow ))
//TimeFrame 2 highest and lowest values  
high1b =request.security(syminfo.ticker,tf2,ta.highest(bool6 ?src : high,Fast ))
low1b  =request.security(syminfo.ticker,tf2,ta.lowest (bool6 ?src : low ,Fast ))
high2b =request.security(syminfo.ticker,tf2,ta.highest(bool6 ?src : high,Slow ))
low2b  =request.security(syminfo.ticker,tf2,ta.lowest (bool6 ?src : low ,Slow ))
//Count since new Highs , Lows
new_high1a=ta.barssince(high1a==high )
new_high1b=ta.barssince(high1b==high )
new_high2a=ta.barssince(high2a==high )
new_high2b=ta.barssince(high2b==high )
new_low1a =ta.barssince(low1a ==low ) 
new_low1b =ta.barssince(low1b ==low ) 
new_low2a =ta.barssince(low2a ==low ) 
new_low2b =ta.barssince(low2b ==low ) 
//get the value of the highest/lowest high/low in 4 bars when a new high/low occurs, and add/subtract
//the Bar counters above and multiply by its price roughly and multiply by a float
//highs subtract , lows add for converging lines
hformula1a=ta.valuewhen(high1a==high,ta.highest(high,4),1) - (new_high1a * ta.swma(ta.vwap(close) * (float1)))
hformula1b=ta.valuewhen(high1b==high,ta.highest(high,4),1) - (new_high1b * ta.swma(ta.vwap(close) * (float1)))
hformula2a=ta.valuewhen(high2a==high,ta.highest(high,4),1) - (new_high2a * ta.swma(ta.vwap(close) * (float2)))
hformula2b=ta.valuewhen(high2b==high,ta.highest(high,4),1) - (new_high2b * ta.swma(ta.vwap(close) * (float2)))
lformula1a=ta.valuewhen(low1a ==low ,ta.lowest(low,4),1)   + (new_low1a  * ta.swma(ta.vwap(close) * (float1)))
lformula1b=ta.valuewhen(low1b ==low ,ta.lowest(low,4),1)   + (new_low1b  * ta.swma(ta.vwap(close) * (float1)))
lformula2a=ta.valuewhen(low2a ==low ,ta.lowest(low,4),1)   + (new_low2a  * ta.swma(ta.vwap(close) * (float2)))
lformula2b=ta.valuewhen(low2b ==low ,ta.lowest(low,4),1)   + (new_low2b  * ta.swma(ta.vwap(close) * (float2)))
//get the centerlines, average it all up and apply an ema 
avg1=math.avg(hformula1a,lformula1a)
avg2=math.avg(hformula2a,lformula2a)
avg3=math.avg(hformula1b,lformula1b)
avg4=math.avg(hformula2b,lformula2b)
centercurve=ta.linreg(ta.swma(math.avg(avg1,avg4,avg3,avg2)),Slow/2,0)
curve1 =ta.ema(centercurve,Slow) 
curve2 =ta.ema(centercurve[Fast],Fast)
cond1=curve1>curve2
//Plotting
plot(bool1?bool3?hformula1a:na:na,style=plot.style_circles,color=color.new(hformula1a<=hformula1a[4]?#acfb00:na,75))
plot(bool2?bool4?hformula1b:na:na,style=plot.style_cross,color=color.new(hformula1b<=hformula1b[4]  ?#ff0000:na,85))
plot(bool1?bool3?hformula2a:na:na,style=plot.style_circles,color=color.new(hformula1a<=hformula1a[4]?color.olive:na ,75))
plot(bool2?bool4?hformula2b:na:na,style=plot.style_cross,color=color.new(hformula2b<=hformula2b[4]  ?color.orange:na,85))
plot(bool1?bool3?lformula1a:na:na,style=plot.style_circles,color=color.new(lformula1a>=lformula1a[4]?#acfb00:na,75))
plot(bool2?bool4?lformula1b:na:na,style=plot.style_cross,color=color.new(lformula1b>=lformula1b[4]  ?#ff0000:na,85))
plot(bool1?bool3?lformula2a:na:na,style=plot.style_circles,color=color.new(lformula2a<=lformula2a[4]?color.olive:na ,75))
plot(bool2?bool4?lformula2b:na:na,style=plot.style_cross,color=color.new(lformula2b>=lformula2b[4]  ?color.orange:na,85))

centercurve1  = plot( curve1,color =bool5?math.round(avg3) == math.round(avg3[1]) and math.round(avg3)==math.round(avg3[4]) ?avg3<close?#acfb00:#ff0000:color.orange:na,style=plot.style_linebr)
centercurve2 = plot(  curve2,color =bool5? math.round(avg4) == math.round(avg4[1]) and math.round(avg4)==math.round(avg4[4]) ?avg4 < close?#acfb00:#ff0000:color.orange:na,style=plot.style_linebr)
color=color.new(bool5?cond1?#ff0000:#acfb00:na,80)
fill(centercurve1,centercurve2,color=color)
