Fast afl code for ZigZag based on Price Change- anyone?

you mean the built in Amibroker ZigZag function? Yes you can use that also it is just that this uses percentage. So if you switch to another timeframe your will have to adjust the percentage. I like ATR because it gives similar waves in all timeframes. Recently I made another one that I like even better than ATR. This one also shows a similar pattern in any timeframe without having to change the parameter settings, see code below

nbar = Param( "Swing number of Bars", 10, 1, 50, 1 );
priceswitch = ParamList( "ZigZag Mode", "Close|High&Low|Mixed 1|Mixed 2", 0 );
showTrailline = ParamToggle( "Show Trail Line", "Off|On", 0 );
showprimarypivotlabels = ParamToggle( "Show Primary Pivot Labels", "Off|On", 1 );
handleBothOutside = ParamToggle( "Long AND Short signal at the SAME bar", "Change Trend|Stay with current Trend", 0 );
sz = Param( "Text Size", 8, 5, 22, 1 );
ft = ParamList( "Font", "Tahoma|Helvetica|Arial Black|Verdana|Courier New|Compact|Segoe UI", 2 );

bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
pk = tr = 0;
ZigZag = line1 = Null;
prcl = prch = crossup = crossdn = 0;

if( priceswitch == "High&Low" )
{
    prch = H;
    prcl = L;
    mode = "High/Low mode";
    // find trend
    res = HHV( prch, nbar );
    sup = LLV( prcl, nbar );
    crossup = prch > Ref( res, -1 );
    crossdn = prcl < Ref( sup, -1 );
    // trail line price
    bp = Max( O, Ref( res, -1 ) );
    sp = Min( O, Ref( sup, -1 ) );
}

if( priceswitch == "Close" )
{
    prch = C;
    prcl = C;
    mode = "Close mode";
    // find trend
    res = HHV( prch, nbar );
    sup = LLV( prcl, nbar );
    crossup = prch > Ref( res, -1 );
    crossdn = prcl < Ref( sup, -1 );
    // trail line price
    bp = C;
    sp = C;
}

if( priceswitch == "Mixed 1" )
{
    prch = H;
    prcl = L;
    mode = "Mixed mode 1";
    // find trend
    res = HHV( prch, nbar );
    sup = LLV( prcl, nbar );
    crossup = C > Ref( res, -1 );
    crossdn = C < Ref( sup, -1 );
    // trail line price
    bp = C;
    sp = C;
}

if( priceswitch == "Mixed 2" )
{
    prch = C;
    prcl = C;
    mode = "Mixed mode 2";
    // find trend
    res = HHV( prch, nbar );
    sup = LLV( prcl, nbar );
    crossup = H > Ref( res, -1 );
    crossdn = L < Ref( sup, -1 );
    // trail line price
    bp = Max( O, Ref( res, -1 ) );
    sp = Min( O, Ref( sup, -1 ) );
}

