/*
** AmiBroker/Win32 scripting Example
**
** File:	 Yahoo.js
** Created:	 Tomasz Janeczko, March 4rd, 2001
** Purpose:	 Download and import DAILY updates from Yahoo
** Language: 	 JavaScript (Windows Scripting Host)
**
**
*/

/**************************************************************/
/* Constants                                                  */
/**************************************************************/

/* The ticker to check */
/* Set it to empty string "" if you don't want to check */
ChkTicker = "^NDX";

/* The folder where the files will be downloaded */
DestDir = "C:\\Yahoo\\";

/* The name and the path to downloader program */
DownloaderPrg  = "URLGet.exe";

/* Force download - if true causes downloading data file */
/* even if it exists on the local drive */  
ForceDownloadFlag = true;

/* URL from where data are downloaded */
/* quote.yahoo.com supports US and Canada markets only */
URLPrefix = "http://quote.yahoo.com/d/quotes.csv?s=";
URLPostfix = "&f=snl1d1t1c1ohgv&e=.csv";

/* extension of file name, YYYYMMDD will be prepended */
FileExt = ".aqd";

/* Max. number of tickers to download in one run */
/* Should be no more than 30 because of the limitations of command line string length */
ChunkSize = 30;

/**************************************************************/
/* Main part                                                  */
/**************************************************************/

/* Create AmiBroker app object */
AmiBroker = new ActiveXObject( "Broker.Application" );
/* ... and file system object */
FileSys = new ActiveXObject( "Scripting.FileSystemObject" );
WshShell = new ActiveXObject( "WScript.Shell" );

function Download( URL, filename )
{
	if( ! ForceDownloadFlag && FileSys.FileExists( filename ) ) return true;

	if( WshShell.Run( DownloaderPrg + " " + URL + " " + filename, 0, true ) == 0 ) return true;

	WScript.echo("Download of " + URL + " failed." );

	return false;
}

function Import( filename )
{
	try
	{
		AmiBroker.Import( 0, filename, "aqd.format" );
	}
	catch( e )
	{
		return false;
	}

	/* refresh ticker list and windows */
	AmiBroker.RefreshAll();
	return true;
}

function CheckFolder()
{
	if( ! FileSys.FolderExists( DestDir ) )
	{
		FileSys.CreateFolder( DestDir );
	}
}

function IsValidDatabase()
{
	if( ChkTicker != "" && AmiBroker.Stocks.Count > 0 )
	{
		try
		{
			return AmiBroker.Stocks( ChkTicker ).Ticker == ChkTicker;
		}
		catch( e )
		{
			WScript.echo("The database currently loaded into AmiBroker does not have " + ChkTicker + "\nSo I guess this is not correct database.\nUpdate failed.");
		}
	}

	return false;
}

function Main()
{
	bOK = true;
	var Today = new Date();

	if( ! IsValidDatabase() ) return;
	
	CheckFolder();

	var Qty = AmiBroker.Stocks.Count;

	var TickerList = "";	

	var j = 0; /* this variable is used for marking different files from one day */

	for( i = 0; i < Qty; i++ )
	{
		TickerList += AmiBroker.Stocks( i ).Ticker;

		if( ( i % ChunkSize ) == (ChunkSize-1) || i == ( Qty - 1) )
		{
							
			y = Today.getFullYear();
			m = Today.getMonth() + 1;
			d = Today.getDate();

			bOK = true;

			filename = y + ( m < 10 ? "0" : "" ) + m + ( d < 10 ? "0" : "" ) + d + "_" + j + FileExt;

			URL = URLPrefix + TickerList + URLPostfix;

			if( Download( URL, DestDir + filename ) )
			{
				if( ! Import( DestDir + filename ) ) bOK = false;
			}
			else
			{
				bOK = false;
			}

			if( ! bOK && WshShell.popup( "The download and/or import of the " + filename + " has failed.\nThis can be because the data are not available or network connection problem.\nDo you want to abort?" , 0, "Abort updating", 4 + 256 ) == 6 )
			{ 
				break;
			}

			j++;

			TickerList = "";
		}
		else
		{
			TickerList += "+";	
		}

	}

    if( bOK )  WScript.echo("Update script finished. Your database is now up-to-date" );
    else       WScript.echo("Update script finished. There were, however, some errors during download/import");

}

Main();


