# Remove excess peaks and troughs

Hi everyone,

I tried to mark swing high and swing low on a chart and found a nearly perfect solution as below

But I would like to remove the peaks if there is a new peak immediately afterwards instead of a new low. The highest peak will be kept. The same goes for the troughs but the lowest trough will be kept. In general, the order will always be peak and trough respectively.

Please see the illustration below. yellow spots should be removed

Any help is appreciated!

best to stick with the original. If you want alternating pivots use ATR pivots in my opinion.

I have programmed some solutions. Best is to use a loop and remove the pivots. I have posted that code also somewhere (look for an "alternate" i posted. Also there are array solutions. Edakad posted 1 of them in 2009 in his Harmonic code. So I post that here. In my code you have to insert these 4 lines (below pk and tr)

``````pk = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
tr = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;

pk = IIf( pk, IIf( ValueWhen( pk, bi, 2 ) < ValueWhen( tr, bi ), pk, IIf( ValueWhen( pk, H, 2 ) > H, False, pk ) ), pk );
pk = IIf( pk AND ValueWhen( pk, bi, 0 ) > bi, IIf( ValueWhen( pk, bi, 0 ) < ValueWhen( tr, bi, 0 ), IIf( ValueWhen( pk, H, 0 ) >= H, False, pk ), pk ), pk );
tr = IIf( tr, IIf( ValueWhen( tr, bi, 2 ) < ValueWhen( pk, bi ), tr, IIf( ValueWhen( tr, L, 2 ) < L, False, tr ) ), tr );
tr = IIf( tr AND ValueWhen( tr, bi, 0 ) > bi , IIf( ValueWhen( tr, bi, 0 ) < ValueWhen( pk, bi, 0 ), IIf( ValueWhen( tr, L, 0 ) <= L, False, tr ), tr ), tr );

``````

but in my opinion the fractal pivots are useful but you need to use them in the pure format and realize that there is a delay. To see the delay you need to do the playback in Amibroker. Once you start removing pivots things really start getting complicated. You see what I mean when you do a playback.

So best to use ATR pivots if you want your pivots to alternate.

These pivots are usually used in hindsight. So to find a level of support or resistance. They are not intended to find a real time bottom or top because that unfortunately only is available for a select few

3 Likes

you are wonderful. Your solution is very simple and concise. Thanks for your comments. I'm not actually trying to use it to identify real-time tops and bottoms, but to simply identify trends. the overabundance of peaks and troughs makes it difficult for me to focus on real peaks and troughs

It's so much better now

I tried to modify so that higher time frame peaks and troughs could appear in the same screen. The result was not really what I expected. Below is the code I added. Can you help me see what's wrong with it?

``````//timeframe 2
Compress2= Param("Compression",4,2,4,1);
TimeFrameSet(Compress2* Interval());

pk2 = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
tr2 = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;

pk2 = IIf( pk2, IIf( ValueWhen( pk2, bi, 2 ) < ValueWhen( tr, bi ), pk2, IIf( ValueWhen( pk2, H, 2 ) > H, False, pk2 ) ), pk2 );
pk2 = IIf( pk2 AND ValueWhen( pk2, bi, 0 ) > bi, IIf( ValueWhen( pk2, bi, 0 ) < ValueWhen( tr2, bi, 0 ), IIf( ValueWhen( pk2, H, 0 ) >= H, False, pk2 ), pk2 ), pk2 );
tr2 = IIf( tr2, IIf( ValueWhen( tr2, bi, 2 ) < ValueWhen( pk2, bi ), tr2, IIf( ValueWhen( tr2, L, 2 ) < L, False, tr2 ) ), tr2 );
tr2 = IIf( tr2 AND ValueWhen( tr2, bi, 0 ) > bi , IIf( ValueWhen( tr2, bi, 0 ) < ValueWhen( pk2, bi, 0 ), IIf( ValueWhen( tr2, L, 0 ) <= L, False, tr2 ), tr2 ), tr2 );

for( i = 0; i < 3; i++ )
{
VarSet( "px2" + i, ValueWhen( pk2, bi, i ) );
VarSet( "tx2" + i, ValueWhen( tr2, bi, i ) );
VarSet( "ph2" + i, ValueWhen( pk2, H, i ) );
VarSet( "tl2" + i, ValueWhen( tr2, L, i ) );
}

ll2 = tr2 AND tl21 < tl22;
hl2 = tr2 AND tl21 > tl22;
hh2 = pk2 AND ph21 > ph22;
lh2 = pk2 AND ph21 < ph22;
dt2 = pk2 AND ph21 == ph22;
db2 = tr2 AND tl21 == tl22;
TimeFrameRestore();
ll2f = TimeFrameExpand( ll2, Compress2* Interval());
hl2f = TimeFrameExpand( hl2, Compress2* Interval());
hh2f = TimeFrameExpand( hh2, Compress2* Interval());
lh2f = TimeFrameExpand( lh2, Compress2* Interval());
dt2f = TimeFrameExpand( dt2, Compress2* Interval());
db2f = TimeFrameExpand( db2, Compress2* Interval());

for( i = lvb; i > fvb; i-- )
{
// troughs
if( ll2f[i] )
{
str = "" + WriteVal(L[i],8.2);
PlotTextSetFont( str, "Arial Black", 8, i, L[i], ColorRGB( 0, 250, 0 ), colorDefault, -60 );
}

if( hl2f[i] )
{
str = "" + WriteVal(L[i],8.2);
PlotTextSetFont( str, "Arial Black", 8, i, L[i], ColorRGB( 0, 250, 0 ), colorDefault, -60 );
}

if( db2f[i] )
{
str = "DB";
PlotTextSetFont( str, "Arial Black", 8, i, L[i], ColorRGB( 0, 250, 0 ), colorDefault, -60 );
}

//peaks
if( hh2f[i] )
{
str = ""+WriteVal(H[i],8.2);
PlotTextSetFont( str, "Arial Black", 8, i, H[i], ColorRGB( 250, 0, 0 ), colorDefault, 60 );
}

if( lh2f[i] )
{
str = "" +WriteVal(H[i],8.2);
PlotTextSetFont( str, "Arial Black", 8, i, H[i], ColorRGB( 250, 0, 0 ), colorDefault, 60 );
}

if( dt2f[i] )
{
str = "DT";
PlotTextSetFont( str, "Arial Black", 8, i, H[i], ColorRGB( 250, 0, 0 ), colorDefault, 60 );
}

}
``````

Above is the original code, below is the modified code. the original is perfect, but the revision is buggy causing the 4 peaks and troughs values to appear in the same place

hi, the code you posted is not complete. So I am not sure

sorry for the unclear. Here is the full code

``````// Fractal Pivots, E.M.Pottasch 2/2018
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

rightstrength = Param( "Right Strength", 10, 2, 50, 1 );
leftstrength = Param( "Left Strength", 10, 2, 50, 1 );
fact = Param( "Chart Time Frame Factor", 1, 1, 10, 1 );

rightStrength = rightStrength * fact;
leftStrength = leftStrength * fact;

pk = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
tr = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;
pkl = H == HHV( H, leftstrength );
trl = L == LLV( L, leftstrength );

pk = IIf( pk, IIf( ValueWhen( pk, bi, 2 ) < ValueWhen( tr, bi ), pk, IIf( ValueWhen( pk, H, 2 ) > H, False, pk ) ), pk );
pk = IIf( pk AND ValueWhen( pk, bi, 0 ) > bi, IIf( ValueWhen( pk, bi, 0 ) < ValueWhen( tr, bi, 0 ), IIf( ValueWhen( pk, H, 0 ) >= H, False, pk ), pk ), pk );
tr = IIf( tr, IIf( ValueWhen( tr, bi, 2 ) < ValueWhen( pk, bi ), tr, IIf( ValueWhen( tr, L, 2 ) < L, False, tr ) ), tr );
tr = IIf( tr AND ValueWhen( tr, bi, 0 ) > bi , IIf( ValueWhen( tr, bi, 0 ) < ValueWhen( pk, bi, 0 ), IIf( ValueWhen( tr, L, 0 ) <= L, False, tr ), tr ), tr );

for( i = 0; i < 3; i++ )
{
VarSet( "px" + i, ValueWhen( pk, bi, i ) );
VarSet( "tx" + i, ValueWhen( tr, bi, i ) );
VarSet( "ph" + i, ValueWhen( pk, H, i ) );
VarSet( "tl" + i, ValueWhen( tr, L, i ) );
}

ll = tr AND tl1 < tl2;
hl = tr AND tl1 > tl2;
hh = pk AND ph1 > ph2;
lh = pk AND ph1 < ph2;
dt = pk AND ph1 == ph2;
db = tr AND tl1 == tl2;

dup = (ph1 - tl1)/tl1*100;
ddn = (tl1 - ph1)/ph1*100;

ft = "Helvetica";
clr = ColorRGB( 10, 10, 10 );//colorDefault;
GfxSetZOrder( 0 );
GfxSetCoordsMode( 1 );
sz = Param( "Font Size", 8, 4, 16, 1 );

upColor = ColorRGB( 0, 250, 250 );
dnColor = ColorRGB( 250, 250, 0 );

GraphXSpace = 5;
SetChartBkColor( colorBlack );
SetChartOptions( 1, chartShowDates, chartGridMiddle, 0, 0, 0 );
SetBarFillColor( IIf( C > O, ColorRGB( 0, 75, 0 ), IIf( C <= O, ColorRGB( 75, 0, 0 ), colorLightGrey ) ) );
Plot( C, "", IIf( C > O, ColorRGB( 0, 255, 0 ), IIf( C <= O, ColorRGB( 255, 0, 0 ), colorLightGrey ) ), 64, Null, Null, 0, 0, 1 );

PlotShapes( shapeSmallCircle * tr, ColorRGB( 0, 0, 255 ), 0, L, -10 );
PlotShapes( shapeSmallCircle * pk, ColorRGB( 255, 0, 0 ), 0, H, 10 );
PlotShapes( shapeSmallCircle * trl, ColorRGB( 80, 0, 0 ), 0, L, -10 );
PlotShapes( shapeSmallCircle * pkl, ColorRGB( 0, 80, 0 ), 0, H, 10 );

for( i = lvb; i > fvb; i-- )
{
// troughs
if( ll[i] )
{
str = "" + WriteVal(L[i],8.2);
PlotTextSetFont( str, "Arial Black", 8, i, L[i], ColorRGB( 0, 250, 0 ), colorDefault, -30 );
}

if( hl[i] )
{
str = "" + WriteVal(L[i],8.2);
PlotTextSetFont( str, "Arial Black", 8, i, L[i], ColorRGB( 0, 250, 0 ), colorDefault, -30 );
}

if( db[i] )
{
str = "DB";
PlotTextSetFont( str, "Arial Black", 8, i, L[i], ColorRGB( 0, 250, 0 ), colorDefault, -30 );
}

//peaks
if( hh[i] )
{
str = ""+WriteVal(H[i],8.2);
PlotTextSetFont( str, "Arial Black", 8, i, H[i], ColorRGB( 250, 0, 0 ), colorDefault, 20 );
}

if( lh[i] )
{
str = "" +WriteVal(H[i],8.2);
PlotTextSetFont( str, "Arial Black", 8, i, H[i], ColorRGB( 250, 0, 0 ), colorDefault, 20 );
}

if( dt[i] )
{
str = "DT";
PlotTextSetFont( str, "Arial Black", 8, i, H[i], ColorRGB( 250, 0, 0 ), colorDefault, 20 );
}
if( tr[i] )
{
GfxSelectPen( dnColor, 1 );
GfxMoveTo( tx1[i], tl1[i] );
//GfxLineTo( px1[i], ph1[i] );
PlotTextSetFont( "" + Prec( ddn[i], 2 )+"%", ft, sz, i, L[i], colorBlack, colorGreen, -60 );
}

if( pk[i] )
{
GfxSelectPen( upColor, 1 );
GfxMoveTo( px1[i], ph1[i] );
//GfxLineTo( tx1[i], tl1[i] );
PlotTextSetFont( "" + Prec( dup[i], 2 )+"%", ft, sz, i, H[i], ColorBlack, colorPink, 60 );
}
}

_N(Title =  Name() +  " - "+MarketID(1)+ " - "+ StrFormat(" {{DATE}} ")+" - "+ " O "
+ EncodeColor(colorGold)+ WriteVal(O,1.2) + EncodeColor(colorWhite)+ " ,H "+ EncodeColor(colorViolet)+WriteVal(H,1.2) + EncodeColor(colorWhite)+" ,L "+EncodeColor(colorRed)+WriteVal(L,1.2) + EncodeColor(colorWhite)+" ,C "+ EncodeColor(colorGreen)+WriteVal(C,1.2) +" ("+ WriteVal( ROC( C, 1),1.1)+"%)  " + StrFormat(" {{VALUES}}")+ EncodeColor(colorGreen) );

ShowHTKC = ParamToggle("KCHT","Yes|No",1);
if(ShowHTKC)
{
Plot( tl1, "", colorRed, styleLine | styleNoRescale |styleDashed, Null, Null, 0, 0, 1 );
Plot( ph1, "", colorGreen, styleLine | styleNoRescale | styleDashed, Null, Null, 0, 0, 1 );
}
SetTradeDelays( 0, 0, 0, 0 );
SetOption( "CommissionMode", 3 );
SetOption( "CommissionAmount", 3.5 );
SetOption( "FuturesMode", True );
NumContracts = 1;
PositionSize = NumContracts * MarginDeposit;
SetOption( "MaxOpenPositions", 1 );

Buy = Cross( C, Ref( ph1, -1 ) );
BuyPrice = Max( O, Ref( ph1, -1 ) );
Short = Cross( Ref( tl1, -1 ), C );
ShortPrice = Min( O, Ref( tl1, -1 ) );
Short = ExRem( Short, Buy );
Sell = Short;
SellPrice = ShortPrice;

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

//timeframe 2
Compress2= Param("Compression",4,2,4,1);
TimeFrameSet(Compress2* Interval());

pk2 = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
tr2 = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;

pk2 = IIf( pk2, IIf( ValueWhen( pk2, bi, 2 ) < ValueWhen( tr, bi ), pk2, IIf( ValueWhen( pk2, H, 2 ) > H, False, pk2 ) ), pk2 );
pk2 = IIf( pk2 AND ValueWhen( pk2, bi, 0 ) > bi, IIf( ValueWhen( pk2, bi, 0 ) < ValueWhen( tr2, bi, 0 ), IIf( ValueWhen( pk2, H, 0 ) >= H, False, pk2 ), pk2 ), pk2 );
tr2 = IIf( tr2, IIf( ValueWhen( tr2, bi, 2 ) < ValueWhen( pk2, bi ), tr2, IIf( ValueWhen( tr2, L, 2 ) < L, False, tr2 ) ), tr2 );
tr2 = IIf( tr2 AND ValueWhen( tr2, bi, 0 ) > bi , IIf( ValueWhen( tr2, bi, 0 ) < ValueWhen( pk2, bi, 0 ), IIf( ValueWhen( tr2, L, 0 ) <= L, False, tr2 ), tr2 ), tr2 );

for( i = 0; i < 3; i++ )
{
VarSet( "px2" + i, ValueWhen( pk2, bi, i ) );
VarSet( "tx2" + i, ValueWhen( tr2, bi, i ) );
VarSet( "ph2" + i, ValueWhen( pk2, H, i ) );
VarSet( "tl2" + i, ValueWhen( tr2, L, i ) );
}

ll2 = tr2 AND tl21 < tl22;
hl2 = tr2 AND tl21 > tl22;
hh2 = pk2 AND ph21 > ph22;
lh2 = pk2 AND ph21 < ph22;
dt2 = pk2 AND ph21 == ph22;
db2 = tr2 AND tl21 == tl22;
TimeFrameRestore();
ll2f = TimeFrameExpand( ll2, Compress2* Interval());
hl2f = TimeFrameExpand( hl2, Compress2* Interval());
hh2f = TimeFrameExpand( hh2, Compress2* Interval());
lh2f = TimeFrameExpand( lh2, Compress2* Interval());
dt2f = TimeFrameExpand( dt2, Compress2* Interval());
db2f = TimeFrameExpand( db2, Compress2* Interval());

for( i = lvb; i > fvb; i-- )
{
// troughs
if( ll2f[i] )
{
str = "" + WriteVal(L[i],8.2);
PlotTextSetFont( str, "Arial Black", 8, i, L[i], ColorRGB( 0, 250, 0 ), colorDefault, -60 );
}

if( hl2f[i] )
{
str = "" + WriteVal(L[i],8.2);
PlotTextSetFont( str, "Arial Black", 8, i, L[i], ColorRGB( 0, 250, 0 ), colorDefault, -60 );
}

if( db2f[i] )
{
str = "DB";
PlotTextSetFont( str, "Arial Black", 8, i, L[i], ColorRGB( 0, 250, 0 ), colorDefault, -60 );
}

//peaks
if( hh2f[i] )
{
str = ""+WriteVal(H[i],8.2);
PlotTextSetFont( str, "Arial Black", 8, i, H[i], ColorRGB( 250, 0, 0 ), colorDefault, 60 );
}

if( lh2f[i] )
{
str = "" +WriteVal(H[i],8.2);
PlotTextSetFont( str, "Arial Black", 8, i, H[i], ColorRGB( 250, 0, 0 ), colorDefault, 60 );
}

if( dt2f[i] )
{
str = "DT";
PlotTextSetFont( str, "Arial Black", 8, i, H[i], ColorRGB( 250, 0, 0 ), colorDefault, 60 );
}

}

``````

this is normal for compress/expand. You calculate the pk/tr in a compressed state. Then when you expand this pk/tr is divided over multiple bars. In such cases it is best to simplify the code and look what is going on see example below:

``````bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

rightstrength = Param( "Right Strength", 10, 2, 50, 1 );
leftstrength = Param( "Left Strength", 10, 2, 50, 1 );
fact = Param( "Chart Time Frame Factor", 1, 1, 10, 1 );

rightStrength = rightStrength * fact;
leftStrength = leftStrength * fact;

pk = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;

GraphXSpace = 5;
SetChartBkColor( colorBlack );
SetChartOptions( 1, chartShowDates, chartGridMiddle, 0, 0, 0 );
SetBarFillColor( IIf( C > O, ColorRGB( 0, 75, 0 ), IIf( C <= O, ColorRGB( 75, 0, 0 ), colorLightGrey ) ) );
Plot( C, "", IIf( C > O, ColorRGB( 0, 255, 0 ), IIf( C <= O, ColorRGB( 255, 0, 0 ), colorLightGrey ) ), 64, Null, Null, 0, 0, 1 );

PlotShapes( shapeSmallCircle * pk, ColorRGB( 255, 0, 0 ), 0, H, 10 );

//timeframe 2
Compress2 = Param( "Compression", 4, 2, 4, 1 );
TimeFrameSet( Compress2* Interval() );
pk2 = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
TimeFrameRestore();

pk2 = TimeFrameExpand( pk2, Compress2* Interval(), expandLast );
PlotShapes( shapeCircle * pk2, ColorRGB( 255, 255, 0 ), 0, H, 10 );
``````
1 Like

now i understand why it has so many highs and lows. Basically, the timeframeexpand just determined the peaks or troughs of higher timeframe and every candle of lower timeframe belong to that peaks or troughs is marked on the chart. But I just want the highest and lowest price. Is there any way to do that?

yes that is possible. You have to also compress and expand the High and Low, see code below how to do this. I just posted the timeframe 2 part

``````//timeframe 2
Compress2 = Param( "Compression", 4, 2, 4, 1 );
TimeFrameSet( Compress2* Interval() );

pk2 = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
tr2 = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;

pk2 = IIf( pk2, IIf( ValueWhen( pk2, bi, 2 ) < ValueWhen( tr, bi ), pk2, IIf( ValueWhen( pk2, H, 2 ) > H, False, pk2 ) ), pk2 );
pk2 = IIf( pk2 AND ValueWhen( pk2, bi, 0 ) > bi, IIf( ValueWhen( pk2, bi, 0 ) < ValueWhen( tr2, bi, 0 ), IIf( ValueWhen( pk2, H, 0 ) >= H, False, pk2 ), pk2 ), pk2 );
tr2 = IIf( tr2, IIf( ValueWhen( tr2, bi, 2 ) < ValueWhen( pk2, bi ), tr2, IIf( ValueWhen( tr2, L, 2 ) < L, False, tr2 ) ), tr2 );
tr2 = IIf( tr2 AND ValueWhen( tr2, bi, 0 ) > bi , IIf( ValueWhen( tr2, bi, 0 ) < ValueWhen( pk2, bi, 0 ), IIf( ValueWhen( tr2, L, 0 ) <= L, False, tr2 ), tr2 ), tr2 );

for( i = 0; i < 3; i++ )
{
VarSet( "px2" + i, ValueWhen( pk2, bi, i ) );
VarSet( "tx2" + i, ValueWhen( tr2, bi, i ) );
VarSet( "ph2" + i, ValueWhen( pk2, H, i ) );
VarSet( "tl2" + i, ValueWhen( tr2, L, i ) );
}

ll2 = tr2 AND tl21 < tl22;
hl2 = tr2 AND tl21 > tl22;
hh2 = pk2 AND ph21 > ph22;
lh2 = pk2 AND ph21 < ph22;
dt2 = pk2 AND ph21 == ph22;
db2 = tr2 AND tl21 == tl22;

H2 = H;
L2 = L;
TimeFrameRestore();
exp1 = expandFirst;
tr2 = TimeFrameExpand( tr2, Compress2* Interval(), exp1 );
pk2 = TimeFrameExpand( pk2, Compress2* Interval(), exp1 );
H2 = TimeFrameExpand( H2, Compress2* Interval(), exp1 );
L2 = TimeFrameExpand( L2, Compress2* Interval(), exp1 );

Plot( H2, "", colorLightBlue, styleLine, Null, Null, 0, 0, 1 );
Plot( L2, "", colorGold, styleLine, Null, Null, 0, 0, 1 );
tr2 = IIf( tr2 AND L2 == L, tr2, 0 );
pk2 = IIf( pk2 AND H2 == H, pk2, 0 );
pk2 = ExRem( pk2, tr2 );
tr2 = ExRem( tr2, pk2 );
ll2f = TimeFrameExpand( ll2, Compress2* Interval(), exp1 ) AND tr2;
hl2f = TimeFrameExpand( hl2, Compress2* Interval(), exp1 ) AND tr2;
hh2f = TimeFrameExpand( hh2, Compress2* Interval(), exp1 ) AND pk2;
lh2f = TimeFrameExpand( lh2, Compress2* Interval(), exp1 ) AND pk2;
dt2f = TimeFrameExpand( dt2, Compress2* Interval(), exp1 ) AND pk2;
db2f = TimeFrameExpand( db2, Compress2* Interval(), exp1 ) AND tr2;
PlotShapes( IIf( tr2, shapeSmallCircle, Null ), ColorRGB( 0, 0, 255 ), 0, L, -10 );
PlotShapes( IIf( pk2, shapeSmallCircle, Null ), ColorRGB( 255, 0, 0 ), 0, H, 10 );
``````
1 Like

or better take some stuff outside of the compression

``````//timeframe 2
Compress2 = Param( "Compression", 4, 2, 4, 1 );
TimeFrameSet( Compress2* Interval() );
pk2 = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
tr2 = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;
pk2 = IIf( pk2, IIf( ValueWhen( pk2, bi, 2 ) < ValueWhen( tr, bi ), pk2, IIf( ValueWhen( pk2, H, 2 ) > H, False, pk2 ) ), pk2 );
pk2 = IIf( pk2 AND ValueWhen( pk2, bi, 0 ) > bi, IIf( ValueWhen( pk2, bi, 0 ) < ValueWhen( tr2, bi, 0 ), IIf( ValueWhen( pk2, H, 0 ) >= H, False, pk2 ), pk2 ), pk2 );
tr2 = IIf( tr2, IIf( ValueWhen( tr2, bi, 2 ) < ValueWhen( pk2, bi ), tr2, IIf( ValueWhen( tr2, L, 2 ) < L, False, tr2 ) ), tr2 );
tr2 = IIf( tr2 AND ValueWhen( tr2, bi, 0 ) > bi , IIf( ValueWhen( tr2, bi, 0 ) < ValueWhen( pk2, bi, 0 ), IIf( ValueWhen( tr2, L, 0 ) <= L, False, tr2 ), tr2 ), tr2 );
H2 = H;
L2 = L;
TimeFrameRestore();

exp1 = expandFirst;
tr2 = TimeFrameExpand( tr2, Compress2* Interval(), exp1 );
pk2 = TimeFrameExpand( pk2, Compress2* Interval(), exp1 );
H2 = TimeFrameExpand( H2, Compress2* Interval(), exp1 );
L2 = TimeFrameExpand( L2, Compress2* Interval(), exp1 );

Plot( H2, "", colorLightBlue, styleLine, Null, Null, 0, 0, 1 );
Plot( L2, "", colorGold, styleLine, Null, Null, 0, 0, 1 );
tr2 = IIf( tr2 AND L2 == L, tr2, 0 );
pk2 = IIf( pk2 AND H2 == H, pk2, 0 );
pk2 = ExRem( pk2, tr2 );
tr2 = ExRem( tr2, pk2 );
PlotShapes( IIf( tr2, shapeSmallCircle, Null ), ColorRGB( 0, 0, 255 ), 0, L, -10 );
PlotShapes( IIf( pk2, shapeSmallCircle, Null ), ColorRGB( 255, 0, 0 ), 0, H, 10 );

for( i = 0; i < 3; i++ )
{
VarSet( "px2" + i, ValueWhen( pk2, bi, i ) );
VarSet( "tx2" + i, ValueWhen( tr2, bi, i ) );
VarSet( "ph2" + i, ValueWhen( pk2, H, i ) );
VarSet( "tl2" + i, ValueWhen( tr2, L, i ) );
}

ll2f = tr2 AND tl21 < tl22;
hl2f = tr2 AND tl21 > tl22;
hh2f = pk2 AND ph21 > ph22;
lh2f = pk2 AND ph21 < ph22;
dt2f = pk2 AND ph21 == ph22;
db2f = tr2 AND tl21 == tl22;
``````
1 Like

also best to use

SetBarsRequired( sbrAll, sbrAll );

on top of your code. If you are using 15min charts that will not be too slow. Else you will have these pivots jumping around a lot. As I already explained I do no recommend using them like this

1 Like

again I have learned a lot from your code. Thank you very much for your help. I will continue to develop more ideas based on these codes

Hi @empottasch ,

i just found out there is a small bug here, that new low is repeated, it only happens when new high is undefined. I don't know why but in your other version below it's perfect. However, it is quite old and more complicated code. Could you please help me to fix that bug.

``````_SECTION_BEGIN("Trendlines Multitimeframe");
// Amibroker AFL code by Edward Pottasch, 6/8/2011
// Using fractals to create automatic trendlines
// Using option to show multiple timeframes
Version(5.21);
xx=BarIndex();x=xx;Lx=LastValue(x);
nbar=Param("N Pivot Bars",3,2,50,1);
tf=Param("Time Frame (min)",5,1,100000,1);tfrm=in1Minute*tf;
CleanPivots=ParamToggle("Use Clean Pivots","Off|On",0);
PivotSymmetry=ParamToggle("Use Symmetric Pivots","Off|On",0);
tld=ParamToggle("All trendlines","Show|Hide",1);
showSignals=ParamToggle("Display signals","Off|On",0);

TimeFrameSet(tfrm);
if (PivotSymmetry)
{
fc=1;
pk=H>Ref(HHV(H,nbar*fc),-1) AND Ref(HHV(H,nbar),nbar)<=H;
tr=L<Ref(LLV(L,nbar*fc),-1) AND Ref(LLV(L,nbar),nbar)>=L;
}
else
{
fc=2;
pk=H>Ref(HHV(H,nbar*fc),-1) AND Ref(HHV(H,nbar),nbar)<=H;
tr=L<Ref(LLV(L,nbar*fc),-1) AND Ref(LLV(L,nbar),nbar)>=L;
}
px0=ValueWhen(pk,x,0); tx0=ValueWhen(tr,x,0);
px1=ValueWhen(pk,x,1); tx1=ValueWhen(tr,x,1);
px2=ValueWhen(pk,x,2); tx2=ValueWhen(tr,x,2);
ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);
if (CleanPivots)
{
tr=IIf(pk AND tr AND ph1>ph2 AND tl1>tl2,False,tr);
pk=IIf(pk AND tr AND ph1<ph2 AND tl1<tl2,False,pk);

px0=ValueWhen(pk,x,0); tx0=ValueWhen(tr,x,0);
px1=ValueWhen(pk,x,1); tx1=ValueWhen(tr,x,1);
px2=ValueWhen(pk,x,2); tx2=ValueWhen(tr,x,2);
ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);

