Display Swing HH and Swing LL in Charts

Dear All,
I like to display SHH and SLL in Charts.

SHH = Should be form Minimum 3 HH in Left Side and Minimum 3 LL in Right Side (Compare with Previous Swing Low).

SLL = Should be Form Minimum 3 LL in Left Side and Minimum 3 HH in Right Side (Compare with Previous Swing High).

Kindly Help to Create AFL for this .

Example%20for%20Swing%20HH%20LL

@muthusudhan, you will find that you will get better assistance if you show your own work, and explain where in the code you are having difficulty.

Many of the members of this board are excellent helpers, but don't want to do your coding work. They love a challenge, but don't want to code up an idea that they don't need or want.

Have you used the SEARCH function to see if something is already on the board?
Have you READ the manual to see if there is something already built in? (Hint - ZigZag)

Have you TRIED any code yourself? (Post it USING the Code Block "</>" feature so we can copy/paste/try it ourselves.).
Can you EXPLAIN what part of the code is not getting you what you want or thing you should get?

Doing the above is far more likely to get you a response and generate ideas on this thread.

3 Likes

this has been posted before:

2 Likes

Thanks for your reply.

Here is the code :

_SECTION_BEGIN( "SwingHighLow" );

SetChartOptions( 0, chartShowArrows | chartShowDates );
SetChartBkColor( colorBlack );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorBlack ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
rightstrength = Param( "Right Strength",4, 2, 50, 1 );
leftstrength = Param( "Left Strength", 4, 2, 50, 1 );
plotFractals = ParamToggle( "Plot Fractals", "Off|On", 0 );
plotLabels = ParamToggle( "Plot Labels", "Off|On", 1);
function GetTop()
{
    Top = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
    Top = Top AND LastValue( bi ) - ValueWhen( Top, bi ) > rightstrength;
    return Top;
}

function GetValley()
{
    Valley = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;
    Valley = Valley AND LastValue( bi ) - ValueWhen( Valley, bi ) > rightstrength;
    return Valley;
}

// Build fractals array
pk = GetTop();
tr = GetValley();

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;

if( PlotFractals )
{
    PlotShapes( shapeSmallCircle * pk, colorRed, 0, H, 10 );
    PlotShapes( shapeSmallCircle * tr, colorBlue, O, L, -10 );
}


function drawPivotLabels()
{
    sz = 10;

    for( i = lvb; i > fvb; i-- )
    {
        {
            if( ll[i] ) PlotTextSetFont( "SL", "Arial Black", sz, i, L[i], colorBlue, colorDefault, -25 );

            if( hl[i] ) PlotTextSetFont( "SL", "Arial Black", sz, i, L[i], colorBlue, colorDefault, -25 );

            if( db[i] ) PlotTextSetFont( "DB", "Arial Black", sz, i, L[i], colorLightBlue, colorDefault, -35 );

            if( hh[i] ) PlotTextSetFont( "SH", "Arial Black", sz, i, H[i], colorRed, colorDefault, 20 );

            if( lh[i] ) PlotTextSetFont( "SH", "Arial Black", sz, i, H[i], colorRed, colorDefault, 20 );

            if( dt[i] ) PlotTextSetFont( "DT", "Arial Black", sz, i, H[i], colorOrange, colorDefault, 30 );
        }
    }
}

if( plotLabels ) drawPivotLabels();
_SECTION_END();

Kindly help to modify as per my requirement .

2

Correct Example :

3

ok I understand. That is a tough one. Will think about it. Is it described or used in some other software package?

I am trading using Fibonacci with swing High swing Low. I am using Amibroker only and watching manually in chart. If create AFL for this it will be very useful to me. Thanks in Advance.

using loops it is possible of course. The challenge is to solve it with arrays :slight_smile:

I think I will leave it for somebody else. Because in your "Correct Example" you paint the trajectory incorrectly.

it should be like below. And that is strange because then it does not find a lower low. It seems overly complicated to me

mur1

Sir thanks for your reply. But previous DB Swing Low (SL) is correct. Because this SL is less than previous SL.

