Ability to change ToDate (End Date) in APX Batch

Is there a way to have the ability to change the End Date when running a batch of apx files. Of course, I currently set my end date to a future date (2050) so when my batch runs, the data is always current. However, I found that I need to have the ability to run the batch for a past date.. primarily for a past month end. When creating fact sheet reports, I need the reports to be as of the month end... for example today, I need to have the 03/29/2018 data... I have to manually run each backtest with the month end date to achieve this now (I have 26 strategies). Please advise...

The APX file is just XML, so one option is to modify it with an external app before running it from the batch.

suggestions on where to start? i am a self taught coder... proficient at vba and now afl code after 5 years... no idea on how to create an app to change the in the 26 xml files in the folder

thanks

@mfu5324, as said by @mradtke XML files are just text files.

So you can also use standard AFL functions to modify them (see the "File Input/Output", "String manipulation" and "Date/Time" sections).

On the other hand, if you want to do it differently and learn something new, since you are already familiar with VBA, you could write some VBScript code to modify them.
VBScript is a "light-weight" sub-set of Visual Basic, and it is one of the default scripting languages in Windows..

The Microsoft reference to scripting documents both the VBScript language and how to use tthe FileSystemObject to work with files and folder.

See also this artiicle and code examples titled Use VBScript to Create, Write, and Modify XML Files.

i almost have it... but am stuck..here is the afl code...

FileDir = "C:\Users\Brian Vestal\Desktop\AmibrokerXML\Core Tactical Moderate.APX";// + List;

// read lines to variables
fh = fopen( FileDir, "r" );
line = 0;
while( ! feof( fh ) )
{
VarSetText("line"+(line++), fgets( fh ));
}
fclose( fh );

//My problem is.. i need to know the line # of the "ToDate" so i can replace the date with one i input
// in my file, the line number is 26 where ToDate appears.. so i have to hardoce LineNumber = 26
// HOW CAN I GET THE LINE NUMBER ... ( 2050-01-11 )....
LineNumber = 26;
ChangeLine = "line" + "26";

//then i can do the String replace and successfully change the ToDate....
ToDate = StrReplace( VarGetText( ChangeLine ), "2050-01-11", "2018-03-29" );
VarSetText( ChangeLine, ToDate );

fh = fopen( FileDir, "w" );
for( i = 0; i < line; i++ )
{
fputs( VarGetText("line"+i ), fh );
}

fclose( fh );

The APX file is in XML format. The whole point of XML is that you find items of interest using the tags, not by the position in the file. So rather than looking for line number X, just look for the line that contains the substring "<ToDate>".

thanks. as you can see I am self taught programmer.. never worked with xml. I really don't know where to go now.... do I use StrFind... StrReplace... do I have to use VarSetText....do I open the file once with "w"... sorry for all the questions, but when I search google for examples it returns C++ results which I can follow, but have trouble translating it into afl

@mfu5324:

Your Best Resource is the Help File / User's Guide.

Look for the AFL Function References (Alphabetical and Categorized).

Also, AFL is similar to C++, so should not be too difficult to translate.

Take your time, READ, Try (Backup first), and Post your code - WITH Code Blocks ("</>", or "```")

Thanks so much.. i solved it. and it works... code below

** only thing im stuck on... in ParamDate, the result is formatted mm-dd-yyyy.. and it writes this to the xml file. I need the date to be yyyy=mm-dd of course.... i know i could just have 3 parameteres (month, day, year).... any advice on how to fix this?

Thanks again for all your help on modifying the xml file!!!!

ToDate = ParamDate("To Date","2050-01-01",1);
ToDateNew = Todate;

FileDir = "C:\Users\Brian Vestal\Desktop\AmibrokerXML\Core Tactical Moderate.APX";// + List;

fh = fopen( FileDir, "r" );
line = 0;
while( ! feof( fh ) )
{
Tag = fgets(fh);

	if (StrFind(Tag,"<ToDate>"))
	{
	CurrentDate = StrMid(tag,8,10);
	Tag = StrReplace( Tag, CurrentDate, ToDateNew );	
	}
	VarSetText("line"+(line++), tag);
}

fclose( fh );

fh = fopen( FileDir, "w" );
for( i = 0; i < line; i++ )
{
fputs( VarGetText("line"+i ), fh );
}

fclose( fh );

The simplest solution would be to simply use ParamStr() instead of ParamDate() so that you can type in whatever characters you want.

Yep... (but I wanted to be fancy and have the calendar date picker ...haha)
thanks so much for all your help

@mfu5324, using AmiBroker version 6.2 or greater:

Version(6.2); // required for Date/Time conversions

ToDate = ParamDate("Pick ToDate","2018-01-01", 2); /// 2 returns a DateTime 
ToDateString = DateTimeFormat( "%Y-%m-%d", ToDate ); 
_TRACE("ToDate string: " + ToDateString);
1 Like