How do I debug my formula?

Tags: #<Tag:0x00007f2d7a705590>


Continuing the discussion from How do I learn AFL?:

Many new users with zero programming experience struggle when their formula works incorrectly. Here are few hints that everyone should use to make finding errors easier.

First of all: you have to get insight into what the formula is actually doing, not what you think it does.

Typically new user has no idea what is happening inside. Pretty often you make an assumption that things work one way but in fact they work differently. Do not assume things. Check the manual and if it is still unclear - try it. If you are want to use a new AFL function, use it alone in chart or exploration and experiment a bit to learn how it works. There are few tools that allow you to get that insight:

  • use _TRACE() function a LOT!

    It is essential tool for every formula. It allows you to know the sequence of operations your formula is doing and the values of variables.

  • use the Exploration as a debug tool to display your arrays

    Using _TRACE for arrays is tedious and you would get a lot clearer picture of what is happening in your arrays if you use exploration’s AddColumn to display value of ANY variable

  • visualise arrays via Plot() and PlotShapes()

    Plot is not only for charts. It can be used to visualise content of any array and and PlotShapes() is very useful in displaying content of arrays holding boolean conditions

  • when backtesting, check the Detailed Log

    Inspecting the Detailed Log will tell you precisely what signals are generated each and every bar, what are position scores and when / why signals are ignored (due to constraints such as insufficient funds)

  • last but not least use the Debugger

    The debugger allows you to single-step thru your formula, set breakpoints, watch the content of your variables and more

Keep in mind that debugging your own code is your task. If you want to use the formula for trading you absolutely must understand how it works. If you rely on somebody else to fix errors in your code you will not understand what is going on and you risk your money. So in your own best interest, spend time on debugging your code yourself.

Amibroker backtest engine mechanism
Help with for statement - Signal recognition to TWS
Rotation of strategies in walk forward testing
Copying and pasting formula doesn't seem to work. Any remedies
Accessing a Matrix
Daily scans in Weekly time frame
How to use debugger in amibroker 6.00.1?
What is wrong in my code
Rotation of 5 Momentum stocks (like Clenow)
Signals "blink"
BUY Stop Order back test
Scale in as percent of allocated equity
Static var to calc an Watchlist AVG
Problem with IF funcion in Fibonacci AFL
How to make backtest of my portfolio
DayofWeek issues in a Rotation strategy
Endless loop detection
Highest Volume Bar - Intra day
Manual backtesting codes
Need help using ROC
Volatile Trailing Stop with ApplyStop
Is it possible to use a Limit Buy Order with EOD data?
Problems when backtesting for a wide range of years
Requested size is less than MinShares
Operators for chaining conditions: "+" or "||"
Highest Volume Bar - Intra day
Need value of previous high candle and current higher high candle
Understanding Backtest Results
Why am I getting Error 60
AFL Context Execution Order
Portfolio Backtesting with symbol sequence
User not reading docs, was: I am not seeing correct values in watch window
Need the help in afl - Candlestick and indicator are shrink
Array always return 0
~~~Equity starts at 10,000.01 instead of 10,000.00
Calculate profit/Loss for the day
Type error for if expression
Type error for if expression


For me, this is the single best piece of advice on how to use these trouble shooting tools, that I have learned. I am now able to see down into the roots of my systems. Where before I was only able to see the trunk and branches and only hope the roots were solid.

Please consider this a “standing thank you” for this and all of your thinking, building and implementation of AB.



The tips on how to debug AFL code is very helpful. Thanks.



Excellent advice @Tomasz.

The other item I would include would be use of the Detailed Log to identify what the backtester is doing internally on each bar. This can be invaluable when trying to figure out why an expected entry or exit isn’t occurring, or an unexpected one is.

Analysis and Chart results are different
The last completed bar, was: Auto Trade Problem