Load Param and Optimize values from CSV file

For easier editing I was thinking to load default, min, max and step parameters values from a file based on trading pair. I was able to implement this and it's working, it loads params from file based on which chart I drop it (although unfortunately it doesn't update params if I then switch the trading pair).

...but now I'm having second thought about how fast this will be or if it will slow down long optimizations.

Could anyone confirm, will code bellow load the file for every optimization step?
Will it have impact on performance in any other way?
Also I'm not 100% sure, will optimization even be able to pick up the correct values for each step it has to perform?
Anyone spots anything else wrong with the code?

tradingPair = Name();
tradingAsset = StrExtract(tradingPair, 0, separator = '/');
tradingCurrency = StrExtract(tradingPair, -1, separator = '/');
optimization_folder = "optimization/";
fh = fopen(optimization_folder + "params_" + tradingAsset + "_" + tradingCurrency + ".csv", "r" );
if(!fh){
	fh = fopen(optimization_folder + "params_default.csv", "r" );
	if(!fh){
		Error("No file");
	}
}

for(i = 0; !feof(fh); i++){
		line = fgets( fh );
		//skip header line
		if(i == 0){
			continue;
		}
		paramName = StrExtract(line, 0);
		StaticVarSet(paramName, ParamOptimize(
			paramName, 
			StrToNum(StrExtract(line, 1)), 
			StrToNum(StrExtract(line, 2)),
			StrToNum(StrExtract(line, 3)), 
			StrToNum(StrExtract(line, 3)), 
			StrToNum(StrExtract(line, 5))
		));		
	}
fclose(fh);

//now we can access UI/optimization params via static variable
myParam = StaticVarGet("myParamNameInFile");

The code above is used to feed inputs of ParamOptimize function (combines native Optimize and Param functions) which then allows me to use the same code for UI and optimization parts + toggle optimization on/off with last param (optimization_enabled). Here is the code

function ParamOptimize(pname, defaultval, minv, maxv, step, optimization_enabled)
{
	r = Param(pname, defaultval, minv, maxv, step);
	if(optimization_enabled){
		r = Optimize(
			pname,
			r,
			minv, 
			maxv, 
			step);
	}
	
	return r;
}
1 Like

EDIT:
I tried to start backtest and optimization and it seems like it's working perfect. Also according to task manager AmiBroker (optimization) disk usage is at 0%, so that makes we wonder if CSV file is read only once...

Could anyone confirm...are AFL files only read, executed and interpreted once, when I drop formula on chart or when it's read by backtest/optimization and then that interpretation is used by AmiBroker engine? Or does every backtest/optimization re-read and executes AFL file?

On the other side, would there be a way to force read UI/optimization parameters from CSV file when symbol is changed on chart (guess that would involve executing AFL file again)?