AmiBroker Library

Place orders from your AmiBroker strategy into one or many accounts, using our ready-made AFL library.

AmiBroker 6.30+Direct connectionAbout 5 minutesBroker independent
In short

Download the AFL library from your AutoTrader Web account, extract it into your AmiBroker folder, and include it at the top of your strategy. Your API key is already inside the download, so there is nothing to type in and nothing else to install. The library ships ready-made sample AFLs for regular, bracket and cover orders, scanners, button trading and multi-account trading.

What it is

The AmiBroker library is an AFL library that lets you place orders from your AmiBroker strategy into single or multiple accounts. It ships ready-made sample AFLs for regular, bracket and cover orders, scanners, button trading and multi-account trading.

How it connects

AmiBroker talks to AutoTrader Web directly, over the internet. There is no second program to install and nothing that has to stay running in the background.

How your orders reach your brokerNothing else to install
DirectExcel · AmiBroker · MetaTrader · Python · Java · C# · HTTP REST
Your code or tool
on your PC or a server
secure web
AutoTrader Web
the platform
Your broker
any supported broker

Before you begin

Have these ready first:

  • AmiBroker 6.30 or newer, installed on your computer.
  • An AutoTrader Web account.
  • At least one broker account added as a pseudo account.

Demos

All AmiBroker demos are available in this YouTube playlist.

Installation

  1. Sign in to your account at webx.stocksdeveloper.in
  2. Go to Tools → Library
  3. Click Download on the AmiBroker card, and enter your AutoTrader password when asked
  4. Save the zip file
  5. Extract it into your AmiBroker folder — the one that holds Broker.exe
    1. Default path: C:\Program Files\AmiBroker
  6. Start AmiBroker, or refresh your charts if it is already open

The download already has your API key inside it. That is why the page asks for your password before it gives you the file.

Keep this file to yourself. Anyone who has it can place orders in your accounts. Do not e-mail it, and never attach it to a support request or a forum post.

Where your key lives

Your key sits in one file: Formulas\Include\autotrader-http-config.afl. If you ever need to change it, edit that file and save.

The same file holds a few optional settings, such as how long the library re-uses portfolio data before asking again, and a switch to print extra logs while you are setting things up.

Integration

Once the library is extracted, the next step is to integrate it into your trading strategy. This is simple. You can also look at the samples available in the “Formulas\AutoTraderWeb” folder (look at the Charts tab in AmiBroker).

AutoTrader Web Sample AFLs

AutoTrader Web Sample AFLs

Warning: Do not modify these files. They are overwritten when you download the library again. Use them as a reference and copy the code into new AFL files if you need. Make sure your personal AFL files are NOT in the AutoTraderWeb folder.

Code

As with any other library, to use its functions in your strategy code, you must include it at the top of your strategy’s AFL.

#include <autotrader-http.afl>

That’s all. This AFL internally includes all dependencies. Now you can use the API functions provided in the library.

Regular Order

orderId = placeOrder(AT_ACCOUNT, AT_EXCHANGE, AT_SYMBOL,
	"BUY", "MARKET", AT_PRODUCT_TYPE, AT_QUANTITY,
	buyPrice, defaultTriggerPrice(), True);

Bracket Order

orderId = placeBracketOrder(AT_ACCOUNT, AT_EXCHANGE, AT_SYMBOL,
	"BUY", "LIMIT", AT_QUANTITY,
	buyPrice, defaultTriggerPrice(), 5, 3, 1, True);

Cover Order

stoplossTriggerPrice = buyPrice - 5;
orderId = placeCoverOrder(AT_ACCOUNT, AT_EXCHANGE, AT_SYMBOL,
	"BUY", "LIMIT", AT_QUANTITY,
	buyPrice, stoplossTriggerPrice, True);

What placeOrder gives you back

placeOrder waits for your broker’s answer and returns your broker’s own order id. It is the same id you see in your broker order book, and it is what you pass to getOrderStatus, modifyOrder and cancelOrder.

