It is good idea to read the manual, was: Malfunction Portfolio Backtest

Dear colleagues,

I have a working problem in the portfolio. It is made up of 15 stocks stored in a list of the Watch List, and to do the Backtesting, prepare the filter by selecting the "Match All" option and selecting "Portfolio Backtest" in the "Bactest" key.

So when I run the backtest, the result is that it only makes backtesting of a single stock, the first in the list of 15 stocks!

I try it anyway and always repeat this malfunction.

Can any of you help me how to fix it?



How did you set your Position Size?

1 Like

There is no malfunction. Your formula is likely incomplete (missing critical parts like position size) You should read the manual first before making assumptions about "malfunction"

and add posirion sizing to your code as instructed.

Dear Tomasz,

My intention was not to discredit her great program, maybe I did not get the expression right, and where she said "this malfunction" should have said "the same".

Sorry and thanks for pointing the solution.


Dear Tomasz,

I am very grateful for the indication on how to implement a good portfolio management, I did it in a basic way and I have checked Amibroker's superb power.

Not long ago I have programmed this powerful tool and there are things I still do not know, but I recognize a wide range of possibilities.

I have a question that you, surely, can guide me to solve it.

When I set out a maximum trade risk (i.e. 2%), and equity is still small, the loss for that reason is still tolerable.

But when equity increases, although the relative proportion (%) stays, the absolute value is very important, producing big drawdowns and, moreover, I think that they can be avoidable.

I think it would be good to introduce comparisons to the formulas, that if the losses of a trade exceed an absolute large amount (this is a personal estimate), even if it has not exceeded the maximum % risk trade, close your operation.

It would be good to limit the losses caused by a night or day GAP that would skip the stoploss, for example, or simply limit even more current losses when a trade does not work well, without having to reach the necessarily established limit.

It could be of the following style:

If trade loss <2% and trade loss> $ absolute amount, Exit or close the trade.

But I have not found the way to express it in AFL, which can surely be implemented if you can access the loss of the specific trade.

I repeat, thank you for your attention and forgive me for my failure to express the previous problem correctly.


1 Like

@Linx, save yourself a lot of time by studying your way through the Tutorial section of the AmiBroker User Guide (Help > Help Contents > Tutorial).
Every one of your listed goals can be programmed in AmiBroker. I recommend that you learn the basic structure and how to search the AFL Function Reference (

@ghanson, thank you very much for your suggestion, I will follow your advice


@mradtke, I really did not have set a Position Size in the formula was my mistake.

But it has spared me the fact that, in other formulas, the baktest of the portfolio worked a lot without set a Position Size .

Thank you very much for your interest in my problem.


If PositionSize is not defined in the formula it defaults to 100% of equity, which means 100% of funds are used to open trade, so no cash is left for other positions for as long as trade is open. Only when trade closes funds are available again to open next position. That is what happened in your case.