AmiBroker Documentation Item No. ab301.html

This is the part of AmiBroker documentation. Copyright 2000 Tomasz Janeczko. All rights reserved.


HOWTO: Import quotes in Metastock™ ASCII format

Introduction

AmiBroker has easy-to-use built-in Metastock importer. However it handles only Metastock™ binary format (*.DAT files). In order to import also popular ASCII format a custom script is needed.

This document will show you how to write and use such a script file. We will use JScript as a language and OLE Automation interface provided by Windows version of AmiBroker. You will need AmiBroker/Win32 version 3.2 or higher and Windows Scripting Host (WSH) installed on your computer. WSH is integrated into Windows 98, Windows 2000 Professional, and Windows 2000 Server. It is also available as a free component for Windows 95 and NT4.0 (you can download it from http://msdn.microsoft.com/scripting/windowshost/default.htm )

Metastock™ ASCII Format description

The format of MS ASCII file is very simple. First line contains fields description. It looks as follows:

<ticker>,<per>,<date>,<high>,<low>,<close>,<volume>

This line means that the data lines will contain seven comma-separated fields. First field will hold the ticker name, second - time period ("D" means daily data), third - quotation date. The rest will hold high, low, close prices and volume. All remaining lines hold just comma-separated data.

Here is an example showing four first lines from such a file:

<ticker>,<per>,<date>,<high>,<low>,<close>,<vol>
AAP,D,1/17/2000,5483.33,5332.01,5362.3,0
AKS,D,1/17/2000,9868.45,9638.03,9687.62,0
FET,D,1/17/2000,3741.3,3540.2,3570.81,0

The algorithm

In order to keep things simple we will not pay any attention to the first, format definition line. We will just assume that the file format is fixed. So, our algorithm will skip the first line then read the rest line by line. The contents of each line will be split into array of strings at each point where a comma occurred. Then the contents of the array will be interpreted as ticker name, date, price and volume data and inserted into AmiBroker's database using its Automation objects.

Implementation

Here is JScript function that implements described algorithm:

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" );

}

In order to import the quotations from the file just call this function with file name as an argument (note that JScript uses double backslash for representing (single) backslash in the path):

ImportMsASCII( "d:\\mstest.txt" );

You can find ready-to-use script file here along with some example data file.

Usage

First you must launch AmiBroker. Then you can run the script. WSH scripts can be run in two ways:

In the first case script output (WScript.Echo calls) is sent to console window, otherwise it is displayed in message boxes.

You can of course import multiple files by calling ImportMsASCII function multiple times within the script (changing the filename appropriatelly)

More information

For more information on using Windows Scripting Host, JScript and so on please check "Microsoft Windows Scripting Host: A Universal Scripting Host for Scripting Languages" article in MSDN.
For more information on AmiBroker's OLE Automation Objects please consult AmiBroker User's Guide.


Metastock is a trademark of Equis International, Inc. Microsoft, MS-DOS, Internet Explorer, MSDN, Windows, Windows NT, Win32, and Win32s are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.