{"id":524,"date":"2014-10-10T07:30:22","date_gmt":"2014-10-10T12:30:22","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=524"},"modified":"2014-12-04T08:29:46","modified_gmt":"2014-12-04T13:29:46","slug":"how-to-draw-regression-channel-programatically","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2014\/10\/10\/how-to-draw-regression-channel-programatically\/","title":{"rendered":"How to draw regression channel programatically"},"content":{"rendered":"
Built-in drawing tool allows to place regression channel on the chart manually and the study works on regular Close array as input. The power of AFL allows to automate this task and draw a customizable regression channel automatically in the chart or choose any custom array for calculation.<\/p>
Here is a sample coding solution showing how to code Standard Deviation based channel. The Parameters<\/strong> dialog allows to control the array the channel is based upon, number of periods used for calculation, position and width of the channel.<\/p> Here is the picture that shows how it looks:<\/p> <\/p>","protected":false},"excerpt":{"rendered":" Built-in drawing tool allows to place regression channel on the chart manually and the study works on regular Close array as input. The power of AFL allows to automate this task and draw a customizable regression channel automatically in the chart or choose any custom array for calculation.Here is a sample coding solution showing how […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[53,55],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/524"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=524"}],"version-history":[{"count":1,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/524\/revisions"}],"predecessor-version":[{"id":527,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/524\/revisions\/527"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}lookback <\/span>= <\/span>Param<\/span>( <\/span>"Look back"<\/span>, <\/span>20<\/span>, <\/span>1<\/span>, <\/span>200<\/span>, <\/span>1 <\/span>);
<\/span>shift <\/span>= <\/span>Param<\/span>( <\/span>"Shift"<\/span>, <\/span>0<\/span>, <\/span>0<\/span>, <\/span>20<\/span>, <\/span>1 <\/span>);
<\/span>multiplier <\/span>= <\/span>Param<\/span>( <\/span>"Width"<\/span>, <\/span>1<\/span>, <\/span>0.25<\/span>, <\/span>5<\/span>, <\/span>0.25 <\/span>);
<\/span>color <\/span>= <\/span>ParamColor<\/span>( <\/span>"Color"<\/span>, <\/span>colorRed <\/span>);
<\/span>style <\/span>= <\/span>ParamStyle<\/span>( <\/span>"Style"<\/span>, <\/span>styleLine <\/span>| <\/span>styleDots <\/span>);
<\/span>pricestyle <\/span>= <\/span>ParamStyle<\/span>( <\/span>"Price style"<\/span>, <\/span>styleBar <\/span>| <\/span>styleThick<\/span>, <\/span>maskPrice <\/span>);
<\/span>\/\/
\/\/ price chart
<\/span>Plot<\/span>( <\/span>Close<\/span>, <\/span>"Close"<\/span>, <\/span>colorDefault<\/span>, <\/span>pricestyle <\/span>);
<\/span>\/\/
<\/span>array = <\/span>ParamField<\/span>( <\/span>"Price field"<\/span>, -<\/span>1 <\/span>);
<\/span>\/\/
<\/span>x <\/span>= <\/span>BarIndex<\/span>() + <\/span>1<\/span>;
<\/span>lastx <\/span>= <\/span>LastValue<\/span>( <\/span>x <\/span>) - <\/span>shift<\/span>;
<\/span>\/\/
\/\/ compute linear regression coefficients
<\/span>aa <\/span>= <\/span>LastValue<\/span>( <\/span>Ref<\/span>( <\/span>LinRegIntercept<\/span>( array, <\/span>lookback <\/span>), -<\/span>shift <\/span>) );
<\/span>bb <\/span>= <\/span>LastValue<\/span>( <\/span>Ref<\/span>( <\/span>LinRegSlope<\/span>( array, <\/span>lookback <\/span>), -<\/span>shift <\/span>) );
<\/span>\/\/
\/\/ the equation for straight line
<\/span>y <\/span>= <\/span>Aa <\/span>+ <\/span>bb <\/span>* ( <\/span>x <\/span>- ( <\/span>Lastx <\/span>- <\/span>lookback <\/span>+ <\/span>1 <\/span>) );
<\/span>\/\/
<\/span>width <\/span>= <\/span>LastValue<\/span>( <\/span>Ref<\/span>( <\/span>StDev<\/span>( array, <\/span>lookback <\/span>), -<\/span>shift <\/span>) );
<\/span>\/\/
<\/span>drawit <\/span>= <\/span>x <\/span>> ( <\/span>lastx <\/span>- <\/span>lookback <\/span>) AND <\/span>BarIndex<\/span>() < <\/span>Lastx<\/span>;
<\/span>\/\/
\/\/ draw regression line...
<\/span>Plot<\/span>( <\/span>IIf<\/span>( <\/span>drawit<\/span>, <\/span>y<\/span>, <\/span>Null <\/span>), <\/span>"LinReg"<\/span>, <\/span>color<\/span>, <\/span>style <\/span>);
<\/span>\/\/ ... and channel
<\/span>Plot<\/span>( <\/span>IIf<\/span>( <\/span>drawit<\/span>, <\/span>y <\/span>+ <\/span>width<\/span>*<\/span>multiplier <\/span>, <\/span>Null <\/span>), <\/span>"LinReg UP"<\/span>, <\/span>color<\/span>, <\/span>style <\/span>);
<\/span>Plot<\/span>( <\/span>IIf<\/span>( <\/span>drawit<\/span>, <\/span>y <\/span>- <\/span>width<\/span>*<\/span>multiplier <\/span>, <\/span>Null <\/span>), <\/span>"LinReg DN"<\/span>, <\/span>color<\/span>, <\/span>style <\/span>)<\/code>