Extract init variables from main program

Posted By: NorbertSz

Extract init variables from main program - 04/25/23 14:06

Hello,

I am making a research paper and looking for a way to easily change the input conditions to demonstrate the constancy of the investigated effect with massive input variations. To achieve this, I wanted to make a single file with the different versions that could store all the variables and changing parts of the code. It's not that simple, like changing just the main program, because the software is more complex, and I also extracted some functions that varies.

So I would make one single file that stores everything that varies, including variables and functions.
Like this:
#include <varies/v1.cpp>
and I can change it anytime for <varies/v2.cpp> or <varies/v152.cpp>

My problem is that there are some system variables that can only be defined in the main program before anything I call, for example, StartDate.
I would like to do something like this:

Code
DLLFUNC void run() {

	if (is(INITRUN)) {
		initRunSetup();
		...
	}
	....
}


The initRunSetup() is defined in the <varies/v1.cpp> like this:

Code
void initRunSetup(){
	StartDate = 20200101;
	EndDate = 20210101;
	...
}

...and also other vars and functions that varies and used by the main software...


But it doesn't work. If I don't set up StartDate in the main program's main block, then nothing happens; it just uses the default values.
How can I achieve this?

Thank you!
Posted By: AndrewAMD

Re: Extract init variables from main program - 04/25/23 15:10

Just put all of the variables in a struct, then save the struct as a file. File size is the size of that struct. Then load the file to the struct as neeeded.
Posted By: NorbertSz

Re: Extract init variables from main program - 04/30/23 09:06

Thank you for the tip; that would work. Finally, I separated the variable parts into multiple files, and this way I can mix them more easily to produce the test cases. Like this:

Code
...
#include "var_tradingrules/8.cpp"
...
DLLFUNC void run() {
	
	//
	//	INIT RUN
	//

	#include "var_period/12.cpp"
	#include "var_asset/3.cpp"
	...
}
Posted By: NorbertSz

Re: Extract init variables from main program - 07/21/23 14:41

Actually, you were right. It's much better to use your method, because that way we don't need to recompile the code every time a variable changes, and therefore we can automate the whole testing process by combining all the inputs with a PowerShell or a Batch script. I take this note for further readers who need the detailed solution.

First make a struct to store the settings:
Code
struct settings_data {
	int startDate;
	int endDate;
	std::string asset;
	int barPeriod;
	...
};
settings_data settings;


Then, in the main() function, you can load all the data into that struct.
For example if you are using file input like that:

Code
2018
2020
EURUSD
5
...


You can read it line by line like this:

Code
#include <string>
#include <fstream>
...
DLLFUNC void main() {
	std::string line;
	ifstream file("C:/zinput/input.txt");

	getline(file, line);
	settings.startDate = atoi(line.c_str()) * 10000 + 101;		//2018 => 20180101
	getline(file, line);
	settings.endDate = atoi(line.c_str()) * 10000 + 1231;		//2020 => 20201231
	getline(file, line);
	settings.asset = line;
	...
}


Or if you have Zorro S you can also use the command line arguments.
Now you can easily reach the settings it the run() function.
Code
DLLFUNC void run() {
	StartDate = settings.startDate;
	EndDate = settings.endDate;
	asset(settings.asset.c_str());
	BarPeriod = settings.barPeriod;
	...
}
© 2024 lite-C Forums