Chart scrolling leads to error in plotting of data points

Hi, I have created a code in which data points from csv files are marked on the charts. I first obtain the data points from csv file and store them in the array. Next, I use PLOTSHAPES the charts using the array.

Problem is that charts are displaying wrong points when scrolled from left to right and back. At first chart shows correct points ... but when i scroll from left to right it shows wrong plots for the foreign array created from csv file. The code is as below.


file1 = "C:\\2015\\var_db.csv"; 
dt = DateTime();
//
// Initialize variables
ipivsh1[0]=0;
ipivsl1[0]=0;
ipivlh1[0]=0;
ipivll1[0]=0;
//
fh = fopen( file1, "r" );
//
if( fh )
 {
     for(i=0;i<barcount;i++ )
     {
         line1 = fgets( fh );
             ipivsh1[i] = StrToNum(StrExtract( line1, 9 ));
             ipivsl1[i] = StrToNum(StrExtract( line1, 10 ));
             ipivlh1[i] = StrToNum(StrExtract( line1, 11));
             ipivll1[i] = StrToNum(StrExtract( line1, 12));
             
     }
     //
     fclose( fh );
 }
 else
 {
     printf("ERROR: file can not be open");
 }
 Filter=1;


PlotShapes(IIf(ipivsh1, shapeSmallCircle, shapeNone), colorWhite, 0, 	High, Offset=15);
PlotShapes(IIf(ipivsl1, shapeSmallCircle, shapeNone), colorBlue, 0, 	Low, Offset=-15);
PlotShapes(IIf(ipivlh1, shapeCircle, shapeNone), colorRed, 0, 	High, Offset=30);
PlotShapes(IIf(ipivll1, shapeCircle, shapeNone), colorGreen, 0, 	Low, Offset=-30);

Filter=1;
AddColumn(ipivsh1,"ipivsh1");
AddColumn(ipivsl1,"ipivsl1");
AddColumn(ipivlh1,"ipivlh1");
AddColumn(ipivll1,"ipivll1");

correct_Screenshot_1 error_Screenshot_1

It is most likely that QuickAFL is enabled.

You can read here for more info http://www.amibroker.com/kb/2008/07/03/quickafl/

What appears from your code is you are running loop on BarCount, but this is not absolute value that will match the data points from your CSV file as it changes dynamically.

As a quick guess there are two simple ways,

  1. Not recommended but you can use all the Bars if the data size is small or fix the number of Bars
    I haven't really thought on this one though.

  2. Or use a timestamp in your File and align the plotting with the timestamp from the bars.
    This would need a bit more code but I think this would be the best way to go.

The above was a suggestion but do keep in mind that AFL is very fast, compared to reading from files, and the code you have written will force the AFL to read every time and in RTD probably for every tick.
This becomes very inefficient.

You should change the approach where you read and load your data from a file to an array that is properly aligned with the DateTime and then store it to a static array variable and then use this instead.

You could use a matrix to store the data too (numerical) and this would resemble the CSV file format which also can be stored as a static variable.
The two methods can be useful for you to choose the approach of storing the data, and then you can decide how you will align the data with bars as in each case it would differ.

If i use all the bars, it gives a subscript out of range error. My data size is around 40000 rows.
What is the best way to load data from csv file that is aligned with datetime? or simply put how can timestamp be associated with csv fields. Can you post a sample code. Data is as below

|nifsm_2015_data|01/01/15|9:00|0|0|0|0|0|
|nifsm_2015_data|01/01/15|9:30|0|0|0|0|0|
|nifsm_2015_data|01/01/15|10:00|0|0|0|0|0|
|nifsm_2015_data|01/01/15|10:30|0|1|0|1|0|

oh well, didn't expect this size, but this is like another symbol in itself :smiley:

You could read this and see if it gives you a direction
http://www.amibroker.com/kb/tag/addtocomposite/
https://www.amibroker.com/guide/a_addtocomposite.html