{"id":1277,"date":"2016-01-22T14:56:28","date_gmt":"2016-01-22T13:56:28","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=1277"},"modified":"2016-01-28T15:09:06","modified_gmt":"2016-01-28T14:09:06","slug":"how-to-fill-background-between-hand-drawn-trend-lines","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2016\/01\/22\/how-to-fill-background-between-hand-drawn-trend-lines\/","title":{"rendered":"How to fill background between hand-drawn trend lines"},"content":{"rendered":"

Among built-in drawing tools, the Triangle, Rectangle and Ellipse allow to fill the background with custom color. However, if we wanted to fill the space between manually drawn trend lines, then we could use AFL formula with Study function that allows to detect the position of the line. Then – knowing the arrays of top and bottom lines we could fill the area between with a cloud plot.<\/p>

A sample formula, which shows such implementation is presented below. The code fills the space between L1 and L2 trendlines (red color) and between upper and lower bands of the regression channel (RU and RL study id’s respectively).<\/p><\/span>\/\/ regular price plot
<\/span>Plot<\/span>( <\/span>Close<\/span>, <\/span>"Close"<\/span>, <\/span>colorDefault<\/span>, <\/span>styleBar <\/span>);

<\/span>\/\/ custom function definition
<\/span>function <\/span>FillSpace<\/span>( <\/span>ID1<\/span>, <\/span>ID2<\/span>, <\/span>color <\/span>)
{
   <\/span>\/\/ get current chart ID
   <\/span>chartID <\/span>= <\/span>GetChartID<\/span>();

   <\/span>\/\/ read the positions of the lines
   <\/span>l1 <\/span>= <\/span>Study<\/span>(<\/span>ID1<\/span>, <\/span>chartID <\/span>);
   <\/span>l2 <\/span>= <\/span>Study<\/span>(<\/span>ID2<\/span>, <\/span>chartID <\/span>);

   <\/span>\/\/ draw cloud chart
   <\/span>PlotOHLC<\/span>( <\/span>l1<\/span>, <\/span>l1<\/span>, <\/span>l2<\/span>, <\/span>l2<\/span>, <\/span>""<\/span>, <\/span>ColorBlend<\/span>(<\/span>color<\/span>, <\/span>GetChartBkColor<\/span>() ), <\/span>styleCloud<\/span>|<\/span>styleNoRescale<\/span>|<\/span>styleNoLabel<\/span>, <\/span>Null<\/span>, <\/span>Null<\/span>, <\/span>0<\/span>, -<\/span>1 <\/span>);
}

<\/span>\/\/ call function and refer to the assigned study ID's
<\/span>FillSpace<\/span>( <\/span>"L1"<\/span>,<\/span>"L2"<\/span>, <\/span>colorRed <\/span>);
<\/span>FillSpace<\/span>( <\/span>"RU"<\/span>,<\/span>"RL"<\/span>, <\/span>colorBlue <\/span>)<\/code>

The chart produced by the formula looks as follows:<\/p>

\"Chart<\/p>

We need to remember that each line needs to have unique Study ID<\/strong> assigned in the Properties window.<\/p>

\"Properties<\/p>

In case of regression channel the ID’s of the upper and lower lines are defined in Regression Channel tab:<\/p>

\"Properties<\/p>

If we wanted to handle more lines, then it may be more practical to process the list of study ID’s defined in a custom string instead of individual function calls.<\/p><\/span>\/\/ regular price plot
<\/span>Plot<\/span>( <\/span>Close<\/span>, <\/span>"Close"<\/span>, <\/span>colorDefault<\/span>, <\/span>styleBar <\/span>);

<\/span>\/\/ custom function definition
<\/span>function <\/span>FillSpace<\/span>( <\/span>ID1<\/span>, <\/span>ID2<\/span>, <\/span>color <\/span>)
{
   <\/span>\/\/ get current chart ID
   <\/span>chartID <\/span>= <\/span>GetChartID<\/span>();

   <\/span>\/\/ read the positions of the lines
   <\/span>l1 <\/span>= <\/span>Study<\/span>(<\/span>ID1<\/span>, <\/span>chartID <\/span>);
   <\/span>l2 <\/span>= <\/span>Study<\/span>(<\/span>ID2<\/span>, <\/span>chartID <\/span>);

   <\/span>\/\/ draw cloud chart
   <\/span>PlotOHLC<\/span>( <\/span>l1<\/span>, <\/span>l1<\/span>, <\/span>l2<\/span>, <\/span>l2<\/span>, <\/span>""<\/span>, <\/span>ColorBlend<\/span>(<\/span>color<\/span>, <\/span>GetChartBkColor<\/span>() ), <\/span>styleCloud<\/span>|<\/span>styleNoRescale<\/span>|<\/span>styleNoLabel<\/span>, <\/span>Null<\/span>, <\/span>Null<\/span>, <\/span>0<\/span>, -<\/span>1 <\/span>);
}

function <\/span>BulkFill<\/span>( <\/span>IDlist<\/span>, <\/span>color <\/span>)
{
    for( <\/span>i <\/span>= <\/span>0<\/span>; ( <\/span>ID1 <\/span>= <\/span>StrExtract<\/span>( <\/span>IDlist<\/span>, <\/span>i <\/span>) ) != <\/span>""<\/span>; <\/span>i <\/span>+= <\/span>2 <\/span>)
    {
      <\/span>ID2 <\/span>= <\/span>StrExtract<\/span>( <\/span>IDlist<\/span>, <\/span>i<\/span>+<\/span>1 <\/span>);
      <\/span>FillSpace<\/span>( <\/span>ID1<\/span>,<\/span>ID2<\/span>, <\/span>color <\/span>);
    }
}

<\/span>\/\/ call function and refer to the assigned study ID's
<\/span>BulkFill<\/span>( <\/span>"L1,L2,RU,RL,R1,R2"<\/span>, <\/span>colorRed <\/span>)<\/code>","protected":false},"excerpt":{"rendered":"

Among built-in drawing tools, the Triangle, Rectangle and Ellipse allow to fill the background with custom color. However, if we wanted to fill the space between manually drawn trend lines, then we could use AFL formula with Study function that allows to detect the position of the line. Then – knowing the arrays of top […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[53,55,86,85],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1277"}],"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=1277"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1277\/revisions"}],"predecessor-version":[{"id":1282,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1277\/revisions\/1282"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=1277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=1277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=1277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}