{"id":320,"date":"2014-09-22T16:59:57","date_gmt":"2014-09-22T21:59:57","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=320"},"modified":"2014-12-03T07:49:04","modified_gmt":"2014-12-03T12:49:04","slug":"do-not-make-assumptions-on-number-of-bars","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2014\/09\/22\/do-not-make-assumptions-on-number-of-bars\/","title":{"rendered":"Do NOT make assumptions on number of bars"},"content":{"rendered":"

From time to time some users face “Error 10. Subscript out of range<\/a>” in their formulas. The error itself is described in the manual<\/a>, but still a few words of explanation why it happens may be useful. <\/p>

The error usually occurs when formula uses hard-coded number of bars in the loop statements like this:<\/p><\/span>for( <\/span>i <\/span>= <\/span>0<\/span>; <\/span>i <\/span>< <\/span>300<\/span>; <\/span>i<\/span>++ ) <\/span>\/\/ MISTAKE: FIXED number of bars
<\/span>{
  <\/span>x <\/span>= <\/span>Close<\/span>[ <\/span>i <\/span>]; <\/span>\/\/ ERROR 10. because 'i' becomes greater than BarCount
<\/span><\/code>

or like this:<\/p><\/span>for( <\/span>i <\/span>= <\/span>BarCount <\/span>- <\/span>1<\/span>; <\/span>i <\/span>> <\/span>BarCount <\/span>- <\/span>300<\/span>; <\/span>i<\/span>-- ) <\/span>\/\/ MISTAKE: FIXED number of bars
<\/span>{
  <\/span>x <\/span>= <\/span>Close<\/span>[ <\/span>i <\/span>]; <\/span>\/\/ ERROR 10. because 'i' becomes LESS than zero
<\/span><\/code>

In both cases the code will FAIL if it is run on symbol that has LESS than 300 bars (BarCount < 300). In fact it will even fail on symbol that has more than 300 bars because of two facts:

  1. during AFL Editor’s Verify Syntax<\/strong> not more than 200 most recent bars are used<\/strong>
  2. a chart may be zoomed in so number of visible bars may be much lower and QuickAFL kicks in<\/a> (so your AFL is executed with visible bars only).<\/ol>

    This all means that one should never make any assumptions on number of bars your formula would get,<\/strong> because if you do, the formula will fail. <\/p>

    The formula should be written so it is able to execute without errors with BarCount as small as 1 (ONE).<\/strong><\/p>

    This is normally done by writing a ‘for’ loop in a way recommended in the manual:<\/p><\/span>for( <\/span>i <\/span>= <\/span>0<\/span>; <\/span>i <\/span>< <\/span>BarCount<\/span>; <\/span>i<\/span>++ )
    {
       <\/span>x <\/span>= <\/span>Close<\/span>[ <\/span>i <\/span>]; <\/span>\/\/ this will never produce Error 10 because i is in the range 0..BarCount-1
    <\/span><\/code>

    If your formula references past data, say 10-bars earlier, you should start your loop with index 10, as below:<\/p><\/span>for( <\/span>i <\/span>= <\/span>10<\/span>; <\/span>i <\/span>< <\/span>BarCount<\/span>; <\/span>i<\/span>++ )
    {
      <\/span>x<\/span>[ <\/span>i <\/span>] = <\/span>Close<\/span>[ <\/span>i <\/span>] - <\/span>Close<\/span>[ <\/span>i <\/span>- <\/span>10 <\/span>]; <\/span>\/\/ both subscripts will be OK
    <\/span><\/code>

    What to do if your formula, for some reason, really requires fixed number of bars? Well, the answer is that you should check<\/strong> if you really get as many bars as you think:<\/p><\/span>if( <\/span>BarCount <\/span>> <\/span>300 <\/span>) <\/span>\/\/ check first if you have enough bars
    <\/span>{
       <\/span>\/\/ here we know that we have more than 300 bars
       <\/span>for( <\/span>i <\/span>= <\/span>0<\/span>; <\/span>i <\/span>< <\/span>300<\/span>; <\/span>i<\/span>++ )
       {
          <\/span>x <\/span>= <\/span>Close<\/span>[ <\/span>i <\/span>]; 
       }
    <\/code>","protected":false},"excerpt":{"rendered":"

    From time to time some users face “Error 10. Subscript out of range” in their formulas. The error itself is described in the manual, but still a few words of explanation why it happens may be useful. The error usually occurs when formula uses hard-coded number of bars in the loop statements like this:for( i = 0; i < 300; i++ ) \/\/ MISTAKE: FIXED number of bars{  x = Close[ i ]; \/\/ ERROR 10. because ‘i’ becomes greater than BarCountor like […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[53],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/320"}],"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=320"}],"version-history":[{"count":1,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/320\/revisions"}],"predecessor-version":[{"id":793,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/320\/revisions\/793"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}