How do I debug my formula?

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:

  1. 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.

  2. 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

  3. 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

  4. 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)

  5. 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.

  6. 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.

46 Likes
Amibroker backtest engine mechanism
Rotation of strategies in walk forward testing
Copying and pasting formula doesn't seem to work. Any remedies
Help with for statement - Signal recognition to TWS
Accessing a Matrix
How to reduce Multiple buy signals when working on hourly breakout
Delayed StopLoss Signal generated by execution of an order
Problem with IF funcion in Fibonacci AFL
AFL coding for Intraday - Back Testing
Lowest value since buy
Scale In Trade Not Working - Only buying on the initial signal
Output of string counting function and matrix dimensions
ApplyStop() Not Firing
Problem with SellPrice
Max open positions 10, but it is taking only 9
Max open positions 10, but it is taking only 9
Mixing up types, was: Loop Counter Not Working
Angle of a line
Portfolio level testing, set manual take profit for multiple signals
Unable to Scan/Explore/Backtest for IB symbols
Futures Backtesting - how to employ a constant leverage
Missing buy / Sell arrow on charts
If statement error - statement has to be numeric (not array)
Short signal Short position
Questions about multiple timeframes and futures data
SetForeign obviously changes OHLC arrays, was: Plortext signal buy and sell
SetPositionSize - results
Backtest help needed
How to use conditions from 3 different systems in one single system?
Conditional stop based on index filter
Last day of month
Please help to improve the performance of AFL
Scaleout rounding
Scaleout rounding
Division by Zero - Odd errors in Daily time frame but not Weekly
Not understanding what is going wrong with this if
Not understanding what is going wrong with this if
-nan(ind) in Backtest result
PositionSize based on a foreign array during backest
InWatchlistName not working when I have two WL in Analysis
Replicating applystop functionality using AFL code
How to act on actual entry exit signals instead of coded signals?
Need help to code rate-of-change filter and price filter
Sell message not send in telegram code is below i try to find solution
A question about BarIndex()
Problems creating own Index with Norgate and store with AddToComposite
For loop : run previous three day issue and get error 10 message
Need help backtesting buy and sell signals for buying all time highs
MarketCap Filter for Small Caps
COVER signals differentiation
Single symbol Equity()
How to add PositionScore to backtest results
Scale in failure due to failure of detecting the first trade
Applystop works fine, was: does not exactly do what it supposed to do
Mismatch between value in exploration and indicator values
Backtest based on existing Buy / Sell periods
How to calculate the exact number of stocks with condition of RSI
Plot and title coding
Lowest price in an array
RSC Mansfield indicator problems plotting the chart - some help please
Buy and sell prices
Using stddev() and log() in portfolio backtest
Sell signal ignored in backtest
Unable to move past random TWS IB API Cancelled issue
Matrix comparison
Set stop at low of entry bar
Loading quotations using FOREIGN() from a file of ticker names
Missing arguments error
Trades sent to bo.EnterTrade missing in backtest analysis results
Liquidity limit hit, was: SetPositionSize
How To - Add custom metric to Backtest Tradelist Report
Weird Difference between Explore & Backtest
List of exit signals of open positions
Variable updates previous values (Expected behavior, need suggestions)
Variable updates previous values (Expected behavior, need suggestions)
Scan shows custom stops not working - perplexed
Scan shows custom stops not working - perplexed
Error 29. Variable <name> used without having been initialized
timeframegetprice not working
An array issue with NormDist function
ApplyStop stopTypeNBar - N-bar stop returning stops greater than N Bars?
Plot shapes don't appear on charts
BarsInTrade logic question
Random entry with N bar(s) random exit
Problem to code filter in rotational trading model
Hello from the Netherlands
User error, was: Cross Function Problem?
Question regarding position sizing
Losing a matrix value after sort
Error during long running optimization
SigscaleOut did not occur like in the chart
Is that possible to read csv file for parameter by each symbol?
Exploration takes a long time once it reaches 100%
Compounding inside a for loop
How to average down (buy when position value drops x percent)?
First Buy pop up not working
AFL code doesn't work on different computer
AccountMargin based on S&P's Close to Moving Average
REF(Day(),-1) and REF(Day(),-2)
Position score issue i cannot resolve
Timeframeset and Timeframeexpand for non Minute boundary
How to pass VWAP array to my custom function?
My auto trading Script does not send a single order
Syntax for Sell: Top Bollinger Band (20MA, 1Std Dev) Crosses 50 MA
Modify limitprice during backtesting procedure
Getting running value of indicators in Multi Timeframe backtest
Getting running value of indicators in Multi Timeframe backtest
Intraday Strategies on Real Market
Back-testing on ieod data
ZigZag ATR vs Percentage
Asking about Back test and optimize Multiple symbols
Asking about Back test and optimize Multiple symbols
Generate Buy Signal with Camarila Pivots
Generating Duplicate orders - 1 Hourtimeframe - Rsi above 60
Move Stop to BE with an array
Problem/Difficulties in Finding Price Level & Bar Index in other Timeframe
Use StaticVarGetText if you want string, Exploration using Static Variables
Use StaticVarGetText if you want string, Exploration using Static Variables
Recreating code from "Weekend trend trader" book
Buy ans sell signal in same bar - Exploration
Weekly Periodicity Setting Yields Only 1st Result for NASDAQ 100 Stocks
Debugging back test scripts with print statements
First, Second and Third Bar of Today's Chart
BarIndex() Question
Don't override Close array, was: Filter = Close>10
Debug your code, was: Unexpected difference between Filter and Categor
Duplicate: Reduction of 3 elements to 2
Trades are not getting squared off - IntraDay, Suggest Correction in Code
Duplicate: Reduction of 3 elements to 2
Previous value of indicator
Scan showing only one signal per Bar
Backtest yields a single trade
How to obtain any past closing price for all stocks in a watchlist?
Backtesting Error 701. Missing Buy / Sell Variable Assignments
What are some possible ways for ExRem to affect backtest results?
Instant price trigger Buy / Short
SellPrice and CoverPrice wrong inside the loop
How to plot Buy/Sell signals on Range High/Low
Subsetting data according to dates
Manual Portfolio interface
Status("BarInRange")
Simple buy certain day and sell few days later
Copy-pasting spaghetti code is not the way to go, was: ApplyStop
User does not understand his own code, was: Exrem not working
Buy Short within a 8 Bars from Signal
Trying to switch type of stop loss while in trade
AddToComposite returns double value
Pulling my hair out trying to get auto trader working - all ideas welcome
Does not provide proper signal
Liquidity limit, was: partial fills on futures contracts within backtest
Too little raw entry signals generated
Backtest - Only first symbol is being traded - Position Size is set
Occasional use of margin
Debug Code - unexpected result
Same Entry Signal Type Closes Position
How to take profit on Heiken Ashi bars
Debugging pivot trading strategy
Shorter and Longer Bars
AFL code for a simple 2 MA Crossover signal with sufficient gap between the MA's
Study values change unexpectedly
Using Custom Backtest to find Min/Max MAE/MFE per Loosing/Winning Trades
How to create this array?
Learner's Questions
User must understand his/her own code, was: Backtest runs WRONG condition
Stopping a Buy Signal from Using an {EMPTY} Variable
Vwap first 15 minute candle high/low cross over scan error
Individual optimization results NOT different than backtester's
Position value is expressed in base currency, was: Incorrect Position Value
Bar Replay LastValue AFL issue (Weekly)
How can I get the Bar Interval for Range Bars?
Testing on multiple symbols with different commission rates
Trailing stop loss using ApplyStop and LLV(L, 2)
Help with Intrabar Exit
Need help with sigScaleOut, was: not working
Rank the top 20 biggest ROC of HHV
Intra day volume for nifty 50 index
Use _TRACE, not GfxTextOut for debugging CBT, was: Marketposition again
Compare buy price after a crossover with previous days EMA
Hidden windows don't paint in Windows OS, was: Signal not getting fired, unable to reproduce
Explorer returning 93375 records instead of 375 records on 1 minute data set
Whats wrong with my SumSince
Service call part of code not executing when running AFL as a Scanner
Watch List Filter
Different exits
How to avoid trades on particulas symbol in rotational test, was: Backtester isn't
Trailing Stop Loss
Fake histogram line in indicator
Lowest price within range
How to migrate MQL4 code Amibroker
Volume at Price --> Explore Possible?
SPY / TLT system
Get BarsSince() for nth occurrence instead of 1st occurrence
User mistake, sigScaleIn without existing trade just opens new trade, was: Debugger showing different results from Backtester
Is there a way to print values of array before plotting
Local computer problem, was: Backtests are running extremely slow
AB Portfolio Rotation CBT Level 1
Scale in as percent of allocated equity
Trouble with backtest
Timeframe AFL: help needed in checking formula
Create an Ichimoku Trend Line
2 actions at the same bar
Backtesting non time based chart
Any side effect with AFL when storing negative values in array?
Fixing Clenow's Strategy
Barssince Moving Averages Crossed
Indicator with lag or referencing past bars, was: Selecting subarray
How to get a Position Price at each moment?
Previous Bollinger Band Breakout and close higher
Please help In color change and added search with many thanks
AFL for multiple Bollinger Bands
How to restrict the barcount to only intraday
Problem with sigScaleOut, instead of Partial Exit, it Exit 100% Positon Size
Buy Signal generated but code not executed
Need Help in AFL code for pyramiding in Intraday
AddToComposite - Problem with field "V"
ExRem() removing all signals including the 1st one
AB Portfolio Rotation CBT Level 1
Rotational system (Differents ROC for each Wachtlist)
Different results from walk-forward and regular back-test
Help with code for Position sizing
Fixed Holding Period
No trades in backtest when reading from textfile
Custom BackTest
Way to detect a blocked trade due to insufficient funds?
Volume Exploration AFL Needed
Trouble referencing earlier values
How to use Trace function ?
How to make a condition close position.
Formula error 29
Timeframeexpand seems to skip ahead an interval?
In exploration why "i" is not changing ?
Is it possible to use a Limit Buy Order with EOD data?
Need the help in afl - Candlestick and indicator are shrink
Scale in as percent of allocated equity
Current portfolio equity and cash
Look for past Areas-candles AFL
"Triple Trouble": Data Alignment & Data Holes in Analysis
Help required for timeframe get price
SetBarsRequired function
How to make a variable freeze after first occurrence
Stop Loss changing dynamically
Help in understanding code part
Need help to Buy only when Moving Avg. cross and only after stochastic cross
Rotational trading
Creating a simple flag to build an indicator
Strange SetForeign() behaviour
Position Score Issue
Position Score Issue
Moving Average touch within last few candles
Problem with User Function in Portfolio explorations
Size of position is very low when back tested, despite available Initial Equity is more
Problem with User Function in Portfolio explorations
Clarifications on getting moving average of daily bars when base time interval is hourly
Simple mean reversion trading system that closes trades not correctly
Simple mean reversion trading system that closes trades not correctly
Difficulty to code sigScalein...please help
Sometimes correct sometimes incorrect results
Backtesting getting 0 results
Help with IIF function in a loop. Horizontal lines with labels. How to check if the code is efficient?
I do not understand Backtester Position Value
Help with IIF function in a loop. Horizontal lines with labels. How to check if the code is efficient?
Not saving data into a file for last hour from analysis with auto repeat
How to get the price of entry into the position?
Problem While Scan and signal are not trigger in Real Time
Latest one minute candle is greater than the average of last 10 one minute candle AFL required
AFL stoch, macd, overbought
Short not occuring where I expect it, was: ApplyStop produces incorrect signals occassionally
Store Yesterday Value Area High and Value Area Low in marektprofile
How to program a kind of state machine in afl?
Add arrow above the 10 days, 20 days, 60 days and 120 days from today
Amibroker Name() function comparison
Using PlotGrid() function in a simple & short AFL: No line is plotted
User Error. Was: Static Variable not working properly
TimeFrameSet and TimeFrameExpand
Right intra-bar exit
Do while conditions
Ent. & Exit signals not generating
Wanting to Backtest 1000's of Symbols
Coding Help With Filter Entries Via Sectors
_Trace every bar
Highest Volume Bar - Intra day
Evaluating condition in AFL
working all all ticker symbols
No Plot in the Chart
Exploration Results differ from PositionScore choices using rankings from Static Variables
How to find yesterday last barindex number through array?
Plot Lines on chart
Plot shapes location problem
Walk Forward Initial Capital Issue
Ross hook code issue for plot signal
How to remove excess shapes from chart?
Manual backtesting codes
Need value of previous high candle and current higher high candle
Understanding Backtest Results
Reference(Foreign("DIA","C"),-2)
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
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
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
Static var to calc an Watchlist AVG
How to make backtest of my portfolio
DayofWeek issues in a Rotation strategy
Endless loop detection
Highest Volume Bar - Intra day
Highest Volume Bar - Intra day
Need help using ROC
Volatile Trailing Stop with ApplyStop
Problems when backtesting for a wide range of years
Requested size is less than MinShares
Operators for chaining conditions: "+" or "||"
Getting different signals for apply option in formula editor and back testing( show arrows for actual trade)
Backtest generate no buy signals
Filter in Candlestick Exploration
LLV & HHV - finding low and high for a number of days
Output in Code Editor Watch window and Indicator plots are different
Output in Code Editor Watch window and Indicator plots are different
UpdateStats() with TimeInsideBar = 1
BackTesting of PDI-MDI strategy - Error
Need help regarding a trailing stop loss..
How can I let buy/sell price to be any number?
Simplified Dow strategy
Result not shows in exploration
GfxMoveTo , GfxLineTo | Looking for help trying to get End Point to work
Progress on the Binance project, but help needed with internetpostrequest()
Simplified Dow strategy
Rebalacing + Rotational Strategy
Amibroker 6.30.5 vs 6.00.2 backtest results
Sample code required for Options Backtesting
How to add ATR multiplier to Entry order after X consecutive highs/lows
Intraday trade re-entry based on a follow-up strategy
Exiting one bar before the signal
Analysis Backtesting Only Shorts are taken , Long positions not showing
RTFM TimeFrame docs, was: Time in Result List not correct
RTFM TimeFrame docs, was: Time in Result List not correct
At any time during the last X days, the reading must have been less than Y
Sell signals not flowing to IBController
Short Selling and Covering happening at the same minute
Plot not drawn on Daily and higher Timeframe
Trace function skips sometimes
Rotational system does not work with multiple conditions in Score
Setting up an InTrade array
How is loop executed
Buy Signals Dont Trigger above Certain Price


