AmiBroker Strategy Debugging

Introduction

In this article, we take a look at how to debug strategy afls in AmiBroker. As most of our automated trading users in India are using free trial version of AmiBroker which does not have debugging feature enabled; we explain a very basic mechanism of debugging.

Demo Video

The demo is available on YouTube.

Prerequisite

You need to install AutoTrader & AmiBroker software.

Workflow

Your commands/functions like (place/cancel order, square off position etc.) flow from AmiBroker to AutoTrader & they are executed on your trading platform by AutoTrader.

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

You can also look at: 

How to debug?

Enable AmiBroker Logs

Go to AmiBroker menu (Window -> Log)

This will show you log window, now inside your log window:

Run-time – this tab shows any errors that might exists in your strategy

Trace – This tab shows logs, Right click & make sure that “Trace Output” is ticked for both Internal & External.

Print Additional Logs Parameter

AutoTrader api provides a chart parameter, which can be set ON to print additional logs. See below:

Find whether placeOrder* function is being executed or not?

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

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

If you do not see anything in the logs then it means your placeOrder* function is not being executed. In such case, add _TRACE() statements in your afl to print additional information. This will help you understand the strategy execution flow. For quick result, use bar replay option and run it over an area which has buy/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);
}

Now if you see that your logs only print “Outside buy condition“; then it means you need to recheck your if condition. Your if condition is not evaluating to true, when you expect it to be true. Here is an example of how to do that:

Remember _TRACE function can be used to print values of variables to the logs as well.

_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 see the logs, this will tell you that the result is not true when you expect it to be. So you might have to verify your code that populates the Buy signal & correct it OR you might have to modify your IF condition to correctly become true when you expect it to be true.

Order is placed from AmiBroker but there are further issues with its execution

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

How to map AmiBroker order to order on Trading Platform?

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

You can see order id in AmiBroker logs:

On AutoTrader menu (Dashboard -> Trading Platform), look at orders table:

Pub-Id: Order id given by AmiBroker

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

AutoTrader Logs

Additionally you can use these order ids to look at AutoTrader logs. Logs path is (C:\autotrader\logs) there is one log file for each day.

After spending more than 12 years developing Financial Software, I took a break from my career to start my own algorithmic trading setup. This new journey started well and along with my own proprietary trading systems, I also started building automation systems for retail traders, fund managers and HNIs. I was employed with firms like Morgan Stanley, HSBC & SunGard. I work on various technologies, but my strengths are building server side cloud based algorithmic trading systems built primarily using Java programming language. Apart from all of this, I am a big fan of Formula 1 racing :-)

1 thought on “AmiBroker Strategy Debugging”
  • Prakash Warrier says:

    Hi Pritesh,

    Thank you very much for this wonderful platform. Your expertise and hard work put in is evident from the quality and usability of the product.

    Kudos to you !

    July 15, 2020 at 7:24 pm

Comments are closed.

Need Help? Chat with us