Exporting watchlists. Trouble with fclose()

Hi all,

I am trying to export several watchlists (from Norgate Premium Data) into CSV files but for some reason the code exits when fclose() is run. The first file is exported successfully but the program exits early. Not sure what’s going on. Any help is appreciated.

By the way, I know I can use the GUI to export CSV files but I’d rather automate as much as possible.

watchlists = "S&P ASX 20,S&P ASX 200,S&P ASX 100";
path = "F:\\Trading Data\\ASCII\\Watchlists\\";

for( i = 0; (wl = StrExtract(watchlists, i)) != ""; i++){
	filename = StrFormat("%s.csv", wl);

	fh = fopen(path + filename, "w");

	if (fh){

		fputs("Ticker\n", fh);
		list = CategoryGetSymbols( categoryWatchlist, CategoryFind(wl, categoryWatchlist));

		for( i = 0; (sym = StrExtract(list, i)) != ""; i++) {
			fputs(sym + "\n", fh);


You are using variable ‘i’ for both the inner and outer loop.

1 Like

All the watchlists are stored in the “WatchLists” subdirectory under the database directory. They have a TLS extension, but they are simply text files with one symbol per line. In other words, they’re the same thing you would get by exporting the watchlist.


You are both correct. Thank you both.

You don’t need fopen, fputs, fclose at all.

Simply use ShellExecute with shell command.
Here I made example:

// Exporting Watchlists to CSV
/// @link http://forum.amibroker.com/t/exporting-watchlists-trouble-with-fclose/1960/5

printf( "<b>Source folder:</b>\n" );
inputpath = "C:\\Program files\\AmiBroker\\Data\\Watchlists\\";// Choose WL folder of source DB

printf( "<b>\nOutput folder:</b>\n" );
outputpath = "C:\\";// Choose export folder here

printf( "<b>\nWatchlist files:</b>\n" );

_N( list = fdir( inputpath + "*.*", 1 ) );

trigger = ParamTrigger( "Export WLs", "CLICK HERE" );

for ( i = 0; ( filename = StrExtract( list, i ) ) != ""; i++ ) {
    printf( filename + "\n" );
    if( trigger && StrFind( filename, ".tls" ) ) 
		ShellExecute( "cmd", "/c copy \"" + inputpath + filename + "\" \"" + outputpath + StrReplace( filename, ".tls", ".csv" ) + "\"", "", 0 );

its throwing an error in list and fdir line

That is because you are running a very old version of AmiBroker (older than version 5.7)! Is it genuine one?

Thanks. I’m just reading the watchlists from their orginal location rather than exporting/copying them now.

ok yes i was thnx for correcting that