pk=IIf(pk AND px1>tx1 AND ph1<tl1,False,pk);
tr=IIf(tr AND tx1>px1 AND tl1>ph1,False,tr);
pk=IIf(pk AND px0<tx0 AND ph0>ph1,False,pk);
tr=IIf(tr AND px0>tx0 AND tl0<tl1,False,tr);
pk=IIf(pk AND px2>tx1 AND ph1<=ph2,False,pk);
tr=IIf(tr AND tx2>px1 AND tl1>=tl2,False,tr);
pk=IIf(pk AND px0>px1 AND px1>tx1 AND px1>tx0 AND ph0>ph1,False,pk);
tr=IIf(tr AND tx0>tx1 AND tx1>px1 AND tx1>px0 AND tl0<tl1,False,tr);

px0=ValueWhen(pk,x,0); tx0=ValueWhen(tr,x,0);
px1=ValueWhen(pk,x,1); tx1=ValueWhen(tr,x,1);
px2=ValueWhen(pk,x,2); tx2=ValueWhen(tr,x,2);
ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);

pk=IIf(pk AND px1>tx1 AND ph1<tl1,False,pk);
tr=IIf(tr AND tx1>px1 AND tl1>ph1,False,tr);
pk=IIf(pk AND px0<tx0 AND ph0>ph1,False,pk);
tr=IIf(tr AND px0>tx0 AND tl0<tl1,False,tr);
pk=IIf(pk AND px2>tx1 AND ph1<=ph2,False,pk);
tr=IIf(tr AND tx2>px1 AND tl1>=tl2,False,tr);
pk=IIf(pk AND px0>px1 AND px1>tx1 AND px1>tx0 AND ph0>ph1,False,pk);
tr=IIf(tr AND tx0>tx1 AND tx1>px1 AND tx1>px0 AND tl0<tl1,False,tr);

