Jeges Jigs
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mildSnail31034

//@version=4
study("Jeges Jigs", overlay=true, max_bars_back=500)
ATR = atr(14)
showma = input (title="Plot Moving Average?", type=input.bool,defval=true)
MAtype = input(title="MA type", defval="SMA", options=["EMA","SMA","ZLEMA","HMA","VWMA","SWMA","WMA","RMA"])
maperiod = input(title="What MA period defines trend", type=input.integer, defval=400,minval=1)
zlema(source,period) => 
    EMA1= ema(source,period)
    EMA2= ema(EMA1,period)
    Difference= EMA1 - EMA2
    ZLEMA = EMA1 + Difference
    ZLEMA

ZLEMA = zlema(close,maperiod)
HMA = hma(close,maperiod)
WMA = wma(close,maperiod)
RMA = rma(close,maperiod)
VWMA = vwma(close,maperiod)
SWMA = swma(close)
EMA = ema(close,maperiod)
SMA = sma(close,maperiod)
ma1 = sma(close, 1)
MA = SMA
if (MAtype=="ZLEMA")
    MA:=ZLEMA
if (MAtype=="EMA")
    MA:=EMA
if (MAtype=="HMA")
    MA:=HMA
if (MAtype=="WMA")
    MA:=WMA
if (MAtype=="VWMA")
    MA:=VWMA
if (MAtype=="SWMA")
    MA:=SWMA
if (MAtype=="RMA")
    MA:=RMA



showBB = input(title="Apply Bollinger Bands to MA?", type=input.bool, defval=true)
BBmult = input (title = "BB Std Dev",type=input.integer, defval=2, minval=0.001, maxval=50)
dev = BBmult * stdev(close, maperiod)
upperBB = MA + dev
lowerBB = MA - dev


showBB1 = input(title="Show other bands", type=input.bool, defval=true)
BBmult1 = input (title = "BB Std Dev",type=input.integer, defval=1, minval=0.001, maxval=50)
dev1 = BBmult1 * stdev(close, maperiod)
upperBB1 = MA + dev1
lowerBB1 = MA - dev1

showBB2 = input(title="Show other bands", type=input.bool, defval=false)
BBmult2 = input (title = "BB Std Dev",type=input.integer, defval=3, minval=0.001, maxval=50)
dev2 = BBmult2 * stdev(close, maperiod)
upperBB2 = MA + dev2
lowerBB2 = MA - dev2


showBBhilo = input(title="Highlight hi/lo BB breaks", type=input.bool,defval=true)
lookback = input(title="Lookback period for ATR spike(bars)", type=input.integer,defval=300, minval=5)
// Make inputs for length of highest high and lowest low
hiLen = input(title="Highest High Lookback (near ATR spike)", type=input.integer,
     defval=15, minval=0)

loLen = input(title="Lowest Low Lookback(near ATR spike)", type=input.integer,
     defval=15, minval=0)
