Objects in Amibroker

hello,

I am an AFL code learner.
My question is related to objects in Amibroker.
I used/use an update system for my database, based on Amibroker objects
** **
specifically .import ....????.format

AB = new ActiveXObject( "Broker.Application" )
AB.LoadDatabase("C:\\Program Files\\AmiBroker\\myBBDD");

// AAII_SENTIMENT
AB.Import(0, MyPath + “ \\MACROUSA\\AAII_SENTIMENT.csv", "AAIISENTIMENT.format");
// CURVASTIPOSFED
AB.Import(0,MyPath + “\\MACROUSA\\CURVASTIPOSFED.csv", "CURVASTIPOSFED.format");
// TIPOS
AB.Import(0,MyPath + “\\MACROUSA\\TIPOS.csv", "TIPOS.format");
// TREASURYYIELD
AB.Import(0,MyPath + “\\MACROUSA\\TREASURYYIELD.csv", "TREASURYYIELD.format");
// USLEADINGINDICATOR
AB.Import(0,MyPath + “\\MACROUSA\\USLEADINGINDICATOR.csv", "USLEADINGINDICATOR.format");
AB.SaveDatabase();

and run an updater on .APX files.

`AB = new ActiveXObject("Broker.Application");                   
AB.LoadDatabase("C:\\Program Files\\AmiBroker\\myBBDD")           
//
var Path = "C:\\Program Files\\AmiBroker\\Formulas\\APX\\";		// Load Path to APX directory
//
var APXList = ["ibex35.apx", "nasdaq100.apx", "russell2000.apx", "SP500.apx"];
var LongArr = APXList.length;
//
for (i = 0; i < LongArr; i++) {
                                                           		 // Runs for the number of items you have el Array
  fch = APXList[ I ];                                     		// Load the file to use, from the list
  Analyzes( fch);                                            	// Call the scan function to explore the APX     
}
….
….
function Analyzes( FichAPX ) {
    try {
            PathYFich = Path + FichAPX;                           	// Add to the path the name of the file to be processed
            NewA = AB.AnalysisDocs.Open( PathYFich );           // Creates the NewA object, New Analysis, 
                                                                    			// from Object AB (amiBroker), with its object
                                                                    			// AnalysisDocs and the Open property (open) the passed file 
            if ( NewA ){                                            		// Asks if it exists so that there is no error, if you find it
                NewA.Run( 1 );                                      		// Runs the scan of the entire object (1), 
                while ( NewA.IsBusy ) WScript.Sleep( 500 );        	// While AmiBroker is busy waiting for 0.5sg, 
                NewA.Close();                                       		// Closes the object of the new analysis 
            
            AB.SaveDatabase();                                      		// Save the database you have in use, the one loaded at the start
            }                                                          
    catch (err) {
        _TRACE ( "Ends_Err."+FichAPX );
    } 
}
`

I read the document https://www.amibroker.com/guide/objects.html and I have been warned of the danger; but if it works in javascript, why doesn`t it work well in AFL?. In the objects use the examples that are in the doc, with javascript and they both work well if I run them from DOS.

I have recently tried to pass these processes to .afl formula and the import process has worked without problems.


specifically .import ....????.format // work well

objAB = CreateObject("Broker.Application");
//objAB.LoadDatabase("C:\\Program Files\\AmiBroker\\miBBDD");                   // It isn't nesesary but BBDD is in use

// AAII_SENTIMENT
objAB.Import(0, MyPath +"MACROUSA\\AAII_SENTIMENT.csv", "AAII_SENTIMENT.format");
// CURVASTIPOSFED
objAB.Import(0, MyPath +"MACROUSA\\CURVASTIPOSFED.csv", "CURVASTIPOSFED.format");
// TIPOS
objAB.Import(0, MyPath + "MACROUSA\\TIPOS.csv", "TIPOS.format");
// TREASURYYIELD
objAB.Import(0, MyPath + "MACROUSA\\TREASURYYIELD.csv", "TREASURYYIELD.format");
// USLEADINGINDICATOR
objAB.Import(0, MyPath + "MACROUSA\\USLEADINGINDICATOR.csv", "USLEADINGINDICATOR.format");
objAB.SaveDatabase();

and run an updater on .APX files

`Path = "C:\\Program Files\\AmiBroker\\Formulas\\APX\\";
DataPath = "C:\\Program Files\\AmiBroker\\External Data\\";
APXList = LeeFichero(DataPath + "FichComposyteAPX.txt");		// Load the list from an external file
								// list Separate by “,”
LongArr = StrCount(APXList,",")+1;

for (i = 0; i < LongArr ; i++ ){                    		 
      Fich = StrExtract( APXList, i );
      FilePath = Path + Fich;                       		// add de path to file process
// AB is an open database ,created at the beginning 
// AB = CreateObject("Broker.Application");
      ObjTrabj = AB.AnalysisDocs.Open( FilePath ); 	 // Creates the work object, New Analysis, 
                                                                    		// from Object AB (amiBroker), with its object
                                                                    		// AnalysisDocs and the Open property (open) the passed file 
      if ( ObjTrabj ){                                    		// Asks if it exists so that there is no error, if you find it
	ObjTrabj.Run( 1 );                              		// Executes the scan on the object (1) 
	while ( ObjTrabj.IsBusy ) ThreadSleep( 100 ); // While AmiBroker is busy waiting for 0.1sg, 
// and repeat this process until the scan is complete
	ObjTrabj.Close();                              		 // Closes the object of the new analysis 
      }
     AB.SaveDatabase();
     
}
`

but the .APX process gives me an error which I don't understand, because the class exists and should be able to be derived by model hierarchy, and even defines it in note(2) of the doc.

Can you help me find more documentation and examples where to learn about these objects?
What object can I use to derive the .AnalysisDocs., its methods and properties?

Thanks for your help.
All the best

Unfortunately your question isn't clear enough and does not provide all necessary details to give you an answer. Please describe the GOAL first. Please follow this advice: How to ask a good question

@andrsmartinbrun, when calling OLE from AFL I always create/get a reference to any object before calling its methods, so I will modify the code as follow:

// assume AB is already instantiated
newA = AB.AnalysisDocs;
ObjTrabj = newA.Open( FilePath );

Hi Beppe.

Thanks for the follow-up, I have implemented it and the error disappears; but I have some problem with the iteration of For.

I am studying it

The problem is solved with the proposal of @beppe, even the failure in the second item in the list (it has been necessary to clean the name of rare characters that are incorporated into the process).

I will try to clarify the question how you ask me to @Tomasz.

I have a database that I update daily, but I do not have the amplitude / MT (Market Timing) values that I use in some indicators.

Therefore, after doing the daily update I pass a formula / code, to calculate values for me through AddToComposite(), for various markets and/or watchList. For example:

IBEX, SP500, Nasdaq100 , Russell, and other.

For each of these WatchLists are calculated among others

// Tickers that rise go down and repeat in the day
Suben = C>Ref(C,-1);
Bajan = C<Ref(C,-1);
Repiten = C==Ref(C,-1);
AdSum = Suben + Bajan + Repiten;
// Linea AD
AdDif = Suben - Bajan;
//
// Additional conditions for the filters to be saved in the database
MediasAlza = MA( C, 50 ) > MA( C, 200);
CierrSobreMA200 = C >= MA(C,200);
CierrSobreMA50 = C >= MA(C,50);
//Maximos y minimos
Maximos = H > Ref(HHV(H,252),-1);
Minimos = L < Ref(LLV(L,252),-1);
//We save the calculated values in the database
AddToComposite(Suben, "#" + nombre + "Suben", "C", atcFlagDefaults);
AddToComposite(Bajan, "#"+ nombre + "Bajan", "C", atcFlagDefaults);
AddToComposite(Repiten, "#"+ nombre + "Repiten", "X", atcFlagDefaults );
AddToComposite(AdSum, "#"+ nombre + "AdSum", "X", atcFlagDefaults);
//AD Values
AddToComposite(AdDif, "#"+ nombre + "AdDif", "C", atcFlagDefaults);
// 
AddToComposite(MediasAlza, "#" + nombre + "MediasAlza", "X", atcFlagDefaults );
AddToComposite(CierrSobreMA200, "#" + nombre + "ActSobrMA200", "X", atcFlagDefaults );
AddToComposite(CierrSobreMA50, "#" + nombre + "ActSobrMA50", "X", atcFlagDefaults );
//Basic amplitude values based on new highs and lows
AddToComposite(Maximos, "#" + nombre + "Max", "X", atcFlagDefaults );
AddToComposite(Minimos, "#" + nombre + "Min", "X", atcFlagDefaults );

Where "nombre" is the literal of the watchlist, so all the new symbols are together for each watchlist
I have created some . APX (one for each watchList/market) that contain the formula and filter to make that update from the Analysis window, and they work correctly.
Create a procedure .bat /.cmd to run those updates without opening Amibroker; in them, I use Amibroker /OLE Objects with JavaScript; for which, I created a table / array with the 4 files I have
(IBEX.apx,NYSE.apx,SP500.apx y NASDAQ.apx) and through an iteration 'For', I pass one by one the updates of each .apx and get the ticker/symbols for each market/watchList.
Recently, I saw a database updater made from an .afl and I said to myself, how can I interface everything I have in this code, so I don't have to do separate processes and then open Amibroker?
I started to do it and everything works for me, including updates from .csv and txt files obtained from the Internet. But the Scan process with .apx resists me.
Initially I did it the same as in JavaScript, because other processes had worked well for me.
(objAB.Import(0, MyPath +"MACROUSA\AAII_SENTIMENT.csv", "AAII_SENTIMENT.format")
but by including the adaptation of JavaScript to the following code

APXList = LeeFichero(DataPath + "FichComposyteAPX.txt"); // Load the list from an external .APX file
							                                          // list Separate by ","
LongArr = StrCount(APXList,",")+1;

newA = AB.AnalysisDocs;                                                   // This proposal by @beppe it woks well.

for (i = 0; i < LongArr ; i++ ){                    		 
    Fich = StrExtract( APXList, i );
    FilePath = Path + Fich;                                                        // add de path to file process

    ObjTrabj = newA.Open( FilePath );                                   

    //ObjTrabj = AB.AnalysisDocs.Open( FilePath );                // Creates the work object, New Analysis, 
                                               	                                       // from Object AB (amiBroker), with its object
                                                                                               // AnalysisDocs and the Open property
                                                                                                // it dosen't work well (error) 
    if ( ObjTrabj ){                         	                                       // Asks if it exists so that there is no error, 
                                                                                               //if you find it
	ObjTrabj.Run( 1 );                     	                               // Executes the scan on the object (1) 
	while ( ObjTrabj.IsBusy ) ThreadSleep( 100 );              // While AmiBroker is busy waiting for 0.1sg, 
                                                                                              // and repeat this process 
                                                                                              //until the scan is complete
	ObjTrabj.Close();                                                        // Closes the object of the new analysis
    }
    AB.SaveDatabase();
     
}
 newA.Close();
…

I had an error when creating the object with the (ObjTrabj = AB. AnalysisDocs.Open( FilePath ); ), told me the mistake that I did not have that method.
With the suggestion of @beppe to split the derivation, the error has disappeared

newA = AB.AnalysisDocs;
ObjTrabj = newA.Open( FilePath );

And it works correctly, for the first item of the list of files to be processed; but when it tries to process the second element of the list, it gives me an new error that the .apx file is not correct, or has some internal error.

This was because the process of reading the list left some characters at the beginning of the filename to be tried that prevented finding that file. (LF, CR or space). When I have cleaned the name the iteration works with any number of files.

Thanks a lot
Greetings

2 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.