px0=ValueWhen(pk,x,0); tx0=ValueWhen(tr,x,0);
px1=ValueWhen(pk,x,1); tx1=ValueWhen(tr,x,1);
px2=ValueWhen(pk,x,2); tx2=ValueWhen(tr,x,2);
ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);

pk=IIf(pk AND px1>tx1 AND px2>tx1 AND ph1<ph2,False,pk);
tr=IIf(tr AND tx1>px1 AND tx2>px1 AND tl1>tl2,False,tr);
}
pkh=IIf(pk,H,Null);
trl=IIf(tr,L,Null);
TimeFrameRestore();
fact=Max(tfrm/60,Interval()/60)/(Interval()/60);
Lkbk=tfrm/Interval();
if(Lkbk>1)
{
pk=TimeFrameExpand(pk,tfrm,expandFirst);
pkh=TimeFrameExpand(pkh,tfrm,expandFirst);
pkhs=IIf(!IsEmpty(pkh),1,0);pkhs=pkhs-Ref(pkhs,-1);
pk=pk AND H==pkh;
pk=pk AND cond1;

tr=TimeFrameExpand(tr,tfrm,expandFirst);
trl=TimeFrameExpand(trl,tfrm,expandFirst);
trls=IIf(!IsEmpty(trl),1,0);trls=trls-Ref(trls,-1);
tr=tr AND L==trl;
tr=tr AND cond1;

px0=ValueWhen(pk,x,0); tx0=ValueWhen(tr,x,0);
px1=ValueWhen(pk,x,1); tx1=ValueWhen(tr,x,1);
px2=ValueWhen(pk,x,2); tx2=ValueWhen(tr,x,2);
ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);
}
y0=ValueWhen(tr,trl,0);
y1=ValueWhen(tr,trl,1);
y2=ValueWhen(tr,trl,2);
y3=ValueWhen(tr,trl,3);
x0=ValueWhen(tr,xx,0);
x1=ValueWhen(tr,xx,1);
x2=ValueWhen(tr,xx,2);
x3=ValueWhen(tr,xx,3);