function calculateZigZag()
{
    // create ZIGZAG array line
    zigup = Flip( tr, pk );
    zigupLow = ValueWhen( tr, prcl, 1 );
    zigupHigh = ValueWhen( pk, prch, 0 );
    zigupLowIndex = ValueWhen( tr, bi, 1 );
    zigupHighIndex = ValueWhen( pk, bi, 0 );
    slopeup = IIf( zigup, ( zigupHigh - zigupLow ) / ( zigupHighIndex - zigupLowIndex ) , Null );
    zigupLine = IIf( zigup, zigupLow + slopeup * BarsSince( tr ), Null );

    zigdn = Flip( pk, tr );
    zigdnLow = ValueWhen( tr, prcl, 0 );
    zigdnHigh = ValueWhen( pk, prch, 1 );
    zigdnLowIndex = ValueWhen( tr, bi, 0 );
    zigdnHighIndex = ValueWhen( pk, bi, 1 );
    slopedn = IIf( zigdn, ( zigdnLow - zigdnHigh ) / ( zigdnLowIndex - zigdnHighIndex ) , Null );
    zigdnLine = IIf( zigdn, zigdnHigh + slopedn * BarsSince( pk ), Null );

    ZigZag = IIf( zigup, zigupLine, IIf( zigdn, zigdnLine, Null ) );
    ZigZag = IIf( bi > Max( LastValue( ValueWhen( tr, bi ) ), LastValue( ValueWhen( pk, bi ) ) ), Null, ZigZag );
}
function calculateLastSegments()
{
    lastidxpk = LastValue( ValueWhen( pk, bi ) );
    lastidxtr = LastValue( ValueWhen( tr, bi ) );
    lastvalpk = LastValue( ValueWhen( pk, prch ) );
    lastvaltr = LastValue( ValueWhen( tr, prcl ) );
    valpk = LastValue( HighestSince( Ref( tr, -1 ), prch , 1 ) );
    idxpk = LastValue( ValueWhen( prch == valpk, bi ) );
    valtr = LastValue( LowestSince( Ref( pk, -1 ), prcl, 1 ) );
    idxtr = LastValue( ValueWhen( prcl == valtr, bi ) );

    if( lastidxpk > lastidxtr )
    {
        x0 = lastidxpk;
        y0 = lastvalpk;
        x1 = idxtr;
        y1 = valtr;
        line1 = linedn = LineArray( x0, y0, x1, y1 );
        tr[idxtr] = 1;
    }

    if( lastidxpk < lastidxtr )
    {
        x0 = lastidxtr;
        y0 = lastvaltr;
        x1 = idxpk;
        y1 = valpk;
        line1 = lineup = LineArray( x0, y0, x1, y1 );
        pk[idxpk] = 1;
    }

    ZigZag = IIf( !IsEmpty( line1 ), line1, ZigZag );
}

// is used in High&Low mode only
// if both crossup and crossdn are true then,
// when handleBothOutside is true then it will stay in the current trend
// when handleBothOutside is false it will change the trend (default)
if( handleBothOutside )
{
    crossup = IIf( ( crossup AND crossdn ) AND( BarsSince( Ref( crossup, -1 ) ) > BarsSince( Ref( crossdn, -1 ) ) ), 0, crossup );
    crossdn = IIf( ( crossup AND crossdn ) AND( BarsSince( Ref( crossdn, -1 ) ) > BarsSince( Ref( crossup, -1 ) ) ), 0, crossdn );
}

crossup = ExRem( crossup, crossdn );
crossdn = ExRem( crossdn, crossup );
trendup = Flip( crossup, crossdn );
trenddn = Flip( crossdn, crossup );

// find pivots
rcrossdn = Reverse( crossdn );
rcrossup = Reverse( crossup );
l1 = LowestSince( crossdn, prcl );
rL = Reverse( prcl );
l2 = LowestSince( Ref( rcrossup, -1 ), rL );
rl2 = Reverse( l2 );
h1 = HighestSince( crossup, prch );
rH = Reverse( prch );
h2 = HighestSince( Ref( rcrossdn, -1 ), rH );
rh2 = Reverse( h2 );
tr = l1 == rl2 AND trenddn;
pk = h1 == rh2 AND trendup;

rpk = Reverse( pk );
rtr = Reverse( tr );
rpk = ExRem( rpk, rtr );
rtr = ExRem( rtr, rpk );
pk = Reverse( rpk );
tr = Reverse( rtr );

calculateZigZag();
calculateLastSegments();

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "Price", colorWhite, styleCandle, Null, Null, 0, 0, 0 );

if( showtrailline )
{
    Plot( IIf( trendup, sup, Null ), "", colorGreen, styleStaircase | styleNoRescale, Null, Null, 0, 0, 1 );
    Plot( IIf( trenddn, res, Null ), "", colorRed, styleStaircase | styleNoRescale, Null, Null, 0, 0, 1 );
    //PlotShapes( IIf( crossup, shapeSmallCircle, shapeNone ), colorWhite, 0, bp, 0 );
    //PlotShapes( IIf( crossdn, shapeSmallCircle, shapeNone ), ColorWhite, 0, sp, 0 );
}

