{"id":7,"date":"2007-04-20T21:27:25","date_gmt":"2007-04-20T21:27:25","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2007\/04\/20\/plotting-trade-zigzag-lines\/"},"modified":"2011-10-25T11:54:23","modified_gmt":"2011-10-25T11:54:23","slug":"plotting-trade-zigzag-lines","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2007\/04\/20\/plotting-trade-zigzag-lines\/","title":{"rendered":"Plotting Trade-Lines"},"content":{"rendered":"

A useful application is to plot straight lines between entry- and exit-signals, giving you the ability to view at a glance the location and magnitude of profits and losses of your trading system. The LineArray() function enables you to draw straight lines from one event or condition to another. In the chart below, which shows a reversal trading system, note how the lines begin and end at the exact trade prices, green being long and red being short. This gives you a quick impression of the profitability and location of individual trades:<\/p>\n

<\/a><\/p>\n

\"Chart<\/a><\/p>\n

Other applications would be plotting of custom ZigZag lines, price channels, trendlines, breakouts, etc.<\/p>\n

There are two afl versions listed below, since I believe many of us use the first method, I decided to show them both for educational purposes.<\/p>\n

The first one shows how you should NOT<\/strong> plot LineArrays. This method calls Plot() repeatedly and plots each LineArray segment as it is calculated. This is very resource consuming (executes slow) and may trigger a Warning 502 when you display a lot of data. Do not use this version.<\/p>\n

The second version shows how Tomasz (Thanks TJ!) combined the individual LineArray segments inside the loop and then plots them with a single Plot() statement outside the loop. This code executes much faster and will never trigger Warning 502. The technique is simple but shows a clever way to combine array segments. Study it :-) it will come in handy one day!<\/p>\n