aa=(Y0-Y1)/(X0-X1);
ls1=aa*(xx-X1)+Y1;
dls1=ls1-Ref(ls1,-1);
aa=(Y1-Y2)/(X1-X2);
ls2=aa*(xx-X1)+Y1;
dls2=ls2-Ref(ls2,-1);
dls2=IIf(tr,Ref(dls1,-1),dls2);
aa=(Y2-Y3)/(X2-X3);
ls3=aa*(xx-X2)+Y2;
dls3=ls3-Ref(ls3,-1);
dls3=IIf(tr,Ref(dls2,-1),dls3);
y0=ValueWhen(pk,pkh,0);
y1=ValueWhen(pk,pkh,1);
y2=ValueWhen(pk,pkh,2);
y3=ValueWhen(pk,pkh,3);
x0=ValueWhen(pk,xx,0);
x1=ValueWhen(pk,xx,1);
x2=ValueWhen(pk,xx,2);
x3=ValueWhen(pk,xx,3);
aa=(Y0-Y1)/(X0-X1);
hs1=aa*(xx-X1)+Y1;
dhs1=hs1-Ref(hs1,-1);
aa=(Y1-Y2)/(X1-X2);
hs2=aa*(xx-X1)+Y1;
dhs2=hs2-Ref(hs2,-1);
dhs2=IIf(pk,Ref(dhs1,-1),dhs2);
aa=(Y2-Y3)/(X2-X3);
hs3=aa*(xx-X2)+Y2;
dhs3=hs3-Ref(hs3,-1);
dhs3=IIf(pk,Ref(dhs2,-1),dhs3);
if (tld)
{
dd=0.0;
Vh=dd*(ValueWhen(pk,C)-ValueWhen(pk,Ref(C,-1)))/ValueWhen(pk,C);
Vl=dd*(ValueWhen(tr,C)-ValueWhen(tr,Ref(C,-1)))/ValueWhen(tr,C);
ls1=IIf(dls1>=Vl,ls1,Null);
ls2=IIf(dls2>=Vl,ls2,Null);
ls3=IIf(dls3>=Vl,ls3,Null);
hs1=IIf(dhs1<=Vh,hs1,Null);
hs2=IIf(dhs2<=Vh,hs2,Null);
hs3=IIf(dhs3<=Vh,hs3,Null);
}
_SECTION_END();

