Portfolio backtesting in reverse alphabetical order

When running backtests on a portfolio I have noticed each symbol is processed alphabetically.
I end up with a disproportionate number of trades for symbols at the start of the list.
Not an important issue but for fun I would like to run it in reverse or random order. Is there any easy way to do that?

You need to set the PositionScore.

See the documentation here:
https://www.amibroker.com/guide/h_portfolio.html

1 Like

Getting random selection is as easy as single line

PositionScore = mtRandom();

More on this at the very end of this chapter of the manual: http://www.amibroker.com/guide/h_montecarlo.html

Keep in mind that adding random element would make your backtest non-reproducible (each run may generate different selection and different results).

You probably need to add more conditions to your Buy line. A huge number of 'A---' stocks in your backtest means your entry conditions are too easy to meet.

And for reverse alphabetical you can calculate score based on ASCII codes, like shown below:

n = Name();

factor = 1;

score = 0;

for( i = 0; ( code = Asc( n, i ) ) != 0; i++ )
{
   score += code * factor;
   factor *= 0.0078125; // (1/128)
}

PositionScore = score;

3 Likes