AmiBroker backtest with fixed number of quantity

By default AmiBroker backtesting engine, sets some Initial capital and displays ending capital in backtest result, but more often we need to backtest a strategy with a given number quantity instead of how much trading capital we have, as later criteria can pose problems with options where If one has fixed the trading capital, then number of traded quantity might vary over trades.

To do this we only need to add one line towards the start of AFL file

 SetPositionSize( 1, spsShares ); 

Here 1 being quantity, also if you are backtesting multiple symbols it is preferred to set max number of open position to 1000, this can be done from
Analysis window -> backtester settings -> portfolio tab

How to export chart historical data from Amibroker

In order to export historical bars as they appear in chart, one has to write an AFL program which can become unmanageable in future, so here at randomticks we are providing a DLL plugin, using which you can export csv files for each AmiBroker symbols in seconds.


  1. Download the dll plugin fromĀ
  2. Copy DLL file inside amibrokers “plugins” directory.
  3. Now start AmiBroker and open any symbol you want to export.
  4. Right click -> edit the AFL, only once you need to do this.
  5. Inside AFL file add a call to the following function
  6. RTT_Export();

This will generate a csv files inside AmiBroker install directory (root path just above plugins folder), now, more csv files can be exported by just clicking next symbol.

NOTE: this uses caching, once a export file is created it does not regenerate them on each AFL run, so if you want to force overwrite of csv file call the function like below