{111 swing1.14 powerEditor, view, options, general, set tabstops=2 to correct the formating} var: dir(0), mtDir(0), inside(false), longRngH(0), longRngL(0), myBar(0), myH(0), myL(0), myC(0), offset(0), Si(4), pi(0), mtLi(0), mtHi(0), BRi(0), TRi(0), rev(0), repeat(0), asc(false), des(false), p2t(false), p2b(false), nullAscPrice(0), nullDesPrice(0), nullP2Tprice(0), nullP2Bprice(0);
array:S[1999,17](0), mtL[199,3](0), mtH[199,3](0), p[1999,7,2](0), BR[1999,3](0), TR[1999,3](0); {increasing 1st value in all arrays increases lookback: s[5555,17](0)} {comment out arrays like p[1999,7,2](0) and it's code segments to decrease calculation time}
if inside[1] then begin if H>longRngH or L<longRngL then inside=false; if H>longRngH and L>longRngL then dir=1; {up} if H<longRngH and L<longRngL then dir= -1; {down} end else begin if H<=H[1] and L>=L[1] then begin longRngL=L[1]; longRngH=H[1]; inside=true; {inside} end; if H>H[1] and L>L[1] then dir= 1; {up} if H<H[1] and L<L[1] then dir= -1; {down} end;
if inside=false then begin myBar=currentbar; myH=H; myL=L; myC=C; end;
if dir<>dir[1] then begin si=si+1; s[si,0]=myBar[1]; s[si,1]=myH[1]; s[si,2]=myL[1]; s[si,3]=myC[1]; if dir>0 then begin S[si,4]=myL[1]; s[si,5]=1; {swing=B} if s[si,2]<mtL[mtLi,2] then begin s[si,15]=-1; {mtL=lowerB} mtLi=mtLi+1; mtL[mtLi,0]=myBar[1]; mtL[mtLi,1]=myH[1]; mtL[mtLi,2]=myL[1]; mtL[mtLi,3]=myC[1]; end; if s[si,1]-s[si,2]>mtL[mtLi,1]-mtL[mtLi,2] then begin s[si,16]=-1; {mtL=largerRangeB} mtLi=mtLi+1; mtL[mtLi,0]=myBar[1]; mtL[mtLi,1]=myH[1]; mtL[mtLi,2]=myL[1]; mtL[mtLi,3]=myC[1]; end; if asc and s[si,1]<nullAscPrice then begin {nullAsc} asc=false; s[si,13]=-1; end; if mtDir<=0 and asc and c>=c[1] then begin {revBuyAsc found} rev=1; asc=false; s[si,8]=1; end; if s[si,2]>mtL[mtLi,1] then begin {asc found} asc=true; nullAscPrice=s[si,2]; s[si,6]=1; end; if p2t and s[si,1]<nullP2Tprice then begin p2t=false; s[si,14]=-1; {nullP2T} end; if mtDir<=0 and p2t and c>=c[1]then begin {revBuyP2T found} rev=1; p2t=false; s[si,9]=1; end; if s[si,3]<s[si-2,2] and s[si,3]<s[si-4,2] then begin {p2b found} p2b=true; nullP2Bprice=s[si-1,1]; s[si,7]=-1; end; if mtDir=mtDir[1] and asc and c>=c[1] then begin {repeatBuyAsc} repeat=1; asc=false; s[si,10]=1; end; if mtDir>0 and s[si,2]<s[si-2,1] and s[si,1]>s[si-2,2] and c>=c[1] then begin repeat=1; s[si,11]=1; {repeatBuyTOB 1st B back} end; if mtDir>0 and s[si,2]<s[si-4,1] and s[si,1]>s[si-4,2] and c>=c[1] then begin repeat=1; s[si,12]=1; {repeatBuyTOB 2nd B back} end; end else begin S[si,4]=myH[1]; s[si,5]=-1; {swing=T} if s[si,1]>mtH[mtHi,1] then begin s[si,15]=1; {mtH=higherTop} mtHi=mtHi+1; mtH[mtHi,0]=myBar[1]; mtH[mtHi,1]=myH[1]; mtH[mtHi,2]=myL[1]; mtH[mtHi,3]=myC[1]; end; if s[si,1]-s[si,2]>mtH[mtHi,1]-mtH[mtHi,2] then begin s[si,16]=1; {mtH=largerRangeTop} mtHi=mtHi+1; mtH[mtHi,0]=myBar[1]; mtH[mtHi,1]=myH[1]; mtH[mtHi,2]=myL[1]; mtH[mtHi,3]=myC[1]; end; if des and s[si,2]>nullDesPrice then begin des=false; s[si,13]=1;{nullDes} end; if mtDir>=0 and des and c<=c[1] then begin {revSellDes found} rev=-1; des=false; s[si,8]=-1; end; if s[si,1]<mtH[mtHi,2] then begin {des found} des=true; nullDesPrice=s[si,1]; s[si,6]=-1; end; if p2b and s[si,2]>nullP2Bprice then begin p2B=false; s[si,14]=1; {nullP2B} end; if mtDir>=0 and p2b and c<=c[1]then begin {revSellP2B found} rev=-1; p2b=false; s[si,9]=-1; end; if s[si,3]>s[si-2,1] and s[si,3]>s[si-4,1] then begin {p2t found} p2t=true; nullP2Tprice=s[si-1,2]; s[si,7]=1; end; if mtDir=mtDir[1] and des and c<=c[1] then begin {repeatSellDes} repeat= -1; des=false; s[si,10]=-1; end; if mtDir<0 and s[si,2]<s[si-2,1] and s[si,1]>s[si-2,2] and c<=c[1] then begin repeat= -1; s[si,11]=-1; {repeatSellTOT 1st T back} end; if mtDir<0 and s[si,2]<s[si-4,1] and s[si,1]>s[si-4,2] and c<=c[1] then begin repeat= -1; s[si,12]=-1; {repeatSellTOT 2nd T back} end; end; end;
if dir<0 and mtDir>0 and inside=false and L<L[1] and c>c[1] then begin {repeatBuyBR} repeat=1; BRi=BRi+1; BR[BRi,0]=currentBar; BR[BRi,1]=H; BR[BRi,2]=L; BR[BRi,3]=C; end; if dir>0 and mtDir<0 and inside=false and H>H[1] and c<c[1] then begin {repeatSellTR} repeat= -1; TRi=TRi+1; TR=currentBar; TR=H; TR=L; TR=C; end;
if mtDir>0 then begin {p1 doesn't use target} if p[pi,3,2]>0 and p[pi,4,2]=0 and L>p[pi,3,2]+p[pi,6,2] then {p2 target hit} p[pi,4,2]=maxList(p[pi,3,2]+p[pi,6,2],open); {exit p2} end else begin if p[pi,3,2]>0 and p[pi,4,2]=0 and H<p[pi,3,2]-p[pi,6,2] then {p2 target hit} p[pi,4,2]=minList(p[pi,3,2]-p[pi,6,2],open); {exit p2} end;
{before revEntry resets mtDir} if mtDir>0 then begin {trailing stop} if p[pi,3,1]>0 and p[pi,4,1]=0 and L<=p[pi,7,1]-p[pi,6,1] then {p1 stop hit} p[pi,4,1]=minList(p[pi,7,1]-p[pi,6,1],open); {exit p1} if p[pi,3,2]>0 and p[pi,4,2]=0 and L<=p[pi,7,2]-p[pi,6,2] then {p2 stop hit} p[pi,4,2]=minList(p[pi,7,2]-p[pi,6,2],open); {exit p2} end else begin if p[pi,3,1]>0 and p[pi,4,1]=0 and H>=p[pi,7,1]+p[pi,6,1] then {p1 stop hit} p[pi,4,1]=maxList(p[pi,7,1]+p[pi,6,1],open); {exit p1} if p[pi,3,2]>0 and p[pi,4,2]=0 and H>=p[pi,7,2]+p[pi,6,2] then {p2 stop hit} p[pi,4,2]=maxList(p[pi,7,2]+p[pi,6,2],open); {exit p2} end;
if rev<>0 then begin {rev exit+entry} if p[pi,3,1]>0 and p[pi,4,1]=0 then p[pi,4,1]=c; {exit} if p[pi,3,2]>0 and p[pi,4,2]=0 then p[pi,4,2]=c; {exit} pi=pi+1; p[pi,0,1]=currentbar; p[pi,1,1]=H; p[pi,2,1]=L; p[pi,3,1]=C; {entry} p[pi,4,1]=0; p[pi,6,1]=squareRoot(c); if rev>0 then begin mtDir=1; s[si,17]=1; p[pi,5,1]=1; p[pi,7,1]=H; {reset mtDir} mtHi=mtHi+1; mtH[mtHi,0]=currentBar; mtH[mtHi,1]=H; mtH[mtHi,2]=L; mtH[mtHi,3]=C; {mtH freshStart} end else begin mtDir=-1; s[si,17]=-1; p[pi,5,1]=-1; p[pi,7,1]=L; {reset mtDir} mtLi=mtLi+1; mtL[mtLi,0]=currentBar; mtL[mtLi,1]=H; mtL[mtLi,2]=L; mtL[mtLi,3]=C; {mtL freshStart} end; rev=0; repeat=0; {reset rev+repeat} end; {p[pi,7,1]=C,H,L?}
if repeat>0 then begin {repeatEntry} if p[pi,4,1]>0 then begin {last p1 closed} p[pi,0,1]=currentBar; p[pi,1,1]=H; p[pi,2,1]=L; p[pi,3,1]=C; p[pi,4,1]=0; p[pi,5,1]=1; p[pi,6,1]=squareRoot(c); p[pi,7,1]=H;{or p[pi,7,1]=c;?} end else begin {p1 still open} if c>p[pi,3,1] and p[pi,4,2]>0 then begin {p1 profitable} {last p2 closed} p[pi,0,2]=currentBar; p[pi,1,2]=H; p[pi,2,2]=L; p[pi,3,2]=C; p[pi,4,2]=0; p[pi,5,2]=1; p[pi,6,2]=squareRoot(c); p[pi,7,2]=H;{or p[pi,7,2]=c;?} end; if c>p[pi,3,1] and p[pi,3,2]=0 then begin {p2 empty} p[pi,0,2]=currentBar; p[pi,1,2]=H; p[pi,2,2]=L; p[pi,3,2]=C; p[pi,4,2]=0; p[pi,5,2]=1; p[pi,6,2]=squareRoot(c); p[pi,7,2]=H;{or p[pi,7,2]=c;?} end; end; repeat=0; {resetRepeat} end; if repeat<0 then begin {repeatEntry} if p[pi,4,1]>0 then begin {last p1 closed} p[pi,0,1]=currentBar; p[pi,1,1]=H; p[pi,2,1]=L; p[pi,3,1]=C; p[pi,4,1]=0; p[pi,5,1]=-1; p[pi,6,1]=squareRoot(c); p[pi,7,1]=L;{or p[pi,7,1]=c;?} end else begin {p1 still open} if c<p[pi,3,1] and p[pi,4,2]>0 then begin {p1 profitable} {last p2 closed} p[pi,0,2]=currentBar; p[pi,1,2]=H; p[pi,2,2]=L; p[pi,3,2]=C; p[pi,4,2]=0; p[pi,5,2]=-1; p[pi,6,2]=squareRoot(c); p[pi,7,2]=L;{or p[pi,7,2]=c;?} end; if c<p[pi,3,1] and p[pi,3,2]=0 then begin {p2 empty} p[pi,0,2]=currentBar; p[pi,1,2]=H; p[pi,2,2]=L; p[pi,3,2]=C; p[pi,4,2]=0; p[pi,5,2]=-1; p[pi,6,2]=squareRoot(c); p[pi,7,2]=L;{or p[pi,7,2]=c;?} end; end; repeat=0; {resetRepeat} end;
if mtDir>0 then begin if p[pi,3,1]>0 and p[pi,4,1]=0 and c>p[pi,7,1] then p[pi,7,1]=c; {raise chandelier} if p[pi,3,2]>0 and p[pi,4,2]=0 and c>p[pi,7,2] then p[pi,7,2]=c; {raise chandelier} end else begin if p[pi,3,1]>0 and p[pi,4,1]=0 and c<p[pi,7,1] then p[pi,7,1]=c; {lower chandelier} if p[pi,3,2]>0 and p[pi,4,2]=0 and c<p[pi,7,2] then p[pi,7,2]=c; {lower chandelier} end; {H,L instead of C moves chandelier?} {repeat+rev entry use p[pi,7,n]=H or L;?}
for offset=currentbar-S[Si,0] downto 0 begin if dir<0 then setPlotColor[offset](1,red); if dir>0 then setPlotColor[offset](1,darkgreen); end; plot1[currentbar-S[Si,0]](S[Si,4]); {if s[si,5]<0 then plotPaintBar[currentbar-s[si,0]](s[si,1],s[si,2]);} {paintBar all B's}
{---------------NOTES------------------ swing array index: 0= myBar 1= H 2= L 3= C 4= swing price {myL or myH} 5= 1 if B, -1 if T 6= 1 if asc, -1 if des 7= 1 if p2t found, -1 if p2b found 8= 1 if revBuyAsc, -1 if revSellDes 9= 1 if revBuyP2T, -1 if revSellP2B 10=1 if repeatBuyAsc, -1 if repeatSellDes 11=1 if repeatBuyTOB, -1 if repeatSellTOT {back[1]} 12=1 if repeatBuyTOB, -1 if repeatSellTOT {back[2] but not both[1]+[2]} 13=1 if nullDes, -1 if nullAsc 14=1 if nullP2B, -1 if nullP2T 15=1 if mtH higherTop, -1 if mtL lower B 16=1 if mtH largerRangeH, -1 if mtL largerRangeB {16 isn't red green logic} 17=1 if mtDir=1, and -1 if mtDir=-1
accessing the index: {asc paintbar} if s[si,6]>0 then plotPaintBar[currentbar-s[si,0]](s[si,1],s[si,2]); {des paintbar} if s[si,6]<0 then plotPaintBar[currentbar-s[si,0]](s[si,1],s[si,2]); {p2t paintbar} if s[si,7]>0 then plotPaintBar[currentbar-s[si,0]](s[si,1],s[si,2]); {p2b paintbar} if s[si,7]<0 then plotPaintBar[currentbar-s[si,0]](s[si,1],s[si,2]);
p array: p1=LT p2=ST 0=barNumber 1=H 2=L 3=C 4=exitPrice {entry resets it to zero} 5= 1 if long, -1 if short. 6= target+stop factor {squareRoot p[pi,3,n];} 7= chandelier, max c since entry if long (5=1), min c since entry if short (5=-1)
TOB+TOT hybrid sweetspot = extreme % of range. Low risk, high reward, high probability variant. Sweetspot = extreme 50% of range if b's overlap, otherwise sweetspot=50% of lowest B. Note Dunnigan defines B as range from H to L of the bar that forms the B rather than the just a single point. The sweetspot simply reduces the size of that zone by 1/2.} |