AI & Code

How Do You Actually Debug a Trading Script?

Ionut Claudiu Fratila · Published Nov 28, 2026 · 7 min read · —
DebuggingMetaEditorMQL5Pine ScriptProfilingAI Code Rescue
Lines of programming code on a dark computer screen

An Expert Advisor that opens a trade at the wrong moment, or a TradingView indicator whose alert fires on one bar and not the next, rarely explains itself. The usual first reaction is to change a line, recompile and watch the chart, then repeat. That can work, but it is slow, and with code written by an AI assistant it often means guessing at logic nobody on the team actually wrote.

Both major retail platforms ship proper tools for this job. MetaEditor has a step-by-step debugger and a profiler, MQL5 has logging and error-code functions, and TradingView has Pine Logs and the Pine Profiler. This article looks at what each tool does, where it helps, and where it stops helping.

Breakpoints and Stepping in MetaEditor

MetaEditor’s built-in debugger lets you place breakpoints in the code, run the program, and pause it when execution reaches one of them. While it is paused you can see the values of variables and step through the logic one call at a time. A breakpoint is set by double-clicking the grey margin beside a line, or with F9 (MetaEditor Help, Code debugging).

Once paused, the Debug tab shows the call stack, listing the file, function and line for each call that led to the current point. Alongside it is a watch list for expressions. You can switch on automatic display of local variables, look inside an array element such as A[3][4], expand an object’s members, and show integers in binary or hexadecimal. Step Into (F11), Step Over (F10) and Step Out (Shift+F11) move through the code at different levels (Code debugging).

For a misbehaving EA this is usually the fastest way to answer the question that matters: what did the code actually believe at the moment it made a decision?

Debugging on Live Prices or on History

MetaEditor offers two ways to start a debugging session. “Start on Real Data” (F5) runs the program on a special chart using prices arriving from the trade server. “Start on History Data” (Ctrl+F5) runs it in the Strategy Tester over whatever history interval you choose, so you do not have to wait for a particular market situation to happen again (Code debugging).

The history option is the one that changes how debugging feels. A fault that only appears around a weekend gap, a session change or a specific bar can be reproduced on demand, as many times as needed, with breakpoints in place. MetaQuotes describes debugging on history as running on either an emulated tick sequence or accumulated ticks received from the broker, which links directly to how the tester models price movement (Code debugging).

Logging and Error Codes in MQL5

Not every fault suits a breakpoint, and logging fills the gap. Print() writes to the Expert Advisor log and accepts up to 64 parameters, though not arrays; PrintFormat() gives control over how numbers are formatted. One detail catches people out: Print() does not work during optimisation in the Strategy Tester, so logging that depends on it goes quiet during optimisation runs (MQL5 Reference, Print).

The other half is error handling. In MQL5, GetLastError() is not reset when you read it. MetaQuotes advises calling ResetLastError() before the operation you want to check (MQL5 Reference, GetLastError; MQL5 Reference, ResetLastError). Without that reset, an old error can be blamed on the wrong call. It is a small, checkable pattern, and one worth looking for in any AI-written MQL5 EA. (MQL4 behaves differently: reading the error clears it (MQL4 Reference, GetLastError).)

Finding Slow Code with the MetaEditor Profiler

When the problem is speed rather than correctness, MetaEditor’s profiler works by sampling. It pauses the running program at a high rate and records where execution was each time. The report shows Total CPU, how often a function appeared on the call stack, and Self CPU, the pauses that happened directly inside that function. MetaQuotes calls Self CPU crucial for spotting bottlenecks, and notes that sampling does not change the code being analysed (MetaEditor Help, Code profiling).

Profiling can run on a live chart or on history in the Strategy Tester. The history option matters for indicators that only calculate when a new tick arrives: they can be profiled even at the weekend, when markets are closed. Results can be viewed by calls and by lines, are highlighted in the source code, and can be exported to Excel, HTML or CSV (Code profiling).

Pine Logs on TradingView

On TradingView, the log.info(), log.warning() and log.error() functions write timestamped messages to the Pine Logs pane. TradingView describes Pine Logs as the primary, most universal technique for debugging Pine code. They can be called from any scope, on historical bars and on every realtime tick. Each message links back to the line of source that produced it and can scroll the chart to its bar. The pane can filter by level, start date, text, Match case, whole word or regular expression, and shows the most recent 10,000 logs from historical bars (TradingView, Pine Script Debugging).

The most useful property for trading code is that logs created on realtime ticks are not rolled back. Plots and labels on the open bar show only that bar’s latest state, whereas the logs keep a record of what the script did on each update. That is exactly the evidence needed when an alert fires and then appears to vanish, or a signal looks different on history from how it behaved live. Adding barstate.isconfirmed to the log condition limits a realtime bar to one message, on its closing tick (Pine Script Debugging).

The same documentation recommends breaking long expressions into named variables so each part can be inspected, and returning tuples or using collections to pull values out of functions and other local scopes. That habit alone makes most AI-written Pine Script far easier to check.

The Pine Profiler

For slow scripts, the Pine Editor’s Profiler mode shows approximate runtime percentages beside significant lines and blocks of an editable Pine Script v6 script on the chart, with execution counts in a hover tooltip, and, when the script has at least four significant lines, flags the three highest-impact regions. Because the profiler wraps the code with its own measurement, TradingView says the results are estimates and won’t add up to exactly 100% (TradingView, Profiling and optimization; TradingView Help, Pine Profiler).

Limits and Caveats

A debugger shows what code does. It cannot say whether the trading idea behind it is sound, and we never make claims about how any strategy will perform.

Pausing a program changes its timing. MetaQuotes notes that platform events keep arriving while an EA sits at a breakpoint; they are queued and processed after the current handler exits (Code debugging). A timing-sensitive fault may behave differently under the debugger, and logging is often the better tool for it.

Debugging on history uses the tester’s tick model. We infer that faults tied to live execution, such as a broker rejecting an order, may not reproduce there. MetaEditor’s profiler reports sample counts rather than measured times, and TradingView calls its results estimates.

Finally, the source code matters. MetaQuotes states that debugging is impossible without the MQ4, MQ5 and MQH source files (Code debugging). On TradingView, only personal scripts can produce Pine Logs; a published script cannot, even if its code contains log calls (Pine Script Debugging). If all you have is a compiled EX4 or EX5 file, there is very little anyone can step through.

Conclusion

How do you actually debug a trading script? Reproduce the fault on history where you can, pause it and look at what the code believed, log what happens on the open bar, and check error codes properly. None of this needs extra software; it is all built into MetaEditor and TradingView. What it does need is the source code and the patience to measure before changing anything.

This is the everyday work behind our AI Code Rescue service, and it applies equally to MetaTrader and TradingView projects. If you have an EA or script that isn’t behaving, you can tell us about it here.

Drafted with AI assistance; reviewed, edited and approved by Ionut Claudiu Fratila.