PlotShapes( IIf( crossup, shapeSmallCircle, shapeNone ), colorWhite, 0, bp, 0 );
PlotShapes( IIf( crossdn, shapeSmallCircle, shapeNone ), colorWhite, 0, sp, 0 );

Plot( zigzag, "ZigZag", colorGold, styleLine | styleNoLabel | styleNoRescale, Null, Null, 0, 1, 1 );
PlotShapes( IIf( tr, shapeSmallCircle, shapeNone ), colorGreen, 0, L, -10 );
PlotShapes( IIf( pk, shapeSmallCircle, shapeNone ), colorRed, 0, H, 10 );

if( showprimaryPivotLabels )
{
    clr = colorDefault;
	upColor = ColorRGB( 0, 255, 0 );
	dnColor = ColorRGB( 255, 0, 0 );
    
    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, prch, i ) );
        VarSet( "tl" + i, ValueWhen( tr, prcl, 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;

    cumulativeVolumeUpleg = SumSince( tr, V );
    cumulativeVolumeDnleg = SumSince( pk, V );
    lengthUpleg = SumSince( tr, 1 );
    lengthDnleg = SumSince( pk, 1 );

    for( i = fvb; i <= lvb; i++ )
    {
        if( ll[i] )
        {
            str = "LL" + " / " + cumulativeVolumeDnleg[i] + " / " + lengthDnleg[i] + " / " + int( SafeDivide( cumulativeVolumeDnleg[i], lengthDnleg[i] ) );
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -sz * 4 );
        }

        if( hl[i] )
        {
            str = "HL" + " / " + cumulativeVolumeDnleg[i] + " / " + lengthDnleg[i] + " / " + int( SafeDivide( cumulativeVolumeDnleg[i], lengthDnleg[i] ) );
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -sz * 4 );
        }

        if( db[i] )
        {
            str = "DB" + " / " + cumulativeVolumeDnleg[i] + " / " + lengthDnleg[i] + " / " + int( SafeDivide( cumulativeVolumeDnleg[i], lengthDnleg[i] ) );
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -sz * 4 );
        }

        if( hh[i] )
        {
            str = "HH" + " / " + cumulativeVolumeUpleg[i] + " / " + lengthUpleg[i] + " / " + int( SafeDivide( cumulativeVolumeUpleg[i], lengthUpleg[i] ) );
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, sz * 3 );
        }

        if( lh[i] )
        {
            str = "LH" + " / " + cumulativeVolumeUpleg[i] + " / " + lengthUpleg[i] + " / " + int( SafeDivide( cumulativeVolumeUpleg[i], lengthUpleg[i] ) );
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, sz * 3 );
        }

        if( dt[i] )
        {
            str = "DT" + " / " + cumulativeVolumeUpleg[i] + " / " + lengthUpleg[i] + " / " + int( SafeDivide( cumulativeVolumeUpleg[i], lengthUpleg[i] ) );
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, sz * 3 );
        }
    }
}

Title = Name() +
        " | " + Now( 1 ) + " | " + Now( 2 ) +
        " | " + EncodeColor( colorAqua ) + priceswitch + EncodeColor( colorWhite ) +
        " | " + EncodeColor( colorLightOrange ) + "nbar: " + nbar + EncodeColor( colorWhite ) +
        " | " + EncodeColor( colorGold ) + "Chart time frame (Min): " + Prec( Interval() / 60, 0 ) + " Min" + EncodeColor( colorWhite ) +
        " | " + EncodeColor( colorWhite ) + Prec( ( C - Ref( C, -1 ) ) / Ref( C, -1 ) * 100, 2 ) + " %";

