Exploration not adhering to date range setting

I have a problem with an exploration not adhering to the date range settings I’ve specified. I have “Apply to” set to “All Symbols”, Range set to “From-To dates”, the “from date” set to 12/31/2013 and the “to date” set to “12/31/2020” (I’ve read that setting the “to date” to a date in the distant future is the way to ensure than analysis always runs through the last available data). However, the exploration is still executing over data back to the beginning of my database in 1896.

The database I’m using is populated and managed by the Norgate Data Updater plug-in. The AFL code attached to the analysis is below.

//#pragma maxthreads 1
#include_once "Formulas\Norgate Data\Norgate Data Functions.afl"

OutputDir = "E:\\Norgate\\AB_Export\\2014";

AssetID = NorgateAssetID();

while ((fh = fopen(OutputDir + "\\" + AssetID + ".csv", "w", True)) == 0) ThreadSleep(10);    //Loop shouldn't be necessary but it is...

fputs("date,open,high,low,close,volume,div_ord,div_spec,unadj_close,unadj_volume,pad_status\n", fh);

dn = DateNum();
uc = NorgateOriginalCloseTimeSeries();
uv = NorgateOriginalVolumeTimeSeries();
pad = NorgatePaddingStatusTimeSeries();
od = NorgateAllDividendsTimeSeries(1);
sd = NorgateAllDividendsTimeSeries(2);

for (i = 0; i < BarCount; i++) {
    if (dn[i] >= 0) {
        mmdd = dn[i] % 10000;
    } else mmdd = (dn[i] + 1000000) % 10000;
    fputs(StrLeft(StrFormat("%.0f", dn[i] + 19000000), 4) + StrRight(StrFormat("%.0f", 10000 + mmdd), 4) + StrFormat(",%.7g,%.7g,%.7g,%.7g,%.7g,%.7g,%.7g,%.7g,%.7g,%.7g\n", Open[i], High[i], Low[i], Close[i], Volume[i], od[i], sd[i], uc[i], uv[i], pad[i]), fh);
}
fclose(fh);

It’s standard behavior for any Analysis to execute over a greater range of bars than you have specified in your date range. For example, if your analysis references MA(C,100), then AB will load at least 100 bars prior to the start date so that you will have a valid MA(100) value on your start date. This is documented in the AB Help.

You can probably solve your problem by using Status("barinrange"), which is documented here: http://www.amibroker.com/guide/afl/status.html

2 Likes

Ah, thanks. The reason I was trying to limit the output date range in the analysis settings instead of the AFL code itself is that I wanted to improve the performance of the exploration by hopefully limiting the data that is loaded into memory from the Norgate database, because the performance is rather poor. (12 minutes on an i9 7980XE with the database and output directory on an m.2 drive.)

Any suggestions on how to improve the performance? I suppose I could set up a separate Norgate database that is limited to my desired date range, but I’d like to avoid that.

Examples on how to export analysis barinrange have been posted here already


Better code

/// @link http://forum.amibroker.com/t/exploration-not-adhering-to-date-range-setting/3592/3
#include_once "Formulas\Norgate Data\Norgate Data Functions.afl"

Version( 6.20 );

OutputDir = "E:\\Norgate\\AB_Export\\2014\\";

AssetID = NorgateAssetID();
uc = NorgateOriginalCloseTimeSeries();
uv = NorgateOriginalVolumeTimeSeries();
pad = NorgatePaddingStatusTimeSeries();
od = NorgateAllDividendsTimeSeries(1);
sd = NorgateAllDividendsTimeSeries(2);

bir = Status( "barinrange" );
dt = DateTime();

if( Status( "action" ) == actionExplore ) {
	Filter = 0;

	fh = fopen(OutputDir + AssetID + ".csv", "w", True);   
	if( fh ) {
		fputs("date,open,high,low,close,volume,div_ord,div_spec,unadj_close,unadj_volume,pad_status\n", fh);

		for (i = 0; i < BarCount; i++) {
			if( bir[i] ) {
				ds = DateTimeFormat("%Y%m%d", dt[i] );
				qs = StrFormat(",%g,%g,%g,%g,%g,%g,%g,%g,%g,%g\n", O[i], H[i], L[i], C[i], V[i], od[i], sd[i], uc[i], uv[i], pad[i]);
				fputs(ds + qs, fh);
			}
		}
		
		fclose(fh);
	}
}
1 Like