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()
/_TRACEF()
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()
andPlotShapes()
Plot is not only for charts. It can be used to visualise content of any array and andPlotShapes()
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) -
use Edit->Prettify Selection in the AFL Editor
This helps understanding code structure. As mentioned in this thread, sometimes incorrect indentation leads to misunderstandings. Using Prettify uncovers true structure of the code and helps finding problems with program flow. -
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.