//
// @author LazyBear
//
// List of my public indicators: http://bit.ly/1LQaPK8
// List of my app-store indicators: http://blog.tradingview.com/?p=970
//
study("Ehlers Stochastic CG Oscillator [LazyBear]", shorttitle="ESCGO_LB", overlay=false, precision=3)
src=input(hl2, title="Source")
length=input(8, title="Length", minval=1, maxval=100)
lvls=input(0.8, title="OB/OS Level")
fr=input(true, title="Fill Osc/Trigger region")
nm0=0, dm0=0
nm1=1<=length ? (nm0+(1 + 0) * src[0]) : nm0, dm1=1<=length ? dm0+src[0] : dm0
nm2=2<=length ? (nm1+(1 + 1) * src[1]) : nm1, dm2=2<=length ? dm1+src[1] : dm1
nm3=3<=length ? (nm2+(1 + 2) * src[2]) : nm2, dm3=3<=length ? dm2+src[2] : dm2
nm4=4<=length ? (nm3+(1 + 3) * src[3]) : nm3, dm4=4<=length ? dm3+src[3] : dm3
nm5=5<=length ? (nm4+(1 + 4) * src[4]) : nm4, dm5=5<=length ? dm4+src[4] : dm4
nm6=6<=length ? (nm5+(1 + 5) * src[5]) : nm5, dm6=6<=length ? dm5+src[5] : dm5
nm7=7<=length ? (nm6+(1 + 6) * src[6]) : nm6, dm7=7<=length ? dm6+src[6] : dm6
nm8=8<=length ? (nm7+(1 + 7) * src[7]) : nm7, dm8=8<=length ? dm7+src[7] : dm7
nm9=9<=length ? (nm8+(1 + 8) * src[8]) : nm8, dm9=9<=length ? dm8+src[8] : dm8
nm10=10<=length ? (nm9+(1 + 9) * src[9]) : nm9, dm10=10<=length ? dm9+src[9] : dm9
nm11=11<=length ? (nm10+(1 + 10) * src[10]) : nm10, dm11=11<=length ? dm10+src[10] : dm10
nm12=12<=length ? (nm11+(1 + 11) * src[11]) : nm11, dm12=12<=length ? dm11+src[11] : dm11
nm13=13<=length ? (nm12+(1 + 12) * src[12]) : nm12, dm13=13<=length ? dm12+src[12] : dm12
nm14=14<=length ? (nm13+(1 + 13) * src[13]) : nm13, dm14=14<=length ? dm13+src[13] : dm13
nm15=15<=length ? (nm14+(1 + 14) * src[14]) : nm14, dm15=15<=length ? dm14+src[14] : dm14
nm16=16<=length ? (nm15+(1 + 15) * src[15]) : nm15, dm16=16<=length ? dm15+src[15] : dm15
nm17=17<=length ? (nm16+(1 + 16) * src[16]) : nm16, dm17=17<=length ? dm16+src[16] : dm16
nm18=18<=length ? (nm17+(1 + 17) * src[17]) : nm17, dm18=18<=length ? dm17+src[17] : dm17
nm19=19<=length ? (nm18+(1 + 18) * src[18]) : nm18, dm19=19<=length ? dm18+src[18] : dm18
nm20=20<=length ? (nm19+(1 + 19) * src[19]) : nm19, dm20=20<=length ? dm19+src[19] : dm19
nm21=21<=length ? (nm20+(1 + 20) * src[20]) : nm20, dm21=21<=length ? dm20+src[20] : dm20
nm22=22<=length ? (nm21+(1 + 21) * src[21]) : nm21, dm22=22<=length ? dm21+src[21] : dm21
nm23=23<=length ? (nm22+(1 + 22) * src[22]) : nm22, dm23=23<=length ? dm22+src[22] : dm22
nm24=24<=length ? (nm23+(1 + 23) * src[23]) : nm23, dm24=24<=length ? dm23+src[23] : dm23
nm25=25<=length ? (nm24+(1 + 24) * src[24]) : nm24, dm25=25<=length ? dm24+src[24] : dm24
nm26=26<=length ? (nm25+(1 + 25) * src[25]) : nm25, dm26=26<=length ? dm25+src[25] : dm25
nm27=27<=length ? (nm26+(1 + 26) * src[26]) : nm26, dm27=27<=length ? dm26+src[26] : dm26
nm28=28<=length ? (nm27+(1 + 27) * src[27]) : nm27, dm28=28<=length ? dm27+src[27] : dm27
nm29=29<=length ? (nm28+(1 + 28) * src[28]) : nm28, dm29=29<=length ? dm28+src[28] : dm28
nm30=30<=length ? (nm29+(1 + 29) * src[29]) : nm29, dm30=30<=length ? dm29+src[29] : dm29
nm31=31<=length ? (nm30+(1 + 30) * src[30]) : nm30, dm31=31<=length ? dm30+src[30] : dm30
nm32=32<=length ? (nm31+(1 + 31) * src[31]) : nm31, dm32=32<=length ? dm31+src[31] : dm31
nm33=33<=length ? (nm32+(1 + 32) * src[32]) : nm32, dm33=33<=length ? dm32+src[32] : dm32
nm34=34<=length ? (nm33+(1 + 33) * src[33]) : nm33, dm34=34<=length ? dm33+src[33] : dm33
nm35=35<=length ? (nm34+(1 + 34) * src[34]) : nm34, dm35=35<=length ? dm34+src[34] : dm34
nm36=36<=length ? (nm35+(1 + 35) * src[35]) : nm35, dm36=36<=length ? dm35+src[35] : dm35
nm37=37<=length ? (nm36+(1 + 36) * src[36]) : nm36, dm37=37<=length ? dm36+src[36] : dm36
nm38=38<=length ? (nm37+(1 + 37) * src[37]) : nm37, dm38=38<=length ? dm37+src[37] : dm37
nm39=39<=length ? (nm38+(1 + 38) * src[38]) : nm38, dm39=39<=length ? dm38+src[38] : dm38
nm40=40<=length ? (nm39+(1 + 39) * src[39]) : nm39, dm40=40<=length ? dm39+src[39] : dm39
nm41=41<=length ? (nm40+(1 + 40) * src[40]) : nm40, dm41=41<=length ? dm40+src[40] : dm40
nm42=42<=length ? (nm41+(1 + 41) * src[41]) : nm41, dm42=42<=length ? dm41+src[41] : dm41
nm43=43<=length ? (nm42+(1 + 42) * src[42]) : nm42, dm43=43<=length ? dm42+src[42] : dm42
nm44=44<=length ? (nm43+(1 + 43) * src[43]) : nm43, dm44=44<=length ? dm43+src[43] : dm43
nm45=45<=length ? (nm44+(1 + 44) * src[44]) : nm44, dm45=45<=length ? dm44+src[44] : dm44
nm46=46<=length ? (nm45+(1 + 45) * src[45]) : nm45, dm46=46<=length ? dm45+src[45] : dm45
nm47=47<=length ? (nm46+(1 + 46) * src[46]) : nm46, dm47=47<=length ? dm46+src[46] : dm46
nm48=48<=length ? (nm47+(1 + 47) * src[47]) : nm47, dm48=48<=length ? dm47+src[47] : dm47
nm49=49<=length ? (nm48+(1 + 48) * src[48]) : nm48, dm49=49<=length ? dm48+src[48] : dm48
nm50=50<=length ? (nm49+(1 + 49) * src[49]) : nm49, dm50=50<=length ? dm49+src[49] : dm49
nm51=51<=length ? (nm50+(1 + 50) * src[50]) : nm50, dm51=51<=length ? dm50+src[50] : dm50
nm52=52<=length ? (nm51+(1 + 51) * src[51]) : nm51, dm52=52<=length ? dm51+src[51] : dm51
nm53=53<=length ? (nm52+(1 + 52) * src[52]) : nm52, dm53=53<=length ? dm52+src[52] : dm52
nm54=54<=length ? (nm53+(1 + 53) * src[53]) : nm53, dm54=54<=length ? dm53+src[53] : dm53
nm55=55<=length ? (nm54+(1 + 54) * src[54]) : nm54, dm55=55<=length ? dm54+src[54] : dm54
nm56=56<=length ? (nm55+(1 + 55) * src[55]) : nm55, dm56=56<=length ? dm55+src[55] : dm55
nm57=57<=length ? (nm56+(1 + 56) * src[56]) : nm56, dm57=57<=length ? dm56+src[56] : dm56
nm58=58<=length ? (nm57+(1 + 57) * src[57]) : nm57, dm58=58<=length ? dm57+src[57] : dm57
nm59=59<=length ? (nm58+(1 + 58) * src[58]) : nm58, dm59=59<=length ? dm58+src[58] : dm58
nm60=60<=length ? (nm59+(1 + 59) * src[59]) : nm59, dm60=60<=length ? dm59+src[59] : dm59
nm61=61<=length ? (nm60+(1 + 60) * src[60]) : nm60, dm61=61<=length ? dm60+src[60] : dm60
nm62=62<=length ? (nm61+(1 + 61) * src[61]) : nm61, dm62=62<=length ? dm61+src[61] : dm61
nm63=63<=length ? (nm62+(1 + 62) * src[62]) : nm62, dm63=63<=length ? dm62+src[62] : dm62
nm64=64<=length ? (nm63+(1 + 63) * src[63]) : nm63, dm64=64<=length ? dm63+src[63] : dm63
nm65=65<=length ? (nm64+(1 + 64) * src[64]) : nm64, dm65=65<=length ? dm64+src[64] : dm64
nm66=66<=length ? (nm65+(1 + 65) * src[65]) : nm65, dm66=66<=length ? dm65+src[65] : dm65
nm67=67<=length ? (nm66+(1 + 66) * src[66]) : nm66, dm67=67<=length ? dm66+src[66] : dm66
nm68=68<=length ? (nm67+(1 + 67) * src[67]) : nm67, dm68=68<=length ? dm67+src[67] : dm67
nm69=69<=length ? (nm68+(1 + 68) * src[68]) : nm68, dm69=69<=length ? dm68+src[68] : dm68
nm70=70<=length ? (nm69+(1 + 69) * src[69]) : nm69, dm70=70<=length ? dm69+src[69] : dm69
nm71=71<=length ? (nm70+(1 + 70) * src[70]) : nm70, dm71=71<=length ? dm70+src[70] : dm70
nm72=72<=length ? (nm71+(1 + 71) * src[71]) : nm71, dm72=72<=length ? dm71+src[71] : dm71
nm73=73<=length ? (nm72+(1 + 72) * src[72]) : nm72, dm73=73<=length ? dm72+src[72] : dm72
nm74=74<=length ? (nm73+(1 + 73) * src[73]) : nm73, dm74=74<=length ? dm73+src[73] : dm73
nm75=75<=length ? (nm74+(1 + 74) * src[74]) : nm74, dm75=75<=length ? dm74+src[74] : dm74
nm76=76<=length ? (nm75+(1 + 75) * src[75]) : nm75, dm76=76<=length ? dm75+src[75] : dm75
nm77=77<=length ? (nm76+(1 + 76) * src[76]) : nm76, dm77=77<=length ? dm76+src[76] : dm76
nm78=78<=length ? (nm77+(1 + 77) * src[77]) : nm77, dm78=78<=length ? dm77+src[77] : dm77
nm79=79<=length ? (nm78+(1 + 78) * src[78]) : nm78, dm79=79<=length ? dm78+src[78] : dm78
nm80=80<=length ? (nm79+(1 + 79) * src[79]) : nm79, dm80=80<=length ? dm79+src[79] : dm79
nm81=81<=length ? (nm80+(1 + 80) * src[80]) : nm80, dm81=81<=length ? dm80+src[80] : dm80
nm82=82<=length ? (nm81+(1 + 81) * src[81]) : nm81, dm82=82<=length ? dm81+src[81] : dm81
nm83=83<=length ? (nm82+(1 + 82) * src[82]) : nm82, dm83=83<=length ? dm82+src[82] : dm82
nm84=84<=length ? (nm83+(1 + 83) * src[83]) : nm83, dm84=84<=length ? dm83+src[83] : dm83
nm85=85<=length ? (nm84+(1 + 84) * src[84]) : nm84, dm85=85<=length ? dm84+src[84] : dm84
nm86=86<=length ? (nm85+(1 + 85) * src[85]) : nm85, dm86=86<=length ? dm85+src[85] : dm85
nm87=87<=length ? (nm86+(1 + 86) * src[86]) : nm86, dm87=87<=length ? dm86+src[86] : dm86
nm88=88<=length ? (nm87+(1 + 87) * src[87]) : nm87, dm88=88<=length ? dm87+src[87] : dm87
nm89=89<=length ? (nm88+(1 + 88) * src[88]) : nm88, dm89=89<=length ? dm88+src[88] : dm88
nm90=90<=length ? (nm89+(1 + 89) * src[89]) : nm89, dm90=90<=length ? dm89+src[89] : dm89
nm91=91<=length ? (nm90+(1 + 90) * src[90]) : nm90, dm91=91<=length ? dm90+src[90] : dm90
nm92=92<=length ? (nm91+(1 + 91) * src[91]) : nm91, dm92=92<=length ? dm91+src[91] : dm91
nm93=93<=length ? (nm92+(1 + 92) * src[92]) : nm92, dm93=93<=length ? dm92+src[92] : dm92
nm94=94<=length ? (nm93+(1 + 93) * src[93]) : nm93, dm94=94<=length ? dm93+src[93] : dm93
nm95=95<=length ? (nm94+(1 + 94) * src[94]) : nm94, dm95=95<=length ? dm94+src[94] : dm94
nm96=96<=length ? (nm95+(1 + 95) * src[95]) : nm95, dm96=96<=length ? dm95+src[95] : dm95
nm97=97<=length ? (nm96+(1 + 96) * src[96]) : nm96, dm97=97<=length ? dm96+src[96] : dm96
nm98=98<=length ? (nm97+(1 + 97) * src[97]) : nm97, dm98=98<=length ? dm97+src[97] : dm97
nm99=99<=length ? (nm98+(1 + 98) * src[98]) : nm98, dm99=99<=length ? dm98+src[98] : dm98
nm100=100<=length ? (nm99+(1 + 99) * src[99]) : nm99, dm100=100<=length ? dm99+src[99] : dm99
nm=nm100, dm=dm100
cg = iff(dm != 0, -nm/dm + (length + 1) / 2.0, 0)
maxc = highest(cg, length)
minc = lowest(cg, length)
v1 = iff(maxc != minc, (cg - minc) / (maxc - minc), 0)
v2_ = (4*v1 + 3*v1[1] + 2*v1[2] + v1[3]) / 10.0
v2 = 2*(v2_ - 0.5)
t = ( 0.96 * ( ( v2[1]) + 0.02 ) )
duml=plot(fr?(v2>t?v2:t):na, style=circles, linewidth=0, color=gray, title="Dummy")
cmil=plot(v2, title="StochCGOsc", color=red)
tl=plot(t, title="Trigger",color=green)
plot(0, title="ZeroLine", color=gray)
u1=abs(lvls),l1=abs(lvls)*-1.0
plot(u1,title="Level+",color=gray)
plot(l1,title="Level-",color=gray)
fill(cmil, duml, color=red, transp=50, title="NegativeFill")
fill(tl, duml, color=lime, transp=50, title="PositiveFill")
ebc=input(false, title="Color bars?")
bc=ebc?(v2>v2[1]?(v2>0?lime:green):v2<v2[1]?(v2>0?orange:red):(v2>u1?lime:v2<l1?red:gray)):na
barcolor(bc)