_SECTION_BEGIN("Chart Colors");
SetChartBkColor(ParamColor("Background Color",ColorRGB(0,0,0)));
SetChartOptions(0,chartShowDates);
SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color",
colorGreen),IIf(C<=O,ParamColor("Candle Down Color",
colorRed),colorLightGrey)));
Plot(C,"Price",IIf(C>O,ParamColor("Wick UP Color",
colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color",
colorDarkRed),colorLightGrey)),64,0,0,0,0);

Plot(pkh,"",colorWhite,styleThick,0,0,0,-1);
Plot(trl,"",colorBrightGreen,styleThick,0,0,0,-1);
PlotShapes(shapeSmallCircle*tr,IIf(Lx-ValueWhen(tr,x)>nbar*fact,ColorRGB(0,100,0),colorWhite),0,L,-10);
PlotShapes(shapeSmallCircle*pk,IIf(Lx-ValueWhen(pk,x)>nbar*fact,ColorRGB(255,0,0),colorWhite),0,H,10);
Plot(ls1,"\nLower Trendline",colorBrightGreen,styleLine,0,0,0,-1);
Plot(ls2a,"",colorLightGrey,styleNoLine|styleDots|styleThick,0,0,0,-1);
Plot(ls2b,"",colorDarkGreen,styleNoLine|styleDots|styleThick,0,0,0,-1);
Plot(ls3,"",colorDarkGreen,styleNoLine|styleDots|styleThick,0,0,0,-1);
Plot(hs1,"\nUpper Trendline",colorRed,styleLine,0,0,0,-1);
Plot(hs2a,"",colorLightGrey,styleNoLine|styleDots|styleThick,0,0,0,-1);
Plot(hs2b,"",colorOrange,styleNoLine|styleDots|styleThick,0,0,0,-1);
Plot(hs3,"",colorOrange,styleNoLine|styleDots|styleThick,0,0,0,-1);
_SECTION_END();

