{"id":967,"date":"2014-12-31T05:45:10","date_gmt":"2014-12-31T10:45:10","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=967"},"modified":"2015-01-03T06:23:42","modified_gmt":"2015-01-03T11:23:42","slug":"drawing-indicators-on-a-subset-of-visible-bars","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2014\/12\/31\/drawing-indicators-on-a-subset-of-visible-bars\/","title":{"rendered":"Drawing indicators on a subset of visible bars"},"content":{"rendered":"

By default, the Plot function draws the graph for all visible bars. In some situations however, we may want to draw some selected bars, leaving remaining chart space unaffected.<\/p>

To achieve that – we simply assign Null<\/strong> value for the bars that we want to skip. Our graph will just be drawn for the non-null bars. <\/p>

This simple example draws candlesticks only on Mondays and leaves empty all the other days.<\/p>IsMonday <\/span>= <\/span>DayOfWeek<\/span>() == <\/span>1<\/span>;
<\/span>\/\/ assign Close for Mondays, otherwise assign Null
<\/span>Data <\/span>= <\/span>IIf<\/span>( <\/span>IsMonday<\/span>, <\/span>Close<\/span>, <\/span>Null <\/span>);
<\/span>\/\/ plot the data
<\/span>Plot<\/span>( <\/span>Data<\/span>, <\/span>"Chart of Mondays"<\/span>, <\/span>colorDefault<\/span>, <\/span>styleCandle <\/span>)<\/code>

The following example shows how to restrict the visibility to last N bars. The code defines a custom function, which can be called later on for the arrays we want to show only partially.<\/p><\/span>\/\/ custom function definition
<\/span>function <\/span>LastNBars<\/span>( array, <\/span>bars <\/span>)
{
    <\/span>bi <\/span>= <\/span>BarIndex<\/span>();
    <\/span>lvbi <\/span>= <\/span>LastValue<\/span>( <\/span>bi <\/span>);

    <\/span>\/\/ use Null value for bars other than last N
    <\/span>return <\/span>IIf<\/span>( <\/span>bi <\/span>> <\/span>lvbi <\/span>- <\/span>bars<\/span>, array, <\/span>Null <\/span>);
}

<\/span>\/\/ price plot
<\/span>Plot<\/span>( <\/span>Close<\/span>, <\/span>"Close"<\/span>, <\/span>colorDefault<\/span>, <\/span>styleBar <\/span>);

<\/span>\/\/ MA-50 restricted to last 10-bars only
<\/span>line <\/span>= <\/span>MA<\/span>( <\/span>Close<\/span>, <\/span>50 <\/span>);
<\/span>Plot<\/span>( <\/span>LastNBars<\/span>( <\/span>line<\/span>, <\/span>10 <\/span>), <\/span>"last 10 bars"<\/span>, <\/span>colorRed <\/span>);

<\/span>\/\/ shaded area
<\/span>Plot<\/span>( <\/span>LastNbars<\/span>( <\/span>True<\/span>, <\/span>10 <\/span>), <\/span>""<\/span>, <\/span>colorYellow<\/span>, <\/span>styleArea<\/span>|<\/span>styleOwnScale<\/span>|<\/span>styleNoLabel<\/span>, <\/span>0<\/span>, <\/span>1<\/span>, <\/span>0<\/span>, -<\/span>1 <\/span>)<\/code>

\"Draw<\/p>

In the above chart both Moving average (red line) and yellow shading area have been restricted to last 10-bars only.<\/p>

In a similar way we can restrict the visibility to most recent day only in intraday chart:<\/p><\/span>\/\/ custom function definition
<\/span>function <\/span>ShowLastDay<\/span>( array )
{
    <\/span>dn <\/span>= <\/span>datenum<\/span>();
    <\/span>lastDay <\/span>= <\/span>dn <\/span>== <\/span>LastValue<\/span>( <\/span>dn <\/span>);

    return <\/span>IIf<\/span>( <\/span>lastDay<\/span>, array, <\/span>Null <\/span>);
}

<\/span>\/\/ price plot
<\/span>Plot<\/span>( <\/span>Close<\/span>, <\/span>"Close"<\/span>, <\/span>colorDefault<\/span>, <\/span>styleBar <\/span>);

<\/span>\/\/ daily high \/ low on last day only
<\/span>dailyH <\/span>= <\/span>TimeFrameGetPrice<\/span>(<\/span>"H"<\/span>, <\/span>inDaily <\/span>);
<\/span>dailyL <\/span>= <\/span>TimeFrameGetPrice<\/span>(<\/span>"L"<\/span>, <\/span>inDaily <\/span>);
<\/span>Plot<\/span>( <\/span>ShowLastDay<\/span>( <\/span>dailyH <\/span>), <\/span>"dailyH"<\/span>, <\/span>colorGreen<\/span>, <\/span>styleThick  <\/span>);
<\/span>Plot<\/span>( <\/span>ShowLastDay<\/span>( <\/span>dailyL <\/span>), <\/span>"dailyL"<\/span>, <\/span>colorRed<\/span>, <\/span>styleThick  <\/span>);

<\/span>\/\/ shaded area
<\/span>colorPaleYellow <\/span>= <\/span>ColorBlend<\/span>(<\/span>colorWhite<\/span>, <\/span>colorYellow<\/span>, <\/span>0.1<\/span>);
<\/span>style <\/span>= <\/span>styleArea <\/span>| <\/span>styleOwnScale <\/span>| <\/span>styleNoLabel<\/span>;
<\/span>Plot<\/span>( <\/span>ShowLastDay<\/span>( <\/span>1 <\/span>), <\/span>""<\/span>, <\/span>colorPaleYellow<\/span>, <\/span>style<\/span>, <\/span>0<\/span>, <\/span>1<\/span>, <\/span>0<\/span>, -<\/span>1 <\/span>)<\/code>

\"Draw<\/p>

Other practical implementations of such technique is presented in these formulas:
http:\/\/www.amibroker.com\/kb\/2007\/03\/24\/how-to-plot-a-trailing-stop-in-the-price-chart\/<\/a>
http:\/\/www.amibroker.com\/kb\/2014\/10\/10\/how-to-draw-regression-channel-programatically\/<\/a><\/p>","protected":false},"excerpt":{"rendered":"

By default, the Plot function draws the graph for all visible bars. In some situations however, we may want to draw some selected bars, leaving remaining chart space unaffected.To achieve that – we simply assign Null value for the bars that we want to skip. Our graph will just be drawn for the non-null bars. […]<\/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\/967"}],"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=967"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/967\/revisions"}],"predecessor-version":[{"id":977,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/967\/revisions\/977"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}