<\/span>\/\/ This version is only listed to show you how it should NOT be programmed\r\/\/ Dummy system to generate some signals\r<\/span>Buy <\/span>= <\/span>Cross<\/span>( <\/span>MACD<\/span>(), <\/span>Signal<\/span>() );\r<\/span>BuyPrice <\/span>= <\/span>Open<\/span>;     <\/span>\/\/ Substitute your own prices\r<\/span>Sell <\/span>= <\/span>Cross<\/span>( <\/span>Signal<\/span>(), <\/span>MACD<\/span>() );\r<\/span>SellPrice <\/span>= <\/span>Close<\/span>;     <\/span>\/\/ Substitute your own prices\r<\/span>PlotShapes<\/span>( <\/span>IIf<\/span>( <\/span>Buy<\/span>, <\/span>shapeSmallUpTriangle<\/span>, <\/span>shapeNone <\/span>), <\/span>colorBrightGreen<\/span>, <\/span>0<\/span>, <\/span>BuyPrice<\/span>, <\/span>0 <\/span>);\r<\/span>PlotShapes<\/span>( <\/span>IIf<\/span>( <\/span>Sell<\/span>, <\/span>shapeSmallDownTriangle<\/span>, <\/span>shapeNone <\/span>), <\/span>colorRed<\/span>, <\/span>0<\/span>, <\/span>SellPrice<\/span>, <\/span>0 <\/span>);\r<\/span>Plot<\/span>( <\/span>C<\/span>, <\/span>""<\/span>, <\/span>1<\/span>, <\/span>128 <\/span>);\r<\/span>\/\/ Plot the Trade Lines\r<\/span>Sig <\/span>= <\/span>Buy <\/span>OR <\/span>Sell<\/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>FirstVisibleBar <\/span>= <\/span>Status<\/span>( <\/span>"FirstVisibleBar" <\/span>);\r<\/span>Lastvisiblebar <\/span>= <\/span>Status<\/span>( <\/span>"LastVisibleBar" <\/span>);\r\rfor ( <\/span>b <\/span>= <\/span>Firstvisiblebar<\/span>; <\/span>b <\/span><= <\/span>Lastvisiblebar <\/span>AND <\/span>b <\/span>< <\/span>BarCount<\/span>; <\/span>b<\/span>++ )\r{\r    if ( <\/span>Buy<\/span>[<\/span>b<\/span>] )\r    {\r        <\/span>Co <\/span>= <\/span>colorRed<\/span>;\r        <\/span>TPrice<\/span>[<\/span>b<\/span>] = <\/span>BuyPrice<\/span>[<\/span>b<\/span>];\r    }\r\r    if ( <\/span>Sell<\/span>[<\/span>b<\/span>] )\r    {\r        <\/span>Co <\/span>= <\/span>colorBrightGreen<\/span>;\r        <\/span>TPrice<\/span>[<\/span>b<\/span>] = <\/span>SellPrice<\/span>[<\/span>b<\/span>];\r    }\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        <\/span>y1 <\/span>= <\/span>TPrice<\/span>[<\/span>b<\/span>];\r        <\/span>Plot<\/span>( <\/span>LineArray<\/span>( <\/span>x0<\/span>, <\/span>x1<\/span>, <\/span>y0<\/span>, <\/span>y1 <\/span>), <\/span>""<\/span>, <\/span>Co<\/span>, <\/span>1 <\/span>);\r    }\r}<\/span><\/pre>\n
<\/span>\/\/ Improved version\r\/\/ Dummy system to generate some signals\r<\/span>Buy <\/span>= <\/span>Cross<\/span>( <\/span>MACD<\/span>(), <\/span>Signal<\/span>() );\r<\/span>BuyPrice <\/span>= <\/span>O<\/span>; <\/span>\/\/ Substitute your own prices\r<\/span>Sell <\/span>= <\/span>Cross<\/span>( <\/span>Signal<\/span>(), <\/span>MACD<\/span>() );\r<\/span>SellPrice <\/span>= <\/span>C<\/span>; <\/span>\/\/ Substitute your own prices\r<\/span>PlotShapes<\/span>( <\/span>IIf<\/span>( <\/span>Buy<\/span>, <\/span>shapeUpTriangle<\/span>, <\/span>shapeNone <\/span>), <\/span>colorBrightGreen<\/span>, <\/span>0<\/span>, <\/span>BuyPrice<\/span>, <\/span>0 <\/span>);\r<\/span>PlotShapes<\/span>( <\/span>IIf<\/span>( <\/span>Sell<\/span>, <\/span>shapeDownTriangle<\/span>, <\/span>shapeNone <\/span>), <\/span>colorRed<\/span>, <\/span>0<\/span>, <\/span>SellPrice<\/span>, <\/span>0 <\/span>);\r<\/span>Plot<\/span>( <\/span>C<\/span>, <\/span>""<\/span>, <\/span>1<\/span>, <\/span>128 <\/span>);\r\r<\/span>\/\/ Plot the Trade Lines\r<\/span>Sig <\/span>= <\/span>Buy <\/span>OR <\/span>Sell<\/span>;\r<\/span>y0 <\/span>= <\/span>0<\/span>;\r<\/span>y1 <\/span>= <\/span>C<\/span>[<\/span>0<\/span>];\r<\/span>FirstVisibleBar <\/span>= <\/span>Status<\/span>( <\/span>"FirstVisibleBar" <\/span>);\r<\/span>Lastvisiblebar <\/span>= <\/span>Status<\/span>( <\/span>"LastVisibleBar" <\/span>);\r<\/span>CombinedColor <\/span>= <\/span>colorWhite<\/span>;\r<\/span>CombinedLine <\/span>= <\/span>Null<\/span>;\r\rfor ( <\/span>b <\/span>= <\/span>Firstvisiblebar<\/span>; <\/span>b <\/span><= <\/span>Lastvisiblebar <\/span>AND <\/span>b <\/span>< <\/span>BarCount<\/span>; <\/span>b<\/span>++ )\r{\r\r    if ( <\/span>Buy<\/span>[<\/span>b<\/span>] )\r    {\r        <\/span>Co <\/span>= <\/span>colorRed<\/span>;\r        <\/span>TPrice<\/span>[<\/span>b<\/span>] = <\/span>BuyPrice<\/span>[<\/span>b<\/span>];\r    }\r    else if ( <\/span>Sell<\/span>[<\/span>b<\/span>] )\r    {\r        <\/span>Co <\/span>= <\/span>colorBrightGreen<\/span>;\r        <\/span>TPrice<\/span>[<\/span>b<\/span>] = <\/span>SellPrice<\/span>[<\/span>b<\/span>];\r    }\r\r    if ( <\/span>Sig<\/span>[<\/span>b<\/span>] )\r\r    {\r\r        <\/span>x0 <\/span>= <\/span>y0<\/span>;\r\r        <\/span>x1 <\/span>= <\/span>y1<\/span>;\r\r        <\/span>y0 <\/span>= <\/span>b<\/span>;\r\r        <\/span>y1 <\/span>= <\/span>TPrice<\/span>[<\/span>b<\/span>];\r\r        <\/span>La <\/span>= <\/span>LineArray<\/span>( <\/span>x0<\/span>, <\/span>x1<\/span>, <\/span>y0<\/span>, <\/span>y1 <\/span>);\r\r        <\/span>CombinedLine <\/span>= <\/span>IIf<\/span>( <\/span>IsNull<\/span>( <\/span>la <\/span>), <\/span>CombinedLine<\/span>, <\/span>la <\/span>);\r        <\/span>CombinedColor <\/span>= <\/span>IIf<\/span>( <\/span>IsNull<\/span>( <\/span>La <\/span>), <\/span>CombinedColor<\/span>, <\/span>Co <\/span>);\r    }\r}\r\r<\/span>Plot<\/span>( <\/span>CombinedLine<\/span>, <\/span>""<\/span>, <\/span>CombinedColor <\/span>);<\/span><\/pre>\n

Edited by Al Venosa<\/p>\n","protected":false},"excerpt":{"rendered":"

A useful application is to plot straight lines between entry- and exit-signals, giving you the ability to view at a glance the location and magnitude of profits and losses of your trading system. The LineArray() function enables you to draw straight lines from one event or condition to another. In the chart below, which shows […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[78],"tags":[],"_links":{"self":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/7"}],"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=7"}],"version-history":[{"count":5,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":3454,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/7\/revisions\/3454"}],"wp:attachment":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}