Replicate AFL → APX conversion Formula Paths?

I'm updating AFL files on the fly with Python during batch runs, and need to generate .apx files that match what AmiBroker expects internally. I’ve tried doing this through OLE, but it only works with the old Analysis window and saves in a binary format that batch mode won’t accept.

The most reliable approach so far has been using shortcuts to load/save via the UI in a separate EXE, but that’s fragile. The main issue is replicating the exact parsing AmiBroker does when embedding AFL into the <FormulaContent> block — even small differences cause the “formula is different” error.

Has anyone figured out the exact encoding or formatting rules AmiBroker uses when saving APX files from the UI?

Thanks!

You have not provided any info on your OS locale.
see what is returned locale — Internationalization services — Python 3.13.2 documentation

you can look for

with open( 'c:\\path\\ApxFile1.apx', encoding="utf-8") as f:
    read_data = f.read()
    ## your operation
    f.write()

try encoding with utf-8 or utf-16 or ascii,
I haven't tested but one of these should work.

If you are using another library to write, it must have an encoding argument as well.

Thanks for the input! The encoding itself (UTF-8, UTF-16, etc.) doesn’t seem to be the main issue—my script can read and write APX files fine. The challenge is replicating AmiBroker’s internal AFL-to-APX formatting rules, like escaping < to &lt;, doubling backslashes (but not in comments), and preserving quotes exactly. I’ve managed to match the output a few times, but every time the AFL code changes slightly, some new edge case breaks the formatting again. If there were a way to export the fully encoded APX directly from AmiBroker, or a library that mimics its exact behavior, it would save a ton of trial and error.

Just use "Keep existing AFL file when opening project" option and you would not need to worry about storing/updating AFL inside APX.

2 Likes

Thank you! That fixed it — I'm no longer getting the 'formula changed' warning. I had seen that before but misread it as meaning it keeps the version stored in the APX rather than the AFL itself.