Debugging the code is one of the must-have skill for any programmer. It helps to smartly identify the errors in code and take corrective actions accordingly. Every IDE (integrated development environment) is equipped with tools to debug the code, and so is Amibroker AFL editor. In this post, we’ll understand step by step ways to debug Amibroker AFL code.
Click this post to read a massive guide to get started with Amibroker software, understand it’s features, pros-cons, and learning resources.
Steps to Debug Amibroker AFL Code
Amibroker started offering a visual debugger starting from version 6.1.0. So if you are using an older version of Amibroker, you won’t see some of the option listed in the below steps:
Step 1: Open your AFL code in the formula editor.
Step 2: Setup the desired breakpoint by moving your cursor to the line of code and press the F9 key. You may also click on the red circle in the toolbar OR use option “Debug –> Insert/Remove Breakpoint” for the same.
What is Breakpoint? – In debugging terms, it is an area of code where program execution is intentionally paused to examine the variables or any calculation step. For example – if you assign the output of simple moving average (SMA) function to a variable, you can set up a breakpoint at that assignment step to check is the value is getting assigned correctly.
You can set up as many breakpoints as you want
Step 3: Start debugging. Click on “Debug –> Go” or press the F5 key. The AFL execution will start, and it will pause at your first breakpoint. A yellow arrow will mark the position of the current execution. See below:
Step 4: When the debugger is stopped at the breakpoint you can check the value of the variable by hovering the mouse over the variable. A tooltip will appear with the variable contents.
Alternatively, you can enable the “Watch Window” to see the list of all variables. Click on “Windows –> Watch” to enable it.
The watch window has several other features too which are noteworthy like ‘Arrays’ tab ‘Expression editor’ etc.
Step 5: Step to the next breakpoint by clicking “Debug — >Step Over” or press the F10 key. If the next breakpoint contains a user-defined function and you want to step inside the function you should use the option “Debug –> Step Into” or F11 key.
Step 6: Repeat step 4 and 5 until the end of the program
Also Read: Best PC Configuration to run Amibroker
Using the TRACE Function to Debug
If you are using a version of Amibroker older than 6.1.0, you may probably use the TRACE function for debugging.
With this approach, you cannot set any breakpoints, instead, you’ll have to surround the line (that you want to examine in real-time) with _TRACE(). See below:
You can see the output of the _TRACE() function in the log window. Press Window –> Log to activate.
You can also direct the output to an external logger like DebugView.
There is another function _TRACEF() which does the same thing, except that the first argument is a printf style formatting string.
Usage of Trace function for debugging may not be as convenient as visual debugger but still, it is helpful and offers customization in terms of formatting what to print, etc.
Other ways to Debug Amibroker AFL Code
There are several other workaround methods to debug your AFL code. Chose the one that suits you:
- Use exploration to see the value of any variable you want (at each candle). You need to use AddColumn() function to do that.
- If you want to debug the backtesting process, use the “Detailed Log” option from the backtester settings. Doing that will help you visualize execution steps and trades taken at each candle.
- Use printf() function within your formula and see the value of variables in the interpretation window