if(showSignals)
{
Buy=( (!IsEmpty(Ref(hs2b,-1)) AND Ref(C<hs2b,-1)) AND (!IsEmpty(hs2b) AND
C>hs2b) )
OR ( (!IsEmpty(Ref(hs3,-1)) AND Ref(C<hs3,-1)) AND (!IsEmpty(hs3) AND C>hs3) )
OR ( (!IsEmpty(Ref(hs2b,-1)) AND Ref(C<hs2b,-1)) AND (!IsEmpty(hs3) AND C>hs3)
);

Short=( (!IsEmpty(Ref(ls2b,-1)) AND Ref(C>ls2b,-1)) AND (!IsEmpty(ls2b) AND
C<ls2b) )
OR ( (!IsEmpty(Ref(ls3,-1)) AND Ref(C>ls3,-1)) AND (!IsEmpty(ls3) AND C<ls3) )
OR ( (!IsEmpty(Ref(ls2b,-1)) AND Ref(C>ls2b,-1)) AND (!IsEmpty(ls3) AND C<ls3)
);
Short=ExRem(Short,tr OR pk);
ShortPrice=C;
PlotShapes(IIf(Short,shapeSmallDownTriangle,shapeNone),colorRed,0,H,-15);
PlotShapes(IIf(Short,shapeSmallCircle,shapeNone),colorWhite,0,ShortPrice,0);
}

qq=Interval()/60;
if(qq < 60){tf=" min";tt=qq;}
else if(qq >= 60 AND qq < 1440){tf=" hrs";tt=qq/60;}
else if(qq >= 1440){tf=" days";tt=(qq/60)/24;}
qq=Max(tfrm/60,Interval()/60);
if(qq < 60){tfa=" min";tta=qq;}
else if(qq >= 60 AND qq < 1440){tfa=" hrs";tta=qq/60;}
else if(qq >= 1440){tfa=" days";tta=(qq/60)/24;}

Title = Name() +
"\nNbar: " + nbar +
"\nChart TF: " + tt + tf +
"\nTrend TF: " + tta + tfa;
_SECTION_END();

_SECTION_BEGIN("Label");
disp=ParamToggle("Display labels","Off|On",1);
dxhm=Param("Shift X Highs (Margin)",14,-100,100,1);
dxlm=Param("Shift X Lows (Margin)",10,-100,100,1);
dxh=Param("Shift X Highs",0,-100,100,1);
dxl=Param("Shift X Lows",0,-100,100,1);
dyhm=Param("Shift Y Highs (Margin)",5,-100,100,1);
dylm=Param("Shift Y Lows (Margin)",3,-100,100,1);
dyh=Param("Shift Y Highs",18,-100,100,1);
dyl=Param("Shift Y Lows",29,-100,100,1);
hm=Param("Marging High",30,-100,100,1);
lm=Param("Margin Low",30,-100,100,1);

