AmiBroker Strategy Debugging

Debug an AmiBroker AFL strategy using logs and trace statements, no paid debugger needed.

AmiBrokerDebuggingAutomation
In short

You can debug an AmiBroker strategy (AFL) without the paid debugger. Turn on the AmiBroker log window, set the AutoTrader additional logs chart parameter ON, and add _TRACE() statements to your AFL. The logs tell you whether your placeOrder function actually ran and why a buy or sell condition did or did not become true.

Introduction

This article shows how to debug strategy AFLs in AmiBroker. Most automated trading users in India use the free trial version of AmiBroker, which does not have the debugging feature enabled. So we explain a very basic way to debug using logs.

Demo video

The demo is available on YouTube.

Prerequisite

You need to install AutoTrader and AmiBroker software.

Workflow

Your commands and functions (place order, cancel order, square off position, and so on) flow from AmiBroker to AutoTrader. AutoTrader then executes them on your trading platform.

When a placeOrder* function is invoked, it performs order validation if the validation flag is set to 1. Once validation succeeds, it writes the order details to the C:\autotrader\data\order\orders.csv file. AutoTrader always keeps a watch on this file and immediately picks up the order details. It parses the order to make sure it has the required fields, and then places the order on the selected trading platform.

You can also look at:

  • Connecting AutoTrader with other software (explains how to connect to AutoTrader from any technology)
  • AmiBroker AutoTrader workflow (insights about how communication with AutoTrader works)

How to debug?

Enable AmiBroker logs

Go to the AmiBroker menu (Window -> Log).

This opens the log window. Inside the log window:

  • Run-time — this tab shows any errors that might exist in your strategy.
  • Trace — this tab shows logs. Right click and make sure that “Trace Output” is ticked for both Internal and External.

Turn on the “additional logs” parameter

The AutoTrader API provides a chart parameter that can be set ON to print additional logs. See below:

Check whether the placeOrder* function is being executed

An order is placed only when the placeOrder* function in your AFL executes. A signal appearing on your chart does not mean an order will be fired. In fact, a signal on the chart has nothing to do with an order being fired. You must make sure the placeOrder* function runs when you expect it to.

AmiBroker logs show whether the placeOrder* function is being executed. Here is an example of how the logs look:

If you do not see anything in the logs, your placeOrder* function is not being executed. In that case, add _TRACE() statements in your AFL to print additional information. This helps you understand the strategy execution flow. For a quick result, use the bar replay option and run it over an area that has a buy or sell signal.

_TRACE("Outside buy condition");

if ( LastValue(Buy) == True )
{

_TRACE("Inside buy condition, calling placeOrder function for a BUY order...");

placeOrderUsingParams(AT_EXCHANGE, AT_SYMBOL, "BUY", AT_ORDER_TYPE, AT_QUANTITY, buyPrice, defaultTriggerPrice(), 1);
}

If your logs only print “Outside buy condition”, it means you need to recheck your if condition. Your if condition is not evaluating to true when you expect it to be true.

The _TRACE function can also print the values of variables to the logs. Here is an example:

_TRACE("Outside buy condition");
_TRACE("IF Buy Condition result = " + LastValue(Buy));

if ( LastValue(Buy) == True )
{
_TRACE("Inside buy condition, calling placeOrder function for a BUY order...");

placeOrderUsingParams(AT_EXCHANGE, AT_SYMBOL, "BUY", AT_ORDER_TYPE, AT_QUANTITY, buyPrice, defaultTriggerPrice(), 1);
}

Now look at the logs. They tell you that the result is not true when you expect it to be. So you may have to verify the code that populates the Buy signal and correct it, or you may have to modify your if condition so it becomes true when you expect it to be true.

Order is placed from AmiBroker but execution still has issues

Look at the AutoTrader Support notes or email support. Once an order is placed from AmiBroker, it is then AutoTrader’s responsibility to place that order onto the trading platform.

How to map an AmiBroker order to the order on your trading platform

Once an order is placed, you can use the order_id returned by the placeOrder* function to trace that order. Here is an example.

You can see the order id in the AmiBroker logs:

On the AutoTrader menu (Trading -> Portfolio), look at the orders table:

  • Pub-Id: Order id given by AmiBroker.
  • Id: Order id given by your trading platform (Kite/Upstox etc.).

AutoTrader logs

You can also use these order ids to look at the AutoTrader logs. The logs path is C:\autotrader\logs, and there is one log file for each day.

References

AmiBroker Forum: How to debug?

Next steps

Was this page helpful?

Last updated 20 June 2026