AmiBroker's OLE Automation Object Model

AmiBroker object model hierarchy. V5.00

Index of objects

ADQuotation

Properties:

Description:

ADQuotation class keeps one bar of advance/decline information

ADQuotations

Methods:

Properties:

Description:

ADQuotations is a collection of ADQuotation objects

Analysis

Properties:

Methods:

Properties:

Description:

Analysis object provides programmatic control of automatic analysis window

Notes:

Analysis.Backtest( Type = 2 ); - runs backtest
Type parameter can be one of the following values:
0 : portfolio backtest/optimize
1 : individual backtest/optimize
2 : old backtest/optimize

Analysis.Optimize(Type = 2 ); - runs optimization
Type parameter can be one of the following values:
0 : portfolio backtest/optimize
1 : individual backtest/optimize
2 : old backtest/optimize

Analysis.Report( FileName: String ) - saves report to the file or displays it if FileName = ""

Analysis.ApplyTo - defines apply to mode: 0 - all stocks, 1 - current stock, 2 - use filter
Analysis.RangeMode - defines range mode: 0 - all quotes, 1 - n last quotes, 2 - n last days, 3 - from-to date
Analysis.RangeN - defines N (number of bars/days to backtest)
Analysis.RangeFromDate - defines "From" date
Analysis.RangeToDate - defines "To" date
Analysis.Filter( nType: short, Category : String ) - sets/retrieves filter setting
nType argument defines type of filter 0 - include, 1 - exclude
Category argument defines filter category:
"index", "favorite", "market", "group", "sector", "index", "watchlist"

Application

Methods:

Properties:

Description:

Application object is main OLE automation object for AmiBroker. You have to create it prior to accesing any other objects. To create Application object use the following code:

JScript:

AB = new ActiveXObject("Broker.Application");

VB/VBScript:

AB = CreateObject("Broker.Application")

AFL:

AB = CreateObject("Broker.Application");

Window

Methods:

Properties:

Description:

Window object provides programmatic control over charting window.

Windows

Methods:

Properties:

Description:

Windows is a collection of Window objects.

Commentary

Methods:

Description:

Commentary object gives programmatic control over guru commentary window.

Document

Methods:

Properties:

Description:

Document object represents active document (of 'chart' type). In document-view architecture each document can have multiple windows (views) connected. Name property defines currently selected symbol for the document.

Documents

Methods:

Properties:

Description:

Documents is a collection of document objects.

Market

Properties:

Description:

Market represents market category and its related data (i.e. per-market advance/decline information)

Markets

Properties:

Description:

Markets is a collection of Market objects

Quotation

Properties:

Description:

Quotation class represents one bar of price data

Quotations

Methods:

Properties:

Description:

Quotations is a collection of Quotation objects. It represents all quotations available for given symbol. Quotations collection is available as a property of Stock object.

Stock

Properties:

Description:

Stock class represents single symbol data. For historical reasons the name of the object is Stock, but it can hold any kind of instrument (including futures, forex, etc).

Stocks

Methods:

Properties:

Description:

Stocks is a collection of Stock objects. It is available as a property of Application object.

Notes:

Stock.WatchListBits (long) - each bit 0..31 represents assignment to one of 32 watch lists to add a stock to nth watch list write (JScript example):
Stock.WatchListBits |= 1 << nth;

Stock.WatchListBits2 (long) - each bit 0..31 represents assignment to one of watch lists numbered from 32..63 to add a stock to nth watch list write (JScript example):
Stock.WatchListBits2 |= 1 << ( nth - 32 );

Stock.DataSource ( 0 - default, 1 - local only )
Stock.DataLocalMode ( 0 - default, 1 - store locally, 2 - don't store locally)

Practical Examples:

Example 1: Running simple backtest

/* create AB object */
AB = new ActiveXObject(
"Broker.Application");

/* retrieve automatic analysis object */
AA = AB.Analysis;

/* load formula from external file */
AA.LoadFormula(
"afl\\macd_c.afl");

/* optional: load settings */
// AA.LoadSettings("the_path_to_the_settings_file.abs");

/* setup filters */
/* backtest over symbols present in market 0 only (zero-based number) */
AA.ClearFilters();
AA.Filter(
0, "market" ) = 0;

// uncomment line below to
// AA.Filter( 1, "market" ) = 2; // exclude 2nd market

/* set apply to and range */
AA.ApplyTo =
2; // use filters
AA.RangeMode =
0; // use all available quotes

/* run backtest and display report */
AA.Backtest();
AA.Report(
""); // empty file name means display report



Example 2: Batch backtesting

Caution: It will run backtest of EVERY formula stored in C:\Program Files\AmiBroker\AFL
on all symbols of current database. After each backtest the report is generated and saved
into the file named <formula name>.HTML.

You can modify this AFL path in the script itself (you can open it with Notepad).

Below comes the listing.

/*****************************
*
* BatchTest.js
*
* Batch testing sample script
* Shows how to use JScript and new AmiBroker 4.23
* 'Analysis' object to perform batch backtesting
* and generate reports
*
* Created: Dec 21, 2002 TJ
* Last modification: Dec 22, 2002 TJ
*
* Copyright (C)2002 Amibroker.com
*
* Status: Freeware
* You can use/modify/adopt this code freely
*
*/


/* The directory where AFL files are stored
** Also reports generated by the bactest
** will be saved here
*/


AFLFolder =
"C:\\Program Files\\AmiBroker\\AFL"; // MODIFY TO FIT YOUR SETUP

WScript.Echo(
"Batch testing of all AFL files stored in " + AFLFolder );

var AB, AA;
var fso, f, f1, fc, s;
var filename;

/* Create AmiBroker object and get Analysis object */
AB = new ActiveXObject(
"Broker.Application");
AA = AB.Analysis;

/* backtest over symbols and all quotes*/
AA.ClearFilters();
AA.ApplyTo =
0; // use symbols
AA.RangeMode =
0; // all quotes


/* to use filters you should uncomment lines below
// AA.ApplyTo = 2; // use filters
// AA.Filter(0,"watchlist") = 2 /* watch list number */
;
// AA.Filter(0,"group") = 0 /* group number */;

/* Create FileSystemObject */
fso = new ActiveXObject(
"Scripting.FileSystemObject");

/* Iterate through all files in the folder */
f = fso.GetFolder(AFLFolder);
fc = new Enumerator(f.files);
for (; !fc.atEnd(); fc.moveNext())
{
   
// we need to add empty string to make sure that filename is a string object
    filename =
"" + fc.item(); 
  
   
/* check the AFL extension */
   
if( filename.substr( filename.length - 4 ).toUpperCase() == ".AFL" )
    { 
      
if( AA.LoadFormula( filename ) )
       {
          AA.Backtest();
         
          reportname = filename.substr(
0, filename.length - 3 ) + "HTML" ;
         
          AA.Report( reportname );
// generate report
       }
    }
}
 

Example 3: Execute commentary

AB = new ActiveXObject("Broker.Application");
AB.Commentary.LoadFormula(
"C:\\Program Files\\AmiBroker\\AFL\\MACD_c.afl");
AB.Commentary.Apply();
AB.Commentary.Save(
"Test.txt");
AB.Commentary.SaveFormula(
"MACDTest.afl");
//AB.Commentary.Close();