There are three possible answers, and it is worth handling all three:

orderId = placeOrder(AT_ACCOUNT, AT_EXCHANGE, AT_SYMBOL,
	"BUY", "MARKET", AT_PRODUCT_TYPE, AT_QUANTITY,
	buyPrice, defaultTriggerPrice(), True);

if(orderId == AT_UNCONFIRMED) {
	// The order reached your broker and your broker did not confirm it.
	// It may be live. Do NOT send it again -- check your order book.
	_TRACE("Order sent but not confirmed. Check the order book.");
} else if(orderId == "") {
	// The order was not placed. Safe to try again if you want to.
	_TRACE("Order was not placed.");
} else {
	// Placed. This is your broker's order id.
	_TRACE("Order placed: " + orderId);
}

The middle case is rare, but it is the one that matters. If you treat a blank answer and an unconfirmed answer the same way, a strategy can place the same order twice.

Cancel Order

orderId = placeOrder(AT_ACCOUNT, AT_EXCHANGE, AT_SYMBOL,
	"BUY", "MARKET", AT_PRODUCT_TYPE, AT_QUANTITY,
	buyPrice, defaultTriggerPrice(), True);

// You can save this orderId in a static variable
saveStaticVariableText(AT_ACCOUNT, "ORDER_ID", orderId);

// Somewhere later in your code,
// when you want to cancel this order

// Read orderId from static variable
orderId = readStaticVariableText(AT_ACCOUNT, "ORDER_ID");

// Cancel the order
cancelOrder(AT_ACCOUNT, orderId);

API Functions

All API functions are available inside “Formulas\Include\autotrader-http-api.afl”. Do NOT modify this AFL file.

Note: A detailed explanation is provided for each function along with examples in the API documentation.

Parameters

The AFL library defines certain parameters that you can set in the chart/exploration parameters window. They are listed below.

ParameterMeaning
AT_ACCOUNTPseudo account
AT_EXCHANGEInstrument’s exchange
AT_SYMBOLInstrument’s symbol
AT_PRODUCT_TYPEDefault product type
AT_QUANTITYDefault quantity
AT_PRICE_PRECISIONPrice precision for rounding
AT_DEBUGPrint additional logs
AT_AVOID_REPEAT_ORDER_DELAYAvoid repeat order delay (in seconds)

A broker independent instrument’s Exchange & Symbol can be looked up using this global instruments search tool.

AmiBroker chart parameters

AmiBroker Chart Parameters

Amibroker Chart Parameters - Derivatives

Amibroker Chart Parameters – Derivatives

Sample Strategies

Sample strategies are available under “Formulas\AutoTraderWeb”. A direct-mode sample is at “Formulas\AutoTraderWeb\Direct”.

Button Trading

Many sample button trading AFLs are available under “Formulas\AutoTraderWeb\Buttons”. We recommend you read and understand the AFL code before using them.

A detailed guide on button trading is available on AmiBroker Multi-Account Button Trading.

Scanner

The most important thing to remember is to use the Name() function to get the scanner symbol. Do not use the AT_SYMBOL parameter defined by our library.

You can map your datafeed provider’s symbol to AutoTrader Web’s symbol using a CSV file. You can also trade a different quantity per scanner symbol by mapping the quantity in a CSV file.

Look for the sample AutoTraderWeb\General\at-supertrend-scanner.afl. You can enable CSV files and set the file path in the chart parameters as shown below.

AmiBroker scanner parameters

AmiBroker Scanner Parameters

Sample “symbol-mapping.csv”

  • Column 1: Datafeed provider symbol
  • Column 2: AutoTrader Web’s broker independent symbol
  • Give a comma (,) at the end of each line
  • See sample entries below, copy them and paste into a CSV file