Plot( ValueWhen( pk, H ), "", colorBlue, styleDashed | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( ValueWhen( tr, L ), "", colorRed, styleDashed | styleNoRescale, Null, Null, 0, 0, 1 );
4 Likes

Hi Sir,

thank you very much .. i realized ATR is much better option after reading some information on internet and now u confirmed it . Thank you

and the above code is perfectly identifying the peaks & troughs . in all desired time frames .

as i am trying to embed the " market Structure code ( BOS & CHoCH) " in the above code .. its fail to identify some of BoS & CHoCH ..

why i am behind Market Structure is - My Trading psychology & Market Structure very well align with each other..

thank you
Ravi

ah ok just looked it up:

https://www.tradingview.com/chart/EURUSD/pwdD6h4y-Understand-the-difference-between-BOS-and-CHoCH/

maybe that code I posted earlier misses some. I used the charts you posted to write the code. Will have a look tomorrow if I can write some simple code that shows trend continuations and trend reversals based on the pivots

1 Like

Sure Sir

Thank you
Ravi

I think this simple code shows all the levels,

res = ValueWhen( pk, prch );
res = IIf( ph1 >= ph0, Null, res );
sup = ValueWhen( tr, prcl );
sup = IIf(tl1 <= tl0, Null, sup );
Plot( res, "", colorBlue, styleDots | styleNoLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( sup, "", colorRed, styleDots | styleNoLine | styleNoRescale, Null, Null, 0, 0, 1 );

total code:

nbar = Param( "Swing number of Bars", 10, 1, 50, 1 );
priceswitch = ParamList( "ZigZag Mode", "Close|High&Low|Mixed 1|Mixed 2", 0 );
showTrailline = ParamToggle( "Show Trail Line", "Off|On", 0 );
showprimarypivotlabels = ParamToggle( "Show Primary Pivot Labels", "Off|On", 1 );
handleBothOutside = ParamToggle( "Long AND Short signal at the SAME bar", "Change Trend|Stay with current Trend", 0 );
sz = Param( "Text Size", 12, 5, 22, 1 );
ft = ParamList( "Font", "Tahoma|Helvetica|Arial Black|Verdana|Courier New|Compact|Segoe UI", 0 );

bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
pk = tr = 0;
ZigZag = line1 = Null;
prcl = prch = crossup = crossdn = 0;

if( priceswitch == "High&Low" )
{
    prch = H;
    prcl = L;
    mode = "High/Low mode";
    // find trend
    res = HHV( prch, nbar );
    sup = LLV( prcl, nbar );
    crossup = prch > Ref( res, -1 );
    crossdn = prcl < Ref( sup, -1 );
    // trail line price
    bp = Max( O, Ref( res, -1 ) );
    sp = Min( O, Ref( sup, -1 ) );
}

if( priceswitch == "Close" )
{
    prch = C;
    prcl = C;
    mode = "Close mode";
    // find trend
    res = HHV( prch, nbar );
    sup = LLV( prcl, nbar );
    crossup = prch > Ref( res, -1 );
    crossdn = prcl < Ref( sup, -1 );
    // trail line price
    bp = C;
    sp = C;
}

if( priceswitch == "Mixed 1" )
{
    prch = H;
    prcl = L;
    mode = "Mixed mode 1";
    // find trend
    res = HHV( prch, nbar );
    sup = LLV( prcl, nbar );
    crossup = C > Ref( res, -1 );
    crossdn = C < Ref( sup, -1 );
    // trail line price
    bp = C;
    sp = C;
}

if( priceswitch == "Mixed 2" )
{
    prch = C;
    prcl = C;
    mode = "Mixed mode 2";
    // find trend
    res = HHV( prch, nbar );
    sup = LLV( prcl, nbar );
    crossup = H > Ref( res, -1 );
    crossdn = L < Ref( sup, -1 );
    // trail line price
    bp = Max( O, Ref( res, -1 ) );
    sp = Min( O, Ref( sup, -1 ) );
}

function calculateZigZag()
{
    // create ZIGZAG array line
    zigup = Flip( tr, pk );
    zigupLow = ValueWhen( tr, prcl, 1 );
    zigupHigh = ValueWhen( pk, prch, 0 );
    zigupLowIndex = ValueWhen( tr, bi, 1 );
    zigupHighIndex = ValueWhen( pk, bi, 0 );
    slopeup = IIf( zigup, ( zigupHigh - zigupLow ) / ( zigupHighIndex - zigupLowIndex ) , Null );
    zigupLine = IIf( zigup, zigupLow + slopeup * BarsSince( tr ), Null );

    zigdn = Flip( pk, tr );
    zigdnLow = ValueWhen( tr, prcl, 0 );
    zigdnHigh = ValueWhen( pk, prch, 1 );
    zigdnLowIndex = ValueWhen( tr, bi, 0 );
    zigdnHighIndex = ValueWhen( pk, bi, 1 );
    slopedn = IIf( zigdn, ( zigdnLow - zigdnHigh ) / ( zigdnLowIndex - zigdnHighIndex ) , Null );
    zigdnLine = IIf( zigdn, zigdnHigh + slopedn * BarsSince( pk ), Null );

    ZigZag = IIf( zigup, zigupLine, IIf( zigdn, zigdnLine, Null ) );
    ZigZag = IIf( bi > Max( LastValue( ValueWhen( tr, bi ) ), LastValue( ValueWhen( pk, bi ) ) ), Null, ZigZag );
}
function calculateLastSegments()
{
    lastidxpk = LastValue( ValueWhen( pk, bi ) );
    lastidxtr = LastValue( ValueWhen( tr, bi ) );
    lastvalpk = LastValue( ValueWhen( pk, prch ) );
    lastvaltr = LastValue( ValueWhen( tr, prcl ) );
    valpk = LastValue( HighestSince( Ref( tr, -1 ), prch , 1 ) );
    idxpk = LastValue( ValueWhen( prch == valpk, bi ) );
    valtr = LastValue( LowestSince( Ref( pk, -1 ), prcl, 1 ) );
    idxtr = LastValue( ValueWhen( prcl == valtr, bi ) );

    if( lastidxpk > lastidxtr )
    {
        x0 = lastidxpk;
        y0 = lastvalpk;
        x1 = idxtr;
        y1 = valtr;
        line1 = linedn = LineArray( x0, y0, x1, y1 );
        tr[idxtr] = 1;
    }

    if( lastidxpk < lastidxtr )
    {
        x0 = lastidxtr;
        y0 = lastvaltr;
        x1 = idxpk;
        y1 = valpk;
        line1 = lineup = LineArray( x0, y0, x1, y1 );
        pk[idxpk] = 1;
    }

    ZigZag = IIf( !IsEmpty( line1 ), line1, ZigZag );
}

// is used in High&Low mode only
// if both crossup and crossdn are true then,
// when handleBothOutside is true then it will stay in the current trend
// when handleBothOutside is false it will change the trend (default)
if( handleBothOutside )
{
    crossup = IIf( ( crossup AND crossdn ) AND( BarsSince( Ref( crossup, -1 ) ) > BarsSince( Ref( crossdn, -1 ) ) ), 0, crossup );
    crossdn = IIf( ( crossup AND crossdn ) AND( BarsSince( Ref( crossdn, -1 ) ) > BarsSince( Ref( crossup, -1 ) ) ), 0, crossdn );
}

crossup = ExRem( crossup, crossdn );
crossdn = ExRem( crossdn, crossup );
trendup = Flip( crossup, crossdn );
trenddn = Flip( crossdn, crossup );

// find pivots
rcrossdn = Reverse( crossdn );
rcrossup = Reverse( crossup );
l1 = LowestSince( crossdn, prcl );
rL = Reverse( prcl );
l2 = LowestSince( Ref( rcrossup, -1 ), rL );
rl2 = Reverse( l2 );
h1 = HighestSince( crossup, prch );
rH = Reverse( prch );
h2 = HighestSince( Ref( rcrossdn, -1 ), rH );
rh2 = Reverse( h2 );
tr = l1 == rl2 AND trenddn;
pk = h1 == rh2 AND trendup;

rpk = Reverse( pk );
rtr = Reverse( tr );
rpk = ExRem( rpk, rtr );
rtr = ExRem( rtr, rpk );
pk = Reverse( rpk );
tr = Reverse( rtr );

calculateZigZag();
calculateLastSegments();

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "Price", colorWhite, styleCandle, Null, Null, 0, 0, 0 );

if( showtrailline )
{
    Plot( IIf( trendup, sup, Null ), "", colorGreen, styleStaircase | styleNoRescale, Null, Null, 0, 0, 1 );
    Plot( IIf( trenddn, res, Null ), "", colorRed, styleStaircase | styleNoRescale, Null, Null, 0, 0, 1 );
    //PlotShapes( IIf( crossup, shapeSmallCircle, shapeNone ), colorWhite, 0, bp, 0 );
    //PlotShapes( IIf( crossdn, shapeSmallCircle, shapeNone ), ColorWhite, 0, sp, 0 );
}

PlotShapes( IIf( crossup, shapeSmallCircle, shapeNone ), colorWhite, 0, bp, 0 );
PlotShapes( IIf( crossdn, shapeSmallCircle, shapeNone ), colorWhite, 0, sp, 0 );

Plot( zigzag, "ZigZag", colorGold, styleLine | styleNoLabel | styleNoRescale, Null, Null, 0, 1, 1 );
PlotShapes( IIf( tr, shapeSmallCircle, shapeNone ), colorGreen, 0, L, -10 );
PlotShapes( IIf( pk, shapeSmallCircle, shapeNone ), colorRed, 0, H, 10 );

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, prch, i ) );
    VarSet( "tl" + i, ValueWhen( tr, prcl, 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( showprimaryPivotLabels )
{
    clr = colorDefault;
    upColor = ColorRGB( 0, 255, 0 );
    dnColor = ColorRGB( 255, 0, 0 );

    cumulativeVolumeUpleg = SumSince( tr, V );
    cumulativeVolumeDnleg = SumSince( pk, V );
    lengthUpleg = SumSince( tr, 1 );
    lengthDnleg = SumSince( pk, 1 );

    for( i = fvb; i <= lvb; i++ )
    {
        if( ll[i] )
        {
            str = "LL" + " / " + cumulativeVolumeDnleg[i] + " / " + lengthDnleg[i] + " / " + int( SafeDivide( cumulativeVolumeDnleg[i], lengthDnleg[i] ) );
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -sz * 4 );
        }

        if( hl[i] )
        {
            str = "HL" + " / " + cumulativeVolumeDnleg[i] + " / " + lengthDnleg[i] + " / " + int( SafeDivide( cumulativeVolumeDnleg[i], lengthDnleg[i] ) );
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -sz * 4 );
        }

        if( db[i] )
        {
            str = "DB" + " / " + cumulativeVolumeDnleg[i] + " / " + lengthDnleg[i] + " / " + int( SafeDivide( cumulativeVolumeDnleg[i], lengthDnleg[i] ) );
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -sz * 4 );
        }

        if( hh[i] )
        {
            str = "HH" + " / " + cumulativeVolumeUpleg[i] + " / " + lengthUpleg[i] + " / " + int( SafeDivide( cumulativeVolumeUpleg[i], lengthUpleg[i] ) );
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, sz * 3 );
        }

        if( lh[i] )
        {
            str = "LH" + " / " + cumulativeVolumeUpleg[i] + " / " + lengthUpleg[i] + " / " + int( SafeDivide( cumulativeVolumeUpleg[i], lengthUpleg[i] ) );
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, sz * 3 );
        }

        if( dt[i] )
        {
            str = "DT" + " / " + cumulativeVolumeUpleg[i] + " / " + lengthUpleg[i] + " / " + int( SafeDivide( cumulativeVolumeUpleg[i], lengthUpleg[i] ) );
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, sz * 3 );
        }
    }
}