function GetVisibleBarCount()
{
lvb=Status("lastvisiblebar");
fvb=Status("firstvisiblebar");
return Min(lvb-fvb,BarCount-fvb);
}
function GfxConvertPixelsToBarX(Pixels)
{
lvb=Status("lastvisiblebar");
fvb=Status("firstvisiblebar");
pxchartleft=Status("pxchartleft");
pxchartwidth=Status("pxchartwidth");
fac=pxchartwidth/Pixels;
bar=(lvb-fvb)/fac;
return bar;
}
function GfxConvertPixelToValueY(Pixels)
{
local Miny,Maxy,pxchartbottom,pxchartheight;
Miny=Status("axisminy");
Maxy=Status("axismaxy");
pxchartbottom=Status("pxchartbottom");
pxchartheight=Status("pxchartheight");
fac=pxchartheight/Pixels;
Value=(Maxy-Miny)/fac;
return Value;
}
if(disp)
{
ll=tr AND tl1<tl2;
hl=tr AND tl1>tl2;
hh=pk AND ph1>ph2;
lh=pk AND ph1<ph2;
dt=pk AND ph1==ph2;
db=tr AND tl1==tl2;

PlotShapes(shapeHollowCircle*lh,ColorRGB(255,193,193),0,H,10);
PlotShapes(shapeHollowCircle*hl,ColorRGB(202,255,112),0,L,-10);

miny=Status("axisminy");
maxy=Status("axismaxy");
AllVisibleBars=GetVisibleBarCount();
fvb=Status("firstvisiblebar");
LowMargin=Miny+GfxConvertPixelToValueY(lm);
HighMargin=Maxy-GfxConvertPixelToValueY(hm);
dyllm=GfxConvertPixelToValueY(dylm);
dyhhm=GfxConvertPixelToValueY(dyhm);
dyll=GfxConvertPixelToValueY(dyl);
dyhh=GfxConvertPixelToValueY(dyh);
dxllm=GfxConvertPixelsToBarX(dxlm);
dxhhm=GfxConvertPixelsToBarX(dxhm);
dxll=GfxConvertPixelsToBarX(dxl);
dxhh=GfxConvertPixelsToBarX(dxh);

for(i=0;i<AllVisibleBars;i++)
{
if(ll[i+fvb] AND L[i+fvb]>LowMargin)
PlotText("LL",i+fvb+dxll,L[i+fvb]-dyll,colorWhite,colorBlack);
if(ll[i+fvb] AND L[i+fvb]<=LowMargin)
PlotText("LL",i+fvb+dxll+dxllm,L[i+fvb]-dyllm,colorWhite,colorBlack);
if(hl[i+fvb] AND L[i+fvb]>LowMargin)
PlotText("HL",i+fvb+dxll,L[i+fvb]-dyll,colorWhite,colorBlack);
if(hl[i+fvb] AND L[i+fvb]<=LowMargin)
PlotText("HL",i+fvb+dxll+dxllm,L[i+fvb]-dyllm,colorWhite,colorBlack);
if(db[i+fvb] AND L[i+fvb]>LowMargin)
PlotText("DB",i+fvb+dxll,L[i+fvb]-dyll,colorWhite,colorBlack);
if(db[i+fvb] AND L[i+fvb]<=LowMargin)
PlotText("DB",i+fvb+dxll+dxllm,L[i+fvb]-dyllm,colorWhite,colorBlack);
if(hh[i+fvb] AND H[i+fvb]<HighMargin)
PlotText("HH",i+fvb+dxhh,H[i+fvb]+dyhh,colorWhite,colorBlack);
if(hh[i+fvb] AND H[i+fvb]>=HighMargin)
PlotText("HH",i+fvb+dxhh+dxhhm,H[i+fvb]+dyhhm,colorWhite,colorBlack);
if(lh[i+fvb] AND H[i+fvb]<HighMargin)
PlotText("LH",i+fvb+dxhh,H[i+fvb]+dyhh,colorWhite,colorBlack);
if(lh[i+fvb] AND H[i+fvb]>=HighMargin)
PlotText("LH",i+fvb+dxhh+dxhhm,H[i+fvb]+dyhhm,colorWhite,colorBlack);
if(dt[i+fvb] AND H[i+fvb]<HighMargin)
PlotText("DT",i+fvb+dxhh,H[i+fvb]+dyhh,colorWhite,colorBlack);
if(dt[i+fvb] AND H[i+fvb]>=HighMargin)
PlotText("DT",i+fvb+dxhh+dxhhm,H[i+fvb]+dyhhm,colorWhite,colorBlack);
}
Plot(tr,"",ColorRGB(0,100,0),styleHistogram|styleDashed|styleOwnScale|styleNoLabel,0,1,0,0);
Plot(pk,"",ColorRGB(100,0,0),styleHistogram|styleDashed|styleOwnScale|styleNoLabel,0,1,0,0);

}
_SECTION_END();

``````

as I said. These fractal pivots are good but should be used in the original form. If you want pivots to "alternate", meaning pk-tr-pk-tr etc. then there are other methods to calculate the pivots like ATR, Percentage, Gann.

If you want the fractal pivots to "alternate" then there will always be some issues on the ends. Meaning, there will be pivots that will be repainted. They move to another location.

here are some attempts I made in the past but none works perfectly:

Method 1)

``````procedure alternate_proc( pk, tr, sumpk, sumtr )
{
global pkg;
global trg;

pkg = pk;
trg = tr;
idxpk = 0;
idxtr = 0;
flgtr = 0;
flgpk = 0;

for( i = 1; i < BarCount; i++ )
{
if( pk[i] AND sumpk[i] == 1 AND !tr[i] AND flgpk == 0 )
{
idxpk = i;
flgtr = 0;
}
else
if( ( pk[i] AND sumpk[i] > 1 AND !tr[i] ) OR( pk[i] AND flgpk == 1 ) )
{
if( H[i] > H[idxpk] )
{
pkg[idxpk] = 0;
idxpk = i;
flgpk = 0;
}
else
if( H[i] <= H[idxpk] )
{
pkg[i] = 0;
flgpk = 0;
}
}
else
if( tr[i] AND sumtr[i] == 1 AND !pk[i] AND flgtr == 0 )
{
idxtr = i;
flgpk = 0;
}
else
if( ( tr[i] AND sumtr[i] > 1 AND !pk[i] ) OR( tr[i] AND flgtr == 1 ) )
{
if( L[i] < L[idxtr] )
{
trg[idxtr] = 0;
idxtr = i;
flgtr = 0;
}
else
if( L[i] >= L[idxtr] )
{
trg[i] = 0;
flgtr = 0;
}
}
else
if( pk[i] AND tr[i] )
{
if( sumpk[i - 1] > sumtr[i - 1] )
{
pkg[i] = 0;
flgtr = 1;
idxtr = i;
}
else
if( sumtr[i - 1] > sumpk[i - 1] )
{
trg[i] = 0;
flgpk = 1;
idxpk = i;
}
}
}
}

// Fractal Pivots
x = bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

rightstrength = Param( "Right Strength", 10, 2, 50, 1 );
leftstrength = Param( "Left Strength", 10, 2, 50, 1 );
leftstrength = rightstrength;

pk = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
tr = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;

sumpk = Sum( pk, BarsSince( tr ) );
sumtr = Sum( tr, BarsSince( pk ) );
alternate_proc( pk, tr, sumpk, sumtr );
pk = pkg;
tr = trg;

for( i = 0; i < 3; i++ )
{
VarSet( "px" + i, ValueWhen( pk, bi, i ) );
VarSet( "tx" + i, ValueWhen( tr, bi, i ) );
VarSet( "ph" + i, ValueWhen( pk, H, i ) );
VarSet( "tl" + i, ValueWhen( tr, L, i ) );
}

ll = tr AND tl1 < tl2;
hl = tr AND tl1 > tl2;
hh = pk AND ph1 > ph2;
lh = pk AND ph1 < ph2;
dt = pk AND ph1 == ph2;
db = tr AND tl1 == tl2;

