/*
** AmiBroker/Win32 scripting Example
**
** File:	 Import.js
** Created:	 Tomasz Janeczko, January 30th, 2000
** Purpose:	 Import quotes from Metastock ASCII file
** Language: JScript (Windows Scripting Host)
**
** The data is stored in lines with following format
** <ticker>,<per>,<date>,<high>,<low>,<close>,<volume>
**
*/


ImportMsASCII( "Import_Test.txt" );

function ImportMsASCII( filename )
{
	var fso, f, r;
	var ForReading = 1;
	var AmiBroker;
	var date;
	var quote;
	var fields;
	var stock;

	/* Create AmiBroker app object */
	AmiBroker = new ActiveXObject( "Broker.Application" );

	/* ... and file system object */
	fso = new ActiveXObject( "Scripting.FileSystemObject" );

	/* open ASCII file */
	f = fso.OpenTextFile( filename, ForReading);

	/* skip first line which contains format definition */
	f.SkipLine(); 

	/* read the file line by line */
	while ( !f.AtEndOfStream )
	{  
		  r =  f.ReadLine();
		  
		  /* split the lines using comma as a separator */
		  fields = r.split(","); 
		  
		  /* add a ticker - this is safe operation, in case that	 */
		  /* ticker already exists, AmiBroker returns existing one */
		  stock = AmiBroker.Stocks.Add( fields[ 0 ] ); 
		  
		  /* notify the user */
		  WScript.Echo( "Importing " + fields[ 0 ] );
		  
		  /* parse the date from the text file */
		  date = new Date( fields[ 2 ] );
		  
		  /* add a new quotation */
		  quote = stock.Quotations.Add( date.getVarDate() );
		  
		  /* put data into it */
		  quote.High  = parseFloat( fields[ 3 ] );
		  quote.Low   = parseFloat( fields[ 4 ] );
		  quote.Close = quote.Open = parseFloat( fields[ 5 ] );
		  quote.Volume = parseInt( fields[ 6 ] );
		  
	}

	/* refresh ticker list and windows */
	AmiBroker.RefreshAll();

	/* notify the user */
	WScript.Echo( "Finished" );

}