in that case then the first the SL before that will be confirmed as a bottom because it has 4 LL on the left and 4 HH on the right. Then the price drops further and then this confirmed bottom becomes invalid because of a new low. This new low is only a new low because the high before that is not yet confirmed to be a high. Because the price then moves higher this potential high will never become a high and only because of that the low 11 bars back will become invalid.

You see what I mean. It is unworkable. You will be many bars in the future before you really know if a peak or a bottom is valid.

It is far from what you want, but might give you some ideas.

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

Lower = Low < Ref(LLV(Low, 4), -1);
scLow = SparseCompress(Lower, Low);
scLower = scLow < Ref(scLow, -1);
nscLower = SparseExpand(Lower, scLower);
Lower = nscLower;
//PlotShapes( IIf(Lower, shapeUpArrow, shapeNone), colorGreen, 0, Low, -10 );


Higher = High > Ref(HHV(High, 4), -1);
scHigh = SparseCompress(Higher, High);
scHigher = scHigh > Ref(scHigh, -1);
nscHigher = SparseExpand(Higher, scHigher);
Higher = nscHigher;
//PlotShapes( IIf(Higher, shapeDownArrow, shapeNone), colorRed, 0, High, -10);

barsSinceBot = LowestSinceBars(Higher, Low);
PlotShapes( IIf(barsSinceBot==0 AND Lower, shapeUpArrow, shapeNone), colorGreen, 0, Low, -10 );

barsSinceTop = HighestSinceBars(Lower, High);
PlotShapes( IIf(barsSinceTop==0 AND Higher, shapeDownArrow, shapeNone), colorRed, 0, High, -19);

2 Likes

by the way, I also posted my code in this thread (my first post in this thread). You just have to press the little down arrow, see image below

will not make much difference which pivots you use. Question is how do you use them?

mur3

@awilson
to understand use of sparsecompress better … please explain the logic?
Can’t it be done via iif function ....

scLow = IIf(Lower, Low,Null);

try with iif function but result differ ...

c

Thanks for your reply. But showing more arrows.

More Examples :
aaBB

is there a big difference when you use your pivots instead of these?

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

rightstrength = Param( "Right Strength", 5, 2, 50, 1 );
leftstrength = Param( "Left Strength", 5, 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;

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 );

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

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

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

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

    if( lh[i] )
    {
        str = "LH";
        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 );
    }
}

Title = Name() +
        " | " + Now( 2 ) +
        " | " + "Pivot Timeframe Factor: " + fact;
1 Like

if you paste this code on the bottom you have a trading system on futures

Plot( tl1, "", colorRed, styleLine | stylenorescale, Null, Null, 0, 0, 1 );
Plot( ph1, "", colorGreen, styleLine | stylenorescale, Null, Null, 0, 0, 1 );

// trading system
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( H, Ref( ph1, -1 ) );
BuyPrice = Max( O, Ref( ph1, -1 ) );
Short = Cross( Ref( tl1, -1 ), L );
ShortPrice = Min( O, Ref( tl1, -1 ) );
Buy = ExRem( Buy, Short );
Short = ExRem( Short, Buy );
Sell = short;
Cover = Buy;
SellPrice = ShortPrice;
CoverPrice = BuyPrice;

PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), ColorRGB( 0, 255, 255 ), 0, L, -15 );
PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), colorLightBlue, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), ColorRGB( 255, 165, 0 ), 0, H, -15 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorOrange, 0, SellPrice, 0 );

PlotShapes( IIf( Short, shapeSmallDownTriangle, shapeNone ), ColorRGB( 255, 165, 0 ), 0, H, IIf( Short AND Sell, -30, -15 ) );
PlotShapes( IIf( Short, shapeSmallCircle, shapeNone ), colorWhite, 0, ShortPrice, 0 );
PlotShapes( IIf( Cover, shapeSmallUpTriangle, shapeNone ), ColorRGB( 0, 255, 255 ), 0, L, IIf( Cover AND Buy, -30, -15 ) );
PlotShapes( IIf( Cover, shapeSmallCircle, shapeNone ), colorWhite, 0, CoverPrice, 0 );
1 Like

Yes Sir.
111222

1 Like

Example Chart :
CPP

1 Like