Hi,
I am using a slightly edited version of a code , I think taken from the library.
Basically it is trying to plot swing lines using "2 bar fractals" to identify pivots.
First the code --
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates | chartHideQuoteMarker);
GraphLabelDecimals = 2;
GraphXSpace =10;
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} | {{DATE}} | Open %g | Hi %g | Lo %g | Close %g | (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
TC = IIf(C>Ref(C,-1),colorGreen,IIf(C<Ref(C,-1),colorRed,colorGold));
Plot( C, "Close", TC, styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_BEGIN( "Fractals" );
x=xx=BarIndex(); Lx=LastValue(x);
nbar=Param("N Pivot Bars",2,2,21,1);
SetChartOptions(0,chartShowArrows|chartShowDates | chartHideQuoteMarker);
GraphLabelDecimals = 2;
GraphXSpace =10;
//Mark Fractals
pk=H>Ref(HHV(H,nbar),-1) AND Ref(HHV(H,nbar),nbar)<=H;
tr=L<Ref(LLV(L,nbar),-1) AND Ref(LLV(L,nbar),nbar)>=L;
PlotShapes(shapeSmallCircle*tr,IIf(Lx-ValueWhen(tr,x)>nbar,colorBlue,colorWhite),0,L,-10);
PlotShapes(shapeSmallCircle*pk,IIf(Lx-ValueWhen(pk,x)>nbar,colorRed,colorWhite),0,H,10);
// DRAWING SWING LINES
plotEWswings = ParamToggle( "EW Swings", "Off|On", 1 );
// Identify Peak and trough
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pk = IIf( pk, IIf( ValueWhen( pk, x, 2 ) < ValueWhen( tr, x,1), pk, IIf( ValueWhen( pk, H, 2 ) > H, False, pk ) ), pk );
pk = IIf( pk AND ValueWhen( pk, x, 0 ) > x, IIf( ValueWhen( pk, x, 0 ) < ValueWhen( tr, x, 0 ), IIf( ValueWhen( pk, H, 0 ) >= H, False, pk ), pk ), pk );
tr = IIf( tr, IIf( ValueWhen( tr, x, 2 ) < ValueWhen( pk, x ), tr, IIf( ValueWhen( tr, L, 2 ) < L, False, tr ) ), tr );
tr = IIf( tr AND ValueWhen( tr, x, 0 ) > x , IIf( ValueWhen( tr, x, 0 ) < ValueWhen( pk, x, 0 ), IIf( ValueWhen( tr, L, 0 ) <= L, False, tr ), tr ), tr );
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//bar index and value
px0=ValueWhen(pk,x,0); ph0=ValueWhen(pk,H,0);
tx0=ValueWhen(tr,x,0); tl0=ValueWhen(tr,L,0);
px1=ValueWhen(pk,x,1); ph1=ValueWhen(pk,H,1);
tx1=ValueWhen(tr,x,1); tl1=ValueWhen(tr,L,1);
//Plotting swing lines
if( plotEWswings )
{
aa1=IIf(px0>tx1 ,(ph0-tl1)/(px0-tx1),0);
aa1=IIf(pk,Ref(aa1,-1),aa1) ;
ls1=aa1*(xx-tx1)+tl1;
bb1=IIf(px0>tx1 AND px1<tx1,1,0);
bb1=(bb1+Ref(bb1,-1)) AND ph0>tl0;
bb1=IIf(bb1,1,0);
ls1=IIf(bb1,ls1,Null) ;
Plot(ls1,"",colorBlue,styleLine);
aa1=IIf(tx0>px1,(tl0-ph1)/(tx0-px1),0);
aa1=IIf(tr,Ref(aa1,-1),aa1);
Ls1=aa1*(xx-px1)+ph1;
bb1=IIf(tx0>px1 AND tx1<px1,1,0);
bb1=bb1+Ref(bb1,-1) AND tl0 < ph1;
bb1=IIf(bb1,1,0) AND H > Ref( tl0,-1);
ls2=IIf(bb1,ls1,Null);
Plot(ls2,"",colorOrange,styleLine);
}
_SECTION_END();
Now a screen capture to show the problem - this occurs when the same bar forms a fractal low and a fractal high

Below is another screen capture where in I have drawn "trend lines in white colour" to show how the trend lines should plot --

I have been trying to get this for the past few months without any luck, a solution will be much appreciated.
Thanks and regards