Bollinger Band ZigZag Indicator

IMPORTANT: Do not use the indicator in a real trading system; it looks ahead in time and will make you lose money. It is meant for research only: to show potential profits and display arrows at highly profitable positions to facilitate formulating better trading rules.

The indicator presented here is very similar to the ZigZag Indicator except that the turning points for this indicator are where the opposite Bollinger Bands are last breached before the next signal.

The formula is written as a trading system. It can be Backtested, and the BB period and width can be optimized. Since this is just an experimental formula no attempt has been made to optimize the code.

function ParamOptimizedescriptiondefaultValminvmaxvstep )
    { 
    return Optimize(descriptionParam(description,defaultValminvmaxvstep ), minvmaxvstep ); 
    }

BlankBars 10// Set to the number set in preferences

Buy Sell Short Cover Pos 0;
Periods ParamOptimize"Periods"3330);
Width ParamOptimize"Width"1050.1 );
BBTOp BBandTopHPeriodsWidth ); // Note H is used instead of the tradional C
BBBot BBandBotLPeriodsWidth ); // Note L is used instead of the tradional C

PlotBBTop"BBTop"colorBluestyleLine );
PlotBBBOt"BBBot"colorBluestyleLine );
PlotC""1128 );

BI BarIndex();
start Status"firstvisiblebarindex" ) - BI[0] - BlankBars;
end     Status"lastvisiblebarindex" ) - BI[0] - BlankBars;
LBI LastValueBarIndex() );

for ( LBIPeriodsb-- )
{
    if ( L[b] <= BBBot[b] AND pos <= )
    {
        pos 1;
        Buy[b] = True;
        BuyPrice[b] = BBBot[b];
    }
    else
        if ( H[b] >= BBTop[b] AND Pos >= )
        {
            Pos = -1;
            Sell[b] = True;
            SellPrice[b] = BBTop[b];
        }
}

Short Sell;
ShortPrice SellPrice;

Cover Buy;
CoverPrice BuyPrice;

Eq Equity);

if ( ParamToggle"Equity""HIDE|SHOW") )
    PlotEq""colorYellowstyleOwnScale );

ShowTriangles ParamToggle"Arrows""HIDE|SHOW");

if ( showTriangles )
{
    PlotShapesIIfBuy,     shapeSmallUpTriangleshapeNone ), 50BuyPrice);
    PlotShapesIIfSell,    shapeHollowDownTriangleshapeNone ), 40SellPrice);
    PlotShapesIIfCovershapeHollowUpTriangleshapeNone ), 50CoverPrice);
    PlotShapesIIfShortshapeSmallDownTriangleshapeNone ), 40ShortPrice);
}

if ( ParamToggle"Trade Lines""HIDE|SHOW") )
{
    Sig Buy OR Short;
    signum CumSig );
    y0 0;
    y1 C[0];
    TPrice C;
    Shortcolor LongColor colorWhite;

    for ( start ;end ;b++ )
    {
        if ( Sig[b] )
        {
            x0 y0;
            x1 y1;
            y0 b;

            if ( Buy[b] )
            {
                y1 BuyPrice[b];
                Color Shortcolor;
            }
            else
                if ( Short[b] )
                {
                    y1 ShortPrice[b];
                    Color Longcolor;
                }

            if( SigNum[b] > PlotLineArrayx0x1y0y1 ), ""ColorstyleThick );
        }
    }
}