res = ValueWhen( pk, prch );
res = IIf( ph1 >= ph0, Null, res );
sup = ValueWhen( tr, prcl );
sup = IIf(tl1 <= tl0, Null, sup );
Plot( res, "", colorBlue, styleDots | styleNoLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( sup, "", colorRed, styleDots | styleNoLine | styleNoRescale, Null, Null, 0, 0, 1 );

Title = Name() +
        " | " + Now( 1 ) + " | " + Now( 2 ) +
        " | " + EncodeColor( colorAqua ) + priceswitch + EncodeColor( colorWhite ) +
        " | " + EncodeColor( colorLightOrange ) + "nbar: " + nbar + EncodeColor( colorWhite ) +
        " | " + EncodeColor( colorGold ) + "Chart time frame (Min): " + Prec( Interval() / 60, 0 ) + " Min" + EncodeColor( colorWhite ) +
        " | " + EncodeColor( colorWhite ) + Prec( ( C - Ref( C, -1 ) ) / Ref( C, -1 ) * 100, 2 ) + " %";

2 Likes

to make the levels better visible you can draw them above the price bars and use a different color like

Plot( res, "", colorLightBlue, styleDots | styleNoLine | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 1 );
Plot( sup, "", colorPink, styleDots | styleNoLine | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 1 );
3 Likes

Hi Sir ,,

a quick query -
is it possible to plot horizontal lines on the candle's High & LOW
whenever the following condition satisfies ( the condition is IV in the code)

And

Condition occurrences number on candle top in last 70days

explore this condition from 70 last days

Thanks
Ravi

//  Volume - IV

MaxVol = HHV(Volume,10);

VolAvg = MA(Volume,10) > 30000;

Vol = V > Ref(MaxVol,-1)*2 AND VolAvg;

Cl = Close > Ref(Close,-1) AND Close > 80;

VolColor = IIf(Vol,colorBrightGreen,colorBrown);

Range = H-L;

DCR = Close >= Range/2 + Low ;

Xy = Range/2 + Low ;

IV = Vol AND Cl AND DCR; 

Filter = DCR  AND VOL AND Cl;

AddColumn(C,"CMP");
AddColumn(Range,"Range");
AddColumn(V,"Volume");
AddColumn(VOl,"Vol");


PlotOHLC(0, IV, 0, MaxVol, "V", VolColor , styleCandle ) ;

you can simply do that as follows:

linecount = Param( "Number of Lines", 10, 1, 50, 1 );

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

//  Volume - IV
MaxVol = HHV( Volume, 10 );
VolAvg = MA( Volume, 10 ) > 30000;
Vol = V > Ref( MaxVol, -1 ) * 2 AND VolAvg;
Cl = Close > Ref( Close, -1 ) AND Close > 80;
VolColor = IIf( Vol, colorBrightGreen, colorBrown );
Range = H - L;
DCR = Close >= Range / 2 + Low ;
Xy = Range / 2 + Low ;
IV = Vol AND Cl AND DCR;
lin = DCR  AND VOL AND Cl;

SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "price", colorAqua, styleCandle, Null, Null, 0, 0, 1 );

lncnt = 0;

for( i = lvb; i > fvb; i-- )
{
    if( lin[i] AND lncnt < linecount )
    {
        line = LineArray( i, L[i], BarCount, L[i] );
        Plot( line, "", colorGreen, styleDashed | styleNoRescale, Null, Null, 0, 0, 1 );
        lncnt = lncnt + 1;
    }
}

2 Likes

Thank you very much sir for your timely response and its really helpful :pray:

can we number the candle based on the conditions

in a given period like say in 50 days , 5 such occurrences happened ,
for 1st occurrence , plot text as 1, for 2nd occurrence 2 and so on

Thank you

Ravi

you can use

PlotTextSetFont

for this (below the Plot line inside the loop. If you just want the last 50 bars then you could use something like

for( i = lvb; i > lvb-50; i-- )