PureBytes Links
Trading Reference Links
|
I maintain an amibroker database of stocks, obtaining the closing prices
from Yahoo. Unfortunately, the quote data does not give an industry categroy
for the stocks. Yahoo does hold industry information but it is hidden on
another page.
So, I've written some javascript to take a list of stocks and go and get the
industry information from Yahoo. The script is given below between the <<<
Start cut here >>> and <<< End cut here >>> instructions.
The script uses a well known control component called ASPTear that basically
is designed for ASP pages to access HTML resources. The ASPTear component is
free and can be obtained from
http://www.alphasierrapapa.com/IisDev/Downloads/AspTear.zip
The ASPTear component should be registered as it is a COM component. So
unzip the file somewhere, start a command prompt and type "regsvr32
ASPTear.dll" in the installed directory.
The script takes two parameters. The first is the path to the file
containing the list of stocks. The second is an optional list of industries.
The broker.industries file supplied with your base installation of amibroker
should be specified.
If the broker.industries file is specified the script will use in the index
of the identified industry name into this file as the industry code and will
set this property on the stock in your amibroker database. If the file is
not specified then the script will simply write out a list of the stocks
followed by their industry name.
So to run the script, first start amibroker with the appropriate workspace,
then type in the following:
cscript industry.js stock_list.tls broker.industries
assuming that all the relevent files are in the same directory.
Hope this is useful. As far as I know this only works for the US exchanges.
But I haven't explored any other exchanges.
David Regan
Enterprise Critical Systems Ltd
EMail: David.Regan@xxxx
<<< Start cut here >>>
/*
/* Create AmiBroker app object */
asptear = new ActiveXObject( "SOFTWING.ASPTear" );
/* ... and file system object */
FileSys = new ActiveXObject( "Scripting.FileSystemObject" );
AmiBroker = new ActiveXObject( "Broker.Application" );
function download( URL)
{
var content = null;
try
{
content = asptear.Retrieve(URL, 2, "", "", "");
}
catch (e)
{
if (e instanceof Error)
{
WScript.Echo("Exception accessing URL:", URL," Error description:
"+e.description);
}
else
{
WScript.Echo("Unknown exception accessing URL:", URL);
}
}
return content;
}
function getIndustry(content)
{
var industry = "unknown";
var start = content.indexOf("href=/p/industries.html>Industry</a>");
if (start > -1)
{
var end = content.indexOf("</a></small>",start);
if (end > -1)
{
var chunk = content.substring(start,end);
var tokens = chunk.split("<small>");
chunk= tokens[0];
for (i=1;i<tokens.length;i++)
{
chunk += tokens[i];
}
tokens = chunk.split("</small>");
chunk= tokens[0];
for (i=1;i<tokens.length;i++)
{
chunk += tokens[i];
}
tokens = chunk.split(">");
var industry = tokens[3];
tokens = industry.split("&");
industry = tokens[0];
for (i=1;i<tokens.length;i++)
{
industry += "&" + tokens[i];
}
}
}
return industry;
}
function getStockListFromFile(listFile)
{
var stocks = null;
if (FileSys.FileExists(listFile))
{
var f = FileSys.OpenTextFile(listFile);
var content = f.ReadAll();
f.Close();
stocks = content.split("\r\n");
}
return stocks;
}
function getIndustryCodesFromFile(listFile)
{
var industryCodes = new Object();
if (FileSys.FileExists(listFile))
{
var f = FileSys.OpenTextFile(listFile);
var code = 0;
while (!f.AtEndOfStream)
{
var line = f.ReadLine();
if (line != null)
{
var firstSpace = line.indexOf(" ");
var name = line.substring(firstSpace+1);
industryCodes[name] = code++;
}
}
f.Close();
}
return industryCodes;
}
function Main()
{
var args = WScript.Arguments;
var listFile = args.Item(0);
if (args.Count() == 0 || args.Count() > 2)
{
WScript.Echo("Usage: cscript categorise.js <ticker file> [<industry code
lookup file>]");
WScript.Quit(1);
}
if (listFile == undefined)
{
WScript.Quit(1);
}
var lookUpFile;
var industryCodes = null;
if (args.Count() == 2)
{
lookUpFile = args.Item(1);
industryCodes = getIndustryCodesFromFile(lookUpFile);
}
if (lookUpFile == undefined)
{
WScript.Echo("No industry lookup file specified. Industry names returned
for information only");
}
WScript.echo("Categorizing stocks from file " + listFile);
var stocks = getStockListFromFile(listFile);
for (j=0; j<stocks.length;j++)
{
var stock = stocks[j].toUpperCase();
if (stock.indexOf("^") == -1 && stock.length != 0)
{
var firstLetter = stock.charAt(0);
var url = "http://biz.yahoo.com/p/"+firstLetter+"/"+stock+".html";
var content = download(url);
if (content != null)
{
var industry = getIndustry(content);
if (lookUpFile != undefined)
{
var code = industryCodes[industry];
if (code != undefined)
{
WScript.Echo(stock +","+ industry + ","+code);
try
{
var aStock = AmiBroker.Stocks.Item( stock );
aStock.IndustryID = parseInt(code);
}
catch (e)
{
WScript.Echo("Stock "+stock+" is not in the AmiBroker database");
}
}
else
{
WScript.Echo(stock+","+ industry +", (code unknown)");
}
}
else
{
WScript.Echo(stock+","+ industry);
}
}
}
}
AmiBroker.RefreshAll();
}
Main();
<<< End cut here >>>
|