Assigning preferred parameter value to each security

I have built a trading system that consists of 5 parameters. I ran it through optimization process for every security in S&P500. I like to be able to do the following:

  • Step#1: To identify and tag which optimization step is preferred for a given security (I can do that either by exporting the result list and importing it to a database for further analysis and tagging the preferred record)

  • Step#2: To run the explorer against the S&P500 stocks to identify trade entry/exit based on preferred parameter values (from Step#1) for each given stock

The way I was thinking of achieving that is:

  1. Used what was done in Step#1
  2. Do an ODBC call to a stored procedure and pass the ticker symbol as the stored procedure parameter
  3. The stored procedure to lookup and return the preferred parameter values
  4. Set the parameter values in formula

Conceptually the above should work (have not tried it yet), but there is a lot of work involved. Is there a cleaner way (something built in to AmiBroker for custom parameter value assignment by stock)?

I still like to export the result list and import it into a Postgres for further analysis but looking alternative ways for setting custom parameter value for each given stock.


@Max, you might want to take a look at StartcVars. You can have them be persistent, so you could do step1 and then Set you Static Vars for each of your 5 parameters. Exporting still if you wish.

Then, when doing step 2, just StaticVarGet to get your variables.

Might be simpler than the ODBC work.

@snoopy.pa30, thanks for the reply. However I am not following how Static Vars can be of help. I need to have parameter value lookup capability based on the stock symbol during Exploration and plotting the indicator. For example, as the result of the Step#1, I will end up with a list of custom parameter values for each S&P500 stock. The following is a sample of how it will look:

Stock Symbol = A, RSI Period = 9, RSI MA = 5, Fast MA = 5, Slow MA = 20, Trailing Stop Multiplier = 2.25
Stock Symbol = AAL, RSI Period = 7, RSI MA = 5, Fast MA = 4, Slow MA = 16, Trailing Stop Multiplier = 2.50
Stock Symbol = ZTS, RSI Period = 8, RSI MA = 4, Fast MA = 5, Slow MA = 18, Trailing Stop Multiplier = 3.50

Having the above, I like to be able to lookup the right parameter record for each given stock at the time of Exploration and when plotting the indicator.

So, again, it is unclear how Static Var will accomplish that in a cleaner way or be simpler than making an ODBC call.

Is there a feature in AmiBroker to perform lookup against an external? For example provide a CSV file of a predefined structure to be used as external lookup data?


Hi Max,
I had similar requirement but related to my own trailing stop calculation. The calc uses symbol specific parameters stored into a an external csv file. I then read the file for each of the symbols and use the stored parameters for calculating my stops.

Here is how I do it in the AFL. This should give you enough of a concept that you could use to achieve what you are trying to do.

Cheers, Ziza

// Get Active Trades Params Stop Parameters
file = "F:\\AdminZ\\Documents\\TWS_Intra\\StopParams.csv"; // this file stores Symbol Specific Parameters
// The above file contains all Symbol Specific Parameters, required for Trailing Stop calc.
//  If SizingStop (i.e initial sizing stop) has not been specified in the file, the SizingStop will be calculated based on default ATR Multipler for 1R specified by Def_OneRmulti
// Initialise variables
EntryDate=StartDate=FillPrice =SizingStop=SymTarget =Sym1RMulti= 0 ;

fh = fopen( file, "r" );
if( fh )
     while( ! feof( fh ) )
         line = fgets( fh );
         // get the ticker symbol from the file
         sym = StrExtract( line, 0 );
         // if ticker matches the current symbol
         if ( Name() == sym )
             EntryDate		= StrExtract( line, 1 ) ;
             StartDate 	=	DateToDateNum ( EntryDate ) ;
			 FillPrice 		= 	StrToNum( StrExtract( line, 2 ) ) ; 
			 SizingStop	=	StrToNum( StrExtract( line, 3 ) ) ;
			 SymTarget 	= 	StrToNum( StrExtract( line, 4 ) ) ;

     fclose( fh );
     Error( "ERROR: file can not be open" );

Moderator comment: USE CODE button/tags! The code without code tags is useless. I added them again so formula is displayed OK, but please next time do this yourself as my time is NOT yours and is better spent on more creative things than editing incorrect formatting.

Hey @Max, the idea behind the static variables is that you can use the stock symbol as part of the name of the symbol, and have the code pull the value back for you.

Here is a topic that might shed some light on it for you. Sector Rotation Strategy Using StaticVarSet and StaticVarGenerateRanks

While most of the link may not apply to your situation, the code in the first post shows how to create the variables with the symbol in it. In your case you would have your 5 sets of variables that you could create, and then when running the explore, you could use a similar code to retrieve the values.

I did not look at @Ziza code, but like most powerful programming languages, there are many ways to accomplish the same thing.

Hope you find an appropriate method.

@Ziza, thanks for the suggestion and providing the sample code. It look simple enough. I'll give it try.

@snoopy.pa30, thanks for your input and further explanation on how StaticVar can be used.

Has anyone here ever used the ODBC and relational database approach or do you know of anyone that has?