BANKNIFTY-I,BANKNIFTY_24-SEP-2020_FUT,
BANKNIFTY-II,BANKNIFTY_26-NOV-2020_FUT,
NIFTY-I,NIFTY_24-SEP-2020_FUT,

Sample “quantity-mapping.csv”

  • Column 1: Datafeed provider symbol
  • Column 2: Quantity for the symbol
  • Give a comma (,) at the end of each line
  • See sample entries below, copy them and paste into a CSV file
BANKNIFTY-I,50,
BANKNIFTY-II,25,
NIFTY-I,75,

Trading in Multiple Accounts

There are two approaches to trading into multiple accounts. You can read about them in Trading Strategies For Multiple Accounts.

Approach 1: One Strategy Instance Per Account

To trade in multiple accounts in AmiBroker, run multiple charts or scanners, with one chart/scanner per account.

Approach 2: One Strategy for All Accounts

A single strategy runs across multiple accounts. We have already provided multi-account trading sample AFLs in our library. See the screenshots below.

Multi-account trading AFLs

Multi-account trading AFLs

Multi-account chart parameter

Set multiple account names in chart parameters

Many charts on the same account share one copy of your portfolio data, so ten charts do not make ten requests.

Place Order on Next Candle or Bar

This feature handles a specific scenario. Sometimes you will see short lived signals. A short lived signal is one that appears on a bar or candle and disappears by the time the bar or candle is complete.

You can avoid this problem by placing the order on the Next Bar or Candle. We have added this feature in our sample AFLs, and you can set it to ON if you need it. See the screenshot below.

Place order on next Bar or Candle

Place order on next Bar or Candle

Debug

A simple way to debug AFL code is to use the _TRACE() function and observe the TRACE logs. First, set up your AmiBroker to view logs.

  • Increase _TRACE() output to max (5000 lines) (see screenshot below)
  • Bring up the log window (AmiBroker Menu: Window -> Log)
  • Go to the Trace tab and enable both Internal & External output (see screenshot below)
  • Enable the switch to print additional logs in the parameters window (see screenshot below)

To see every request and reply the library makes, set AT_HTTP_DEBUG to True in “Formulas\Include\autotrader-http-config.afl”. It never prints your API key. Turn it off again once things are working — it is noisy.

Trace output max lines

Trace output max lines

Trace enable internal and external output

Trace enable internal and external output

Print additional logs parameter

Print additional logs parameter

Logging

Logging helps you understand how your strategy works during live market hours. It also helps when investigating issues. It is similar to the _TRACE function above, but has more advantages: there is no limit on the file length, and the logs are kept even after AmiBroker is closed.

You can configure file based logging in the chart parameters.

AmiBroker Logging Library Parameters

AmiBroker Logging Library Parameters

Additional Information

You can find solutions to common issues in the knowledgebase.

Also be aware of this AmiBroker issue. Make sure you use the RequestTimedRefresh() function as shown in the samples provided.

Portfolio summary functions

The portfolio summary functions — getPortfolioMtm, getPortfolioPnl, and the position and order counts — are not part of this library. Use the position and order functions instead. For example, add up getPositionMtm for the positions you care about.

If you are on an older AmiBroker

This library needs AmiBroker 6.30 or newer. Older versions cannot make the web requests it depends on. Upgrading AmiBroker is the only way forward; it is a free upgrade within the same major version for most users.

Frequently asked questions

Do I need to install anything besides AmiBroker?

No. The library talks to AutoTrader Web over the internet from inside AmiBroker. You download one zip file, extract it into your AmiBroker folder, and add one line to your strategy.

What does placeOrder give me back?

Your broker's own order id. It is the same id you see in your broker order book, and it is what you pass to getOrderStatus, modifyOrder and cancelOrder.

My order did not go through. Where do I look?

Turn on AT_HTTP_DEBUG in autotrader-http-config.afl and watch the AmiBroker trace window. The library prints the reason for every failed request, including a wrong API key.

Next steps

Was this page helpful?

Last updated 6 September 2026