filterbyvol = input(title="Filter by high volume (Experimental, may break things)", type=input.bool,defval=false)
volmaperiod = input(title="Volume MA period", type=input.integer, defval=50, minval=1)
volma = sma(volume, volmaperiod)
volmult = input(title="High volume (default 1.1x the average)", type=input.float, defval=1.1, minval=1)
showatrpeak = input(title="Highlight ATR peak areas?", type=input.bool, defval=true)
var float xhi = na
var float xlo = na
var label labelHi = na
var label labelLo = na
Bup = showBBhilo and crossover(ma1,upperBB)
Bupp = showBBhilo and ma1 > upperBB
Bdn = showBBhilo and crossunder(ma1,lowerBB)
Bdnn = showBBhilo and ma1 < lowerBB
if Bup
    xhi := high
    labelHi := label.new(bar_index, na, "▼", yloc = yloc.abovebar, color = #000000,textcolor  = color.lime, style = label.style_none)
else if Bdn
    xlo := low
    labelLo := label.new(bar_index,na, "▲", yloc =yloc.belowbar,color = #000000, textcolor = color.red, style =label.style_none)
    
if Bupp and high>xhi
    xhi := high
    label.set_xy(labelHi,bar_index,na)
else if Bdnn and low<xlo
    xlo := low
    label.set_xy(labelLo,bar_index,na)
hb=abs(highestbars(ATR,lookback))
atrpeak=ATR[hb]
hiHighs = high[hb]
loLows = low[hb]
hiHighsvol= if (close[hb]>MA[hb] and volume[hb]>volmult*volma[hb])
    highest(high, hiLen)[hb+1]
else
    na
loLowsvol=if (close[hb]<MA[hb] and volume[hb]>volmult*volma[hb])
    lowest(low,loLen)[hb+1]
else
    na
hiHighsnovol = if (close[hb]>MA[hb])
    highest(high, hiLen)[hb+1]
else
    na
loLowsnovol = if (close[hb]<MA[hb])
    lowest(low, loLen)[hb+1]
else 
    na
if (filterbyvol==true)
    hiHighs := hiHighsvol
    loLows := loLowsvol
else
    hiHighs := hiHighsnovol
    loLows := loLowsnovol
MULT1 = input(title="ATR Multiplier 1", type=input.float, defval=3)     
MULT2 = input(title="ATR Multiplier 2", type=input.float, defval=9)
MULT3 = input(title="ATR Multiplier 3", type=input.float, defval=27)
U1=hiHighs+atrpeak*MULT1
U2=hiHighs+atrpeak*MULT2
U3=hiHighs+atrpeak*MULT3
L1=loLows-atrpeak*MULT1
L2=loLows-atrpeak*MULT2
L3=loLows-atrpeak*MULT3
showsar = input(title="Show Parabolic SAR", type=input.bool, defval=true)
showzigzagsar = input(title="Show Zigzag SAR", type=input.bool, defval=true)
start = input(title="SAR Start", type=input.float, step=0.001,defval=0.02)
increment = input(title="SAR increment", type=input.float, step=0.001,defval=0.02)
maxsar = input(title="SAR max", type=input.float, step=0.01, defval=0.2)
psar = sar(start,increment, maxsar)
dir = psar<close ? 1 : -1
sarcolor = dir==1 ? color.red : color.green
plotsar = plot(showsar ? psar : na, title= "PSAR", style=plot.style_circles, color=sarcolor, linewidth=2 )
// Plot values for visual confirmation
peakcolor = showatrpeak and hb==false ? color.rgb(233,0,118,90) : na
bgcolor(peakcolor, title="ATR peak bg color")
plot(series=U1,title="Mark up 1",style=plot.style_circles,show_last=1,color=color.red,linewidth=2)
plot(series=U2,title="Mark up 2", style=plot.style_circles, show_last=1, color=color.red,linewidth=3)
plot(series=U3,title="Mark up 3", style=plot.style_circles, show_last=1, color=color.red,linewidth=4)
plot(series=L1,title="Mark down 1", style=plot.style_circles, show_last=1, color=color.green,linewidth=2)
plot(series=L2,title="Mark down 2",style=plot.style_circles, show_last=1, color=color.green,linewidth=3)
plot(series=L3,title="Mark down 3", style=plot.style_circles, show_last=1, color=color.green,linewidth=4)
plot(series=loLows,style=plot.style_circles,show_last=1, color=color.teal, linewidth=1,
     title="Lowest Low")
plot(series=hiHighs,
 style=plot.style_circles,show_last=1, color=color.fuchsia, linewidth=1,
     title="Highest High")
plot(showma ? MA : na, style=plot.style_line,color=color.blue, linewidth=2, title="MA")
uz = plot(showma and showBB ? upperBB : na, color=color.gray, linewidth=1,title="Upper BB line")
lz = plot(showma and showBB ? lowerBB : na, color=color.gray, linewidth=1,title="Lower BB line")
uz1 = plot(showma and showBB1 ? upperBB1 : na, color=color.gray, linewidth=1,title="Upper BB line")
lz1 = plot(showma and showBB1 ? lowerBB1 : na, color=color.gray, linewidth=1,title="Lower BB line")
uz2 = plot(showma and showBB2 ? upperBB2 : na, color=color.gray, linewidth=1,title="Upper BB line")
lz2 = plot(showma and showBB2 ? lowerBB2 : na, color=color.gray, linewidth=1,title="Lower BB line")
color fillcolor = color.rgb(0,212,188,95)
fill(uz,lz, title="BB fill",color=fillcolor)

showmtfsar = input(title="Show multi timeframe SAR levels", type=input.bool, defval=true)
lbloff = input(title="Labels offset(bars)", type=input.integer,defval=3,minval=0)
sar1 = security(syminfo.tickerid,"1",psar)
sar5 = security(syminfo.tickerid,"5",psar)
sar15 = security(syminfo.tickerid,"15",psar)
sar60 = security(syminfo.tickerid,"60",psar)
sar4hr = security(syminfo.tickerid,"240",psar)
sarD = security(syminfo.tickerid,"D",psar)
sarW = security(syminfo.tickerid,"W",psar)
sarM = security(syminfo.tickerid,"M",psar)
bullcolor = input(title="SAR MTF bull color",type=input.color,defval=color.green)
bearcolor = input(title="SAR MTF bear color",type=input.color,defval=color.red)

sar1lvl = input(title="Show 1m level",type=input.bool,defval=true)
sar1line = input(title="Show 1m line",type=input.bool,defval=false)
sar1color = sar1[0]>close ? bearcolor:bullcolor
plot(showmtfsar and sar1lvl and sar1line ? sar1:na,show_last=1,offset=3,linewidth=2,trackprice=true,color=sar1color,title="1m SAR")
plotshape(showmtfsar and sar1lvl?sar1:na,style=shape.cross ,location=location.absolute,offset=lbloff,show_last=1,text="1m",color=sar1color,title="1m SAR")

sar5lvl = input(title="Show 5m level",type=input.bool,defval=true)
sar5line = input(title="Show 5m line",type=input.bool,defval=false)
sar5color = sar5[0]>close ? bearcolor:bullcolor
plot(showmtfsar and sar5lvl and sar5line ? sar5:na,show_last=1,offset=3,linewidth=2,trackprice=true,color=sar5color,title="5m SAR")
plotshape(showmtfsar and sar5lvl?sar5:na,style=shape.cross ,location=location.absolute,offset=lbloff,show_last=1,text="5m",color=sar5color,title="5m SAR")

sar15lvl = input(title="Show 15m level",type=input.bool,defval=true)
sar15line = input(title="Show 15m line",type=input.bool,defval=false)
sar15color = sar15[0]>close ? bearcolor:bullcolor
plot(showmtfsar and sar15lvl and sar15line ? sar15:na,show_last=1,offset=3,linewidth=2,trackprice=true,color=sar15color,title="15m SAR")
plotshape(showmtfsar and sar15lvl?sar15:na,style=shape.cross ,location=location.absolute,offset=lbloff,show_last=1,text="15m",color=sar15color,title="15m SAR")

sar60lvl = input(title="Show 1hr level",type=input.bool,defval=true)
sar60line = input(title="Show 1hr line",type=input.bool,defval=false)
sar60color = sar60[0]>close ? bearcolor:bullcolor
plot(showmtfsar and sar60lvl and sar60line ? sar60:na,show_last=1,offset=3,linewidth=2,trackprice=true,color=sar60color,title="1hr SAR")
plotshape(showmtfsar and sar60lvl?sar60:na,style=shape.cross ,location=location.absolute,offset=lbloff,show_last=1,text="1hr",color=sar60color,title="1hr SAR")

sar4hrlvl = input(title="Show 4hrs level",type=input.bool,defval=true)
sar4hrline = input(title="Show 4hrs line",type=input.bool,defval=false)
sar4hrcolor = sar4hr[0]>close ? bearcolor:bullcolor
plot(showmtfsar and sar4hrlvl and sar4hrline ? sar4hr:na,show_last=1,offset=3,linewidth=2,trackprice=true,color=sar4hrcolor,title="4hr SAR")
plotshape(showmtfsar and sar4hrlvl?sar4hr:na,style=shape.cross ,location=location.absolute,offset=lbloff,show_last=1,text="4h",color=sar4hrcolor,title="4hr SAR")


sarDlvl = input(title="Show daily level",type=input.bool,defval=true)
sarDline = input(title="Show daily line",type=input.bool,defval=false)
sarDcolor = sarD[0]>close ? bearcolor:bullcolor
plot(showmtfsar and sarDlvl and sarDline ? sarD:na,show_last=1,offset=3,linewidth=2,trackprice=true,color=sarDcolor,title="Daily SAR")
plotshape(showmtfsar and sarDlvl?sarD:na,style=shape.cross ,location=location.absolute,offset=lbloff,show_last=1,text="D",color=sarDcolor,title="Daily SAR")

sarWlvl = input(title="Show weekly level",type=input.bool,defval=true)
sarWline = input(title="Show weekly line",type=input.bool,defval=false)
sarWcolor = sarW[0]>close ? bearcolor:bullcolor
plot(showmtfsar and sarWlvl and sarWline ? sarW:na,show_last=1,offset=3,linewidth=2,trackprice=true,color=sarWcolor,title="Weekly SAR")
plotshape(showmtfsar and sarWlvl?sarW:na,style=shape.cross ,location=location.absolute,offset=lbloff,show_last=1,text="W",color=sarWcolor,title="Weekly SAR")

sarMlvl = input(title="Show monthly level",type=input.bool,defval=true)
sarMline = input(title="Show monthly line",type=input.bool,defval=false)
sarMcolor = sarM[0]>close ? bearcolor:bullcolor
plot(showmtfsar and sarMlvl and sarMline ? sarM:na,show_last=1,offset=3,linewidth=2,trackprice=true,color=sarMcolor,title="Monthly SAR")
plotshape(showmtfsar and sarMlvl?sarM:na,style=shape.cross ,location=location.absolute,offset=lbloff,show_last=1,text="M",color=sarMcolor,title="Monthly SAR")




dirz = psar<close ? true : false
up = dirz and dirz[1]==false
dn = dirz==false and dirz[1]
//sarcolor = dirz ? color.green : color.red
lineupcolor = input(title="Zigzag up color", defval=color.green)
linedncolor = input(title="Zigzag down color", defval=color.red)
linewidth = input(title="Zigzag line width",type=input.integer,minval=1,maxval=4,defval=2)
//plotsar = plot(showsar ? psar : na, title= "PSAR", style=plot.style_circles, color=sarcolor, linewidth=2 )
var line zzup = na
var line zzdn = na
var bt1 = 0
var bt2 = 0
var float hi = na
var float lo = na
if showzigzagsar and up
    hi :=high
    bt1 := time
    zzup := line.new(bt2,lo,bt1,hi,color=lineupcolor, width =linewidth,xloc=xloc.bar_time)
if showzigzagsar and dn
    lo :=low
    bt2 := time
    zzdn := line.new(bt1,hi,bt2,lo,color=linedncolor, width=linewidth,xloc=xloc.bar_time)
if (showzigzagsar and dirz and high > hi)
    hi :=high
    bt1 := time
    line.set_xy2(zzup,bt1,hi)
    
if (showzigzagsar and dirz==false and low < lo)
    lo := low
    bt2 := time
    line.set_xy2(zzdn,bt2,lo)

dummytrendlines = input(false,"==TREND LINES SECTION. ENABLE/DISABLE USING CONTROLS BELOW==")
length = input(6, "Period (change this if it doesn't identify highs/lows correctly)", inline = "length")
hist1 = input(true,"Lines between 1st pivot and 2nd")
hist2 = input(true,"Lines between 2nd pivot and 3rd")
hist3 = input(true,"Line between 3rd pivot and 4th")
csrc = input(false,"Use Custom Source?", inline = "custom")
src = input(close,"Select:", inline = "custom")
//color = input("Black", "Line Color:", options=["Black","Red/Green", "White", "Yellow", "Blue"], inline = "custom2")
color1 = input(title="Trend up color", defval=color.black)
color2 = input(title="Trend down color", defval=color.black)
lwidth = input("1", "Line Width:", options=["1", "2", "3", "4"], inline = "custom2")

//Conditions
upl = pivothigh(iff(csrc,src,high),length,length)
dnl = pivotlow(iff(csrc,src,low),length,length)



n = bar_index
a1 = valuewhen(not na(upl),n,1)
b1 = valuewhen(not na(dnl),n,1)
a2 = valuewhen(not na(upl),n,0)
b2 = valuewhen(not na(dnl),n,0)

c1 = valuewhen(not na(upl),n,2)
d1 = valuewhen(not na(dnl),n,2)
c2 = valuewhen(not na(upl),n,1)
d2 = valuewhen(not na(dnl),n,1)

e1 = valuewhen(not na(upl),n,3)
f1 = valuewhen(not na(dnl),n,3)
e2 = valuewhen(not na(upl),n,2)
f2 = valuewhen(not na(dnl),n,2)

	

width1 = 1
if lwidth == "1"
	width1 := 1
if lwidth == "2"
	width1 := 2
if lwidth == "3"
	width1 := 3
if lwidth == "4"
	width1 := 4



line upper1 = hist1 ? line.new(n[n - a1 + length],upl[n - a1],n[n - a2 + length],upl[n - a2],
  extend=extend.right,color=color1,width=width1) : na
line lower1 = hist1 ? line.new(n[n - b1 + length],dnl[n - b1],n[n - b2 + length],dnl[n - b2],
  extend=extend.right,color=color2,width=width1) : na
line.delete(upper1[1])
line.delete(lower1[1])

line upper2 = hist2 ? line.new(n[n - c1 + length],upl[n - c1],n[n - c2 + length],upl[n - c2],
  extend=extend.right,color=color1,width=width1) : na
line lower2 = hist2 ? line.new(n[n - d1 + length],dnl[n - d1],n[n - d2 + length],dnl[n - d2],
  extend=extend.right,color=color2,width=width1) : na
line.delete(upper2[1])
line.delete(lower2[1])


line upper3 = hist3 ? line.new(n[n - e1 + length],upl[n - e1],n[n - e2 + length],upl[n - e2],
  extend=extend.right,color=color1,width=width1) : na
line lower3 = hist3 ? line.new(n[n - f1 + length],dnl[n - f1],n[n - f2 + length],dnl[n - f2],
  extend=extend.right,color=color2,width=width1) : na
line.delete(upper3[1])
line.delete(lower3[1])









