{"id":3331,"date":"2011-08-25T20:43:46","date_gmt":"2011-08-25T20:43:46","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/?p=3331"},"modified":"2011-09-01T11:50:54","modified_gmt":"2011-09-01T11:50:54","slug":"bollinger-band-zigzag-indicator","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2011\/08\/25\/bollinger-band-zigzag-indicator\/","title":{"rendered":"Bollinger Band ZigZag Indicator"},"content":{"rendered":"

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.<\/p>\n

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.<\/p>\n

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.<\/p>\n

\n\"\"<\/a><\/p>\n
\n
<\/span>function <\/span>ParamOptimize<\/span>( <\/span>description<\/span>, <\/span>defaultVal<\/span>, <\/span>minv<\/span>, <\/span>maxv<\/span>, <\/span>step <\/span>)\r    { \r    return <\/span>Optimize<\/span>(<\/span>description<\/span>, <\/span>Param<\/span>(<\/span>description<\/span>,<\/span>defaultVal<\/span>, <\/span>minv<\/span>, <\/span>maxv<\/span>, <\/span>step <\/span>), <\/span>minv<\/span>, <\/span>maxv<\/span>, <\/span>step <\/span>); \r    }\r\r<\/span>BlankBars <\/span>= <\/span>10<\/span>; <\/span>\/\/ Set to the number set in preferences\r\r<\/span>Buy <\/span>= <\/span>Sell <\/span>= <\/span>Short <\/span>= <\/span>Cover <\/span>= <\/span>Pos <\/span>= <\/span>0<\/span>;\r<\/span>Periods <\/span>= <\/span>ParamOptimize<\/span>( <\/span>"Periods"<\/span>, <\/span>3<\/span>, <\/span>3<\/span>, <\/span>30<\/span>, <\/span>1 <\/span>);\r<\/span>Width <\/span>= <\/span>ParamOptimize<\/span>( <\/span>"Width"<\/span>, <\/span>1<\/span>, <\/span>0<\/span>, <\/span>5<\/span>, <\/span>0.1 <\/span>);\r<\/span>BBTOp <\/span>= <\/span>BBandTop<\/span>( <\/span>H<\/span>, <\/span>Periods<\/span>, <\/span>Width <\/span>); <\/span>\/\/ Note H is used instead of the tradional C\r<\/span>BBBot <\/span>= <\/span>BBandBot<\/span>( <\/span>L<\/span>, <\/span>Periods<\/span>, <\/span>Width <\/span>); <\/span>\/\/ Note L is used instead of the tradional C\r\r<\/span>Plot<\/span>( <\/span>BBTop<\/span>, <\/span>"BBTop"<\/span>, <\/span>colorBlue<\/span>, <\/span>styleLine <\/span>);\r<\/span>Plot<\/span>( <\/span>BBBOt<\/span>, <\/span>"BBBot"<\/span>, <\/span>colorBlue<\/span>, <\/span>styleLine <\/span>);\r<\/span>Plot<\/span>( <\/span>C<\/span>, <\/span>""<\/span>, <\/span>1<\/span>, <\/span>128 <\/span>);\r\r<\/span>BI <\/span>= <\/span>BarIndex<\/span>();\r<\/span>start <\/span>= <\/span>Status<\/span>( <\/span>"firstvisiblebarindex" <\/span>) - <\/span>BI<\/span>[<\/span>0<\/span>] - <\/span>BlankBars<\/span>;\r<\/span>end     <\/span>= <\/span>Status<\/span>( <\/span>"lastvisiblebarindex" <\/span>) - <\/span>BI<\/span>[<\/span>0<\/span>] - <\/span>BlankBars<\/span>;\r<\/span>LBI <\/span>= <\/span>LastValue<\/span>( <\/span>BarIndex<\/span>() );\r\rfor ( <\/span>b <\/span>= <\/span>LBI<\/span>; <\/span>b <\/span>> <\/span>Periods<\/span>; <\/span>b<\/span>-- )\r{\r    if ( <\/span>L<\/span>[<\/span>b<\/span>] <= <\/span>BBBot<\/span>[<\/span>b<\/span>] AND <\/span>pos <\/span><= <\/span>0 <\/span>)\r    {\r        <\/span>pos <\/span>= <\/span>1<\/span>;\r        <\/span>Buy<\/span>[<\/span>b<\/span>] = <\/span>True<\/span>;\r        <\/span>BuyPrice<\/span>[<\/span>b<\/span>] = <\/span>BBBot<\/span>[<\/span>b<\/span>];\r    }\r    else\r        if ( <\/span>H<\/span>[<\/span>b<\/span>] >= <\/span>BBTop<\/span>[<\/span>b<\/span>] AND <\/span>Pos <\/span>>= <\/span>0 <\/span>)\r        {\r            <\/span>Pos <\/span>= -<\/span>1<\/span>;\r            <\/span>Sell<\/span>[<\/span>b<\/span>] = <\/span>True<\/span>;\r            <\/span>SellPrice<\/span>[<\/span>b<\/span>] = <\/span>BBTop<\/span>[<\/span>b<\/span>];\r        }\r}\r\r<\/span>Short <\/span>= <\/span>Sell<\/span>;\r<\/span>ShortPrice <\/span>= <\/span>SellPrice<\/span>;\r\r<\/span>Cover <\/span>= <\/span>Buy<\/span>;\r<\/span>CoverPrice <\/span>= <\/span>BuyPrice<\/span>;\r\r<\/span>Eq <\/span>= <\/span>Equity<\/span>( <\/span>1 <\/span>);\r\rif ( <\/span>ParamToggle<\/span>( <\/span>"Equity"<\/span>, <\/span>"HIDE|SHOW"<\/span>, <\/span>0 <\/span>) )\r    <\/span>Plot<\/span>( <\/span>Eq<\/span>, <\/span>""<\/span>, <\/span>colorYellow<\/span>, <\/span>1 <\/span>| <\/span>styleOwnScale <\/span>);\r\r<\/span>ShowTriangles <\/span>= <\/span>ParamToggle<\/span>( <\/span>"Arrows"<\/span>, <\/span>"HIDE|SHOW"<\/span>, <\/span>1 <\/span>);\r\rif ( <\/span>showTriangles <\/span>)\r{\r    <\/span>PlotShapes<\/span>( <\/span>IIf<\/span>( <\/span>Buy<\/span>,     <\/span>shapeSmallUpTriangle<\/span>, <\/span>shapeNone <\/span>), <\/span>5<\/span>, <\/span>0<\/span>, <\/span>BuyPrice<\/span>, <\/span>0 <\/span>);\r    <\/span>PlotShapes<\/span>( <\/span>IIf<\/span>( <\/span>Sell<\/span>,    <\/span>shapeHollowDownTriangle<\/span>, <\/span>shapeNone <\/span>), <\/span>4<\/span>, <\/span>0<\/span>, <\/span>SellPrice<\/span>, <\/span>0 <\/span>);\r    <\/span>PlotShapes<\/span>( <\/span>IIf<\/span>( <\/span>Cover<\/span>, <\/span>shapeHollowUpTriangle<\/span>, <\/span>shapeNone <\/span>), <\/span>5<\/span>, <\/span>0<\/span>, <\/span>CoverPrice<\/span>, <\/span>0 <\/span>);\r    <\/span>PlotShapes<\/span>( <\/span>IIf<\/span>( <\/span>Short<\/span>, <\/span>shapeSmallDownTriangle<\/span>, <\/span>shapeNone <\/span>), <\/span>4<\/span>, <\/span>0<\/span>, <\/span>ShortPrice<\/span>, <\/span>0 <\/span>);\r}\r\rif ( <\/span>ParamToggle<\/span>( <\/span>"Trade Lines"<\/span>, <\/span>"HIDE|SHOW"<\/span>, <\/span>0 <\/span>) )\r{\r    <\/span>Sig <\/span>= <\/span>Buy <\/span>OR <\/span>Short<\/span>;\r    <\/span>signum <\/span>= <\/span>Cum<\/span>( <\/span>Sig <\/span>);\r    <\/span>y0 <\/span>= <\/span>0<\/span>;\r    <\/span>y1 <\/span>= <\/span>C<\/span>[<\/span>0<\/span>];\r    <\/span>TPrice <\/span>= <\/span>C<\/span>;\r    <\/span>Shortcolor <\/span>= <\/span>LongColor <\/span>= <\/span>colorWhite<\/span>;\r\r    for ( <\/span>b <\/span>= <\/span>start <\/span>;<\/span>b <\/span>< <\/span>end <\/span>;<\/span>b<\/span>++ )\r    {\r        if ( <\/span>Sig<\/span>[<\/span>b<\/span>] )\r        {\r            <\/span>x0 <\/span>= <\/span>y0<\/span>;\r            <\/span>x1 <\/span>= <\/span>y1<\/span>;\r            <\/span>y0 <\/span>= <\/span>b<\/span>;\r\r            if ( <\/span>Buy<\/span>[<\/span>b<\/span>] )\r            {\r                <\/span>y1 <\/span>= <\/span>BuyPrice<\/span>[<\/span>b<\/span>];\r                <\/span>Color <\/span>= <\/span>Shortcolor<\/span>;\r            }\r            else\r                if ( <\/span>Short<\/span>[<\/span>b<\/span>] )\r                {\r                    <\/span>y1 <\/span>= <\/span>ShortPrice<\/span>[<\/span>b<\/span>];\r                    <\/span>Color <\/span>= <\/span>Longcolor<\/span>;\r                }\r\r            if( <\/span>SigNum<\/span>[<\/span>b<\/span>] > <\/span>1 <\/span>) <\/span>Plot<\/span>( <\/span>LineArray<\/span>( <\/span>x0<\/span>, <\/span>x1<\/span>, <\/span>y0<\/span>, <\/span>y1 <\/span>), <\/span>""<\/span>, <\/span>Color<\/span>, <\/span>1 <\/span>| <\/span>styleThick <\/span>);\r        }\r    }\r}<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"

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 […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/3331"}],"collection":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/comments?post=3331"}],"version-history":[{"count":14,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/3331\/revisions"}],"predecessor-version":[{"id":3349,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/3331\/revisions\/3349"}],"wp:attachment":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/media?parent=3331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/categories?post=3331"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/tags?post=3331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}