Tomasz,

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.

Jim

5 Likes

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.

2 Likes

The _Trace() function link is broken.

https://www.amibroker.com/guide/afl/_trace.html

See also:

_TRACEF() : print formatted text to system debug viewer

2 Likes

You're too nice. I was going to introduce him to google.com. :slight_smile:

1 Like

To view debug messages generated _TRACE function we can use two programs:

  1. DebugView, from SysInternals. @Tomasz please note thet the link in the documentation has changed (Microsoft bought SysInternals some time ago). You can get it from Microsoft in the new link:
    https://docs.microsoft.com/es-es/sysinternals/downloads/debugview
  2. DebugViewPlus. You can get it from:
    https://debugviewpp.wordpress.com/2014/01/04/7/
    It's more powerful that the Microsoft one, and (if you are programmer) you can get it's code from GitHub:
    https://github.com/CobaltFusion/DebugViewPP
6 Likes

Most of the comments here, so far, deal with tools, debugger, plots, etc. Instead, below, I present a more general approach to debugging, and design in general. (I know what I am talking about, >45 years as a successful electronic design engineer, hardware and software).

  1. "Divide and Conquer"! When you are having problems with your code, reduce it to the smallest snippet that still is problematic. In fact, even if you are not having problems (yet), you may start with small snippet and test, add, test, add, test,... until you are successful or you found that small addition that is problematic.

  2. Start with a "Written Plan" in your native language. This may be in the form of a specification, flow diagram, block diagram, hierarchy of user commands or routine calls, whatever you are most comfortable with and which you are most likely to refer to as you proceed. You are not limited to just one of the proceeding forms.

  3. BTW, 1. above is sometimes referred to as "Bottom Up", while 2. as "Top Down". Personally, I have done both, and still find both to be necessary and fun.

  4. If you have many years of experience, and are responsible for a 'product', you best start with 2. above. And might never do 1. yourself, but assign to others instead.

  5. If you are a true newbie, you best start with 1. above, but try to venture into as much of 2. as you can. At your level it is smart to start with the easiest stuff (builds familiarity, confidence, and good technique).

5 - more. Look over others' shoulders'. Learn from their successes and mistakes. Ask questions only after you have tried everything else first.

  1. "One learns little from one's own successes, but most from one's own mistakes". Mistakes should be like voting, "early and often".
17 Likes

I agree with the plot() function for debugging. Its the best tool to see how the arrays are behaving.

1 Like

...or misbehaving! :smiley:

2 Likes

Really, no one mention about using Interpretation Window for a quick checking? As easy as printf().

1 Like

Another solution, which in my opinion can be useful in some specific cases, is using Say() function. This time you don't use any visual output, but you simply hear what's going on and you are notified about some events right away even if you don't look at the screen or when for example AmiBroker is minimized.

Speak requests can be queued (spoken when previous requests are done) or next event can purge all previous speak requests. Of course such notifications can also be highly customized and provide detailed information.

https://www.amibroker.com/guide/afl/say.html