{"id":1045,"date":"2015-01-14T16:45:15","date_gmt":"2015-01-14T21:45:15","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=1045"},"modified":"2018-12-11T00:56:10","modified_gmt":"2018-12-10T23:56:10","slug":"drawing-line-extensions-on-future-bars-using-afl","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2015\/01\/14\/drawing-line-extensions-on-future-bars-using-afl\/","title":{"rendered":"Drawing line extensions on future bars using AFL"},"content":{"rendered":"

AmiBroker allows to display the AFL-based chart output on the future blank bars area with use of XSHIFT<\/strong> argument of the Plot<\/strong> function. This functionality allows to move the particular chart by certain number of bars and place the output within the blank bars area (provided that we use positive value for XSHIFT<\/strong>, i.e. we are shifting the chart to the right).<\/p>

The following code shows price chart with 20-period MA overlay and additionally \u2013 with the same 20-period MA shifted to the right.<\/p>Plot<\/span>( <\/span>Close<\/span>, <\/span>"Close"<\/span>, <\/span>colorDefault<\/span>, <\/span>styleBar <\/span>);

<\/span>Plot<\/span>( <\/span>MA<\/span>( <\/span>Close<\/span>, <\/span>20 <\/span>), <\/span>"MA-20"<\/span>, <\/span>colorRed<\/span>, <\/span>styledashed <\/span>);
<\/span>Plot<\/span>( <\/span>MA<\/span>( <\/span>Close<\/span>, <\/span>20 <\/span>), <\/span>"MA-shift"<\/span>, <\/span>colorRed<\/span>, <\/span>styleThick<\/span>, <\/span>Null<\/span>, <\/span>Null<\/span>, <\/span>10<\/span>)<\/code>

\"Chart<\/p>

However \u2013 there may be some situations where we not only want to shift the chart position, but actually calculate the position of the line on the blank bars, for example if we are producing an extension of the existing indicator line.<\/p>

Let us consider a simple example, which draws a line connecting the last record of the input array with the value 50-bars ago, using LineArray() function.<\/p>

The code is the following:<\/p>inputArray <\/span>= <\/span>Close<\/span>;
<\/span>Plot<\/span>( <\/span>inputArray<\/span>, <\/span>"input"<\/span>, <\/span>colorDefault<\/span>, <\/span>styleDots <\/span>);

<\/span>bi <\/span>= <\/span>BarIndex<\/span>();
<\/span>lvbi <\/span>= <\/span>LastValue<\/span>( <\/span>bi <\/span>);
<\/span>x0 <\/span>= <\/span>lvbi <\/span>- <\/span>50<\/span>;
<\/span>x1 <\/span>= <\/span>lvbi<\/span>;
<\/span>y0 <\/span>= <\/span>inputArray<\/span>[ <\/span>lvbi <\/span>- <\/span>50 <\/span>];
<\/span>y1 <\/span>= <\/span>inputArray<\/span>[ <\/span>lvbi <\/span>];

<\/span>Plot<\/span>( <\/span>LineArray<\/span>( <\/span>x0<\/span>, <\/span>y0<\/span>, <\/span>x1<\/span>, <\/span>y1<\/span>, <\/span>True <\/span>, <\/span>True <\/span>), <\/span>"line"<\/span>, <\/span>colorRed<\/span>, <\/span>styleThick <\/span>)<\/code>

and the output looks like this:
\"Chart<\/p>

LineArray<\/strong> function allows to calculate the extension automatically if we set EXTEND argument to True, however \u2013 all the calculations in AFL language are performed within the \u2018real bars\u2019 area, i.e. on the available elements of the array, between array item 0 until array item (Barcount-1).<\/p>

To calculate and display the values that extend past the very last bar available in the array we will use technique explained below:<\/p>

  1. first we shift the input back (to the left) by N bars, so the real input data would occupy earlier part of the array and we would have extra bars at the end for the calculation of extended arrays<\/li>
  2. now we calculate the position of arrays on such shifted<\/li>
  3. shift the displayed output forwards with XSHIFT functionality of Plot function (so the calculated extensions would get aligned onto the blank bars as a result).<\/li><\/ol>

    If our original input array contains 200 bars and the line was drawn between bar 150 and the bar 200, then our aim is to shift the array that way, so the line would occupy the bars between bar 140 and bar 190, while the remaining 10 bars at the end of the array could be used for calculating the extended part of the line. Then \u2013 using XSHIFT we could place the array back into its original position.<\/p>inputArray <\/span>= <\/span>Close<\/span>;
    <\/span>Plot<\/span>( <\/span>inputArray<\/span>, <\/span>"input"<\/span>, <\/span>colorDefault<\/span>, <\/span>styleDots <\/span>);

    <\/span>\/\/ here we shift the input array to the left
    <\/span>shift <\/span>= <\/span>10<\/span>;
    <\/span>inputArray <\/span>= <\/span>Ref<\/span>( <\/span>inputArray<\/span>, <\/span>shift <\/span>);

    <\/span>\/\/ calculations of the x-y coordinates of the LineArray
    \/\/ take into account the fact that array has been shifted
    <\/span>bi <\/span>= <\/span>BarIndex<\/span>();
    <\/span>lvbi <\/span>= <\/span>LastValue<\/span>( <\/span>bi <\/span>);
    <\/span>x0 <\/span>= <\/span>lvbi <\/span>- <\/span>50 <\/span>- <\/span>shift<\/span>;
    <\/span>x1 <\/span>= <\/span>lvbi <\/span>- <\/span>shift<\/span>;
    <\/span>y0 <\/span>= <\/span>inputArray<\/span>[ <\/span>lvbi <\/span>- <\/span>50 <\/span>- <\/span>shift <\/span>];
    <\/span>y1 <\/span>= <\/span>inputArray<\/span>[ <\/span>lvbi <\/span>- <\/span>shift <\/span>];

    <\/span>\/\/ lineArray shifted back to original (correct) position with XSHIFT
    <\/span>Plot<\/span>( <\/span>LineArray<\/span>( <\/span>x0<\/span>, <\/span>y0<\/span>, <\/span>x1<\/span>, <\/span>y1<\/span>, <\/span>True<\/span>, <\/span>True <\/span>), <\/span>"line"<\/span>, <\/span>colorRed<\/span>, <\/span>styleThick<\/span>, <\/span>Null<\/span>, <\/span>Null<\/span>, <\/span>shift <\/span>);

    <\/span>\/\/positions that were used for calclations before using xshift
    <\/span>Plot<\/span>( <\/span>inputArray<\/span>, <\/span>"input shifted"<\/span>, <\/span>colorLightGrey<\/span>, <\/span>styleDashed <\/span>);
    <\/span>Plot<\/span>( <\/span>LineArray<\/span>( <\/span>x0<\/span>, <\/span>y0<\/span>, <\/span>x1<\/span>, <\/span>y1<\/span>, <\/span>True<\/span>, <\/span>True <\/span>), <\/span>"line shifted"<\/span>, <\/span>colorRed<\/span>, <\/span>styleDashed <\/span>)<\/code>

    \"Chart<\/p>

    Dashed lines in the above chart show the shifted positions of the input array and calculated LineArray<\/strong> before using XSHIFT<\/strong> (i.e. on the bars that were used for actual calculations)<\/p>","protected":false},"excerpt":{"rendered":"

    AmiBroker allows to display the AFL-based chart output on the future blank bars area with use of XSHIFT argument of the Plot function. This functionality allows to move the particular chart by certain number of bars and place the output within the blank bars area (provided that we use positive value for XSHIFT, i.e. we […]<\/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],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1045"}],"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=1045"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1045\/revisions"}],"predecessor-version":[{"id":1532,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1045\/revisions\/1532"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=1045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=1045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=1045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}