GraphXSpace = 5;
SetChartBkColor( colorBlack );
SetChartOptions( 1, chartShowDates, chartGridMiddle, 0, 0, 0 );
SetBarFillColor( IIf( C > O, ColorRGB( 0, 75, 0 ), IIf( C <= O, ColorRGB( 75, 0, 0 ), colorLightGrey ) ) );
Plot( C, "", IIf( C > O, ColorRGB( 0, 255, 0 ), IIf( C <= O, ColorRGB( 255, 0, 0 ), colorLightGrey ) ), 64, Null, Null, 0, 0, 1 );

PlotShapes( shapeSmallCircle * tr, ColorRGB( 0, 0, 255 ), 0, L, -10 );
PlotShapes( shapeSmallCircle * pk, ColorRGB( 255, 0, 0 ), 0, H, 10 );

clr = ColorRGB( 10, 10, 10 );
clrpk = ColorRGB( 255, 0, 0 );
clrtr = ColorRGB( 0, 0, 255 );

for( i = lvb; i > fvb; i-- )
{
// troughs
if( ll[i] )
{
str = "LL";
PlotTextSetFont( str, "Arial Black", 8, i, L[i], clrtr, colorDefault, -30 );
}

if( hl[i] )
{
str = "HL";
PlotTextSetFont( str, "Arial Black", 8, i, L[i], clrtr, colorDefault, -30 );
}

if( db[i] )
{
str = "DB";
PlotTextSetFont( str, "Arial Black", 8, i, L[i], clrtr, colorDefault, -30 );
}

//peaks
if( hh[i] )
{
str = "HH";
PlotTextSetFont( str, "Arial Black", 8, i, H[i], clrpk, colorDefault, 20 );
}

if( lh[i] )
{
str = "LH";
PlotTextSetFont( str, "Arial Black", 8, i, H[i], clrpk, colorDefault, 20 );
}

if( dt[i] )
{
str = "DT";
PlotTextSetFont( str, "Arial Black", 8, i, H[i], clrpk, colorDefault, 20 );
}
}

Title = Name() +
" | " + Now( 2 ) +
" | " + "Rightstrength: " + rightstrength +
" | " + "Leftstrength: " + leftstrength;
``````

Method 2)

``````// Fractal Pivots
x = bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

rightstrength = Param( "Right Strength", 10, 2, 50, 1 );
leftstrength = Param( "Left Strength", 10, 2, 50, 1 );
leftstrength = rightstrength;

//pk = H > Ref( HHV( H, leftStrength ), -1 ) AND Ref( HHV( H, rightStrength ), rightStrength ) <= H;
//tr = L < Ref( LLV( L, leftStrength ), -1 ) AND Ref( LLV( L, rightStrength ), rightStrength ) >= L;
pk = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
tr = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;

// clean pivots
reversepk = Reverse( pk );
reversepk[0] = 1;
reversetr = Reverse( tr );
reversetr[0] = 1;
reversell = Reverse( L );
reversehh = Reverse( H );
lsforward = LowestSince( pk, L );
lsbackward = LowestSince( reversepk, reversell );
lsbackward = Reverse( lsbackward );
hsforward = HighestSince( tr, H );
hsbackward = HighestSince( reversetr, reversehh );
hsbackward = Reverse( hsbackward );
tr = tr AND lsforward == lsbackward;
pk = pk AND hsforward == hsbackward;

// when peak and trough at same bar
bs1 = Ref( BarsSince( tr ), -1 ) < Ref( BarsSince( pk ), -1 );
pk = IIf( pk == 1 AND tr == 1 AND bs1 == 1, 1, pk );
pk = IIf( pk == 1 AND tr == 1 AND bs1 == 0, 0, pk );
tr = IIf( pk == 1 AND tr == 1 AND bs1 == 1, 0, tr );
tr = IIf( pk == 1 AND tr == 1 AND bs1 == 0, 1, tr );

for( i = 0; i < 3; i++ )
{
VarSet( "px" + i, ValueWhen( pk, bi, i ) );
VarSet( "tx" + i, ValueWhen( tr, bi, i ) );
VarSet( "ph" + i, ValueWhen( pk, H, i ) );
VarSet( "tl" + i, ValueWhen( tr, L, i ) );
}

ll = tr AND tl1 < tl2;
hl = tr AND tl1 > tl2;
hh = pk AND ph1 > ph2;
lh = pk AND ph1 < ph2;
dt = pk AND ph1 == ph2;
db = tr AND tl1 == tl2;

GraphXSpace = 5;
SetChartBkColor( colorBlack );
SetChartOptions( 1, chartShowDates, chartGridMiddle, 0, 0, 0 );
SetBarFillColor( IIf( C > O, ColorRGB( 0, 75, 0 ), IIf( C <= O, ColorRGB( 75, 0, 0 ), colorLightGrey ) ) );
Plot( C, "", IIf( C > O, ColorRGB( 0, 255, 0 ), IIf( C <= O, ColorRGB( 255, 0, 0 ), colorLightGrey ) ), 64, Null, Null, 0, 0, 1 );

PlotShapes( shapeSmallCircle * tr, ColorRGB( 0, 0, 255 ), 0, L, -10 );
PlotShapes( shapeSmallCircle * pk, ColorRGB( 255, 0, 0 ), 0, H, 10 );

clr = ColorRGB( 10, 10, 10 );
clrpk = ColorRGB( 255, 0, 0 );
clrtr = ColorRGB( 0, 0, 255 );

for( i = lvb; i > fvb; i-- )
{
// troughs
if( ll[i] )
{
str = "LL";
PlotTextSetFont( str, "Arial Black", 8, i, L[i], clrtr, colorDefault, -30 );
}

if( hl[i] )
{
str = "HL";
PlotTextSetFont( str, "Arial Black", 8, i, L[i], clrtr, colorDefault, -30 );
}

if( db[i] )
{
str = "DB";
PlotTextSetFont( str, "Arial Black", 8, i, L[i], clrtr, colorDefault, -30 );
}

//peaks
if( hh[i] )
{
str = "HH";
PlotTextSetFont( str, "Arial Black", 8, i, H[i], clrpk, colorDefault, 20 );
}

if( lh[i] )
{
str = "LH";
PlotTextSetFont( str, "Arial Black", 8, i, H[i], clrpk, colorDefault, 20 );
}

if( dt[i] )
{
str = "DT";
PlotTextSetFont( str, "Arial Black", 8, i, H[i], clrpk, colorDefault, 20 );
}
}

Title = Name() +
" | " + Now( 2 ) +
" | " + "Rightstrength: " + rightstrength +
" | " + "Leftstrength: " + leftstrength;
``````
1 Like

Thank you a lot. It seems method 1 works great!

yes I agree. That second method has issues. The first 1 works pretty good. I made a couple and couldn't remember which 1 worked best. I also have another version with a loop but the 1 I posted as "method 1" seems to work best.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.